From b6bde000fe675ed725d7d5dbd7db3f44fb33af0e Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Wed, 22 Jun 2022 15:22:34 +0200 Subject: [PATCH] Option to drop short stitches (#1693) --- lib/api/stitch_plan.py | 3 ++- lib/extensions/embroider_settings.py | 5 +++++ lib/extensions/generate_palette.py | 2 +- lib/extensions/output.py | 4 +++- lib/extensions/print_pdf.py | 3 ++- lib/extensions/stitch_plan_preview.py | 3 ++- lib/extensions/zip.py | 3 ++- lib/stitch_plan/color_block.py | 8 +++++--- lib/stitch_plan/stitch_plan.py | 8 ++++---- templates/embroider_settings.xml | 3 +++ 10 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/api/stitch_plan.py b/lib/api/stitch_plan.py index 6d64d781e..c70efd982 100644 --- a/lib/api/stitch_plan.py +++ b/lib/api/stitch_plan.py @@ -18,7 +18,8 @@ def get_stitch_plan(): metadata = g.extension.get_inkstitch_metadata() collapse_len = metadata['collapse_len_mm'] + min_stitch_len = metadata['min_stitch_len_mm'] patches = g.extension.elements_to_stitch_groups(g.extension.elements) - stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len) + stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len, min_stitch_len=min_stitch_len) return jsonify(stitch_plan) diff --git a/lib/extensions/embroider_settings.py b/lib/extensions/embroider_settings.py index f504c88f6..cdf189912 100644 --- a/lib/extensions/embroider_settings.py +++ b/lib/extensions/embroider_settings.py @@ -16,7 +16,12 @@ class EmbroiderSettings(InkstitchExtension): action="store", type=float, dest="collapse_length_mm", default=3.0, help="max collapse length (mm)") + self.arg_parser.add_argument("-l", "--min_stitch_len_mm", + action="store", type=float, + dest="min_stitch_len_mm", default=0, + help="minimum stitch length (mm)") def effect(self): self.metadata = self.get_inkstitch_metadata() self.metadata['collapse_len_mm'] = self.options.collapse_length_mm + self.metadata['min_stitch_len_mm'] = self.options.min_stitch_len_mm diff --git a/lib/extensions/generate_palette.py b/lib/extensions/generate_palette.py index 280be90fd..fdb76735d 100644 --- a/lib/extensions/generate_palette.py +++ b/lib/extensions/generate_palette.py @@ -73,7 +73,7 @@ class GeneratePalette(InkstitchExtension): else: number = 0 name = ' '.join(color_name) - color = "\n%s\t%s\t%s\t%s %s" % (str(color[0]).rjust(3), str(color[1]).rjust(3), str(color[2]).rjust(3), name.rjust(30), number) + color = "\n%s\t%s\t%s\t%s %s" % (str(color[0]).rjust(3), str(color[1]).rjust(3), str(color[2]).rjust(3), name.rjust(30), number) colors.append(color) return colors diff --git a/lib/extensions/output.py b/lib/extensions/output.py index 7cc12ee05..a3f35d2f4 100644 --- a/lib/extensions/output.py +++ b/lib/extensions/output.py @@ -52,8 +52,10 @@ class Output(InkstitchExtension): self.metadata = self.get_inkstitch_metadata() collapse_len = self.metadata['collapse_len_mm'] + min_stitch_len = self.metadata['min_stitch_len_mm'] patches = self.elements_to_stitch_groups(self.elements) - stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len, disable_ties=self.settings.get('laser_mode', False)) + stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len, disable_ties=self.settings.get('laser_mode', False), + min_stitch_len=min_stitch_len) temp_file = tempfile.NamedTemporaryFile(suffix=".%s" % self.file_extension, delete=False) diff --git a/lib/extensions/print_pdf.py b/lib/extensions/print_pdf.py index 63c3c699a..47e68c5d0 100644 --- a/lib/extensions/print_pdf.py +++ b/lib/extensions/print_pdf.py @@ -308,8 +308,9 @@ class Print(InkstitchExtension): self.metadata = self.get_inkstitch_metadata() collapse_len = self.metadata['collapse_len_mm'] + min_stitch_len = self.metadata['min_stitch_len_mm'] patches = self.elements_to_stitch_groups(self.elements) - stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len) + stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len, min_stitch_len=min_stitch_len) palette = ThreadCatalog().match_and_apply_palette(stitch_plan, self.get_inkstitch_metadata()['thread-palette']) overview_svg, color_block_svgs = self.render_svgs(stitch_plan, realistic=False) diff --git a/lib/extensions/stitch_plan_preview.py b/lib/extensions/stitch_plan_preview.py index 2a4287814..571d5040c 100644 --- a/lib/extensions/stitch_plan_preview.py +++ b/lib/extensions/stitch_plan_preview.py @@ -35,8 +35,9 @@ class StitchPlanPreview(InkstitchExtension): visual_commands = True self.metadata = self.get_inkstitch_metadata() collapse_len = self.metadata['collapse_len_mm'] + min_stitch_len = self.metadata['min_stitch_len_mm'] patches = self.elements_to_stitch_groups(self.elements) - stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len) + stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len, min_stitch_len=min_stitch_len) render_stitch_plan(svg, stitch_plan, realistic, visual_commands) # apply options diff --git a/lib/extensions/zip.py b/lib/extensions/zip.py index 226545604..280c2a140 100644 --- a/lib/extensions/zip.py +++ b/lib/extensions/zip.py @@ -43,8 +43,9 @@ class Zip(InkstitchExtension): self.metadata = self.get_inkstitch_metadata() collapse_len = self.metadata['collapse_len_mm'] + min_stitch_len = self.metadata['min_stitch_len_mm'] patches = self.elements_to_stitch_groups(self.elements) - stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len) + stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len, min_stitch_len=min_stitch_len) base_file_name = self.get_base_file_name() path = tempfile.mkdtemp() diff --git a/lib/stitch_plan/color_block.py b/lib/stitch_plan/color_block.py index cd7b9c6dd..2f3874606 100644 --- a/lib/stitch_plan/color_block.py +++ b/lib/stitch_plan/color_block.py @@ -98,19 +98,21 @@ class ColorBlock(object): return False - def filter_duplicate_stitches(self): + def filter_duplicate_stitches(self, min_stitch_len=0.1): if not self.stitches: return - stitches = [self.stitches[0]] + if min_stitch_len is None: + min_stitch_len = 0.1 + stitches = [self.stitches[0]] for stitch in self.stitches[1:]: if stitches[-1].jump or stitch.stop or stitch.trim or stitch.color_change: # Don't consider jumps, stops, color changes, or trims as candidates for filtering pass else: length = (stitch - stitches[-1]).length() - if length <= 0.1 * PIXELS_PER_MM: + if length <= min_stitch_len * PIXELS_PER_MM: # duplicate stitch, skip this one continue diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 4593781ae..04d587c2e 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -13,7 +13,7 @@ from .color_block import ColorBlock from .ties import add_ties -def stitch_groups_to_stitch_plan(stitch_groups, collapse_len=None, disable_ties=False): # noqa: C901 +def stitch_groups_to_stitch_plan(stitch_groups, collapse_len=None, min_stitch_len=0.1, disable_ties=False): # noqa: C901 """Convert a collection of StitchGroups to a StitchPlan. @@ -71,7 +71,7 @@ def stitch_groups_to_stitch_plan(stitch_groups, collapse_len=None, disable_ties= # last block ended in a stop, so now we have an empty block del stitch_plan.color_blocks[-1] - stitch_plan.filter_duplicate_stitches() + stitch_plan.filter_duplicate_stitches(min_stitch_len) if not disable_ties: stitch_plan.add_ties() @@ -101,9 +101,9 @@ class StitchPlan(object): def add_color_block(self, color_block): self.color_blocks.append(color_block) - def filter_duplicate_stitches(self): + def filter_duplicate_stitches(self, min_stitch_len): for color_block in self: - color_block.filter_duplicate_stitches() + color_block.filter_duplicate_stitches(min_stitch_len) def add_ties(self): # see ties.py diff --git a/templates/embroider_settings.xml b/templates/embroider_settings.xml index 96339fcb0..362aa7f99 100644 --- a/templates/embroider_settings.xml +++ b/templates/embroider_settings.xml @@ -15,6 +15,9 @@ 3 + 0.1