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-02-23 03:07:15 +00:00
|
|
|
import os
|
2018-08-23 02:48:40 +00:00
|
|
|
import sys
|
2020-04-25 12:45:27 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
from random import random
|
2024-08-14 23:40:42 +00:00
|
|
|
from typing import List
|
2020-04-25 12:45:27 +00:00
|
|
|
|
2019-02-23 03:07:15 +00:00
|
|
|
import inkex
|
2021-03-04 17:40:53 +00:00
|
|
|
from shapely import geometry as shgeo
|
2024-08-14 23:40:42 +00:00
|
|
|
from shapely import get_coordinates
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2020-04-25 12:45:27 +00:00
|
|
|
from .i18n import N_, _
|
|
|
|
from .svg import (apply_transforms, generate_unique_id,
|
|
|
|
get_correction_transform, get_document, get_node_transform)
|
2024-08-14 23:40:42 +00:00
|
|
|
from .svg.svg import copy_no_children, point_upwards
|
2020-04-25 12:45:27 +00:00
|
|
|
from .svg.tags import (CONNECTION_END, CONNECTION_START, CONNECTOR_TYPE,
|
2025-03-02 19:54:56 +00:00
|
|
|
INKSCAPE_LABEL, SVG_SYMBOL_TAG, SVG_USE_TAG, XLINK_HREF)
|
2020-04-25 12:45:27 +00:00
|
|
|
from .utils import Point, cache, get_bundled_dir
|
2025-03-02 19:54:56 +00:00
|
|
|
from .utils.geometry import ensure_multi_polygon
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2018-08-18 00:55:43 +00:00
|
|
|
COMMANDS = {
|
2018-08-20 19:49:19 +00:00
|
|
|
# L10N command attached to an object
|
2024-12-07 14:20:11 +00:00
|
|
|
"starting_point": N_("Starting position"),
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2018-08-20 19:49:19 +00:00
|
|
|
# L10N command attached to an object
|
2024-12-07 14:20:11 +00:00
|
|
|
"ending_point": N_("Ending position"),
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2022-05-24 17:40:30 +00:00
|
|
|
# L10N command attached to an object
|
2024-12-07 14:20:11 +00:00
|
|
|
"target_point": N_("Target position"),
|
2022-05-24 17:40:30 +00:00
|
|
|
|
2022-05-18 14:02:07 +00:00
|
|
|
# L10N command attached to an object
|
2024-12-07 14:20:11 +00:00
|
|
|
"autoroute_start": N_("Auto-route starting position"),
|
2022-05-18 14:02:07 +00:00
|
|
|
|
|
|
|
# L10N command attached to an object
|
2024-12-07 14:20:11 +00:00
|
|
|
"autoroute_end": N_("Auto-route ending position"),
|
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
|
|
|
|
2018-08-20 19:49:19 +00:00
|
|
|
# L10N command attached to an object
|
2020-04-02 16:35:43 +00:00
|
|
|
"stop": N_("Stop (pause machine) after sewing this object"),
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2018-08-20 19:49:19 +00:00
|
|
|
# L10N command attached to an object
|
2020-04-02 16:35:43 +00:00
|
|
|
"trim": N_("Trim thread after sewing this object"),
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2018-08-20 19:49:19 +00:00
|
|
|
# L10N command attached to an object
|
2020-04-02 16:35:43 +00:00
|
|
|
"ignore_object": N_("Ignore this object (do not stitch)"),
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2018-09-29 20:00:36 +00:00
|
|
|
# L10N command attached to an object
|
2020-04-02 16:35:43 +00:00
|
|
|
"satin_cut_point": N_("Satin cut point (use with Cut Satin Column)"),
|
2018-09-29 20:00:36 +00:00
|
|
|
|
2018-08-23 02:13:51 +00:00
|
|
|
# L10N command that affects a layer
|
2020-04-02 16:35:43 +00:00
|
|
|
"ignore_layer": N_("Ignore layer (do not stitch any objects in this layer)"),
|
2018-08-23 02:13:51 +00:00
|
|
|
|
|
|
|
# L10N command that affects entire document
|
2020-04-02 16:35:43 +00:00
|
|
|
"origin": N_("Origin for exported embroidery files"),
|
2018-08-23 02:48:40 +00:00
|
|
|
|
|
|
|
# L10N command that affects entire document
|
2020-04-02 16:35:43 +00:00
|
|
|
"stop_position": N_("Jump destination for Stop commands (a.k.a. \"Frame Out position\")."),
|
2018-08-18 00:55:43 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 14:20:11 +00:00
|
|
|
OBJECT_COMMANDS = ["starting_point", "ending_point", "target_point", "autoroute_start", "autoroute_end",
|
2022-05-24 17:40:30 +00:00
|
|
|
"stop", "trim", "ignore_object", "satin_cut_point"]
|
2025-03-02 19:54:56 +00:00
|
|
|
HIDDEN_CONNECTOR_COMMANDS = ["starting_point", "ending_point", "autoroute_start", "autoroute_end"]
|
2024-12-07 14:20:11 +00:00
|
|
|
FREE_MOVEMENT_OBJECT_COMMANDS = ["autoroute_start", "autoroute_end"]
|
2018-08-22 00:32:50 +00:00
|
|
|
LAYER_COMMANDS = ["ignore_layer"]
|
2018-08-24 20:29:13 +00:00
|
|
|
GLOBAL_COMMANDS = ["origin", "stop_position"]
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2018-08-03 00:04:08 +00:00
|
|
|
class CommandParseError(Exception):
|
|
|
|
pass
|
2018-06-21 19:41:06 +00:00
|
|
|
|
|
|
|
|
2018-08-17 00:30:37 +00:00
|
|
|
class BaseCommand(object):
|
2018-08-18 00:55:43 +00:00
|
|
|
@property
|
|
|
|
@cache
|
|
|
|
def description(self):
|
|
|
|
return get_command_description(self.command)
|
|
|
|
|
2018-08-17 00:30:37 +00:00
|
|
|
def parse_symbol(self):
|
|
|
|
if self.symbol.tag != SVG_SYMBOL_TAG:
|
|
|
|
raise CommandParseError("use points to non-symbol")
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2018-08-17 00:30:37 +00:00
|
|
|
self.command = self.symbol.get('id')
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2018-08-17 00:30:37 +00:00
|
|
|
if self.command.startswith('inkstitch_'):
|
|
|
|
self.command = self.command[10:]
|
2023-06-23 04:34:30 +00:00
|
|
|
# It is possible that through copy paste or whatever user action a command is defined multiple times
|
|
|
|
# in the defs section. In this case the id will be altered with an additional number (e.g. inkstitch_trim-5)
|
|
|
|
# Let's make sure to remove the number part to recognize the command correctly
|
|
|
|
self.command = self.command.split("-")[0]
|
2018-08-17 00:30:37 +00:00
|
|
|
else:
|
|
|
|
raise CommandParseError("symbol is not an Ink/Stitch command")
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
def get_node_by_url(self, url):
|
2018-08-17 00:30:37 +00:00
|
|
|
# url will be #path12345. Find the corresponding object.
|
|
|
|
if url is None:
|
|
|
|
raise CommandParseError("url is None")
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2018-08-17 00:30:37 +00:00
|
|
|
if not url.startswith('#'):
|
|
|
|
raise CommandParseError("invalid connection url: %s" % url)
|
2018-08-03 00:04:08 +00:00
|
|
|
|
2018-08-17 00:30:37 +00:00
|
|
|
id = url[1:]
|
|
|
|
|
|
|
|
try:
|
|
|
|
return self.svg.xpath(".//*[@id='%s']" % id)[0]
|
|
|
|
except (IndexError, AttributeError):
|
|
|
|
raise CommandParseError("could not find node by url %s" % id)
|
2018-08-03 00:04:08 +00:00
|
|
|
|
2018-08-17 00:30:37 +00:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2018-08-03 00:04:08 +00:00
|
|
|
def __init__(self, connector):
|
2024-08-14 23:40:42 +00:00
|
|
|
self.connector: inkex.Path = connector
|
2018-08-03 00:04:08 +00:00
|
|
|
self.svg = self.connector.getroottree().getroot()
|
|
|
|
|
|
|
|
self.parse_command()
|
2018-06-21 19:41:06 +00:00
|
|
|
|
|
|
|
def parse_connector_path(self):
|
2021-03-04 17:40:53 +00:00
|
|
|
path = inkex.paths.Path(self.connector.get('d')).to_superpath()
|
2018-06-21 19:41:06 +00:00
|
|
|
return apply_transforms(path, self.connector)
|
|
|
|
|
|
|
|
def parse_command(self):
|
|
|
|
path = self.parse_connector_path()
|
2023-08-08 16:57:23 +00:00
|
|
|
if len(path) == 0:
|
|
|
|
raise CommandParseError("connector has no path information")
|
2018-06-21 19:41:06 +00:00
|
|
|
|
|
|
|
neighbors = [
|
2025-03-02 19:54:56 +00:00
|
|
|
self.get_node_by_url(self.connector.get(CONNECTION_START)),
|
|
|
|
self.get_node_by_url(self.connector.get(CONNECTION_END))
|
2018-06-21 19:41:06 +00:00
|
|
|
]
|
|
|
|
|
2025-03-02 19:54:56 +00:00
|
|
|
self.symbol_is_end = neighbors[0].tag != SVG_USE_TAG
|
2024-08-14 23:40:42 +00:00
|
|
|
if self.symbol_is_end:
|
2018-06-21 19:41:06 +00:00
|
|
|
neighbors.reverse()
|
|
|
|
|
2025-03-02 19:54:56 +00:00
|
|
|
if neighbors[0].tag != SVG_USE_TAG:
|
2018-08-03 00:04:08 +00:00
|
|
|
raise CommandParseError("connector does not point to a use tag")
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2025-03-02 19:54:56 +00:00
|
|
|
self.use = neighbors[0]
|
2018-09-29 20:00:36 +00:00
|
|
|
|
2025-03-02 19:54:56 +00:00
|
|
|
self.symbol = self.get_node_by_url(neighbors[0].get(XLINK_HREF))
|
2018-08-17 00:30:37 +00:00
|
|
|
self.parse_symbol()
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2025-03-02 19:54:56 +00:00
|
|
|
self.target: inkex.BaseElement = neighbors[1]
|
|
|
|
|
|
|
|
pos = [float(self.use.get("x", 0)), float(self.use.get("y", 0))]
|
|
|
|
transform = get_node_transform(self.use)
|
|
|
|
pos = inkex.Transform(transform).apply_to_point(pos)
|
|
|
|
self.target_point = pos
|
2018-06-21 19:41:06 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Command('%s', %s)" % (self.command, self.target_point)
|
|
|
|
|
2024-08-14 23:40:42 +00:00
|
|
|
def clone(self, new_target: inkex.BaseElement) -> inkex.BaseElement:
|
|
|
|
"""
|
2024-08-21 03:18:13 +00:00
|
|
|
Clone this command and point it to the new target, positioning it relative to the new target the same as the target
|
2024-08-14 23:40:42 +00:00
|
|
|
"""
|
2024-08-21 03:18:13 +00:00
|
|
|
group: inkex.BaseElement = self.connector.getparent()
|
|
|
|
transform_relative_to_target = -self.target.composed_transform() @ group.composed_transform()
|
2024-08-14 23:40:42 +00:00
|
|
|
|
|
|
|
# Clone group
|
|
|
|
cloned_group = copy_no_children(self.connector.getparent())
|
2024-08-21 03:18:13 +00:00
|
|
|
cloned_group.transform = new_target.transform @ transform_relative_to_target
|
2024-08-14 23:40:42 +00:00
|
|
|
new_target.getparent().append(cloned_group)
|
|
|
|
|
|
|
|
symbol = copy_no_children(self.use)
|
|
|
|
cloned_group.append(symbol)
|
|
|
|
point_upwards(symbol)
|
|
|
|
|
|
|
|
# Copy connector
|
|
|
|
connector = copy_no_children(self.connector)
|
2024-08-21 03:18:13 +00:00
|
|
|
cloned_group.insert(0, connector)
|
2024-08-14 23:40:42 +00:00
|
|
|
if self.symbol_is_end:
|
|
|
|
symbol_attr = CONNECTION_END
|
|
|
|
target_attr = CONNECTION_START
|
|
|
|
else:
|
|
|
|
symbol_attr = CONNECTION_START
|
|
|
|
target_attr = CONNECTION_END
|
|
|
|
connector.set(symbol_attr, f"#{symbol.get_id()}")
|
|
|
|
connector.set(target_attr, f"#{new_target.get_id()}")
|
|
|
|
|
|
|
|
return cloned_group
|
|
|
|
|
2018-08-17 00:30:37 +00:00
|
|
|
|
|
|
|
class StandaloneCommand(BaseCommand):
|
|
|
|
def __init__(self, use):
|
|
|
|
self.node = use
|
2018-08-17 02:50:34 +00:00
|
|
|
self.svg = self.node.getroottree().getroot()
|
2018-08-03 00:04:08 +00:00
|
|
|
|
|
|
|
self.parse_command()
|
|
|
|
|
|
|
|
def parse_command(self):
|
2018-08-17 00:30:37 +00:00
|
|
|
self.symbol = self.get_node_by_url(self.node.get(XLINK_HREF))
|
|
|
|
|
|
|
|
if self.symbol.tag != SVG_SYMBOL_TAG:
|
|
|
|
raise CommandParseError("use points to non-symbol")
|
|
|
|
|
|
|
|
self.parse_symbol()
|
2018-08-03 00:04:08 +00:00
|
|
|
|
2018-08-23 01:56:36 +00:00
|
|
|
@property
|
|
|
|
@cache
|
|
|
|
def point(self):
|
|
|
|
pos = [float(self.node.get("x", 0)), float(self.node.get("y", 0))]
|
|
|
|
transform = get_node_transform(self.node)
|
2021-03-04 17:40:53 +00:00
|
|
|
pos = inkex.transforms.Transform(transform).apply_to_point(pos)
|
2018-08-23 01:56:36 +00:00
|
|
|
|
|
|
|
return Point(*pos)
|
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2024-08-21 03:18:13 +00:00
|
|
|
def get_command_description(command: str) -> str:
|
2018-08-23 02:48:40 +00:00
|
|
|
return COMMANDS[command]
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2018-08-03 00:04:08 +00:00
|
|
|
|
2024-08-21 03:18:13 +00:00
|
|
|
def point_command_symbols_up(node: inkex.BaseElement) -> None:
|
|
|
|
"""
|
|
|
|
Find all command symbols in the subtree and alter their transformations so they're pointing upwards.
|
|
|
|
"""
|
|
|
|
xpath = ".//svg:use"
|
|
|
|
uses = node.xpath(xpath, namespaces=inkex.NSS)
|
|
|
|
for use in uses:
|
|
|
|
if use.href.get('id').startswith('inkstitch_'):
|
|
|
|
point_upwards(use)
|
|
|
|
|
|
|
|
|
|
|
|
def find_commands(node: inkex.BaseElement) -> List[Command]:
|
2018-06-21 19:41:06 +00:00
|
|
|
"""Find the symbols this node is connected to and return them as Commands"""
|
|
|
|
|
|
|
|
# find all paths that have this object as a connection
|
2024-08-21 03:18:13 +00:00
|
|
|
id = node.get('id')
|
|
|
|
xpath = f".//*[@inkscape:connection-start='#{id}' or @inkscape:connection-end='#{id}']"
|
2018-06-21 19:41:06 +00:00
|
|
|
connectors = node.getroottree().getroot().xpath(xpath, namespaces=inkex.NSS)
|
|
|
|
|
|
|
|
# try to turn them into commands
|
|
|
|
commands = []
|
|
|
|
for connector in connectors:
|
|
|
|
try:
|
|
|
|
commands.append(Command(connector))
|
2018-08-17 00:30:37 +00:00
|
|
|
except CommandParseError:
|
2018-06-21 19:41:06 +00:00
|
|
|
# Parsing the connector failed, meaning it's not actually an Ink/Stitch command.
|
|
|
|
pass
|
|
|
|
|
|
|
|
return commands
|
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2018-08-03 00:04:08 +00:00
|
|
|
def layer_commands(layer, command):
|
|
|
|
"""Find standalone (unconnected) command symbols in this layer."""
|
|
|
|
|
2018-08-23 02:13:51 +00:00
|
|
|
for global_command in global_commands(layer.getroottree().getroot(), command):
|
|
|
|
if layer in global_command.node.iterancestors():
|
|
|
|
yield global_command
|
2018-08-17 00:30:37 +00:00
|
|
|
|
|
|
|
|
2018-08-23 02:13:51 +00:00
|
|
|
def global_commands(svg, command):
|
|
|
|
"""Find standalone (unconnected) command symbols anywhere in the document."""
|
2018-08-17 00:30:37 +00:00
|
|
|
|
2018-08-23 02:13:51 +00:00
|
|
|
for standalone_command in _standalone_commands(svg):
|
|
|
|
if standalone_command.command == command:
|
|
|
|
yield standalone_command
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2018-08-24 01:46:22 +00:00
|
|
|
|
2018-08-23 02:48:40 +00:00
|
|
|
@cache
|
|
|
|
def global_command(svg, command):
|
|
|
|
"""Find a single command of the specified type.
|
|
|
|
|
|
|
|
If more than one is found, print an error and exit.
|
|
|
|
"""
|
|
|
|
|
|
|
|
commands = list(global_commands(svg, command))
|
|
|
|
|
|
|
|
if len(commands) == 1:
|
|
|
|
return commands[0]
|
|
|
|
elif len(commands) > 1:
|
2021-03-04 17:40:53 +00:00
|
|
|
print(_("Error: there is more than one %(command)s command in the document, but there can only be one. "
|
|
|
|
"Please remove all but one.") % dict(command=command), file=sys.stderr)
|
2018-08-23 02:48:40 +00:00
|
|
|
|
|
|
|
# L10N This is a continuation of the previous error message, letting the user know
|
|
|
|
# what command we're talking about since we don't normally expose the actual
|
|
|
|
# command name to them. Contents of %(description)s are in a separate translation
|
|
|
|
# string.
|
2021-03-04 17:40:53 +00:00
|
|
|
print(_("%(command)s: %(description)s") % dict(command=command, description=_(get_command_description(command))), file=sys.stderr)
|
2018-08-23 02:48:40 +00:00
|
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-08-23 02:13:51 +00:00
|
|
|
|
|
|
|
def _standalone_commands(svg):
|
2018-08-03 00:04:08 +00:00
|
|
|
"""Find all unconnected command symbols in the SVG."""
|
|
|
|
|
|
|
|
xpath = ".//svg:use[starts-with(@xlink:href, '#inkstitch_')]"
|
2018-08-17 02:50:34 +00:00
|
|
|
symbols = svg.xpath(xpath, namespaces=inkex.NSS)
|
2018-08-03 00:04:08 +00:00
|
|
|
|
|
|
|
for symbol in symbols:
|
|
|
|
try:
|
2018-08-23 02:13:51 +00:00
|
|
|
yield StandaloneCommand(symbol)
|
2018-08-03 00:04:08 +00:00
|
|
|
except CommandParseError:
|
|
|
|
pass
|
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2018-06-21 19:41:06 +00:00
|
|
|
def is_command(node):
|
|
|
|
return CONNECTION_START in node.attrib or CONNECTION_END in node.attrib
|
2019-02-23 03:07:15 +00:00
|
|
|
|
|
|
|
|
2020-05-16 21:01:00 +00:00
|
|
|
def is_command_symbol(node):
|
|
|
|
symbol = None
|
|
|
|
xlink = node.get(XLINK_HREF, "")
|
|
|
|
if xlink.startswith("#inkstitch_"):
|
|
|
|
symbol = node.get(XLINK_HREF)[11:]
|
|
|
|
return symbol in COMMANDS
|
|
|
|
|
|
|
|
|
2019-02-23 03:07:15 +00:00
|
|
|
@cache
|
|
|
|
def symbols_path():
|
|
|
|
return os.path.join(get_bundled_dir("symbols"), "inkstitch.svg")
|
|
|
|
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def symbols_svg():
|
|
|
|
with open(symbols_path()) as symbols_file:
|
2021-07-25 05:24:34 +00:00
|
|
|
return inkex.load_svg(symbols_file).getroot()
|
2019-02-23 03:07:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def symbol_defs():
|
2021-07-25 05:24:34 +00:00
|
|
|
return symbols_svg().defs
|
2019-02-23 03:07:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cache
|
2021-07-25 05:24:34 +00:00
|
|
|
def ensure_symbol(svg, command):
|
2019-02-23 03:07:15 +00:00
|
|
|
"""Make sure the command's symbol definition exists in the <svg:defs> tag."""
|
|
|
|
|
2021-07-25 05:24:34 +00:00
|
|
|
# using @cache really just makes sure that we don't bother ensuring the
|
|
|
|
# same symbol is there twice, which would be wasted work
|
|
|
|
|
2019-02-23 03:07:15 +00:00
|
|
|
path = "./*[@id='inkstitch_%s']" % command
|
2021-07-25 05:24:34 +00:00
|
|
|
defs = svg.defs
|
2019-02-23 03:07:15 +00:00
|
|
|
if defs.find(path) is None:
|
2025-03-02 19:54:56 +00:00
|
|
|
symbol = deepcopy(symbol_defs().find(path))
|
|
|
|
symbol.transform = 'scale(0.2)'
|
|
|
|
defs.append(symbol)
|
2019-02-23 03:07:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_group(document, node, command):
|
2021-07-25 05:24:34 +00:00
|
|
|
parent = node.getparent()
|
2024-07-14 13:40:16 +00:00
|
|
|
description = _(get_command_description(command))
|
2021-07-25 05:24:34 +00:00
|
|
|
group = inkex.Group(attrib={
|
|
|
|
"id": generate_unique_id(document, "command_group"),
|
2024-07-14 13:40:16 +00:00
|
|
|
INKSCAPE_LABEL: _("Ink/Stitch Command") + f": {description}",
|
2021-07-25 05:24:34 +00:00
|
|
|
"transform": get_correction_transform(node)
|
|
|
|
})
|
|
|
|
parent.insert(parent.index(node) + 1, group)
|
2021-06-12 09:33:59 +00:00
|
|
|
return group
|
2019-02-23 03:07:15 +00:00
|
|
|
|
|
|
|
|
2022-05-18 14:02:07 +00:00
|
|
|
def add_connector(document, symbol, command, element):
|
2024-08-14 23:40:42 +00:00
|
|
|
# "I'd like it if I could position the connector endpoint nicely but inkscape just
|
|
|
|
# moves it to the element's center immediately after the extension runs." - Lex Neva, rev. 4baced7085
|
|
|
|
# "Maybe we should have the target point be a seperately-moveable node? Sometimes moving the command
|
|
|
|
# node so the line drawn from the command to the centroid of the target is awkward anyway?" - CapellanCitizen
|
|
|
|
|
|
|
|
# Inkscape will draw this connector line from the bounding box center of the two nodes, but
|
|
|
|
# will stop at the first intersection with the path it's pointing to. It is necessary to
|
|
|
|
# compute the target point accurately to what inkscape will do.
|
|
|
|
# If not, then the target position will change when the document is loaded by inkscape and break.
|
|
|
|
# For example, not doing this caused issues when implementing commands attached to clones.
|
2019-02-23 03:07:15 +00:00
|
|
|
start_pos = (symbol.get('x'), symbol.get('y'))
|
2024-08-14 23:40:42 +00:00
|
|
|
centroid_pos = element.node.bounding_box(inkex.Transform(get_node_transform(element.node.getparent()))).center
|
|
|
|
connector_line = shgeo.LineString([start_pos, centroid_pos])
|
|
|
|
if connector_line.intersects(element.shape):
|
|
|
|
end_pos = get_coordinates(connector_line.intersection(element.shape))[0]
|
|
|
|
else:
|
|
|
|
# Sometimes the line won't intersect anything and will go straight to the centroid.
|
|
|
|
end_pos = centroid_pos
|
2019-02-23 03:07:15 +00:00
|
|
|
|
2019-07-04 21:20:31 +00:00
|
|
|
# Make sure the element's XML node has an id so that we can reference it.
|
|
|
|
if element.node.get('id') is None:
|
2021-08-15 16:37:07 +00:00
|
|
|
element.node.set('id', document.get_unique_id("object"))
|
2019-07-04 21:20:31 +00:00
|
|
|
|
2021-07-25 05:24:34 +00:00
|
|
|
path = inkex.PathElement(attrib={
|
|
|
|
"id": generate_unique_id(document, "command_connector"),
|
2024-08-14 23:40:42 +00:00
|
|
|
"d": f"M {start_pos[0]},{start_pos[1]} {end_pos[0]},{end_pos[1]}",
|
2025-03-02 19:54:56 +00:00
|
|
|
"style": "fill:none;stroke:#000000;stroke-width:1;stroke-opacity:0.5;vector-effect: non-scaling-stroke;-inkscape-stroke: hairline;",
|
2024-08-14 23:40:42 +00:00
|
|
|
CONNECTION_START: f"#{symbol.get('id')}",
|
|
|
|
CONNECTION_END: f"#{element.node.get('id')}",
|
2019-02-23 03:07:15 +00:00
|
|
|
|
2021-07-25 05:24:34 +00:00
|
|
|
# l10n: the name of the line that connects a command to the object it applies to
|
|
|
|
INKSCAPE_LABEL: _("connector")
|
|
|
|
})
|
2019-02-23 03:07:15 +00:00
|
|
|
|
2022-05-18 14:02:07 +00:00
|
|
|
if command not in FREE_MOVEMENT_OBJECT_COMMANDS:
|
|
|
|
path.attrib[CONNECTOR_TYPE] = "polyline"
|
2025-03-02 19:54:56 +00:00
|
|
|
if command in HIDDEN_CONNECTOR_COMMANDS:
|
|
|
|
path.style['display'] = 'none'
|
2022-05-18 14:02:07 +00:00
|
|
|
|
2019-02-23 03:07:15 +00:00
|
|
|
symbol.getparent().insert(0, path)
|
|
|
|
|
|
|
|
|
|
|
|
def add_symbol(document, group, command, pos):
|
2021-07-25 05:24:34 +00:00
|
|
|
symbol = inkex.Use(attrib={
|
2021-08-15 16:37:07 +00:00
|
|
|
"id": document.get_unique_id("command_use"),
|
2021-07-25 05:24:34 +00:00
|
|
|
XLINK_HREF: "#inkstitch_%s" % command,
|
|
|
|
"height": "100%",
|
|
|
|
"width": "100%",
|
|
|
|
"x": str(pos.x),
|
|
|
|
"y": str(pos.y),
|
|
|
|
|
|
|
|
# l10n: the name of a command symbol (example: scissors icon for trim command)
|
|
|
|
INKSCAPE_LABEL: _("command marker"),
|
|
|
|
})
|
|
|
|
group.append(symbol)
|
2019-02-23 03:07:15 +00:00
|
|
|
|
2021-07-25 05:24:34 +00:00
|
|
|
return symbol
|
2019-02-23 03:07:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_command_pos(element, index, total):
|
2025-03-02 19:54:56 +00:00
|
|
|
# Put command symbols on the outline of the shape, spaced evenly around it.
|
2022-11-22 09:46:44 +00:00
|
|
|
|
2025-03-02 19:54:56 +00:00
|
|
|
if element.name == "Stroke":
|
|
|
|
shape = element.as_multi_line_string()
|
2022-11-22 09:46:44 +00:00
|
|
|
else:
|
2025-03-02 19:54:56 +00:00
|
|
|
shape = element.shape
|
|
|
|
polygon = ensure_multi_polygon(shape.buffer(0.01)).geoms[-1]
|
|
|
|
outline = polygon.exterior
|
2019-03-09 01:51:23 +00:00
|
|
|
|
|
|
|
# pick this item's spot around the outline and perturb it a bit to avoid
|
|
|
|
# stacking up commands if they add commands multiple times
|
2019-02-23 03:07:15 +00:00
|
|
|
position = index / float(total)
|
2019-03-09 01:51:23 +00:00
|
|
|
position += random() * 0.05
|
2019-02-23 03:07:15 +00:00
|
|
|
|
|
|
|
return outline.interpolate(position, normalized=True)
|
|
|
|
|
|
|
|
|
2023-05-08 15:21:51 +00:00
|
|
|
def add_commands(element, commands, pos=None):
|
2021-07-25 05:24:34 +00:00
|
|
|
svg = get_document(element.node)
|
2019-02-23 03:07:15 +00:00
|
|
|
|
|
|
|
for i, command in enumerate(commands):
|
2021-07-25 05:24:34 +00:00
|
|
|
ensure_symbol(svg, command)
|
2019-02-23 03:07:15 +00:00
|
|
|
|
2021-07-25 05:24:34 +00:00
|
|
|
group = add_group(svg, element.node, command)
|
2023-05-16 16:42:35 +00:00
|
|
|
position = pos
|
|
|
|
if position is None:
|
|
|
|
position = get_command_pos(element, i, len(commands))
|
|
|
|
|
|
|
|
symbol = add_symbol(svg, group, command, position)
|
2022-05-18 14:02:07 +00:00
|
|
|
add_connector(svg, symbol, command, element)
|
2019-08-06 02:42:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_layer_commands(layer, commands):
|
2021-08-03 15:50:01 +00:00
|
|
|
svg = layer.root
|
2022-12-17 11:27:09 +00:00
|
|
|
|
|
|
|
if not layer.tag_name == 'svg':
|
|
|
|
correction_transform = get_correction_transform(layer)
|
|
|
|
else:
|
|
|
|
# No layer selected while trying to include only layer commands: return a error message and exit
|
|
|
|
# Since global and layer commands will not be inserted at the same time, we can check the first command only
|
|
|
|
if commands[0] in LAYER_COMMANDS:
|
|
|
|
inkex.errormsg(_('Please select a layer to include layer commands.'))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# global commands do not necesarrily need a layer
|
|
|
|
correction_transform = ''
|
2019-08-06 02:42:48 +00:00
|
|
|
|
2021-07-25 05:24:34 +00:00
|
|
|
for i, command in enumerate(commands):
|
|
|
|
ensure_symbol(svg, command)
|
2024-07-14 13:40:16 +00:00
|
|
|
description = _(get_command_description(command))
|
2021-07-25 05:24:34 +00:00
|
|
|
layer.append(inkex.Use(attrib={
|
|
|
|
"id": generate_unique_id(svg, "use"),
|
2024-07-14 13:40:16 +00:00
|
|
|
INKSCAPE_LABEL: _("Ink/Stitch Command") + f": {description}",
|
2021-07-25 05:24:34 +00:00
|
|
|
XLINK_HREF: "#inkstitch_%s" % command,
|
|
|
|
"height": "100%",
|
|
|
|
"width": "100%",
|
|
|
|
"x": str(i * 20),
|
|
|
|
"y": "-10",
|
|
|
|
"transform": correction_transform
|
|
|
|
}))
|