From 2cd4963d09ef78dd25a7401cc47a69474d7fa952 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sun, 15 Jul 2018 22:53:18 -0400 Subject: adjust stitch plan code for pyembroidery --- lib/stitch_plan/stop.py | 82 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 25 deletions(-) (limited to 'lib/stitch_plan/stop.py') diff --git a/lib/stitch_plan/stop.py b/lib/stitch_plan/stop.py index 81dec1da..12a88d3a 100644 --- a/lib/stitch_plan/stop.py +++ b/lib/stitch_plan/stop.py @@ -1,43 +1,75 @@ -def process_stop(color_block): +from ..svg import PIXELS_PER_MM + +def process_stop(stitch_plan): """Handle the "stop after" checkbox. The user wants the machine to pause after this patch. This can be useful for applique and similar on multi-needle machines that normally would not stop between colors. - In machine embroidery files, there's no such thing as an actual - "STOP" instruction. All that exists is a "color change" command - (which libembroidery calls STOP just to be confusing). + In most machine embroidery file formats, there's no such thing as + an actual "STOP" instruction. All that exists is a "color change" + command. On multi-needle machines, the user assigns needles to the colors in - the design before starting stitching. C01, C02, etc are normal + the design before starting stitching. C01, C02, etc are the normal needles, but C00 is special. For a block of stitches assigned to C00, the machine will continue sewing with the last color it - had and pause after it completes the C00 block. + had and pause after it completes the C00 block. Machines that don't + call it C00 still have a similar concept. + + We'll add a STOP instruction at the end of this color block. + Unfortunately, we have a bit of a catch-22: the user needs to set + C00 (or equivalent) for the _start_ of this block to get the + machine to stop at the end of this block. That means it will use + the previous color, which isn't the right color at all! - That means we need to add an artificial color change instruction - shortly before the current stitch so that the user can set that color - block to C00. We'll go back 3 stitches and mark the start of the C00 - block: + For the first STOP in a given thread color, we'll need to + introduce an extra color change. The user can then set the correct + color for the first section and C00 for the second, resulting in + a stop where we want it. + + We'll try to find a logical place to split the color block, like + a TRIM or a really long stitch. Failing that, we'll just split + it in half. """ - if len(color_block.stitches) >= 3: - # make a copy of the stitch and set it as a color change - stitch = color_block.stitches[-3].copy() - stitch.color_change = True + if not stitch_plan.last_color_block or len(stitch_plan.last_color_block) < 2: + return + + last_stitch = stitch_plan.last_color_block.last_stitch + stitch_plan.last_color_block.add_stitch(last_stitch.x, last_stitch.y, stop=True) + + if len(stitch_plan) > 1: + # if this isn't the first stop in this color, then we're done + if stitch_plan.color_blocks[-2].stop_after and \ + stitch_plan.color_blocks[-2].color == stitch_plan.last_color_block.color: + return + + # We need to split this color block. Pick the last TRIM or + # the last long stitch (probably between distant patches). + + for i in xrange(len(stitch_plan.last_color_block) - 2, -1, -1): + stitch = stitch_plan.last_color_block.stitches[i] - # mark this stitch as a "stop" so that we can avoid - # adding tie stitches in ties.py - stitch.stop = True + if stitch.trim: + # ignore the trim right before the stop we just added + if i < len(stitch_plan.last_color_block) - 2: + # split after the trim + i = i + 1 + break - # insert it after the stitch - color_block.stitches.insert(-2, stitch) + if i > 0: + next_stitch = stitch_plan.last_color_block.stitches[i + 1] - # and also add a color change on this stitch, completing the C00 - # block: + if (stitch - next_stitch).length() > 20 * PIXELS_PER_MM: + break - stitch = color_block.stitches[-1].copy() - stitch.color_change = True - color_block.add_stitch(stitch) + if i == 0: + # Darn, we didn't find a TRIM or long stitch. Just chop the + # block in half. + i = len(stitch_plan.last_color_block) / 2 - # reference for the above: https://github.com/lexelby/inkstitch/pull/29#issuecomment-359175447 + new_color_block = stitch_plan.last_color_block.split_at(i) + stitch_plan.last_color_block.add_stitch(color_change=True, fake_color_change=True) + stitch_plan.add_color_block(new_color_block) -- cgit v1.2.3 From b1912157579299212131b86f0b7267d7d91df047 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Mon, 16 Jul 2018 19:38:42 -0400 Subject: tidy up code --- lib/stitch_plan/stop.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'lib/stitch_plan/stop.py') diff --git a/lib/stitch_plan/stop.py b/lib/stitch_plan/stop.py index 12a88d3a..0ccaeaf8 100644 --- a/lib/stitch_plan/stop.py +++ b/lib/stitch_plan/stop.py @@ -1,5 +1,8 @@ +from copy import copy + from ..svg import PIXELS_PER_MM + def process_stop(stitch_plan): """Handle the "stop after" checkbox. @@ -34,33 +37,35 @@ def process_stop(stitch_plan): it in half. """ - if not stitch_plan.last_color_block or len(stitch_plan.last_color_block) < 2: + color_block = stitch_plan.last_color_block + + if not color_block or len(color_block) < 2: return - last_stitch = stitch_plan.last_color_block.last_stitch - stitch_plan.last_color_block.add_stitch(last_stitch.x, last_stitch.y, stop=True) + last_stitch = color_block.last_stitch + color_block.add_stitch(stop=True) if len(stitch_plan) > 1: # if this isn't the first stop in this color, then we're done if stitch_plan.color_blocks[-2].stop_after and \ - stitch_plan.color_blocks[-2].color == stitch_plan.last_color_block.color: + stitch_plan.color_blocks[-2].color == color_block.color: return # We need to split this color block. Pick the last TRIM or # the last long stitch (probably between distant patches). - for i in xrange(len(stitch_plan.last_color_block) - 2, -1, -1): - stitch = stitch_plan.last_color_block.stitches[i] + for i in xrange(len(color_block) - 2, -1, -1): + stitch = color_block.stitches[i] if stitch.trim: # ignore the trim right before the stop we just added - if i < len(stitch_plan.last_color_block) - 2: + if i < len(color_block) - 2: # split after the trim i = i + 1 break if i > 0: - next_stitch = stitch_plan.last_color_block.stitches[i + 1] + next_stitch = color_block.stitches[i + 1] if (stitch - next_stitch).length() > 20 * PIXELS_PER_MM: break @@ -68,8 +73,16 @@ def process_stop(stitch_plan): if i == 0: # Darn, we didn't find a TRIM or long stitch. Just chop the # block in half. - i = len(stitch_plan.last_color_block) / 2 + i = len(color_block) / 2 + + new_color_block = color_block.split_at(i) + + # If we're splitting in the middle of a run of stitches, we don't + # want a gap to appear in the preview and the PDF printout, so + # add an extra stitch to bridge the gap. Technically this will + # result in a double needle penetration but it's no big deal. + if not color_block.last_stitch.trim: + color_block.add_stitch(copy(new_color_block.stitches[0])) - new_color_block = stitch_plan.last_color_block.split_at(i) - stitch_plan.last_color_block.add_stitch(color_change=True, fake_color_change=True) + color_block.add_stitch(color_change=True, fake_color_change=True) stitch_plan.add_color_block(new_color_block) -- cgit v1.2.3 From 5ce8df77a07b11b902792d299d4cb89b6951ccd8 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Fri, 20 Jul 2018 21:41:28 -0400 Subject: remove incorrect stop logic --- lib/stitch_plan/stop.py | 88 ------------------------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 lib/stitch_plan/stop.py (limited to 'lib/stitch_plan/stop.py') diff --git a/lib/stitch_plan/stop.py b/lib/stitch_plan/stop.py deleted file mode 100644 index 0ccaeaf8..00000000 --- a/lib/stitch_plan/stop.py +++ /dev/null @@ -1,88 +0,0 @@ -from copy import copy - -from ..svg import PIXELS_PER_MM - - -def process_stop(stitch_plan): - """Handle the "stop after" checkbox. - - The user wants the machine to pause after this patch. This can - be useful for applique and similar on multi-needle machines that - normally would not stop between colors. - - In most machine embroidery file formats, there's no such thing as - an actual "STOP" instruction. All that exists is a "color change" - command. - - On multi-needle machines, the user assigns needles to the colors in - the design before starting stitching. C01, C02, etc are the normal - needles, but C00 is special. For a block of stitches assigned - to C00, the machine will continue sewing with the last color it - had and pause after it completes the C00 block. Machines that don't - call it C00 still have a similar concept. - - We'll add a STOP instruction at the end of this color block. - Unfortunately, we have a bit of a catch-22: the user needs to set - C00 (or equivalent) for the _start_ of this block to get the - machine to stop at the end of this block. That means it will use - the previous color, which isn't the right color at all! - - For the first STOP in a given thread color, we'll need to - introduce an extra color change. The user can then set the correct - color for the first section and C00 for the second, resulting in - a stop where we want it. - - We'll try to find a logical place to split the color block, like - a TRIM or a really long stitch. Failing that, we'll just split - it in half. - """ - - color_block = stitch_plan.last_color_block - - if not color_block or len(color_block) < 2: - return - - last_stitch = color_block.last_stitch - color_block.add_stitch(stop=True) - - if len(stitch_plan) > 1: - # if this isn't the first stop in this color, then we're done - if stitch_plan.color_blocks[-2].stop_after and \ - stitch_plan.color_blocks[-2].color == color_block.color: - return - - # We need to split this color block. Pick the last TRIM or - # the last long stitch (probably between distant patches). - - for i in xrange(len(color_block) - 2, -1, -1): - stitch = color_block.stitches[i] - - if stitch.trim: - # ignore the trim right before the stop we just added - if i < len(color_block) - 2: - # split after the trim - i = i + 1 - break - - if i > 0: - next_stitch = color_block.stitches[i + 1] - - if (stitch - next_stitch).length() > 20 * PIXELS_PER_MM: - break - - if i == 0: - # Darn, we didn't find a TRIM or long stitch. Just chop the - # block in half. - i = len(color_block) / 2 - - new_color_block = color_block.split_at(i) - - # If we're splitting in the middle of a run of stitches, we don't - # want a gap to appear in the preview and the PDF printout, so - # add an extra stitch to bridge the gap. Technically this will - # result in a double needle penetration but it's no big deal. - if not color_block.last_stitch.trim: - color_block.add_stitch(copy(new_color_block.stitches[0])) - - color_block.add_stitch(color_change=True, fake_color_change=True) - stitch_plan.add_color_block(new_color_block) -- cgit v1.2.3