diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2021-11-07 20:13:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-07 20:13:08 +0100 |
| commit | 87403aec4edd9496645556b7358ecc5f02637e6d (patch) | |
| tree | 6248b12a62ba627c9366f70061bed517d2a41a65 | |
| parent | 8d25615ce5cdb84e4ff1a9bf0dafce7b2e85c917 (diff) | |
add stitch plan options (#1418)
| -rw-r--r-- | lib/extensions/stitch_plan_preview.py | 58 | ||||
| -rw-r--r-- | templates/stitch_plan_preview.xml | 9 |
2 files changed, 65 insertions, 2 deletions
diff --git a/lib/extensions/stitch_plan_preview.py b/lib/extensions/stitch_plan_preview.py index c50fa738..e893e8fd 100644 --- a/lib/extensions/stitch_plan_preview.py +++ b/lib/extensions/stitch_plan_preview.py @@ -3,12 +3,23 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. +from inkex import Boolean +from lxml import etree + from ..stitch_plan import stitch_groups_to_stitch_plan from ..svg import render_stitch_plan +from ..svg.tags import (INKSCAPE_GROUPMODE, SVG_DEFS_TAG, SVG_GROUP_TAG, + SVG_PATH_TAG) from .base import InkstitchExtension class StitchPlanPreview(InkstitchExtension): + def __init__(self, *args, **kwargs): + InkstitchExtension.__init__(self, *args, **kwargs) + self.arg_parser.add_argument("-s", "--move-to-side", type=Boolean, default=True, dest="move_to_side") + self.arg_parser.add_argument("-v", "--layer-visibility", type=int, default=0, dest="layer_visibility") + self.arg_parser.add_argument("-n", "--needle-points", type=Boolean, default=False, dest="needle_points") + def effect(self): # delete old stitch plan svg = self.document.getroot() @@ -27,6 +38,49 @@ class StitchPlanPreview(InkstitchExtension): stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len) render_stitch_plan(svg, stitch_plan, realistic) - # translate stitch plan to the right side of the canvas + # apply options layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']") - layer.set('transform', 'translate(%s)' % svg.get('viewBox', '0 0 800 0').split(' ')[2]) + + # update layer visibilty 0 = unchanged, 1 = hidden, 2 = lower opacity + if self.options.layer_visibility == 1: + self.hide_all_layers() + layer.set('style', None) + + if self.options.layer_visibility == 2: + for g in self.document.getroot().findall(SVG_GROUP_TAG): + if g.get(INKSCAPE_GROUPMODE) == "layer" and not g == layer: + g.set("style", "opacity:0.4") + + # translate stitch plan to the right side of the canvas + if self.options.move_to_side: + layer.set('transform', 'translate(%s)' % svg.get('viewBox', '0 0 800 0').split(' ')[2]) + else: + layer.set('transform', None) + + # display needle points + if self.options.needle_points: + markers = 'marker-mid:url(#inkstitch-needle-point);marker-start:url(#inkstitch-needle-point);marker-end:url(#inkstitch-needle-point)' + for element in layer.iterdescendants(SVG_PATH_TAG): + style = ';'.join([element.get('style'), markers]) + element.set('style', style) + self.ensure_marker() + + def ensure_marker(self): + xpath = ".//svg:marker[@id='inkstitch-needle-point']" + point_marker = self.document.getroot().xpath(xpath) + + if not point_marker: + # get or create def element + defs = self.document.find(SVG_DEFS_TAG) + if defs is None: + defs = etree.SubElement(self.document, SVG_DEFS_TAG) + + # insert marker + marker = """<marker + orient="auto" + id="inkstitch-needle-point"> + <circle + cx="0" cy="0" r="1.5" + style="fill:context-stroke;opacity:0.8;" /> + </marker>""" + defs.append(etree.fromstring(marker)) diff --git a/templates/stitch_plan_preview.xml b/templates/stitch_plan_preview.xml index 57762743..a40f79e0 100644 --- a/templates/stitch_plan_preview.xml +++ b/templates/stitch_plan_preview.xml @@ -11,6 +11,15 @@ </submenu> </effects-menu> </effect> + <param name="move-to-side" type="boolean" gui-text="Move stitch plan beside the canvas">true</param> + <param name="layer-visibility" type="optiongroup" appearance="combo" gui-text="Design layer visibity"> + <option value="0">Unchanged</option> + <option value="1">Hidden</option> + <option value="2">Lower opacity</option> + </param> + <param name="needle-points" type="boolean" gui-text="Needle points">false</param> + <separator /> + <label>Hit Ctrl+Z to undo this action after inspection.</label> <script> {{ command_tag | safe }} </script> |
