2018-08-23 02:48:40 +00:00
|
|
|
import sys
|
2018-06-21 19:41:06 +00:00
|
|
|
import inkex
|
|
|
|
import cubicsuperpath
|
2018-08-23 02:13:51 +00:00
|
|
|
import simpletransform
|
2018-06-21 19:41:06 +00:00
|
|
|
|
2018-08-23 01:56:36 +00:00
|
|
|
from .svg import apply_transforms, get_node_transform
|
2018-06-21 19:41:06 +00:00
|
|
|
from .svg.tags import SVG_USE_TAG, SVG_SYMBOL_TAG, CONNECTION_START, CONNECTION_END, XLINK_HREF
|
2018-08-23 01:56:36 +00:00
|
|
|
from .utils import cache, Point
|
2018-08-20 19:49:19 +00:00
|
|
|
from .i18n import _, N_
|
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
|
2018-08-23 02:48:40 +00:00
|
|
|
N_("fill_start"): N_("Fill stitch starting position"),
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2018-08-20 19:49:19 +00:00
|
|
|
# L10N command attached to an object
|
2018-08-23 02:48:40 +00:00
|
|
|
N_("fill_end"): N_("Fill stitch ending position"),
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2018-08-20 19:49:19 +00:00
|
|
|
# L10N command attached to an object
|
2018-08-23 02:48:40 +00:00
|
|
|
N_("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
|
2018-08-23 02:48:40 +00:00
|
|
|
N_("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
|
2018-08-23 02:48:40 +00:00
|
|
|
N_("ignore_object"): N_("Ignore this object (do not stitch)"),
|
2018-08-18 00:55:43 +00:00
|
|
|
|
2018-08-23 02:13:51 +00:00
|
|
|
# L10N command that affects a layer
|
2018-08-23 02:48:40 +00:00
|
|
|
N_("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
|
2018-08-23 02:48:40 +00:00
|
|
|
N_("origin"): N_("Origin for exported embroidery files"),
|
|
|
|
|
|
|
|
# L10N command that affects entire document
|
|
|
|
N_("stop_point"): N_("Jump destination for Stop commands (a.k.a. \"Frame Out position\")."),
|
2018-08-18 00:55:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
OBJECT_COMMANDS = ["fill_start", "fill_end", "stop", "trim", "ignore_object"]
|
|
|
|
LAYER_COMMANDS = ["ignore_layer"]
|
|
|
|
|
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:]
|
|
|
|
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 = cubicsuperpath.parsePath(self.connector.get('d'))
|
|
|
|
return apply_transforms(path, self.connector)
|
|
|
|
|
|
|
|
def parse_command(self):
|
|
|
|
path = self.parse_connector_path()
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
simpletransform.applyTransformToPoint(transform, pos)
|
|
|
|
|
|
|
|
return Point(*pos)
|
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2018-08-18 00:55:43 +00:00
|
|
|
def get_command_description(command):
|
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
|
|
|
|
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."""
|
|
|
|
|
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:
|
|
|
|
print >> sys.stderr, _("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)
|
|
|
|
|
|
|
|
# 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 >> sys.stderr, _("%(command)s: %(description)s") % dict(command=command, description=_(get_command_description(command)))
|
|
|
|
|
|
|
|
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
|