summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaalleen <36401965+kaalleen@users.noreply.github.com>2024-05-01 19:44:04 +0200
committerGitHub <noreply@github.com>2024-05-01 19:44:04 +0200
commit298f911ec588b99786e6adcc4ae5015c1717fc3d (patch)
treea0cd60788f90f98d909757615032b9971bd0ca4a
parent2ee4175437bbfcddf98e5eacba9b019113716ac8 (diff)
Add Jump to Trim Extension (#2864)
-rw-r--r--lib/extensions/__init__.py2
-rw-r--r--lib/extensions/jump_to_trim.py64
-rw-r--r--templates/jump_to_trim.xml34
3 files changed, 100 insertions, 0 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py
index 86c75395..c0c3bb65 100644
--- a/lib/extensions/__init__.py
+++ b/lib/extensions/__init__.py
@@ -27,6 +27,7 @@ from .input import Input
from .install import Install
from .install_custom_palette import InstallCustomPalette
from .jump_to_stroke import JumpToStroke
+from .jump_to_trim import JumpToTrim
from .layer_commands import LayerCommands
from .lettering import Lettering
from .lettering_along_path import LetteringAlongPath
@@ -88,6 +89,7 @@ __all__ = extensions = [ApplyPalette,
Install,
InstallCustomPalette,
JumpToStroke,
+ JumpToTrim,
LayerCommands,
Lettering,
LetteringAlongPath,
diff --git a/lib/extensions/jump_to_trim.py b/lib/extensions/jump_to_trim.py
new file mode 100644
index 00000000..44cf460d
--- /dev/null
+++ b/lib/extensions/jump_to_trim.py
@@ -0,0 +1,64 @@
+# Authors: see git history
+#
+# Copyright (c) 2024 Authors
+# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
+
+from inkex import Boolean, DirectedLineSegment
+
+from ..commands import add_commands
+from ..svg import PIXELS_PER_MM
+from .base import InkstitchExtension
+
+
+class JumpToTrim(InkstitchExtension):
+ """Adds a trim command to avoid jump stitches."""
+
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self, *args, **kwargs)
+ self.arg_parser.add_argument("--tab")
+
+ self.arg_parser.add_argument("-i", "--minimum-jump-length", type=float, default=3.0, dest="min_jump")
+ self.arg_parser.add_argument("-a", "--maximum-jump-length", type=float, default=0, dest="max_jump")
+ self.arg_parser.add_argument("-t", "--use-command-symbols", type=Boolean, default=False, dest="use_command_symbols")
+
+ def effect(self):
+ self._set_selection()
+ self.get_elements()
+
+ last_element = None
+ last_stitch_group = None
+ for element in self.elements:
+ stitch_groups = element.to_stitch_groups(last_stitch_group)
+
+ for stitch_group in stitch_groups:
+ if last_stitch_group is None or stitch_group.color != last_stitch_group.color:
+ last_stitch_group = stitch_group
+ continue
+ start = last_stitch_group.stitches[-1]
+ end = stitch_group.stitches[0]
+
+ last_stitch_group = stitch_group
+
+ line = DirectedLineSegment((start.x, start.y), (end.x, end.y))
+ # do not add a running stitch if the distance is smaller than min_jump setting
+ if line.length < self.options.min_jump * PIXELS_PER_MM:
+ continue
+ # do not add a running stitch if the distance is longer than max_jump setting
+ if self.options.max_jump > 0 and line.length > self.options.max_jump * PIXELS_PER_MM:
+ continue
+ self._add_trim(last_element)
+ last_element = element
+
+ def _add_trim(self, element):
+ if self.options.use_command_symbols:
+ add_commands(element, ["trim"])
+ else:
+ element.node.set('inkstitch:trim_after', True)
+
+ def _set_selection(self):
+ if not self.svg.selection:
+ self.svg.selection.clear()
+
+
+if __name__ == '__main__':
+ JumpToTrim().run()
diff --git a/templates/jump_to_trim.xml b/templates/jump_to_trim.xml
new file mode 100644
index 00000000..5d0831a3
--- /dev/null
+++ b/templates/jump_to_trim.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<inkscape-extension translationdomain="inkstitch" xmlns="http://www.inkscape.org/namespace/inkscape/extension">
+ <name>Jump Stitch to Trim Command</name>
+ <id>org.{{ id_inkstitch }}.jump_to_trim</id>
+ <param name="extension" type="string" gui-hidden="true">jump_to_trim</param>
+ <effect>
+ <object-type>all</object-type>
+ <effects-menu>
+ <submenu name="{{ menu_inkstitch }}" translatable="no">
+ <submenu name="Commands" />
+ </submenu>
+ </effects-menu>
+ </effect>
+ <param name="tab" type="notebook">
+ <page name="options" gui-text="Options">
+ <param name="minimum-jump-length" type="float" precision="2" min="0" max="100"
+ gui-text="Convert jumps not shorter than (mm)">3.0</param>
+ <param name="maximum-jump-length" type="float" precision="2" min="0" max="10000"
+ gui-text="Convert jumps not longer than (mm)">0</param>
+ <spacer />
+ <separator />
+ <spacer />
+ <param name="use-command-symbol" type="boolean" indent="1" gui-text="Use command symbol">false</param>
+ </page>
+ <page name="info" gui-text="Help">
+ <label appearance="header">This extension inserts trim commands to avoid jump stitches.</label>
+ <label>Get more information on our website</label>
+ <label appearance="url">https://inkstitch.org/docs/commands/#jump-to-trim</label>
+ </page>
+ </param>
+ <script>
+ {{ command_tag | safe }}
+ </script>
+</inkscape-extension>