Add selectable inkscape SVG export

This commit is contained in:
jaseg 2022-06-18 23:52:22 +02:00
parent 4b38d0905e
commit dfaf23b718
2 changed files with 24 additions and 15 deletions

View file

@ -480,13 +480,14 @@ class LayerStack:
return setup_svg(tags, bounds, margin=margin, arg_unit=arg_unit, svg_unit=svg_unit, pagecolor=bg, tag=tag)
def to_pretty_svg(self, side='top', margin=0, arg_unit=MM, svg_unit=MM, force_bounds=None, tag=Tag):
def to_pretty_svg(self, side='top', margin=0, arg_unit=MM, svg_unit=MM, force_bounds=None, tag=Tag, inkscape=False):
if force_bounds:
bounds = svg_unit.convert_bounds_from(arg_unit, force_bounds)
else:
bounds = self.outline.instance.bounding_box(svg_unit, default=((0, 0), (0, 0)))
bounds = selfboard_bounds(unit=svg_unit, default=((0, 0), (0, 0)))
tags = []
inkscape_attrs = lambda label: dict(inkscape__groupmode='layer', inkscape__label=label) if inkscape else {}
for use, color in {'copper': 'black', 'mask': 'blue', 'silk': 'red'}.items():
if (side, use) not in self:
@ -495,13 +496,13 @@ class LayerStack:
layer = self[(side, use)]
tags.append(tag('g', list(layer.instance.svg_objects(svg_unit=svg_unit, fg=color, bg="white", tag=Tag)),
id=f'l-{side}-{use}'))
id=f'l-{side}-{use}', **inkscape_attrs(f'{side} {use}')))
for i, layer in enumerate(self.drill_layers):
tags.append(tag('g', list(layer.instance.svg_objects(svg_unit=svg_unit, fg='magenta', bg="white", tag=Tag)),
id=f'l-drill-{i}'))
id=f'l-drill-{i}', **inkscape_attrs(f'drill-{i}')))
return setup_svg(tags, bounds, margin=margin, arg_unit=arg_unit, svg_unit=svg_unit, pagecolor="white", tag=tag)
return setup_svg(tags, bounds, margin=margin, arg_unit=arg_unit, svg_unit=svg_unit, pagecolor="white", tag=tag, inkscape=inkscape)
def bounding_box(self, unit=MM, default=None):
return sum_bounds(( layer.bounding_box(unit, default=default)

View file

@ -432,7 +432,7 @@ def svg_arc(old, new, center, clockwise):
def svg_rotation(angle_rad, cx=0, cy=0):
return f'rotate({float(math.degrees(angle_rad)):.4} {float(cx):.6} {float(cy):.6})'
def setup_svg(tags, bounds, margin=0, arg_unit=MM, svg_unit=MM, pagecolor='white', tag=Tag):
def setup_svg(tags, bounds, margin=0, arg_unit=MM, svg_unit=MM, pagecolor='white', tag=Tag, inkscape=False):
(min_x, min_y), (max_x, max_y) = bounds
if margin:
@ -446,17 +446,25 @@ def setup_svg(tags, bounds, margin=0, arg_unit=MM, svg_unit=MM, pagecolor='white
w = 1.0 if math.isclose(w, 0.0) else w
h = 1.0 if math.isclose(h, 0.0) else h
view = tag('sodipodi:namedview', [], id='namedview1', pagecolor=pagecolor,
inkscape__document_units=svg_unit.shorthand)
svg_unit = 'in' if svg_unit == 'inch' else 'mm'
# TODO export apertures as <uses> where reasonable.
return tag('svg', [view, *tags],
width=f'{w}{svg_unit}', height=f'{h}{svg_unit}',
viewBox=f'{min_x} {min_y} {w} {h}',
if inkscape:
tags.insert(0, tag('sodipodi:namedview', [], id='namedview1', pagecolor=pagecolor,
inkscape__document_units=svg_unit.shorthand))
namespaces = dict(
xmlns="http://www.w3.org/2000/svg",
xmlns__xlink="http://www.w3.org/1999/xlink",
xmlns__sodipodi='http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
xmlns__inkscape='http://www.inkscape.org/namespaces/inkscape',
xmlns__inkscape='http://www.inkscape.org/namespaces/inkscape')
else:
namespaces = dict(
xmlns="http://www.w3.org/2000/svg",
xmlns__xlink="http://www.w3.org/1999/xlink")
svg_unit = 'in' if svg_unit == 'inch' else 'mm'
# TODO export apertures as <uses> where reasonable.
return tag('svg', tags,
width=f'{w}{svg_unit}', height=f'{h}{svg_unit}',
viewBox=f'{min_x} {min_y} {w} {h}',
**namespaces,
root=True)