From 857b8b974e62d9d9b57955ad176ec6613eede483 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Sat, 23 Apr 2022 07:46:04 -0300 Subject: [PATCH] Added pcb_print options to control the background --- README.md | 2 ++ docs/samples/generic_plot.kibot.yaml | 4 ++++ kibot/out_pcb_print.py | 19 +++++++++++++------ kibot/svgutils/transform.py | 13 +++++++++++++ tests/yaml_samples/pcb_print.kibot.yaml | 2 ++ 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 28f3aa9e..fc99258e 100644 --- a/README.md +++ b/README.md @@ -1703,6 +1703,8 @@ Next time you need this list just use an alias, like this: - `name`: [string=''] Used to identify this particular output definition. - `options`: [dict] Options for the `pcb_print` output. * Valid keys: + - `add_background`: [boolean=false] Add a background to the pages, see `background_color`. + - `background_color`: [string='#FFFFFF'] Color for the background when `add_background` is enabled. - `blind_via_color`: [string=''] Color used for blind/buried `colored_vias`. - `color_theme`: [string='_builtin_classic'] Selects the color theme. Only applies to KiCad 6. To use the KiCad 6 default colors select `_builtin_default`. diff --git a/docs/samples/generic_plot.kibot.yaml b/docs/samples/generic_plot.kibot.yaml index cce918d5..121ea4cf 100644 --- a/docs/samples/generic_plot.kibot.yaml +++ b/docs/samples/generic_plot.kibot.yaml @@ -966,6 +966,10 @@ outputs: type: 'pcb_print' dir: 'Example/pcb_print_dir' options: + # [boolean=false] Add a background to the pages, see `background_color` + add_background: false + # [string='#FFFFFF'] Color for the background when `add_background` is enabled + background_color: '#FFFFFF' # [string=''] Color used for blind/buried `colored_vias` blind_via_color: '' # [string='_builtin_classic'] Selects the color theme. Only applies to KiCad 6. diff --git a/kibot/out_pcb_print.py b/kibot/out_pcb_print.py index e73bccb9..98b46270 100644 --- a/kibot/out_pcb_print.py +++ b/kibot/out_pcb_print.py @@ -11,7 +11,7 @@ import subprocess from pcbnew import B_Cu, F_Cu, FromMM, IsCopperLayer, PLOT_CONTROLLER, PLOT_FORMAT_SVG, wxSize, F_Mask, B_Mask from shutil import rmtree, which from tempfile import NamedTemporaryFile, mkdtemp -from .svgutils.transform import fromstring +from .svgutils.transform import fromstring, RectElement from .error import KiPlotConfigurationError from .gs import GS from .optionable import Optionable @@ -93,9 +93,10 @@ def load_svg(file, color, colored_holes, holes_color, monochrome): return content -def get_width(svg): - """ Finds the width in viewBox units """ - return float(svg.root.get('viewBox').split(' ')[2]) +def get_size(svg): + """ Finds the width and height in viewBox units """ + view_box = svg.root.get('viewBox').split(' ') + return float(view_box[2]), float(view_box[3]) def create_pdf_from_pages(input_files, output_fn): @@ -309,7 +310,9 @@ class PCB_PrintOptions(VariantOptions): In order to get a good looking select a color with transparency, i.e. '#14332440'. PcbDraw must be installed in order to use this option """ self.add_background = False - """ Add a background to the SVG/PNG """ + """ Add a background to the pages, see `background_color` """ + self.background_color = '#FFFFFF' + """ Color for the background when `add_background` is enabled """ super().__init__() self._expand_id = 'assembly' @@ -353,6 +356,8 @@ class PCB_PrintOptions(VariantOptions): self.sheet_reference_layout = KiConf.expand_env(self.sheet_reference_layout) if not os.path.isfile(self.sheet_reference_layout): raise KiPlotConfigurationError("Missing page layout file: "+self.sheet_reference_layout) + if self.add_background: + self.validate_color('background_color') def filter_components(self): if not self._comps: @@ -707,7 +712,7 @@ class PCB_PrintOptions(VariantOptions): logger.debug(' - Loading layer file '+file) file = os.path.join(input_folder, file) new_layer = fromstring(load_svg(file, color, p.colored_holes, p.holes_color, p.monochrome)) - width = get_width(new_layer) + width, height = get_size(new_layer) if GS.ki5() and file.endswith('frame.svg'): # Workaround for polygon fill on KiCad 5 if p.monochrome: @@ -718,6 +723,8 @@ class PCB_PrintOptions(VariantOptions): # This is the width declared at the beginning of the file base_width = width first = False + if self.add_background: + svg_out.append(RectElement(0, 0, width, height, color=self.background_color)) self.add_frame_images(svg_out, p.monochrome) else: root = new_layer.getroot() diff --git a/kibot/svgutils/transform.py b/kibot/svgutils/transform.py index ef15f9e3..07f7d092 100644 --- a/kibot/svgutils/transform.py +++ b/kibot/svgutils/transform.py @@ -209,6 +209,19 @@ class LineElement(FigureElement): FigureElement.__init__(self, line) +class RectElement(FigureElement): + """Rectangle element. + + Corresponds to SVG ```` tag. + """ + + def __init__(self, x, y, w, h, color="black"): + line = etree.Element( + SVG + "rect", {"x": str(x), "y": str(y), "width": str(w), "height": str(h), "fill": color} + ) + FigureElement.__init__(self, line) + + class GroupElement(FigureElement): """Group element. diff --git a/tests/yaml_samples/pcb_print.kibot.yaml b/tests/yaml_samples/pcb_print.kibot.yaml index 9f87c5e0..d5eb6ab5 100644 --- a/tests/yaml_samples/pcb_print.kibot.yaml +++ b/tests/yaml_samples/pcb_print.kibot.yaml @@ -16,6 +16,8 @@ outputs: format: 'PDF' # enable_ki6_frame_fix: true keep_temporal_files: true + add_background: true + background_color: "#C5C4BF" pages: - # monochrome: true scaling: 2.0