From a448b2c0ea4e61b28dccd406ffcc5a5ebb96cdd2 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Wed, 22 Aug 2018 22:13:51 -0400 Subject: add origin command and remove guides method --- lib/commands.py | 30 ++++++++++++++----------- lib/extensions/base.py | 2 +- lib/output.py | 59 ++++++++++++-------------------------------------- messages.po | 21 +++++++++++------- 4 files changed, 45 insertions(+), 67 deletions(-) diff --git a/lib/commands.py b/lib/commands.py index b9cf9610..37196990 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -1,5 +1,6 @@ import inkex import cubicsuperpath +import simpletransform from .svg import apply_transforms, get_node_transform from .svg.tags import SVG_USE_TAG, SVG_SYMBOL_TAG, CONNECTION_START, CONNECTION_END, XLINK_HREF @@ -22,8 +23,11 @@ COMMANDS = { # L10N command attached to an object "ignore_object": N_("Ignore this object (do not stitch)"), - # L10N command that affects entire layer - "ignore_layer": N_("Ignore layer (do not stitch any objects in this layer)") + # L10N command that affects a layer + "ignore_layer": N_("Ignore layer (do not stitch any objects in this layer)"), + + # L10N command that affects entire document + "origin": N_("Origin for exported embroidery files"), } OBJECT_COMMANDS = ["fill_start", "fill_end", "stop", "trim", "ignore_object"] @@ -153,31 +157,31 @@ def find_commands(node): def layer_commands(layer, command): """Find standalone (unconnected) command symbols in this layer.""" - commands = [] + for global_command in global_commands(layer.getroottree().getroot(), command): + if layer in global_command.node.iterancestors(): + yield global_command - for standalone_command in standalone_commands(layer.getroottree().getroot()): - if standalone_command.command == command: - if layer in standalone_command.node.iterancestors(): - commands.append(command) - return commands +def global_commands(svg, command): + """Find standalone (unconnected) command symbols anywhere in the document.""" + for standalone_command in _standalone_commands(svg): + if standalone_command.command == command: + yield standalone_command -def standalone_commands(svg): + +def _standalone_commands(svg): """Find all unconnected command symbols in the SVG.""" xpath = ".//svg:use[starts-with(@xlink:href, '#inkstitch_')]" symbols = svg.xpath(xpath, namespaces=inkex.NSS) - commands = [] for symbol in symbols: try: - commands.append(StandaloneCommand(symbol)) + yield StandaloneCommand(symbol) except CommandParseError: pass - return commands - def is_command(node): return CONNECTION_START in node.attrib or CONNECTION_END in node.attrib diff --git a/lib/extensions/base.py b/lib/extensions/base.py index 22bc82db..25de441f 100644 --- a/lib/extensions/base.py +++ b/lib/extensions/base.py @@ -123,7 +123,7 @@ class InkstitchExtension(inkex.Effect): return [] if node.tag == SVG_GROUP_TAG and node.get(INKSCAPE_GROUPMODE) == "layer": - if layer_commands(node, "ignore_layer"): + if len(list(layer_commands(node, "ignore_layer"))): return [] if element.has_style('display') and element.get_style('display') is None: diff --git a/lib/output.py b/lib/output.py index eed665ed..ae15ce4e 100644 --- a/lib/output.py +++ b/lib/output.py @@ -5,6 +5,7 @@ import shapely.geometry as shgeo from .utils import Point from .svg import PIXELS_PER_MM, get_doc_size, get_viewbox_transform +from .commands import global_commands def get_command(stitch): @@ -26,57 +27,25 @@ def _string_to_floats(string): def get_origin(svg): - # The user can specify the embroidery origin by defining two guides - # named "embroidery origin" that intersect. + origin_commands = list(global_commands(svg, "origin")) - namedview = svg.find(inkex.addNS('namedview', 'sodipodi')) - all_guides = namedview.findall(inkex.addNS('guide', 'sodipodi')) - label_attribute = inkex.addNS('label', 'inkscape') - guides = [guide for guide in all_guides - if guide.get(label_attribute, "").startswith("embroidery origin")] + if origin_commands: + origin = origin_commands[0].point - # document size used below - doc_size = list(get_doc_size(svg)) - - # convert the size from viewbox-relative to real-world pixels - viewbox_transform = get_viewbox_transform(svg) - simpletransform.applyTransformToPoint(simpletransform.invertTransform(viewbox_transform), doc_size) - - default = [doc_size[0] / 2.0, doc_size[1] / 2.0] - simpletransform.applyTransformToPoint(viewbox_transform, default) - default = Point(*default) - - if len(guides) < 2: - return default - - # Find out where the guides intersect. Only pay attention to the first two. - guides = guides[:2] - - lines = [] - for guide in guides: - # inkscape's Y axis is reversed from SVG's, and the guide is in inkscape coordinates - position = Point(*_string_to_floats(guide.get('position'))) - position.y = doc_size[1] - position.y + return origin + else: + # default: center of the canvas - # This one baffles me. I think inkscape might have gotten the order of - # their vector wrong? - parts = _string_to_floats(guide.get('orientation')) - direction = Point(parts[1], parts[0]) + doc_size = list(get_doc_size(svg)) - # We have a theoretically infinite line defined by a point on the line - # and a vector direction. Shapely can only deal in concrete line - # segments, so we'll pick points really far in either direction on the - # line and call it good enough. - lines.append(shgeo.LineString((position + 100000 * direction, position - 100000 * direction))) + # convert the size from viewbox-relative to real-world pixels + viewbox_transform = get_viewbox_transform(svg) + simpletransform.applyTransformToPoint(simpletransform.invertTransform(viewbox_transform), doc_size) - intersection = lines[0].intersection(lines[1]) + default = [doc_size[0] / 2.0, doc_size[1] / 2.0] + simpletransform.applyTransformToPoint(viewbox_transform, default) + default = Point(*default) - if isinstance(intersection, shgeo.Point): - origin = [intersection.x, intersection.y] - simpletransform.applyTransformToPoint(viewbox_transform, origin) - return Point(*origin) - else: - # Either the two guides are the same line, or they're parallel. return default diff --git a/messages.po b/messages.po index 5443542a..2d082dd5 100644 --- a/messages.po +++ b/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2018-08-24 20:45-0400\n" +"POT-Creation-Date: 2018-08-24 20:54-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -18,35 +18,40 @@ msgstr "" "Generated-By: Babel 2.5.3\n" #. command attached to an object -#: lib/commands.py:11 +#: lib/commands.py:12 msgid "Fill stitch starting position" msgstr "" #. command attached to an object -#: lib/commands.py:14 +#: lib/commands.py:15 msgid "Fill stitch ending position" msgstr "" #. command attached to an object -#: lib/commands.py:17 +#: lib/commands.py:18 msgid "Stop (pause machine) after sewing this object" msgstr "" #. command attached to an object -#: lib/commands.py:20 +#: lib/commands.py:21 msgid "Trim thread after sewing this object" msgstr "" #. command attached to an object -#: lib/commands.py:23 +#: lib/commands.py:24 msgid "Ignore this object (do not stitch)" msgstr "" -#. command that affects entire layer -#: lib/commands.py:26 +#. command that affects a layer +#: lib/commands.py:27 msgid "Ignore layer (do not stitch any objects in this layer)" msgstr "" +#. command that affects entire document +#: lib/commands.py:30 +msgid "Origin for exported embroidery files" +msgstr "" + #: lib/elements/auto_fill.py:11 msgid "Auto-Fill" msgstr "" -- cgit v1.2.3