From b34b948f0ae88fa632b196d5fb0682b552555764 Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Thu, 6 Aug 2015 16:19:06 +0200 Subject: Reworked Inkscape command line generation. The old version was brittle and made conditional operations hard. --- support/inkscape/__main__.py | 120 ++++++++++--------------------------------- support/inkscape/inkscape.py | 120 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 92 deletions(-) create mode 100644 support/inkscape/inkscape.py (limited to 'support/inkscape') diff --git a/support/inkscape/__main__.py b/support/inkscape/__main__.py index 3052e6f..7b3ea1a 100644 --- a/support/inkscape/__main__.py +++ b/support/inkscape/__main__.py @@ -1,114 +1,50 @@ -import sys, os, xml.etree.ElementTree, shutil +import sys, os, shutil from lib import util -from . import effect +from . import effect, inkscape -def _export_dxf(in_path, out_path, layers): - dxf_effect = effect.ExportEffect(layers) - dxf_effect.affect(args = [in_path], output = False) +def _unfuck_svg_document(temp_svg_path): + """ + Unfucks an SVG document so is can be processed by the better_dxf_export plugin (or what's left of it). + """ - with open(out_path, 'w') as file: - dxf_effect.write_dxf(file) - - -def _get_inkscape_layers(svg_path): - document = xml.etree.ElementTree.parse(svg_path) + command_line = inkscape.InkscapeCommandLine(temp_svg_path) + layers = command_line.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) + command_line.apply_to_document('LayerUnlockAll', 'LayerShowAll') - return list(iter_layers()) - - -def _inkscape(svg_path, verbs): - def iter_args(): - yield os.environ['INKSCAPE'] + layer_copies = [] + + for i in layers: + layer_copy = command_line.duplicate_layer(i) + layer_copies.append(layer_copy) - for i in verbs: - yield '--verb' - yield i + command_line.apply_to_layer_content(layer_copy, 'ObjectToPath') + command_line.apply_to_layer_content(layer_copy, 'SelectionUnGroup') - yield svg_path + if not i.use_paths: + command_line.apply_to_layer_content(layer_copy, 'StrokeToPath') + command_line.apply_to_layer_content(layer_copy, 'SelectionUnion') - util.command(list(iter_args())) - - -def _unfuck_svg_document(temp_svg_path, layers): - """ - Unfucks an SVG document so is can be processed by the better_dxf_export plugin (or what's left of it). - """ + for original, copy in zip(layers, layer_copies): + command_line.clear_layer(original) + command_line.move_content(copy, original) + command_line.delete_layer(copy) - def iter_inkscape_verbs(): - yield 'LayerUnlockAll' - yield 'LayerShowAll' - - # Go to the first layer. - for _ in layers: - yield 'LayerPrev' - - # Copy each layer and flatten it to a single path object. - for i in layers: - yield 'LayerDuplicate' - yield 'EditSelectAll' - yield 'ObjectToPath' - yield 'EditSelectAll' - yield 'SelectionUnGroup' - - 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 * len(layers)): - yield 'LayerPrev' - - # Move the flattened shapes to the original layers. - for _ in layers: - yield 'EditSelectAll' - yield 'EditDelete' - yield 'LayerNext' - - yield 'EditSelectAll' - yield 'LayerMoveToPrev' - yield 'LayerNext' - yield 'LayerDelete' - - yield 'FileSave' - yield 'FileClose' - yield 'FileQuit' + command_line.apply_to_document('FileSave', 'FileClose', 'FileQuit') - _inkscape(temp_svg_path, list(iter_inkscape_verbs())) + command_line.run() def main(in_path, out_path): + _, out_suffix = os.path.splitext(out_path) + with util.TemporaryDirectory() as temp_dir: temp_svg_path = os.path.join(temp_dir, 'temp.svg') shutil.copyfile(in_path, temp_svg_path) - layers = _get_inkscape_layers(temp_svg_path) - _unfuck_svg_document(temp_svg_path, layers) + _unfuck_svg_document(temp_svg_path) _export_dxf(temp_svg_path, out_path, layers) diff --git a/support/inkscape/inkscape.py b/support/inkscape/inkscape.py new file mode 100644 index 0000000..09e7a7b --- /dev/null +++ b/support/inkscape/inkscape.py @@ -0,0 +1,120 @@ +import os +import xml.etree.ElementTree as etree +from lib import util + + +def get_inkscape_layers(svg_path): + document = etree.parse(svg_path) + + 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 Layer(inkscape_name, dxf_name, use_paths = use_paths) + + return list(iter_layers()) + + +def _inkscape(svg_path, verbs): + def iter_args(): + yield os.environ['INKSCAPE'] + + for i in verbs: + yield '--verb' + yield i + + yield svg_path + + util.command(list(iter_args())) + + +class Layer(object): + def __init__(self, inkscape_name, export_name, use_paths): + self.inkscape_name = inkscape_name + self.export_name = export_name + self.use_paths = use_paths + + +class InkscapeCommandLine(object): + def __init__(self, path): + self._path = path + self._layers = get_inkscape_layers(path) + self._current_layer_index = None + self._verbs = [] + + def apply_to_document(self, *verb): + self._verbs.extend(verb) + + def apply_to_layer(self, layer, *verb): + self._go_to_layer(layer) + self.apply_to_document(*verb) + + def select_all_in_layer(self, layer): + self.apply_to_layer(layer, 'EditSelectAll') + + def apply_to_layer_content(self, layer, *verbs): + self.select_all_in_layer(layer) + self.apply_to_document(*verbs) + + def _go_to_layer(self, layer, with_selection = False): + if self._current_layer_index is None: + # Initialize to a known state. + self._current_layer_index = len(self._layers) - 1 + self._go_to_layer(self._layers[0]) + + target_index = self._layers.index(layer) + + while True: + if self._current_layer_index < target_index: + self.apply_to_document('LayerMoveToNext' if with_selection else 'LayerNext') + self._current_layer_index += 1 + elif self._current_layer_index > target_index: + self.apply_to_document('LayerMoveToPrev' if with_selection else 'LayerPrev') + self._current_layer_index -= 1 + else: + break + + def duplicate_layer(self, layer): + self.apply_to_layer(layer, 'LayerDuplicate') + + # Inkscape 0.91 places a duplicated layer above (after) the selected one and selects the new layer. + new_layer = Layer(layer.inkscape_name + ' copy', layer.export_name, layer.use_paths) + self._current_layer_index += 1 + self._layers.insert(self._current_layer_index, new_layer) + + return new_layer + + def delete_layer(self, layer): + self.apply_to_layer(layer, 'LayerDelete') + + # Inkscape 0.91 selects the layer above (after) the deleted layer. + del self._layers[self._current_layer_index] + + def clear_layer(self, layer): + self.select_all_in_layer(layer) + self.apply_to_document('EditDelete') + + def move_content(self, source_layer, target_layer): + self.select_all_in_layer(source_layer) + self._go_to_layer(target_layer, True) + + def run(self): + _inkscape(self._path, self._verbs) + + @property + def layers(self): + return list(self._layers) -- cgit