From 5ea98e5e89405645f6e36125056a131c923b0324 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Sun, 4 Jul 2021 11:45:06 +0200 Subject: [PATCH] convert satin to stroke (#1265) --- lib/extensions/__init__.py | 2 + lib/extensions/convert_to_stroke.py | 58 +++++++++++++++++++++++++++++ templates/convert_to_stroke.xml | 20 ++++++++++ 3 files changed, 80 insertions(+) create mode 100644 lib/extensions/convert_to_stroke.py create mode 100644 templates/convert_to_stroke.xml diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py index 25f835c33..9a11e3bd8 100644 --- a/lib/extensions/__init__.py +++ b/lib/extensions/__init__.py @@ -9,6 +9,7 @@ from .auto_satin import AutoSatin from .break_apart import BreakApart from .cleanup import Cleanup from .convert_to_satin import ConvertToSatin +from .convert_to_stroke import ConvertToStroke from .cut_satin import CutSatin from .duplicate_params import DuplicateParams from .embroider_settings import EmbroiderSettings @@ -44,6 +45,7 @@ __all__ = extensions = [StitchPlanPreview, LayerCommands, GlobalCommands, ConvertToSatin, + ConvertToStroke, CutSatin, AutoSatin, Lettering, diff --git a/lib/extensions/convert_to_stroke.py b/lib/extensions/convert_to_stroke.py new file mode 100644 index 000000000..dfaef6153 --- /dev/null +++ b/lib/extensions/convert_to_stroke.py @@ -0,0 +1,58 @@ +# Authors: see git history +# +# Copyright (c) 2010 Authors +# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. + +import inkex +from lxml import etree + +from ..elements import SatinColumn +from ..i18n import _ +from ..svg import get_correction_transform +from ..svg.tags import SVG_PATH_TAG +from .base import InkstitchExtension + + +class ConvertToStroke(InkstitchExtension): + """Convert a satin column into a running stitch.""" + + def __init__(self, *args, **kwargs): + InkstitchExtension.__init__(self, *args, **kwargs) + self.arg_parser.add_argument("-k", "--keep_satin", type=inkex.Boolean, default=False, dest="keep_satin") + + def effect(self): + if not self.svg.selected or not self.get_elements(): + inkex.errormsg(_("Please select at least one satin column to convert to a running stitch.")) + return + + if not any(isinstance(item, SatinColumn) for item in self.elements): + # L10N: Convert To Satin extension, user selected one or more objects that were not lines. + inkex.errormsg(_("Please select at least one satin column to convert to a running stitch.")) + return + + for element in self.elements: + if not isinstance(element, SatinColumn): + continue + + parent = element.node.getparent() + center_line = element.center_line.simplify(0.05) + + d = "M" + for x, y in center_line.coords: + d += "%s,%s " % (x, y) + d += " " + + stroke_element = etree.Element(SVG_PATH_TAG, + { + "id": self.uniqueId("path"), + "style": self.path_style(element), + "transform": get_correction_transform(element.node), + "d": d + }) + parent.insert(parent.index(element.node), stroke_element) + if not self.options.keep_satin: + parent.remove(element.node) + + def path_style(self, element): + color = element.get_style('stroke', '#000000') + return "stroke:%s;stroke-width:1px;stroke-dasharray:3, 1;fill:none" % (color) diff --git a/templates/convert_to_stroke.xml b/templates/convert_to_stroke.xml new file mode 100644 index 000000000..620258bae --- /dev/null +++ b/templates/convert_to_stroke.xml @@ -0,0 +1,20 @@ + + + {% trans %}Convert Satin to Stroke{% endtrans %} + org.inkstitch.convert_to_stroke.{{ locale }} + convert_to_stroke + {% trans %}Converts a satin column into a running stitch.{% endtrans %} + false + + all + + + + + + + +