summaryrefslogtreecommitdiff
path: root/lib/extensions
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2019-02-22 22:07:15 -0500
committerLex Neva <github.com@lexneva.name>2019-03-08 19:57:11 -0500
commit4ba3cd708561870a731d9634d9cdd5c18579cac7 (patch)
treecd901daa9bd9f1901f3dbbca57940a981215267d /lib/extensions
parent3611e2340997b917cc89e7d405b3c7d9bc86aab5 (diff)
refactor add_commands() out into commands module
Diffstat (limited to 'lib/extensions')
-rw-r--r--lib/extensions/auto_satin.py4
-rw-r--r--lib/extensions/base.py11
-rw-r--r--lib/extensions/commands.py118
-rw-r--r--lib/extensions/layer_commands.py4
-rw-r--r--lib/extensions/object_commands.py9
5 files changed, 9 insertions, 137 deletions
diff --git a/lib/extensions/auto_satin.py b/lib/extensions/auto_satin.py
index f846ac6b..90d8fe33 100644
--- a/lib/extensions/auto_satin.py
+++ b/lib/extensions/auto_satin.py
@@ -2,6 +2,7 @@ import sys
import inkex
+from ..commands import add_commands
from ..elements import SatinColumn
from ..i18n import _
from ..stitches.auto_satin import auto_satin
@@ -97,6 +98,5 @@ class AutoSatin(CommandsExtension):
def add_trims(self, new_elements, trim_indices):
if self.options.trim and trim_indices:
- self.ensure_symbol("trim")
for i in trim_indices:
- self.add_commands(new_elements[i], ["trim"])
+ add_commands(new_elements[i], ["trim"])
diff --git a/lib/extensions/base.py b/lib/extensions/base.py
index 98673541..8d45f790 100644
--- a/lib/extensions/base.py
+++ b/lib/extensions/base.py
@@ -9,6 +9,7 @@ from stringcase import snakecase
from ..commands import layer_commands
from ..elements import EmbroideryElement, nodes_to_elements
from ..i18n import _
+from ..svg import generate_unique_id
from ..svg.tags import SVG_GROUP_TAG, INKSCAPE_GROUPMODE, SVG_DEFS_TAG, EMBROIDERABLE_TAGS
@@ -194,15 +195,7 @@ class InkstitchExtension(inkex.Effect):
def uniqueId(self, prefix, make_new_id=True):
"""Override inkex.Effect.uniqueId with a nicer naming scheme."""
- i = 1
- while True:
- new_id = "%s%d" % (prefix, i)
- if new_id not in self.doc_ids:
- break
- i += 1
- self.doc_ids[new_id] = 1
-
- return new_id
+ return generate_unique_id(self.document, prefix)
def parse(self):
"""Override inkex.Effect.parse to add Ink/Stitch xml namespace"""
diff --git a/lib/extensions/commands.py b/lib/extensions/commands.py
index 07b450e1..86e291fd 100644
--- a/lib/extensions/commands.py
+++ b/lib/extensions/commands.py
@@ -1,16 +1,4 @@
-import os
-import inkex
-from copy import deepcopy
-from random import random
-
-
from .base import InkstitchExtension
-from ..utils import get_bundled_dir, cache
-from ..commands import get_command_description
-from ..i18n import _
-from ..svg.tags import SVG_DEFS_TAG, SVG_PATH_TAG, CONNECTION_START, CONNECTION_END, \
- CONNECTOR_TYPE, INKSCAPE_LABEL, SVG_GROUP_TAG, SVG_USE_TAG, XLINK_HREF
-from ..svg import get_correction_transform
class CommandsExtension(InkstitchExtension):
@@ -20,109 +8,3 @@ class CommandsExtension(InkstitchExtension):
InkstitchExtension.__init__(self, *args, **kwargs)
for command in self.COMMANDS:
self.OptionParser.add_option("--%s" % command, type="inkbool")
-
- @property
- def symbols_path(self):
- return os.path.join(get_bundled_dir("symbols"), "inkstitch.svg")
-
- @property
- @cache
- def symbols_svg(self):
- with open(self.symbols_path) as symbols_file:
- return inkex.etree.parse(symbols_file)
-
- @property
- @cache
- def symbol_defs(self):
- return self.symbols_svg.find(SVG_DEFS_TAG)
-
- @property
- @cache
- def defs(self):
- return self.document.find(SVG_DEFS_TAG)
-
- def ensure_symbol(self, command):
- path = "./*[@id='inkstitch_%s']" % command
- if self.defs.find(path) is None:
- self.defs.append(deepcopy(self.symbol_defs.find(path)))
-
- def add_connector(self, symbol, 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
-
- path = inkex.etree.Element(SVG_PATH_TAG,
- {
- "id": self.uniqueId("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'),
- CONNECTOR_TYPE: "polyline",
-
- # l10n: the name of the line that connects a command to the object it applies to
- INKSCAPE_LABEL: _("connector")
- }
- )
-
- symbol.getparent().insert(0, path)
-
- def get_command_pos(self, 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
- outline = element.shape.buffer(30).exterior
-
- # pick this item's spot arond the outline and perturb it a bit to avoid
- # stacking up commands if they run the extension multiple times
- position = index / float(total)
- position += random() * 0.1
-
- return outline.interpolate(position, normalized=True)
-
- def remove_legacy_param(self, element, command):
- if command == "trim" or command == "stop":
- # If they had the old "TRIM after" or "STOP after" attributes set,
- # 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]
-
- def add_commands(self, element, commands):
- for i, command in enumerate(commands):
- self.remove_legacy_param(element, command)
-
- pos = self.get_command_pos(element, i, len(commands))
-
- group = inkex.etree.SubElement(element.node.getparent(), SVG_GROUP_TAG,
- {
- "id": self.uniqueId("group"),
- INKSCAPE_LABEL: _("Ink/Stitch Command") + ": %s" % get_command_description(command),
- "transform": get_correction_transform(element.node)
- }
- )
-
- symbol = inkex.etree.SubElement(group, SVG_USE_TAG,
- {
- "id": self.uniqueId("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"),
- }
- )
-
- self.add_connector(symbol, element)
diff --git a/lib/extensions/layer_commands.py b/lib/extensions/layer_commands.py
index 3a746fcf..c124ec95 100644
--- a/lib/extensions/layer_commands.py
+++ b/lib/extensions/layer_commands.py
@@ -1,6 +1,6 @@
import inkex
-from ..commands import LAYER_COMMANDS, get_command_description
+from ..commands import LAYER_COMMANDS, get_command_description, ensure_symbol
from ..i18n import _
from ..svg import get_correction_transform
from ..svg.tags import SVG_USE_TAG, INKSCAPE_LABEL, XLINK_HREF
@@ -21,7 +21,7 @@ class LayerCommands(CommandsExtension):
correction_transform = get_correction_transform(self.current_layer, child=True)
for i, command in enumerate(commands):
- self.ensure_symbol(command)
+ ensure_symbol(command)
inkex.etree.SubElement(self.current_layer, SVG_USE_TAG,
{
diff --git a/lib/extensions/object_commands.py b/lib/extensions/object_commands.py
index 47fb361d..d33ab2ba 100644
--- a/lib/extensions/object_commands.py
+++ b/lib/extensions/object_commands.py
@@ -1,8 +1,8 @@
import inkex
-from .commands import CommandsExtension
-from ..commands import OBJECT_COMMANDS
+from ..commands import OBJECT_COMMANDS, add_commands
from ..i18n import _
+from .commands import CommandsExtension
class ObjectCommands(CommandsExtension):
@@ -24,14 +24,11 @@ class ObjectCommands(CommandsExtension):
inkex.errormsg(_("Please choose one or more commands to attach."))
return
- for command in commands:
- self.ensure_symbol(command)
-
# Each object (node) in the SVG may correspond to multiple Elements of different
# types (e.g. stroke + fill). We only want to process each one once.
seen_nodes = set()
for element in self.elements:
if element.node not in seen_nodes:
- self.add_commands(element, commands)
+ add_commands(element, commands)
seen_nodes.add(element.node)