Added background image to pcb_print

- Enables the use of watermarks
This commit is contained in:
Salvador E. Tropea 2022-04-23 08:48:32 -03:00
parent 0554742e86
commit f4638fdc66
7 changed files with 161 additions and 1497 deletions

View File

@ -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 pcbnew import B_Cu, F_Cu, FromMM, IsCopperLayer, PLOT_CONTROLLER, PLOT_FORMAT_SVG, wxSize, F_Mask, B_Mask
from shutil import rmtree, which from shutil import rmtree, which
from tempfile import NamedTemporaryFile, mkdtemp from tempfile import NamedTemporaryFile, mkdtemp
from .svgutils.transform import fromstring, RectElement from .svgutils.transform import fromstring, RectElement, fromfile
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from .gs import GS from .gs import GS
from .optionable import Optionable from .optionable import Optionable
@ -314,6 +314,8 @@ class PCB_PrintOptions(VariantOptions):
""" Add a background to the pages, see `background_color` """ """ Add a background to the pages, see `background_color` """
self.background_color = '#FFFFFF' self.background_color = '#FFFFFF'
""" Color for the background when `add_background` is enabled """ """ Color for the background when `add_background` is enabled """
self.background_image = ''
""" Background image, must be an SVG, only when `add_background` is enabled """
super().__init__() super().__init__()
self._expand_id = 'assembly' self._expand_id = 'assembly'
@ -359,6 +361,13 @@ class PCB_PrintOptions(VariantOptions):
raise KiPlotConfigurationError("Missing page layout file: "+self.sheet_reference_layout) raise KiPlotConfigurationError("Missing page layout file: "+self.sheet_reference_layout)
if self.add_background: if self.add_background:
self.validate_color('background_color') self.validate_color('background_color')
if self.background_image:
if not os.path.isfile(self.background_image):
raise KiPlotConfigurationError("Missing background image file: "+self.background_image)
with open(self.background_image, 'rt') as f:
ln = f.readline()
if not ln.startswith('<?xml') and not ln.startswith('<!DOCTYPE svg'):
raise KiPlotConfigurationError("Background image must be an SVG ({})".format(self.background_image))
def filter_components(self): def filter_components(self):
if not self._comps: if not self._comps:
@ -706,6 +715,19 @@ class PCB_PrintOptions(VariantOptions):
cnt = cnt+1 cnt = cnt+1
logger.debug('- Filled {} polygons'.format(cnt)) logger.debug('- Filled {} polygons'.format(cnt))
def process_background(self, svg_out, width, height):
""" Applies the background options """
if not self.add_background:
return
if self.background_image:
img = fromfile(self.background_image)
w, h = get_size(img)
root = img.getroot()
root.moveto(0, 0)
root.scale(width/w, height/h)
svg_out.insert([root])
svg_out.insert(RectElement(0, 0, width, height, color=self.background_color))
def merge_svg(self, input_folder, input_files, output_folder, output_file, p): def merge_svg(self, input_folder, input_files, output_folder, output_file, p):
""" Merge all pages into one """ """ Merge all pages into one """
first = True first = True
@ -714,8 +736,8 @@ class PCB_PrintOptions(VariantOptions):
file = os.path.join(input_folder, file) file = os.path.join(input_folder, file)
new_layer = fromstring(load_svg(file, color, p.colored_holes, p.holes_color, p.monochrome)) new_layer = fromstring(load_svg(file, color, p.colored_holes, p.holes_color, p.monochrome))
width, height = get_size(new_layer) width, height = get_size(new_layer)
# Workaround for polygon fill on KiCad 5
if GS.ki5() and file.endswith('frame.svg'): if GS.ki5() and file.endswith('frame.svg'):
# Workaround for polygon fill on KiCad 5
if p.monochrome: if p.monochrome:
color = to_gray_hex(color) color = to_gray_hex(color)
self.fill_polygons(new_layer, color) self.fill_polygons(new_layer, color)
@ -724,8 +746,7 @@ class PCB_PrintOptions(VariantOptions):
# This is the width declared at the beginning of the file # This is the width declared at the beginning of the file
base_width = width base_width = width
first = False first = False
if self.add_background: self.process_background(svg_out, width, height)
svg_out.append(RectElement(0, 0, width, height, color=self.background_color))
self.add_frame_images(svg_out, p.monochrome) self.add_frame_images(svg_out, p.monochrome)
else: else:
root = new_layer.getroot() root = new_layer.getroot()

View File

@ -293,6 +293,13 @@ class SVGFigure(object):
except AttributeError: except AttributeError:
self.root.append(GroupElement(element).root) self.root.append(GroupElement(element).root)
def insert(self, element, pos=0):
"""Insert new element to the SVG figure, default is at the beginning"""
try:
self.root.insert(pos, element.root)
except AttributeError:
self.root.insert(pos, GroupElement(element).root)
def getroot(self): def getroot(self):
"""Return the root element of the figure. """Return the root element of the figure.

View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="297mm"
height="210mm"
viewBox="0 0 297 210"
version="1.1"
id="svg8"
inkscape:version="1.0.2 (e86c870879, 2021-01-15)"
sodipodi:docname="inkscape.svg">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.7"
inkscape:cx="-475.4461"
inkscape:cy="271.72412"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
inkscape:document-rotation="0"
showgrid="false"
inkscape:window-width="3840"
inkscape:window-height="2123"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Capa 1"
inkscape:groupmode="layer"
id="layer1">
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:9.36837px;line-height:1.25;font-family:sans-serif;fill:#cbcbcb;fill-opacity:1;stroke:none;stroke-width:0.23421;"
x="-74.364159"
y="183.57529"
id="text835"
transform="rotate(-36.240038)"><tspan
sodipodi:role="line"
id="tspan833"
x="-74.364159"
y="183.57529"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:31.228px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Bold';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;stroke-width:0.23421;fill:#cbcbcb;fill-opacity:1;">CONFIDENTIAL</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="297mm" height="210mm" version="1.1" viewBox="0 0 297 210" xmlns="http://www.w3.org/2000/svg">
<text transform="rotate(-36.24)" x="-74.364159" y="183.57529" fill="#cbcbcb" font-family="sans-serif" font-size="9.3684px" stroke-width=".23421" style="line-height:1.25" xml:space="preserve"><tspan x="-74.364159" y="183.57529" fill="#cbcbcb" font-family="sans-serif" font-size="31.228px" font-weight="bold" stroke-width=".23421" style="font-variant-caps:normal;font-variant-east-asian:normal;font-variant-ligatures:normal;font-variant-numeric:normal">CONFIDENTIAL</tspan></text>
</svg>

After

Width:  |  Height:  |  Size: 632 B

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
width="297mm"
height="210mm"
viewBox="0 0 297 210"
version="1.1"
id="svg8">
<defs
id="defs2" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
id="layer1">
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:9.36837px;line-height:1.25;font-family:sans-serif;fill:#cbcbcb;fill-opacity:1;stroke:none;stroke-width:0.23421;"
x="-74.364159"
y="183.57529"
id="text835"
transform="rotate(-36.240038)"><tspan
id="tspan833"
x="-74.364159"
y="183.57529"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:31.228px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Bold';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;stroke-width:0.23421;fill:#cbcbcb;fill-opacity:1;">CONFIDENTIAL</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,9 @@ outputs:
keep_temporal_files: true keep_temporal_files: true
add_background: true add_background: true
background_color: "#C5C4BF" background_color: "#C5C4BF"
# background_color: "#FFFFFF"
background_image: "tests/data/confidential_optimized.svg"
sheet_reference_layout: "${KIPRJMOD}/../../data/test_img.kicad_wks"
pages: pages:
- # monochrome: true - # monochrome: true
scaling: 2.0 scaling: 2.0