summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaalleen <36401965+kaalleen@users.noreply.github.com>2020-04-25 14:24:01 +0200
committerGitHub <noreply@github.com>2020-04-25 14:24:01 +0200
commit1d3b89111e627b3662a7ec2749bb3dcfe694be66 (patch)
tree1103390c4b57490d4e35dc17b450b98bfc4878d4
parent3b2c7ba1a9caa87e2a7b09af2e0af2c9221063d9 (diff)
import threadlist (#666)
-rw-r--r--lib/extensions/__init__.py4
-rw-r--r--lib/extensions/import_threadlist.py102
-rwxr-xr-xlib/inx/extensions.py7
-rw-r--r--templates/import_threadlist.inx27
4 files changed, 139 insertions, 1 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py
index 53a6638a..6f9ef5c4 100644
--- a/lib/extensions/__init__.py
+++ b/lib/extensions/__init__.py
@@ -5,6 +5,7 @@ from cut_satin import CutSatin
from embroider import Embroider
from flip import Flip
from global_commands import GlobalCommands
+from import_threadlist import ImportThreadlist
from input import Input
from install import Install
from layer_commands import LayerCommands
@@ -38,4 +39,5 @@ __all__ = extensions = [Embroider,
Lettering,
Troubleshoot,
RemoveEmbroiderySettings,
- BreakApart]
+ BreakApart,
+ ImportThreadlist]
diff --git a/lib/extensions/import_threadlist.py b/lib/extensions/import_threadlist.py
new file mode 100644
index 00000000..d31c0d69
--- /dev/null
+++ b/lib/extensions/import_threadlist.py
@@ -0,0 +1,102 @@
+import os
+import re
+import sys
+
+import inkex
+
+from ..i18n import _
+from ..threads import ThreadCatalog
+from .base import InkstitchExtension
+
+
+class ImportThreadlist(InkstitchExtension):
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self, *args, **kwargs)
+ self.OptionParser.add_option("-f", "--filepath", type="str", default="", dest="filepath")
+ self.OptionParser.add_option("-m", "--method", type="int", default=1, dest="method")
+ self.OptionParser.add_option("-t", "--palette", type="str", default=None, dest="palette")
+
+ def effect(self):
+ # Remove selection, we want all the elements in the document
+ self.selected = {}
+
+ if not self.get_elements():
+ return
+
+ path = self.options.filepath
+ if not os.path.exists(path):
+ print >> sys.stderr, _("File not found.")
+ sys.exit(1)
+
+ method = self.options.method
+ if method == 1:
+ colors = self.parse_inkstitch_threadlist(path)
+ else:
+ colors = self.parse_threadlist_by_catalog_number(path)
+
+ if all(c is None for c in colors):
+ print >>sys.stderr, _("Couldn't find any matching colors in the file.")
+ if method == 1:
+ print >>sys.stderr, _('Please try to import as "other threadlist" and specify a color palette below.')
+ else:
+ print >>sys.stderr, _("Please chose an other color palette for your design.")
+ sys.exit(1)
+
+ # Iterate through the color blocks to apply colors
+ element_color = ""
+ i = -1
+ for element in self.elements:
+ if element.color != element_color:
+ element_color = element.color
+ i += 1
+
+ # No more colors in the list, stop here
+ if i == len(colors):
+ break
+
+ style = element.node.get('style').replace("%s" % element_color, "%s" % colors[i])
+ element.node.set('style', style)
+
+ def parse_inkstitch_threadlist(self, path):
+ colors = []
+ with open(path) as threadlist:
+ for line in threadlist:
+ if line[0].isdigit():
+ m = re.search(r"\((#[0-9A-Fa-f]{6})\)", line)
+ if m:
+ colors.append(m.group(1))
+ else:
+ # Color not found
+ colors.append(None)
+ return colors
+
+ def parse_threadlist_by_catalog_number(self, path):
+ palette_name = self.options.palette
+ palette = ThreadCatalog().get_palette_by_name(palette_name)
+
+ colors = []
+ palette_numbers = []
+ palette_colors = []
+
+ for color in palette:
+ palette_numbers.append(color.number)
+ palette_colors.append('#%s' % color.hex_digits.lower())
+ with open(path) as threadlist:
+ for line in threadlist:
+ if line[0].isdigit():
+ # some threadlists may add a # in front of the catalof number
+ # let's remove it from the entire string before splitting it up
+ thread = line.replace('#', '').split()
+ catalog_number = set(thread[1:]).intersection(palette_numbers)
+ if catalog_number:
+ color_index = palette_numbers.index(next(iter(catalog_number)))
+ colors.append(palette_colors[color_index])
+ else:
+ # No color found
+ colors.append(None)
+ return colors
+
+ def find_elements(self, xpath):
+ svg = self.document.getroot()
+ elements = svg.xpath(xpath, namespaces=inkex.NSS)
+ return elements
diff --git a/lib/inx/extensions.py b/lib/inx/extensions.py
index d1a0c7f3..030e8aa6 100755
--- a/lib/inx/extensions.py
+++ b/lib/inx/extensions.py
@@ -4,6 +4,7 @@ from .utils import build_environment, write_inx_file
from .outputs import pyembroidery_output_formats
from ..extensions import extensions, Input, Output
from ..commands import LAYER_COMMANDS, OBJECT_COMMANDS, GLOBAL_COMMANDS, COMMANDS
+from ..threads import ThreadCatalog
def layer_commands():
@@ -27,6 +28,11 @@ def pyembroidery_debug_formats():
yield format['extension'], format['description']
+def threadcatalog():
+ threadcatalog = ThreadCatalog().palette_names()
+ return threadcatalog
+
+
def generate_extension_inx_files():
env = build_environment()
@@ -38,6 +44,7 @@ def generate_extension_inx_files():
template = env.get_template('%s.inx' % name)
write_inx_file(name, template.render(formats=pyembroidery_output_formats(),
debug_formats=pyembroidery_debug_formats(),
+ threadcatalog=threadcatalog(),
layer_commands=layer_commands(),
object_commands=object_commands(),
global_commands=global_commands()))
diff --git a/templates/import_threadlist.inx b/templates/import_threadlist.inx
new file mode 100644
index 00000000..081a881a
--- /dev/null
+++ b/templates/import_threadlist.inx
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
+ <name>{% trans %}Import Threadlist{% endtrans %}</name>
+ <id>org.inkstitch.import_threadlist.{{ locale }}</id>
+ <param name="extension" type="string" gui-hidden="true">import_threadlist</param>
+ <!-- This doesn't work at the moment. It's an Inkstitch 1.0 feature, but would be desireable.
+ <param type="path" name="filepath" gui-text="{{ _('Choose file') }}" mode="file" filetypes="txt"/> -->
+ <param name="filepath" type="string" _gui-text="{{ _('File Path') }}">{{ _("Enter path to file") }}</param>
+ <param name="method" type="optiongroup" _gui-text="Choose method">
+ <option value="1">Import Ink/Stitch threadlist</option>
+ <option value="2">Import other threadlist*</option>
+ </param>
+ <param name="palette" type="enum" _gui-text="*Choose color palette">
+ {%- for item in threadcatalog %}
+ <item value="{{ item }}">{{ item }}</item>
+ {%- endfor %}
+ </param>
+ <effect>
+ <object-type>all</object-type>
+ <effects-menu>
+ <submenu name="Ink/Stitch" />
+ </effects-menu>
+ </effect>
+ <script>
+ {{ command_tag | safe }}
+ </script>
+</inkscape-extension>