summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/output.py6
-rw-r--r--lib/stitch_plan/stitch.py14
-rw-r--r--lib/stitch_plan/stitch_plan.py94
-rw-r--r--lib/stitch_plan/stop.py82
-rw-r--r--lib/stitch_plan/ties.py18
-rw-r--r--lib/stitch_plan/trim.py23
6 files changed, 140 insertions, 97 deletions
diff --git a/lib/output.py b/lib/output.py
index 491c190a..1c580f04 100644
--- a/lib/output.py
+++ b/lib/output.py
@@ -111,7 +111,11 @@ def write_embroidery_file(file_path, stitch_plan, svg):
# convert from pixels to millimeters
# also multiply by 10 to get tenths of a millimeter as required by pyembroidery
- "scale": (scale, scale)
+ "scale": (scale, scale),
+
+ # This forces a jump at the start of the design and after each trim,
+ # even if we're close enough not to need one.
+ "full_jump": True,
}
pyembroidery.write(pattern, file_path, settings)
diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py
index 12642a60..5230efec 100644
--- a/lib/stitch_plan/stitch.py
+++ b/lib/stitch_plan/stitch.py
@@ -2,7 +2,7 @@ from ..utils.geometry import Point
class Stitch(Point):
- def __init__(self, x, y, color=None, jump=False, stop=False, trim=False, color_change=False, no_ties=False):
+ def __init__(self, x, y, color=None, jump=False, stop=False, trim=False, color_change=False, fake_color_change=False, no_ties=False):
self.x = x
self.y = y
self.color = color
@@ -10,10 +10,20 @@ class Stitch(Point):
self.trim = trim
self.stop = stop
self.color_change = color_change
+ self.fake_color_change = fake_color_change
self.no_ties = no_ties
def __repr__(self):
- return "Stitch(%s, %s, %s, %s, %s, %s, %s)" % (self.x, self.y, self.color, "JUMP" if self.jump else " ", "TRIM" if self.trim else " ", "STOP" if self.stop else " ", "NO TIES" if self.no_ties else " ")
+ return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s%s)" % (self.x,
+ self.y,
+ self.color,
+ "JUMP" if self.jump else " ",
+ "TRIM" if self.trim else " ",
+ "STOP" if self.stop else " ",
+ "NO TIES" if self.no_ties else " ",
+ "FAKE " if self.fake_color_change else "",
+ "COLOR CHANGE" if self.color_change else " "
+ )
def copy(self):
return Stitch(self.x, self.y, self.color, self.jump, self.stop, self.trim, self.color_change, self.no_ties)
diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py
index 742916f0..1a466295 100644
--- a/lib/stitch_plan/stitch_plan.py
+++ b/lib/stitch_plan/stitch_plan.py
@@ -16,64 +16,40 @@ def patches_to_stitch_plan(patches, collapse_len=3.0 * PIXELS_PER_MM):
"""
stitch_plan = StitchPlan()
- color_block = stitch_plan.new_color_block()
- need_trim = False
+ if not patches:
+ return stitch_plan
+
+ color_block = stitch_plan.new_color_block(color=patches[0].color)
+
for patch in patches:
if not patch.stitches:
continue
- if not color_block.has_color():
- # set the color for the first color block
- color_block.color = patch.color
-
- if color_block.color == patch.color:
- if need_trim:
- process_trim(color_block, patch.stitches[0])
- need_trim = False
-
- # add a jump stitch between patches if the distance is more
- # than the collapse length
- if color_block.last_stitch:
- if (patch.stitches[0] - color_block.last_stitch).length() > collapse_len:
- color_block.add_stitch(patch.stitches[0].x, patch.stitches[0].y, jump=True)
-
- else:
+ if color_block.color != patch.color or color_block.stop_after:
# add a color change (only if we didn't just do a "STOP after")
- if not color_block.last_stitch.color_change:
- stitch = color_block.last_stitch.copy()
- stitch.color_change = True
- color_block.add_stitch(stitch)
+ if not color_block.stop_after:
+ color_block.add_stitch(color_change=True)
- color_block = stitch_plan.new_color_block()
- color_block.color = patch.color
+ color_block = stitch_plan.new_color_block(color=patch.color)
color_block.filter_duplicate_stitches()
color_block.add_stitches(patch.stitches, no_ties=patch.stitch_as_is)
if patch.trim_after:
- # a trim needs to be followed by a jump to the next stitch, so
- # we'll process it when we start the next patch
- need_trim = True
+ color_block.add_stitch(trim=True)
if patch.stop_after:
- process_stop(color_block)
+ process_stop(stitch_plan)
+
+ # process_stop() may have split the block into two
+ color_block = stitch_plan.last_color_block
- add_jumps(stitch_plan)
add_ties(stitch_plan)
return stitch_plan
-def add_jumps(stitch_plan):
- """Add a JUMP stitch at the start of each color block."""
-
- for color_block in stitch_plan:
- stitch = color_block.stitches[0].copy()
- stitch.jump = True
- color_block.stitches.insert(0, stitch)
-
-
class StitchPlan(object):
"""Holds a set of color blocks, each containing stitches."""
@@ -85,6 +61,9 @@ class StitchPlan(object):
self.color_blocks.append(color_block)
return color_block
+ def add_color_block(self, color_block):
+ self.color_blocks.append(color_block)
+
def __iter__(self):
return iter(self.color_blocks)
@@ -100,6 +79,10 @@ class StitchPlan(object):
return len({block.color for block in self})
@property
+ def num_color_blocks(self):
+ return len(self.color_blocks)
+
+ @property
def num_stops(self):
return sum(block.num_stops for block in self)
@@ -137,6 +120,13 @@ class StitchPlan(object):
dimensions = self.dimensions
return (dimensions[0] / PIXELS_PER_MM, dimensions[1] / PIXELS_PER_MM)
+ @property
+ def last_color_block(self):
+ if self.color_blocks:
+ return self.color_blocks[-1]
+ else:
+ return None
+
class ColorBlock(object):
"""Holds a set of stitches, all with the same thread color."""
@@ -148,6 +138,9 @@ class ColorBlock(object):
def __iter__(self):
return iter(self.stitches)
+ def __len__(self):
+ return len(self.stitches)
+
def __repr__(self):
return "ColorBlock(%s, %s)" % (self.color, self.stitches)
@@ -191,6 +184,13 @@ class ColorBlock(object):
return sum(1 for stitch in self if stitch.trim)
+ @property
+ def stop_after(self):
+ if self.last_stitch is not None:
+ return self.last_stitch.stop
+ else:
+ return False
+
def filter_duplicate_stitches(self):
if not self.stitches:
return
@@ -212,11 +212,21 @@ class ColorBlock(object):
self.stitches = stitches
def add_stitch(self, *args, **kwargs):
+ if not args:
+ # They're adding a command, e.g. `color_block.add_stitch(stop=True)``.
+ # Use the position from the last stitch.
+ if self.last_stitch:
+ args = (self.last_stitch.x, self.last_stitch.y)
+ else:
+ raise ValueError("internal error: can't add a command to an empty stitch block")
+
if isinstance(args[0], Stitch):
self.stitches.append(args[0])
elif isinstance(args[0], Point):
self.stitches.append(Stitch(args[0].x, args[0].y, *args[1:], **kwargs))
else:
+ if not args and self.last_stitch:
+ args = (self.last_stitch.x, self.last_stitch.y)
self.stitches.append(Stitch(*args, **kwargs))
def add_stitches(self, stitches, *args, **kwargs):
@@ -237,3 +247,11 @@ class ColorBlock(object):
maxy = max(stitch.y for stitch in self)
return minx, miny, maxx, maxy
+
+ def split_at(self, index):
+ """Split this color block into two at the specified stitch index"""
+
+ new_color_block = ColorBlock(self.color, self.stitches[index:])
+ del self.stitches[index:]
+
+ return new_color_block
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)
diff --git a/lib/stitch_plan/ties.py b/lib/stitch_plan/ties.py
index 6d07ac71..573469f5 100644
--- a/lib/stitch_plan/ties.py
+++ b/lib/stitch_plan/ties.py
@@ -30,15 +30,16 @@ def add_tie_in(stitches, upcoming_stitches):
def add_ties(stitch_plan):
"""Add tie-off before and after trims, jumps, and color changes."""
+ need_tie_in = True
for color_block in stitch_plan:
- need_tie_in = True
new_stitches = []
for i, stitch in enumerate(color_block.stitches):
- # Tie before and after TRIMs, JUMPs, and color changes, but ignore
- # the fake color change introduced by a "STOP after" (see stop.py).
- is_special = stitch.trim or stitch.jump or (stitch.color_change and not stitch.stop)
+ is_special = stitch.trim or stitch.jump or stitch.color_change or stitch.stop
- if is_special and not need_tie_in:
+ # see stop.py for an explanation of the fake color change
+ is_fake = stitch.fake_color_change
+
+ if is_special and not is_fake and not need_tie_in:
add_tie_off(new_stitches)
new_stitches.append(stitch)
need_tie_in = True
@@ -49,7 +50,8 @@ def add_ties(stitch_plan):
else:
new_stitches.append(stitch)
- if not need_tie_in:
- add_tie_off(new_stitches)
-
color_block.replace_stitches(new_stitches)
+
+ if not need_tie_in:
+ # tie off at the end if we haven't already
+ add_tie_off(color_block.stitches)
diff --git a/lib/stitch_plan/trim.py b/lib/stitch_plan/trim.py
deleted file mode 100644
index f692a179..00000000
--- a/lib/stitch_plan/trim.py
+++ /dev/null
@@ -1,23 +0,0 @@
-def process_trim(color_block, next_stitch):
- """Handle the "trim after" checkbox.
-
- DST (and maybe other formats?) has no actual TRIM instruction.
- Instead, 3 sequential JUMPs cause the machine to trim the thread.
-
- To support both DST and other formats, we'll add a TRIM and two
- JUMPs. The TRIM will be converted to a JUMP by libembroidery
- if saving to DST, resulting in the 3-jump sequence.
- """
-
- delta = next_stitch - color_block.last_stitch
- delta = delta * (1/4.0)
-
- pos = color_block.last_stitch
-
- for i in xrange(3):
- pos += delta
- color_block.add_stitch(pos.x, pos.y, jump=True)
-
- # first one should be TRIM instead of JUMP
- color_block.stitches[-3].jump = False
- color_block.stitches[-3].trim = True