From dfaf23b71803bdaf331e8353b93e3862838da042 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 18 Jun 2022 23:52:22 +0200 Subject: Add selectable inkscape SVG export --- gerbonara/layers.py | 11 ++++++----- gerbonara/utils.py | 24 ++++++++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/gerbonara/layers.py b/gerbonara/layers.py index 2c48a83..47ade7d 100644 --- a/gerbonara/layers.py +++ b/gerbonara/layers.py @@ -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) diff --git a/gerbonara/utils.py b/gerbonara/utils.py index 290a44a..04d1e54 100644 --- a/gerbonara/utils.py +++ b/gerbonara/utils.py @@ -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) + 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') + + 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 where reasonable. - return tag('svg', [view, *tags], + return tag('svg', tags, width=f'{w}{svg_unit}', height=f'{h}{svg_unit}', viewBox=f'{min_x} {min_y} {w} {h}', - 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', + **namespaces, root=True) -- cgit