inkstitch/lib/commands.py

444 wiersze
15 KiB
Python

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.
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
import inkex
from shapely import geometry as shgeo
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)
from .svg.tags import (CONNECTION_END, CONNECTION_START, CONNECTOR_TYPE,
INKSCAPE_LABEL, INKSTITCH_ATTRIBS, SVG_SYMBOL_TAG,
2020-04-25 12:45:27 +00:00
SVG_USE_TAG, XLINK_HREF)
from .utils import Point, cache, get_bundled_dir
2018-06-21 19:41:06 +00:00
COMMANDS = {
2018-08-20 19:49:19 +00:00
# L10N command attached to an object
"fill_start": N_("Fill stitch starting position"),
2018-08-20 19:49:19 +00:00
# L10N command attached to an object
"fill_end": N_("Fill stitch ending position"),
2022-05-24 17:40:30 +00:00
# L10N command attached to an object
2023-05-01 10:02:49 +00:00
"ripple_target": N_("Target position"),
2022-05-24 17:40:30 +00:00
# L10N command attached to an object
"run_start": N_("Auto-route running stitch starting position"),
# L10N command attached to an object
"run_end": N_("Auto-route running stitch ending position"),
# L10N command attached to an object
"satin_start": N_("Auto-route satin stitch starting position"),
# L10N command attached to an object
"satin_end": N_("Auto-route satin stitch ending position"),
2018-08-20 19:49:19 +00:00
# L10N command attached to an object
"stop": N_("Stop (pause machine) after sewing this object"),
2018-08-20 19:49:19 +00:00
# L10N command attached to an object
"trim": N_("Trim thread after sewing this object"),
2018-08-20 19:49:19 +00:00
# L10N command attached to an object
"ignore_object": N_("Ignore this object (do not stitch)"),
# L10N command attached to an object
"satin_cut_point": N_("Satin cut point (use with Cut Satin Column)"),
# L10N command that affects a layer
"ignore_layer": N_("Ignore layer (do not stitch any objects in this layer)"),
# L10N command that affects entire document
"origin": N_("Origin for exported embroidery files"),
2018-08-23 02:48:40 +00:00
# L10N command that affects entire document
"stop_position": N_("Jump destination for Stop commands (a.k.a. \"Frame Out position\")."),
}
2022-05-24 17:40:30 +00:00
OBJECT_COMMANDS = ["fill_start", "fill_end", "ripple_target", "run_start", "run_end", "satin_start", "satin_end",
"stop", "trim", "ignore_object", "satin_cut_point"]
FREE_MOVEMENT_OBJECT_COMMANDS = ["run_start", "run_end", "satin_start", "satin_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):
@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):
self.connector = connector
self.svg = self.connector.getroottree().getroot()
self.parse_command()
2018-06-21 19:41:06 +00:00
def parse_connector_path(self):
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()
if len(path) == 0:
raise CommandParseError("connector has no path information")
2018-06-21 19:41:06 +00:00
neighbors = [
2018-08-17 00:30:37 +00:00
(self.get_node_by_url(self.connector.get(CONNECTION_START)), path[0][0][1]),
(self.get_node_by_url(self.connector.get(CONNECTION_END)), path[0][-1][1])
2018-06-21 19:41:06 +00:00
]
if neighbors[0][0].tag != SVG_USE_TAG:
neighbors.reverse()
if neighbors[0][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
self.use = neighbors[0][0]
2018-08-17 00:30:37 +00:00
self.symbol = self.get_node_by_url(neighbors[0][0].get(XLINK_HREF))
self.parse_symbol()
2018-06-21 19:41:06 +00:00
self.target = neighbors[1][0]
self.target_point = neighbors[1][1]
def __repr__(self):
return "Command('%s', %s)" % (self.command, self.target_point)
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
@property
@cache
def point(self):
pos = [float(self.node.get("x", 0)), float(self.node.get("y", 0))]
transform = get_node_transform(self.node)
pos = inkex.transforms.Transform(transform).apply_to_point(pos)
return Point(*pos)
2018-08-22 00:32:50 +00:00
def get_command_description(command):
2018-08-23 02:48:40 +00:00
return COMMANDS[command]
2018-08-03 00:04:08 +00:00
2018-06-21 19:41:06 +00:00
def find_commands(node):
"""Find the symbols this node is connected to and return them as Commands"""
# find all paths that have this object as a connection
xpath = ".//*[@inkscape:connection-start='#%(id)s' or @inkscape:connection-end='#%(id)s']" % dict(id=node.get('id'))
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."""
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
def global_commands(svg, command):
"""Find standalone (unconnected) command symbols anywhere in the document."""
2018-08-17 00:30:37 +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:
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.
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
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:
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
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
@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:
return inkex.load_svg(symbols_file).getroot()
@cache
def symbol_defs():
return symbols_svg().defs
@cache
def ensure_symbol(svg, command):
"""Make sure the command's symbol definition exists in the <svg:defs> tag."""
# using @cache really just makes sure that we don't bother ensuring the
# same symbol is there twice, which would be wasted work
path = "./*[@id='inkstitch_%s']" % command
defs = svg.defs
if defs.find(path) is None:
defs.append(deepcopy(symbol_defs().find(path)))
def add_group(document, node, command):
parent = node.getparent()
group = inkex.Group(attrib={
"id": generate_unique_id(document, "command_group"),
INKSCAPE_LABEL: _("Ink/Stitch Command") + ": %s" % get_command_description(command),
"transform": get_correction_transform(node)
})
parent.insert(parent.index(node) + 1, group)
2021-06-12 09:33:59 +00:00
return group
def add_connector(document, symbol, command, element):
# 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.
start_pos = (symbol.get('x'), symbol.get('y'))
end_pos = element.shape.centroid
# Make sure the element's XML node has an id so that we can reference it.
if element.node.get('id') is None:
element.node.set('id', document.get_unique_id("object"))
path = inkex.PathElement(attrib={
"id": generate_unique_id(document, "command_connector"),
"d": "M %s,%s %s,%s" % (start_pos[0], start_pos[1], end_pos.x, end_pos.y),
"style": "stroke:#000000;stroke-width:1px;stroke-opacity:0.5;fill:none;",
CONNECTION_START: "#%s" % symbol.get('id'),
CONNECTION_END: "#%s" % element.node.get('id'),
# l10n: the name of the line that connects a command to the object it applies to
INKSCAPE_LABEL: _("connector")
})
if command not in FREE_MOVEMENT_OBJECT_COMMANDS:
path.attrib[CONNECTOR_TYPE] = "polyline"
symbol.getparent().insert(0, path)
def add_symbol(document, group, command, pos):
symbol = inkex.Use(attrib={
"id": document.get_unique_id("command_use"),
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)
return symbol
def get_command_pos(element, index, total):
# Put command symbols 30 pixels out from the shape, spaced evenly around it.
# get a line running 30 pixels out from the shape
if not isinstance(element.shape.buffer(30), shgeo.MultiPolygon):
outline = element.shape.buffer(30).exterior
else:
polygons = element.shape.buffer(30).geoms
polygon = polygons[len(polygons)-1]
outline = polygon.exterior
# find the top center point on the outline and start there
top_center = shgeo.Point(outline.centroid.x, outline.bounds[1])
start_position = outline.project(top_center, normalized=True)
# pick this item's spot around the outline and perturb it a bit to avoid
# stacking up commands if they add commands multiple times
position = index / float(total)
position += random() * 0.05
position += start_position
return outline.interpolate(position, normalized=True)
def remove_legacy_param(element, command):
if command == "trim" or command == "stop":
# If they had the old "TRIM after" or "STOP after" attributes set,
2020-04-25 12:45:27 +00:00
# automatically delete them. The new commands will do the same
# thing.
#
# If we didn't delete these here, then things would get confusing.
# If the user were to delete a "trim" symbol added by this extension
# but the "embroider_trim_after" attribute is still set, then the
# trim would keep happening.
attribute = "embroider_%s_after" % command
if attribute in element.node.attrib:
del element.node.attrib[attribute]
2020-04-25 12:45:27 +00:00
# Attributes have changed to be namespaced.
# Let's check for them as well, they might have automatically changed.
attribute = INKSTITCH_ATTRIBS["%s_after" % command]
if attribute in element.node.attrib:
del element.node.attrib[attribute]
def add_commands(element, commands, pos=None):
svg = get_document(element.node)
for i, command in enumerate(commands):
ensure_symbol(svg, command)
remove_legacy_param(element, command)
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)
add_connector(svg, symbol, command, element)
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 = ''
for i, command in enumerate(commands):
ensure_symbol(svg, command)
layer.append(inkex.Use(attrib={
"id": generate_unique_id(svg, "use"),
INKSCAPE_LABEL: _("Ink/Stitch Command") + ": %s" % get_command_description(command),
XLINK_HREF: "#inkstitch_%s" % command,
"height": "100%",
"width": "100%",
"x": str(i * 20),
"y": "-10",
"transform": correction_transform
}))