diff options
| author | George Steel <george.steel@gmail.com> | 2022-12-23 19:26:43 -0500 |
|---|---|---|
| committer | George Steel <george.steel@gmail.com> | 2022-12-23 19:26:43 -0500 |
| commit | 7d60b8e9d31ff8b0d98eba2bbc5ad9415c18170d (patch) | |
| tree | dbdca3cc6ae29392c8a4be5cd1f91211aaf0d499 /lib/extensions | |
| parent | 54b0a3d6bf77b4c7b23f299ae1600399b80092df (diff) | |
| parent | aaa9daa86ccd37fd9d8f8840ce9bd9dcc95b2c92 (diff) | |
Merge branch 'main' of https://github.com/inkstitch/inkstitch into george-steel/random-base-satin
Diffstat (limited to 'lib/extensions')
| -rw-r--r-- | lib/extensions/__init__.py | 2 | ||||
| -rw-r--r-- | lib/extensions/cutwork_segmentation.py | 9 | ||||
| -rw-r--r-- | lib/extensions/jump_to_stroke.py | 56 |
3 files changed, 64 insertions, 3 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py index a2261a80..365a47d8 100644 --- a/lib/extensions/__init__.py +++ b/lib/extensions/__init__.py @@ -25,6 +25,7 @@ from .gradient_blocks import GradientBlocks from .input import Input from .install import Install from .install_custom_palette import InstallCustomPalette +from .jump_to_stroke import JumpToStroke from .layer_commands import LayerCommands from .lettering import Lettering from .lettering_custom_font_dir import LetteringCustomFontDir @@ -69,6 +70,7 @@ __all__ = extensions = [StitchPlanPreview, CommandsScaleSymbols, ConvertToSatin, ConvertToStroke, + JumpToStroke, CutSatin, AutoSatin, AutoRun, diff --git a/lib/extensions/cutwork_segmentation.py b/lib/extensions/cutwork_segmentation.py index 672aeade..22b39e4b 100644 --- a/lib/extensions/cutwork_segmentation.py +++ b/lib/extensions/cutwork_segmentation.py @@ -61,9 +61,9 @@ class CutworkSegmentation(InkstitchExtension): self.sectors = {index: sector for index, sector in self.sectors.items() if sector['start'] != sector['end']} self.new_elements = [] + parent = None for element in self.elements: if isinstance(element, Stroke): - # save parent and index to be able to position and insert new elements later on parent = element.node.getparent() index = parent.index(element.node) @@ -73,8 +73,11 @@ class CutworkSegmentation(InkstitchExtension): # fill self.new_elements list with line segments self._prepare_line_sections(element, linestring.coords) - self._insert_elements(parent, element, index) + if parent is None: + inkex.errormsg(_("Please select at least one element with a stroke color.")) + return + self._insert_elements(parent, index) self._remove_originals() def _get_sectors(self, angle): @@ -150,7 +153,7 @@ class CutworkSegmentation(InkstitchExtension): # clear point_list in self.sectors self.sectors[sector['id']].update({'point_list': []}) - def _insert_elements(self, parent, element, index): + def _insert_elements(self, parent, index): self.new_elements.reverse() if self.options.sort_by_color is True: self.new_elements = sorted(self.new_elements, key=lambda x: x[1], reverse=True) diff --git a/lib/extensions/jump_to_stroke.py b/lib/extensions/jump_to_stroke.py new file mode 100644 index 00000000..7cf6ff17 --- /dev/null +++ b/lib/extensions/jump_to_stroke.py @@ -0,0 +1,56 @@ +# Authors: see git history +# +# Copyright (c) 2010 Authors +# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. + +from inkex import DirectedLineSegment, PathElement, errormsg + +from ..i18n import _ +from ..svg import PIXELS_PER_MM, get_correction_transform +from .base import InkstitchExtension + + +class JumpToStroke(InkstitchExtension): + """Adds a running stitch as a connection between two (or more) selected elements. + The elements must have the same color and a minimum distance (collapse_len).""" + + def effect(self): + if not self.svg.selection or not self.get_elements() or len(self.elements) < 2: + errormsg(_("Please select at least two elements to convert the jump stitch to a running stitch.")) + return + + last_stitch_group = None + last_color = None + for element in self.elements: + stitch_group = element.to_stitch_groups(last_stitch_group) + end = stitch_group[-1].stitches[-1] + if last_stitch_group is not None and element.color == last_color: + start = last_stitch_group.stitches[-1] + self.generate_stroke(element, start, end) + + last_stitch_group = stitch_group[-1] + last_color = element.color + + def generate_stroke(self, element, start, end): + node = element.node + parent = node.getparent() + index = parent.index(node) + + # do not add a running stitch if the distance is smaller than the collapse setting + self.metadata = self.get_inkstitch_metadata() + collapse_len = self.metadata['collapse_len_mm'] or 3.0 + collapse_len *= PIXELS_PER_MM + line = DirectedLineSegment((start.x, start.y), (end.x, end.y)) + if collapse_len > line.length: + return + + path = f'M {start.x}, {start.y} L {end.x}, {end.y}' + color = element.color + style = f'stroke:{color};stroke-width:1px;stroke-dasharray:3, 1;fill:none;' + + line = PathElement(d=path, style=style, transform=get_correction_transform(node)) + parent.insert(index, line) + + +if __name__ == '__main__': + JumpToStroke().run() |
