From a7b2122d91703d72177cf980cecbef8fca3d54ec Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Sat, 7 Jan 2023 09:32:33 +0100 Subject: Cutwork needle info (#1988) * add cutwork needle info to thread color * apply threadlist: apply cutwork_needle attribute --- lib/elements/stroke.py | 13 ++++++++++++- lib/extensions/apply_threadlist.py | 30 ++++++++++++++++++------------ lib/extensions/cutwork_segmentation.py | 1 + lib/svg/tags.py | 1 + lib/threads/color.py | 14 +++++++++++--- pyembroidery | 2 +- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/lib/elements/stroke.py b/lib/elements/stroke.py index 2854adaf..00e069b2 100644 --- a/lib/elements/stroke.py +++ b/lib/elements/stroke.py @@ -18,6 +18,7 @@ from ..svg import get_node_transform, parse_length_with_units from ..utils import Point, cache from .element import EmbroideryElement, param from .validation import ValidationWarning +from ..threads import ThreadColor warned_about_legacy_running_stitch = False @@ -57,7 +58,17 @@ class Stroke(EmbroideryElement): @property def color(self): - return self.get_style("stroke") + color = self.get_style("stroke") + if self.cutwork_needle is not None: + color = ThreadColor(color, description=self.cutwork_needle, chart=self.cutwork_needle) + return color + + @property + def cutwork_needle(self): + needle = self.get_int_param('cutwork_needle') or None + if needle is not None: + needle = f'Cut {needle}' + return needle @property def dashed(self): diff --git a/lib/extensions/apply_threadlist.py b/lib/extensions/apply_threadlist.py index 2779a901..e17b4a3c 100644 --- a/lib/extensions/apply_threadlist.py +++ b/lib/extensions/apply_threadlist.py @@ -8,9 +8,11 @@ import re import sys import inkex + import pyembroidery from ..i18n import _ +from ..svg.tags import INKSTITCH_ATTRIBS from ..threads import ThreadCatalog from .base import InkstitchExtension @@ -41,6 +43,7 @@ class ApplyThreadlist(InkstitchExtension): method = self.options.method + # colors: [[color, cutwork_needle],[...]] if path.endswith(('col', 'inf', 'edr')): colors = self.parse_color_format(path) elif method == 1: @@ -62,9 +65,13 @@ class ApplyThreadlist(InkstitchExtension): if i == len(colors): break - style = element.node.get('style').replace("%s" % element_color, "%s" % colors[i]) + style = element.node.get('style').replace("%s" % element_color, "%s" % colors[i][0]) element.node.set('style', style) + # apply cutwork + if colors[i][1] is not None: + element.node.set(INKSTITCH_ATTRIBS['cutwork_needle'], colors[i][1]) + def verify_path(self, path): if not os.path.exists(path): inkex.errormsg(_("File not found.")) @@ -89,17 +96,21 @@ class ApplyThreadlist(InkstitchExtension): if line[0].isdigit(): m = re.search(r"\((#[0-9A-Fa-f]{6})\)", line) if m: - colors.append(m.group(1)) + colors.append([m.group(1), None]) else: # Color not found - colors.append(None) + colors.append([None, None]) return colors def parse_color_format(self, path): colors = [] threads = pyembroidery.read(path).threadlist for color in threads: - colors.append(color.hex_color()) + if color.description.startswith("Cut"): + # there is a maximum of 4 needles, we can simply take the last element from the description string + colors.append([color.hex_color(), color.description[-1]]) + else: + colors.append([color.hex_color(), None]) return colors def parse_threadlist_by_catalog_number(self, path): @@ -116,19 +127,14 @@ class ApplyThreadlist(InkstitchExtension): with open(path) as threadlist: for line in threadlist: if line[0].isdigit(): - # some threadlists may add a # in front of the catalof number + # some threadlists may add a # in front of the catalog 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]) + colors.append([palette_colors[color_index], None]) else: # No color found - colors.append(None) + colors.append([None, 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/extensions/cutwork_segmentation.py b/lib/extensions/cutwork_segmentation.py index 22b39e4b..537cfadf 100644 --- a/lib/extensions/cutwork_segmentation.py +++ b/lib/extensions/cutwork_segmentation.py @@ -147,6 +147,7 @@ class CutworkSegmentation(InkstitchExtension): "transform": get_correction_transform(element.node), INKSTITCH_ATTRIBS["ties"]: "3", INKSTITCH_ATTRIBS["running_stitch_length_mm"]: "1", + INKSTITCH_ATTRIBS["cutwork_needle"]: str(sector['id']), "d": d }) self.new_elements.append([stroke_element, sector['id']]) diff --git a/lib/svg/tags.py b/lib/svg/tags.py index e2de7348..059396bd 100644 --- a/lib/svg/tags.py +++ b/lib/svg/tags.py @@ -89,6 +89,7 @@ inkstitch_attribs = [ 'repeats', 'running_stitch_length_mm', 'running_stitch_tolerance_mm', + 'cutwork_needle', # ripples 'line_count', 'exponent', diff --git a/lib/threads/color.py b/lib/threads/color.py index 458752da..8dc1ea01 100644 --- a/lib/threads/color.py +++ b/lib/threads/color.py @@ -15,7 +15,7 @@ from inkex import Color class ThreadColor(object): hex_str_re = re.compile('#([0-9a-z]{3}|[0-9a-z]{6})', re.I) - def __init__(self, color, name=None, number=None, manufacturer=None): + def __init__(self, color, name=None, number=None, manufacturer=None, description=None, chart=None): # set colors with a gradient to black (avoiding an error message) if type(color) == str and color.startswith('url'): color = None @@ -26,6 +26,8 @@ class ThreadColor(object): self.name = color.description self.number = color.catalog_number self.manufacturer = color.brand + self.description = color.description + self.chart = color.chart self.rgb = (color.get_red(), color.get_green(), color.get_blue()) return elif isinstance(color, str): @@ -42,6 +44,8 @@ class ThreadColor(object): self.name = name self.number = number self.manufacturer = manufacturer + self.description = description + self.chart = chart def __json__(self): jsonified = self._as_dict() @@ -53,6 +57,8 @@ class ThreadColor(object): return dict(name=self.name, number=self.number, manufacturer=self.manufacturer, + description=self.description, + chart=self.chart, rgb=self.rgb, hex=self.to_hex_str(), ) @@ -81,6 +87,8 @@ class ThreadColor(object): "name": self.name, "id": self.number, "manufacturer": self.manufacturer, + "description": self.description, + "chart": self.chart, "rgb": int(self.hex_digits, 16), } @@ -125,7 +133,7 @@ class ThreadColor(object): # convert back to values in the range of 0-255 color = tuple(value * 255 for value in color) - return ThreadColor(color, name=self.name, number=self.number, manufacturer=self.manufacturer) + return ThreadColor(color, name=self.name, number=self.number, manufacturer=self.manufacturer, description=self.description, chart=self.chart) @property def darker(self): @@ -140,4 +148,4 @@ class ThreadColor(object): # convert back to values in the range of 0-255 color = tuple(value * 255 for value in color) - return ThreadColor(color, name=self.name, number=self.number, manufacturer=self.manufacturer) + return ThreadColor(color, name=self.name, number=self.number, manufacturer=self.manufacturer, description=self.description, chart=self.chart) diff --git a/pyembroidery b/pyembroidery index 83276c06..9347ea88 160000 --- a/pyembroidery +++ b/pyembroidery @@ -1 +1 @@ -Subproject commit 83276c06291c624bfd320a33417da4e42ac05a2f +Subproject commit 9347ea882a40764cfc712d1bb9f90324945767c0 -- cgit v1.2.3