From 23ac0a4f7fb6d7c47728d42a03085a732cb75a10 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Tue, 2 Jul 2019 18:38:48 +0200 Subject: Ignore commands on input files --- lib/extensions/input.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/extensions/input.py b/lib/extensions/input.py index 0ca85df5..fbc37bd7 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -19,9 +19,9 @@ class Input(object): for raw_stitches, thread in pattern.get_as_colorblocks(): color_block = stitch_plan.new_color_block(thread) for x, y, command in raw_stitches: - color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0, - jump=(command == pyembroidery.JUMP), - trim=(command == pyembroidery.TRIM)) + # let's ignore commands for now + if command == pyembroidery.STITCH: + color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0) extents = stitch_plan.extents svg = etree.Element("svg", nsmap=inkex.NSS, attrib={ -- cgit v1.2.3 From f039ef4c8d05628ebf6e13fb011ec054dc4c8740 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Thu, 4 Jul 2019 15:35:31 +0200 Subject: respect trim commands --- lib/extensions/input.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/extensions/input.py b/lib/extensions/input.py index fbc37bd7..9f1314eb 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -18,10 +18,21 @@ class Input(object): for raw_stitches, thread in pattern.get_as_colorblocks(): color_block = stitch_plan.new_color_block(thread) + trim_after = False + jump_counter = 0 for x, y, command in raw_stitches: - # let's ignore commands for now if command == pyembroidery.STITCH: + if trim_after: + color_block.add_stitch(trim=True) + trim_after = False color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0) + jump_counter = 0 + elif len(color_block) > 0: + # some file formats use 3 or more jump stitches in a row to indicate a trim + if command == pyembroidery.JUMP: + jump_counter += 1 + if command == pyembroidery.TRIM or jump_counter >=3: + trim_after = True extents = stitch_plan.extents svg = etree.Element("svg", nsmap=inkex.NSS, attrib={ -- cgit v1.2.3 From 3ef2ad9be4173d5c154b3ba0ea39439fb2a274d8 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Thu, 4 Jul 2019 15:41:52 +0200 Subject: fix style --- lib/extensions/input.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/extensions/input.py b/lib/extensions/input.py index 9f1314eb..a8402fdc 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -31,7 +31,7 @@ class Input(object): # some file formats use 3 or more jump stitches in a row to indicate a trim if command == pyembroidery.JUMP: jump_counter += 1 - if command == pyembroidery.TRIM or jump_counter >=3: + if command == pyembroidery.TRIM or jump_counter >= 3: trim_after = True extents = stitch_plan.extents -- cgit v1.2.3 From bec33656ece737e134ca4b7ec2dc9a7dc9c71fd1 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 4 Jul 2019 17:20:31 -0400 Subject: add trim commands in import and stitch plan --- lib/commands.py | 4 ++++ lib/elements/stroke.py | 12 +++++++++--- lib/svg/rendering.py | 40 +++++++++++++++++++--------------------- 3 files changed, 32 insertions(+), 24 deletions(-) (limited to 'lib') diff --git a/lib/commands.py b/lib/commands.py index 8e35d7ee..c735185f 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -289,6 +289,10 @@ def add_connector(document, symbol, element): 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', generate_unique_id(document, "object")) + path = inkex.etree.Element(SVG_PATH_TAG, { "id": generate_unique_id(document, "connector"), diff --git a/lib/elements/stroke.py b/lib/elements/stroke.py index 3ae2b143..e0a0aacc 100644 --- a/lib/elements/stroke.py +++ b/lib/elements/stroke.py @@ -1,11 +1,13 @@ import sys + import shapely.geometry -from .element import param, EmbroideryElement, Patch from ..i18n import _ -from ..utils import cache, Point from ..stitches import running_stitch, bean_stitch from ..svg import parse_length_with_units +from ..utils import cache, Point +from .element import param, EmbroideryElement, Patch + warned_about_legacy_running_stitch = False @@ -85,7 +87,11 @@ class Stroke(EmbroideryElement): @cache def shape(self): line_strings = [shapely.geometry.LineString(path) for path in self.paths] - return shapely.geometry.MultiLineString(line_strings) + + # Using convex_hull here is an important optimization. Otherwise + # complex paths cause operations on the shape to take a long time. + # This especially happens when importing machine embroidery files. + return shapely.geometry.MultiLineString(line_strings).convex_hull @property @param('manual_stitch', diff --git a/lib/svg/rendering.py b/lib/svg/rendering.py index 41ed53d7..532748bf 100644 --- a/lib/svg/rendering.py +++ b/lib/svg/rendering.py @@ -167,9 +167,7 @@ def get_correction_transform(svg): return transform -def color_block_to_realistic_stitches(color_block, svg): - paths = [] - +def color_block_to_realistic_stitches(color_block, svg, destination): for point_list in color_block_to_point_lists(color_block): if not point_list: continue @@ -177,7 +175,7 @@ def color_block_to_realistic_stitches(color_block, svg): color = color_block.color.visible_on_white.darker.to_hex_str() start = point_list[0] for point in point_list[1:]: - paths.append(inkex.etree.Element( + destination.append(inkex.etree.Element( SVG_PATH_TAG, {'style': simplestyle.formatStyle( { @@ -190,16 +188,23 @@ def color_block_to_realistic_stitches(color_block, svg): })) start = point - return paths - -def color_block_to_paths(color_block, svg): - paths = [] +def color_block_to_paths(color_block, svg, destination): # We could emit just a single path with one subpath per point list, but # emitting multiple paths makes it easier for the user to manipulate them. + first = True for point_list in color_block_to_point_lists(color_block): + if first: + first = False + else: + # If we try to import these above, we get into a mess of circular + # imports. + from ..commands import add_commands + from ..elements.stroke import Stroke + add_commands(Stroke(destination[-1]), ["trim"]) + color = color_block.color.visible_on_white.to_hex_str() - paths.append(inkex.etree.Element( + destination.append(inkex.etree.Element( SVG_PATH_TAG, {'style': simplestyle.formatStyle( {'stroke': color, @@ -207,16 +212,9 @@ def color_block_to_paths(color_block, svg): 'fill': 'none'}), 'd': "M" + " ".join(" ".join(str(coord) for coord in point) for point in point_list), 'transform': get_correction_transform(svg), - 'embroider_manual_stitch': 'true', - 'embroider_trim_after': 'true', + 'embroider_manual_stitch': 'true' })) - # no need to trim at the end of a thread color - if paths: - paths[-1].attrib.pop('embroider_trim_after') - - return paths - def render_stitch_plan(svg, stitch_plan, realistic=False): layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']") @@ -232,17 +230,17 @@ def render_stitch_plan(svg, stitch_plan, realistic=False): # make sure the layer is visible layer.set('style', 'display:inline') + svg.append(layer) + for i, color_block in enumerate(stitch_plan): group = inkex.etree.SubElement(layer, SVG_GROUP_TAG, {'id': '__color_block_%d__' % i, INKSCAPE_LABEL: "color block %d" % (i + 1)}) if realistic: - group.extend(color_block_to_realistic_stitches(color_block, svg)) + color_block_to_realistic_stitches(color_block, svg, group) else: - group.extend(color_block_to_paths(color_block, svg)) - - svg.append(layer) + color_block_to_paths(color_block, svg, group) if realistic: defs = svg.find(SVG_DEFS_TAG) -- cgit v1.2.3 From 62d4e35be91b04d71c560a73d5f4c510606388d7 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Sat, 6 Jul 2019 10:18:01 +0200 Subject: fix layer commands --- lib/extensions/layer_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/extensions/layer_commands.py b/lib/extensions/layer_commands.py index c124ec95..e710e351 100644 --- a/lib/extensions/layer_commands.py +++ b/lib/extensions/layer_commands.py @@ -21,7 +21,7 @@ class LayerCommands(CommandsExtension): correction_transform = get_correction_transform(self.current_layer, child=True) for i, command in enumerate(commands): - ensure_symbol(command) + ensure_symbol(self.document, command) inkex.etree.SubElement(self.current_layer, SVG_USE_TAG, { -- cgit v1.2.3 From 1b18d915a91f838dce7126d80674e3207efe6374 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Sat, 6 Jul 2019 11:24:56 +0200 Subject: use get_pattern_interpolate_trim This has the same inaccuracy as the previous version, but we can actually use a pyembroidery function to turn a specific number of jumps into trims. --- lib/extensions/input.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/extensions/input.py b/lib/extensions/input.py index a8402fdc..bfb488f1 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -12,6 +12,7 @@ class Input(object): def affect(self, args): embroidery_file = args[0] pattern = pyembroidery.read(embroidery_file) + pattern = pattern.get_pattern_interpolate_trim(2) stitch_plan = StitchPlan() color_block = None @@ -19,20 +20,14 @@ class Input(object): for raw_stitches, thread in pattern.get_as_colorblocks(): color_block = stitch_plan.new_color_block(thread) trim_after = False - jump_counter = 0 for x, y, command in raw_stitches: if command == pyembroidery.STITCH: if trim_after: color_block.add_stitch(trim=True) trim_after = False color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0) - jump_counter = 0 - elif len(color_block) > 0: - # some file formats use 3 or more jump stitches in a row to indicate a trim - if command == pyembroidery.JUMP: - jump_counter += 1 - if command == pyembroidery.TRIM or jump_counter >= 3: - trim_after = True + if len(color_block) > 0 and command == pyembroidery.TRIM: + trim_after = True extents = stitch_plan.extents svg = etree.Element("svg", nsmap=inkex.NSS, attrib={ -- cgit v1.2.3 From fccefb3e0d9cf9a512983e3dbf4927ade1867f81 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Sun, 7 Jul 2019 15:55:48 +0200 Subject: rescue VP3 --- lib/extensions/input.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/extensions/input.py b/lib/extensions/input.py index bfb488f1..53961e91 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -18,7 +18,8 @@ class Input(object): color_block = None for raw_stitches, thread in pattern.get_as_colorblocks(): - color_block = stitch_plan.new_color_block(thread) + if len(raw_stitches) > 1: + color_block = stitch_plan.new_color_block(thread) trim_after = False for x, y, command in raw_stitches: if command == pyembroidery.STITCH: -- cgit v1.2.3 From 0cd562a4fcfe7ddec45103cf417e14409e378bff Mon Sep 17 00:00:00 2001 From: Momo Date: Sun, 7 Jul 2019 16:25:21 +0200 Subject: delete empty color block --- lib/extensions/input.py | 5 +++-- lib/stitch_plan/stitch_plan.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/extensions/input.py b/lib/extensions/input.py index 53961e91..826e7e04 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -18,8 +18,7 @@ class Input(object): color_block = None for raw_stitches, thread in pattern.get_as_colorblocks(): - if len(raw_stitches) > 1: - color_block = stitch_plan.new_color_block(thread) + color_block = stitch_plan.new_color_block(thread) trim_after = False for x, y, command in raw_stitches: if command == pyembroidery.STITCH: @@ -30,6 +29,8 @@ class Input(object): if len(color_block) > 0 and command == pyembroidery.TRIM: trim_after = True + stitch_plan.delete_empty_color_block(color_block) + extents = stitch_plan.extents svg = etree.Element("svg", nsmap=inkex.NSS, attrib={ "width": str(extents[0] * 2), diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 808fa626..0b12c2c5 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -72,6 +72,10 @@ class StitchPlan(object): self.color_blocks.append(color_block) return color_block + def delete_empty_color_block(self, color_block): + if len(color_block) == 0: + self.color_blocks.remove(color_block) + def add_color_block(self, color_block): self.color_blocks.append(color_block) -- cgit v1.2.3 From cff12ce18ff94a4d7b779b3c7063eb6b82afa526 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Tue, 9 Jul 2019 20:39:07 -0400 Subject: remove all empty color blocks --- lib/extensions/input.py | 7 ++++--- lib/stitch_plan/stitch_plan.py | 16 ++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/extensions/input.py b/lib/extensions/input.py index 826e7e04..03148832 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -1,11 +1,12 @@ import os +import pyembroidery + from inkex import etree import inkex -import pyembroidery +from ..stitch_plan import StitchPlan from ..svg import PIXELS_PER_MM, render_stitch_plan from ..svg.tags import INKSCAPE_LABEL -from ..stitch_plan import StitchPlan class Input(object): @@ -29,7 +30,7 @@ class Input(object): if len(color_block) > 0 and command == pyembroidery.TRIM: trim_after = True - stitch_plan.delete_empty_color_block(color_block) + stitch_plan.delete_empty_color_blocks() extents = stitch_plan.extents svg = etree.Element("svg", nsmap=inkex.NSS, attrib={ diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 0b12c2c5..59c54e3d 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -1,8 +1,8 @@ -from .stitch import Stitch -from .ties import add_ties from ..svg import PIXELS_PER_MM -from ..utils.geometry import Point from ..threads import ThreadColor +from ..utils.geometry import Point +from .stitch import Stitch +from .ties import add_ties def patches_to_stitch_plan(patches, collapse_len=3.0 * PIXELS_PER_MM): @@ -72,9 +72,13 @@ class StitchPlan(object): self.color_blocks.append(color_block) return color_block - def delete_empty_color_block(self, color_block): - if len(color_block) == 0: - self.color_blocks.remove(color_block) + def delete_empty_color_blocks(self): + color_blocks = [] + for color_block in self.color_blocks: + if len(color_block) > 0: + color_blocks.append(color_block) + + self.color_block = color_blocks def add_color_block(self, color_block): self.color_blocks.append(color_block) -- cgit v1.2.3 From 7ce4387ff6f8d4302ff374921d0b93b7b491b9a5 Mon Sep 17 00:00:00 2001 From: Momo Date: Wed, 10 Jul 2019 08:08:39 +0200 Subject: trim >= 3 jumps --- lib/extensions/input.py | 2 +- lib/stitch_plan/stitch_plan.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/extensions/input.py b/lib/extensions/input.py index 03148832..a82cdfca 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -13,7 +13,7 @@ class Input(object): def affect(self, args): embroidery_file = args[0] pattern = pyembroidery.read(embroidery_file) - pattern = pattern.get_pattern_interpolate_trim(2) + pattern = pattern.get_pattern_interpolate_trim(3) stitch_plan = StitchPlan() color_block = None diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 59c54e3d..899c502f 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -78,7 +78,7 @@ class StitchPlan(object): if len(color_block) > 0: color_blocks.append(color_block) - self.color_block = color_blocks + self.color_blocks = color_blocks def add_color_block(self, color_block): self.color_blocks.append(color_block) -- cgit v1.2.3