2021-03-12 04:17:19 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
2019-04-10 15:42:49 +00:00
|
|
|
import os
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2023-04-15 06:48:27 +00:00
|
|
|
import inkex
|
2020-05-16 21:01:00 +00:00
|
|
|
|
2024-12-26 15:19:35 +00:00
|
|
|
from ..elements.utils import iterate_nodes, nodes_to_elements
|
2018-08-22 01:43:09 +00:00
|
|
|
from ..i18n import _
|
2023-04-15 06:48:27 +00:00
|
|
|
from ..metadata import InkStitchMetadata
|
2019-02-23 03:07:15 +00:00
|
|
|
from ..svg import generate_unique_id
|
2024-08-29 00:12:16 +00:00
|
|
|
from ..svg.tags import INKSCAPE_GROUPMODE, SVG_GROUP_TAG
|
2023-04-15 06:48:27 +00:00
|
|
|
from ..update import update_inkstitch_document
|
2022-07-29 22:17:50 +00:00
|
|
|
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2023-04-15 06:48:27 +00:00
|
|
|
class InkstitchExtension(inkex.EffectExtension):
|
2018-03-31 00:37:11 +00:00
|
|
|
"""Base class for Inkstitch extensions. Not intended for direct use."""
|
|
|
|
|
2025-02-01 03:32:58 +00:00
|
|
|
# Set to True to hide this extension from release builds of Ink/Stitch. It will
|
|
|
|
# only be available in development installations.
|
|
|
|
DEVELOPMENT_ONLY = False
|
|
|
|
|
2023-04-15 06:48:27 +00:00
|
|
|
def load(self, *args, **kwargs):
|
|
|
|
document = super().load(*args, **kwargs)
|
|
|
|
update_inkstitch_document(document)
|
|
|
|
return document
|
|
|
|
|
2018-08-20 02:14:10 +00:00
|
|
|
@classmethod
|
|
|
|
def name(cls):
|
2023-07-27 09:28:55 +00:00
|
|
|
# Convert CamelCase to snake_case
|
2023-07-29 08:14:50 +00:00
|
|
|
return cls.__name__[0].lower() + ''.join([x if x.islower() else f'_{x.lower()}'
|
|
|
|
for x in cls.__name__[1:]])
|
2018-08-20 02:14:10 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
def hide_all_layers(self):
|
|
|
|
for g in self.document.getroot().findall(SVG_GROUP_TAG):
|
|
|
|
if g.get(INKSCAPE_GROUPMODE) == "layer":
|
|
|
|
g.set("style", "display:none")
|
|
|
|
|
2021-07-21 15:15:46 +00:00
|
|
|
def get_current_layer(self):
|
2018-11-15 01:23:06 +00:00
|
|
|
# if no layer is selected, inkex defaults to the root, which isn't
|
|
|
|
# particularly useful
|
2021-07-21 15:15:46 +00:00
|
|
|
current_layer = self.svg.get_current_layer()
|
|
|
|
if current_layer is self.document.getroot():
|
2018-11-15 01:23:06 +00:00
|
|
|
try:
|
2021-07-21 15:15:46 +00:00
|
|
|
current_layer = self.document.xpath(".//svg:g[@inkscape:groupmode='layer']", namespaces=inkex.NSS)[0]
|
2018-11-15 01:23:06 +00:00
|
|
|
except IndexError:
|
|
|
|
# No layers at all?? Fine, we'll stick with the default.
|
|
|
|
pass
|
2021-07-21 15:15:46 +00:00
|
|
|
return current_layer
|
2018-11-15 01:23:06 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
def no_elements_error(self):
|
2022-04-24 06:27:42 +00:00
|
|
|
if self.svg.selection:
|
2019-05-09 00:17:49 +00:00
|
|
|
# l10n This was previously: "No embroiderable paths selected."
|
2023-05-03 18:03:07 +00:00
|
|
|
inkex.errormsg(_("Ink/Stitch doesn't know how to work with any of the objects you've selected. "
|
|
|
|
"Please check if selected elements are visible.") + "\n")
|
2018-08-22 00:32:50 +00:00
|
|
|
else:
|
2019-05-09 00:17:49 +00:00
|
|
|
inkex.errormsg(_("There are no objects in the entire document that Ink/Stitch knows how to work with.") + "\n")
|
|
|
|
|
2021-07-21 15:15:46 +00:00
|
|
|
inkex.errormsg(_("Tip: Run Extensions > Ink/Stitch > Troubleshoot > Troubleshoot Objects") + "\n")
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2020-05-16 21:01:00 +00:00
|
|
|
def get_nodes(self, troubleshoot=False):
|
2022-12-12 03:18:09 +00:00
|
|
|
# Postorder traversal of selected nodes and their descendants.
|
|
|
|
# Returns all nodes if there is no selection.
|
2024-08-29 00:12:16 +00:00
|
|
|
if self.svg.selection:
|
|
|
|
selection = list(self.svg.selection)
|
|
|
|
else:
|
|
|
|
selection = None
|
|
|
|
|
|
|
|
return iterate_nodes(self.document.getroot(), selection=selection, troubleshoot=troubleshoot)
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2020-05-16 21:01:00 +00:00
|
|
|
def get_elements(self, troubleshoot=False):
|
|
|
|
self.elements = nodes_to_elements(self.get_nodes(troubleshoot))
|
2018-03-31 00:37:11 +00:00
|
|
|
if self.elements:
|
|
|
|
return True
|
2020-05-16 21:01:00 +00:00
|
|
|
if not troubleshoot:
|
2018-03-31 00:37:11 +00:00
|
|
|
self.no_elements_error()
|
2020-05-16 21:01:00 +00:00
|
|
|
return False
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2021-08-07 16:37:17 +00:00
|
|
|
def elements_to_stitch_groups(self, elements):
|
2024-12-26 15:19:35 +00:00
|
|
|
next_elements = [None]
|
|
|
|
if len(elements) > 1:
|
|
|
|
next_elements = elements[1:] + next_elements
|
2024-03-26 06:10:40 +00:00
|
|
|
stitch_groups = []
|
2024-12-26 15:19:35 +00:00
|
|
|
for element, next_element in zip(elements, next_elements):
|
2024-03-26 06:10:40 +00:00
|
|
|
if stitch_groups:
|
|
|
|
last_stitch_group = stitch_groups[-1]
|
2018-03-31 00:37:11 +00:00
|
|
|
else:
|
2024-03-26 06:10:40 +00:00
|
|
|
last_stitch_group = None
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2024-12-26 15:19:35 +00:00
|
|
|
stitch_groups.extend(element.embroider(last_stitch_group, next_element))
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2024-03-26 06:10:40 +00:00
|
|
|
return stitch_groups
|
2018-04-13 00:05:01 +00:00
|
|
|
|
2018-04-14 01:23:00 +00:00
|
|
|
def get_inkstitch_metadata(self):
|
2021-07-25 05:24:34 +00:00
|
|
|
return InkStitchMetadata(self.svg)
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-06-13 02:15:32 +00:00
|
|
|
def get_base_file_name(self):
|
|
|
|
svg_filename = self.document.getroot().get(inkex.addNS('docname', 'sodipodi'), "embroidery.svg")
|
|
|
|
|
2019-04-10 15:42:49 +00:00
|
|
|
return os.path.splitext(svg_filename)[0]
|
2018-06-13 02:15:32 +00:00
|
|
|
|
new extension: Auto-Route Satin Columns (#330)
**video demo:** https://www.youtube.com/watch?v=tbghtqziB1g
This branch adds a new extension, Auto-Route Satin Columns, implementing #214! This is a huge new feature that opens the door wide for exciting stuff like lettering (#142).
To use it, select some satin columns and run the extension. After a few seconds, it will replace your satins with a new set with a logical stitching order. Under-pathing and jump-stitches will be added as necessary, and satins will be broken to facilitate jumps. The resulting satins will retain all of the parameters you had set on the original satins, including underlay, zig-zag spacing, etc.
By default, it will choose the left-most extreme as the starting point and the right-most extreme as the ending point (even if these occur partway through a satin such as the left edge of a letter "o"). You can override this by attaching the new "Auto-route satin stitch starting/ending position" commands.
There's also an option to add trims instead of jump stitches. Any jump stitch over 1mm is trimmed. I might make this configurable in the future but in my tests it seems to do a good job. Trim commands are added to the SVG, so it's easy enough to modify/delete as you see fit.
2018-10-30 23:43:21 +00:00
|
|
|
def uniqueId(self, prefix, make_new_id=True):
|
|
|
|
"""Override inkex.Effect.uniqueId with a nicer naming scheme."""
|
2019-02-23 03:07:15 +00:00
|
|
|
return generate_unique_id(self.document, prefix)
|