From d0d9c3706456c2d4bb32a215b6f15a9348334f80 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Thu, 19 Mar 2020 19:59:19 -0300 Subject: [PATCH] Added schematic printing to PDF. Partially added PCB printing to PDF. --- kiplot/config_reader.py | 21 ++++++++++++++++---- kiplot/kiplot.py | 44 +++++++++++++++++++++++++++++++++++++++++ kiplot/misc.py | 1 + kiplot/plot_config.py | 17 ++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/kiplot/config_reader.py b/kiplot/config_reader.py index 7a91fc00..5df3dcb4 100644 --- a/kiplot/config_reader.py +++ b/kiplot/config_reader.py @@ -4,6 +4,7 @@ Class to read KiPlot config files import os import re +import sys import pcbnew @@ -19,7 +20,6 @@ try: except: log.init(False,False) logger.error('No yaml module for Python, install python3-yaml') - import sys sys.exit(misc.NO_YAML_MODULE) @@ -371,6 +371,18 @@ class CfgYamlReader(CfgReader): 'to': 'name_format', 'required': lambda opts: False, }, + { + 'key': 'output', + 'types': ['pdf_sch_print'], + 'to': 'output', + 'required': lambda opts: False, + }, + { + 'key': 'output_name', + 'types': ['pdf_pcb_print'], + 'to': 'output_name', + 'required': lambda opts: True, + }, ] po = PC.OutputOptions(otype) @@ -457,14 +469,15 @@ class CfgYamlReader(CfgReader): if otype not in ['gerber', 'ps', 'hpgl', 'dxf', 'pdf', 'svg', 'gerb_drill', 'excellon', 'position', - 'kibom', 'ibom']: + 'kibom', 'ibom', 'pdf_sch_print', 'pdf_pcb_print']: raise YamlError("Unknown output type: {}".format(otype)) try: options = o_obj['options'] except KeyError: - if otype != 'ibom': - raise YamlError("Output need to have options specified") + if not otype in ['ibom', 'pdf_sch_print']: + logger.error('Output "'+name+'" needs options') + sys.exit(misc.EXIT_BAD_CONFIG) options = None logger.debug("Parsing output options for {} ({})".format(name, otype)) diff --git a/kiplot/kiplot.py b/kiplot/kiplot.py index efdfd12c..34027da8 100644 --- a/kiplot/kiplot.py +++ b/kiplot/kiplot.py @@ -108,6 +108,10 @@ class Plotter(object): self._do_position_plot(board, pc, op) elif self._output_is_bom(op): self._do_bom(board, pc, op, brd_file) + elif self._output_is_sch_print(op): + self._do_sch_print(board, pc, op, brd_file) + elif self._output_is_pcb_print(op): + self._do_pcb_print(board, pc, op, brd_file) else: raise PlotError("Don't know how to plot type {}" .format(op.options.type)) @@ -220,6 +224,12 @@ class Plotter(object): def _output_is_position(self, output): return output.options.type == PCfg.OutputOptions.POSITION + def _output_is_sch_print(self, output): + return output.options.type == PCfg.OutputOptions.PDF_SCH_PRINT + + def _output_is_pcb_print(self, output): + return output.options.type == PCfg.OutputOptions.PDF_PCB_PRINT + def _output_is_bom(self, output): return output.options.type in [ PCfg.OutputOptions.KIBOM, @@ -536,6 +546,27 @@ class Plotter(object): else: raise PlotError("Format is invalid: {}".format(to.format)) + def _do_sch_print(self, board, plot_ctrl, output, brd_file): + sch_file = check_eeschema_do(brd_file) + outdir = plot_ctrl.GetPlotOptions().GetOutputDirectory() + cmd = [misc.CMD_EESCHEMA_DO, 'export', '--all_pages', + '--file_format', 'pdf', sch_file, outdir] + if logger.getEffectiveLevel() <= logging.DEBUG: + cmd.insert(1, '-vv') + cmd.insert(1, '-r') + logger.debug('Executing: '+str(cmd)) + ret = call(cmd) + if ret: + logger.error(misc.CMD_EESCHEMA_DO+' returned %d', ret) + exit(misc.PDF_SCH_PRINT) + to = output.options.type_options + if to.output: + cur = os.path.join(outdir, os.path.splitext(brd_file)[0]) + '.pdf' + new = os.path.join(outdir, to.output) + logger.debug('Moving '+cur+' -> '+new) + os.rename(cur,new) + + def _do_bom(self, board, plot_ctrl, output, brd_file): if output.options.type == 'kibom': self._do_kibom(board, plot_ctrl, output, brd_file) @@ -637,6 +668,14 @@ class Plotter(object): assert(output.options.type == PCfg.OutputOptions.POSITION) + def _configure_sch_print_opts(self, po, output): + + assert(output.options.type == PCfg.OutputOptions.PDF_SCH_PRINT) + + def _configure_pcb_print_opts(self, po, output): + + assert(output.options.type == PCfg.OutputOptions.PDF_PCB_PRINT) + def _configure_kibom_opts(self, po, output): assert(output.options.type == PCfg.OutputOptions.KIBOM) @@ -704,6 +743,11 @@ class Plotter(object): self._configure_kibom_opts(po, output) elif output.options.type == PCfg.OutputOptions.IBOM: self._configure_ibom_opts(po, output) + elif output.options.type == PCfg.OutputOptions.PDF_SCH_PRINT: + self._configure_sch_print_opts(po, output) + elif output.options.type == PCfg.OutputOptions.PDF_PCB_PRINT: + self._configure_pcb_print_opts(po, output) + po.SetDrillMarksType(opts.drill_marks) diff --git a/kiplot/misc.py b/kiplot/misc.py index 9f77dbe3..deded919 100644 --- a/kiplot/misc.py +++ b/kiplot/misc.py @@ -13,6 +13,7 @@ NO_PCB_FILE = 8 NO_SCH_FILE = 9 ERC_ERROR = 10 BOM_ERROR = 11 +PDF_SCH_PRINT = 12 CMD_EESCHEMA_DO = 'eeschema_do' URL_EESCHEMA_DO = 'https://github.com/INTI-CMNB/kicad-automation-scripts' diff --git a/kiplot/plot_config.py b/kiplot/plot_config.py index 5b5ca989..dd061815 100644 --- a/kiplot/plot_config.py +++ b/kiplot/plot_config.py @@ -395,6 +395,17 @@ class IBoMOptions(TypeOptions): self.blacklist = None self.name_format = None +class SchPrintOptions(TypeOptions): + + def __init__(self): + self.output = None + + +class PcbPrintOptions(TypeOptions): + + def __init__(self): + self.output = None + class OutputOptions(object): @@ -410,6 +421,8 @@ class OutputOptions(object): POSITION = 'position' KIBOM = 'kibom' IBOM = 'ibom' + PDF_SCH_PRINT = 'pdf_sch_print' + PDF_PCB_PRINT = 'pdf_pcb_print' def __init__(self, otype): self.type = otype @@ -436,6 +449,10 @@ class OutputOptions(object): self.type_options = KiBoMOptions() elif otype == self.IBOM: self.type_options = IBoMOptions() + elif otype == self.PDF_SCH_PRINT: + self.type_options = SchPrintOptions() + elif otype == self.PDF_PCB_PRINT: + self.type_options = PcbPrintOptions() else: self.type_options = None