From ac795218d34d0fa15e374a881149f377070fde35 Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Tue, 4 Aug 2015 16:14:21 +0200 Subject: Inkscape export: Optionally don't transform strokes to shapes. --- support/dxf_export/__main__.py | 60 ++++++++++++++++++++++++++++-------------- support/dxf_export/effect.py | 21 ++++++++++++--- 2 files changed, 57 insertions(+), 24 deletions(-) (limited to 'support/dxf_export') diff --git a/support/dxf_export/__main__.py b/support/dxf_export/__main__.py index 22af532..ff2a38f 100644 --- a/support/dxf_export/__main__.py +++ b/support/dxf_export/__main__.py @@ -3,20 +3,38 @@ from lib import util from . import effect -def _export_dxf(in_path, out_path): - dxf_export = effect.DXFExportEffect() +def _export_dxf(in_path, out_path, layers): + dxf_export = effect.DXFExportEffect(layers) dxf_export.affect(args = [in_path], output = False) with open(out_path, 'w') as file: dxf_export.write(file) -def _get_inkscape_layer_count(svg_path): +def _get_inkscape_layers(svg_path): document = xml.etree.ElementTree.parse(svg_path) - layers = document.findall( - '{http://www.w3.org/2000/svg}g[@{http://www.inkscape.org/namespaces/inkscape}groupmode="layer"]') - return len(layers) + def iter_layers(): + nodes = document.findall( + '{http://www.w3.org/2000/svg}g[@{http://www.inkscape.org/namespaces/inkscape}groupmode="layer"]') + + for i in nodes: + inkscape_name = i.get('{http://www.inkscape.org/namespaces/inkscape}label').strip() + + if inkscape_name.endswith(']'): + dxf_name, args = inkscape_name[:-1].rsplit('[', 1) + + dxf_name = dxf_name.strip() + args = args.strip() + + use_paths = 'p' in args + else: + use_paths = False + dxf_name = inkscape_name + + yield effect.Layer(inkscape_name, dxf_name, use_paths = use_paths) + + return list(iter_layers()) def _inkscape(svg_path, verbs): @@ -32,40 +50,41 @@ def _inkscape(svg_path, verbs): util.command(list(iter_args())) -def _unfuck_svg_document(temp_svg_path): +def _unfuck_svg_document(temp_svg_path, layers): """ - Unfucks an SVG document so is can be processed by the better_dxf_export plugin. + Unfucks an SVG document so is can be processed by the better_dxf_export plugin (or what's left of it). """ - layers_count = _get_inkscape_layer_count(temp_svg_path) - def iter_inkscape_verbs(): yield 'LayerUnlockAll' yield 'LayerShowAll' # Go to the first layer. - for _ in range(layers_count): + for _ in layers: yield 'LayerPrev' # Copy each layer and flatten it to a single path object. - for _ in range(layers_count): + for i in layers: yield 'LayerDuplicate' yield 'EditSelectAll' yield 'ObjectToPath' yield 'EditSelectAll' yield 'SelectionUnGroup' - yield 'EditSelectAll' - yield 'StrokeToPath' - yield 'EditSelectAll' - yield 'SelectionUnion' + + if not i.use_paths: + yield 'EditSelectAll' + yield 'StrokeToPath' + yield 'EditSelectAll' + yield 'SelectionUnion' + yield 'LayerNext' # Go to the first layer again. - for _ in range(2 * layers_count): + for _ in range(2 * len(layers)): yield 'LayerPrev' # Move the flattened shapes to the original layers. - for _ in range(layers_count): + for _ in layers: yield 'EditSelectAll' yield 'EditDelete' yield 'LayerNext' @@ -88,9 +107,10 @@ def main(in_path, out_path): shutil.copyfile(in_path, temp_svg_path) - _unfuck_svg_document(temp_svg_path) + layers = _get_inkscape_layers(temp_svg_path) + _unfuck_svg_document(temp_svg_path, layers) - _export_dxf(temp_svg_path, out_path) + _export_dxf(temp_svg_path, out_path, layers) try: diff --git a/support/dxf_export/effect.py b/support/dxf_export/effect.py index 8168d3f..03c81c5 100644 --- a/support/dxf_export/effect.py +++ b/support/dxf_export/effect.py @@ -24,12 +24,20 @@ def _get_unit_factors_map(): 'yd': pixels_per_inch * 36 } +class Layer(object): + def __init__(self, inkscape_name, dxf_name, use_paths): + self.inkscape_name = inkscape_name + self.dxf_name = dxf_name + self.use_paths = use_paths + + class DXFExportEffect(inkex.Effect): _unit_factors = _get_unit_factors_map() - def __init__(self): + def __init__(self, layers): inkex.Effect.__init__(self) + self._layers_by_inkscape_name = { i.inkscape_name: i for i in layers } self._dxf_instructions = [] self._handle = 255 self._layer_indices = { } @@ -104,8 +112,13 @@ class DXFExportEffect(inkex.Effect): self._add_dxf_line(layer_name, [s[1], e[1]]) def _add_dxf_shape(self, node, document_transform, element_transform): - layer_name = self._get_inkscape_layer_name(node) path = cubicsuperpath.parsePath(node.get('d')) + layer = self._layers_by_inkscape_name.get(self._get_inkscape_layer_name(node)) + + if layer is None: + layer_name = '' + else: + layer_name = layer.dxf_name transform = simpletransform.composeTransform( document_transform, @@ -149,7 +162,7 @@ class DXFExportEffect(inkex.Effect): unit = string[unit_match.start():unit_match.end()] else: unit = None - + return value, unit @classmethod @@ -172,7 +185,7 @@ class DXFExportEffect(inkex.Effect): node = node.getparent() - return '' + return None @classmethod def _get_unit_factor(cls, unit, default = None): -- cgit