2018-05-02 01:21:07 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
from .stitch import Stitch
|
2018-09-16 01:33:44 +00:00
|
|
|
from ..svg import PIXELS_PER_MM
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
def add_tie(stitches, tie_path):
|
2018-10-24 00:08:46 +00:00
|
|
|
if len(tie_path) < 2 or tie_path[0].no_ties:
|
2018-04-07 02:09:26 +00:00
|
|
|
# It's from a manual stitch block, so don't add tie stitches. The user
|
|
|
|
# will add them if they want them.
|
|
|
|
return
|
|
|
|
|
2018-09-16 01:33:44 +00:00
|
|
|
to_previous = tie_path[1] - tie_path[0]
|
|
|
|
length = to_previous.length()
|
|
|
|
if length > 0.5 * PIXELS_PER_MM:
|
|
|
|
# Travel back one stitch, stopping halfway there.
|
|
|
|
# Then go forward one stitch, stopping halfway between
|
|
|
|
# again.
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2018-09-16 01:33:44 +00:00
|
|
|
# but travel at most 1.5mm
|
|
|
|
length = min(length, 1.5 * PIXELS_PER_MM)
|
|
|
|
|
|
|
|
direction = to_previous.unit()
|
|
|
|
for delta in (0.5, 1.0, 0.5, 0):
|
|
|
|
stitches.append(Stitch(tie_path[0] + delta * length * direction))
|
|
|
|
else:
|
|
|
|
# Too short to travel part of the way to the previous stitch; ust go
|
|
|
|
# back and forth to it a couple times.
|
|
|
|
for i in (1, 0, 1, 0):
|
|
|
|
stitches.append(deepcopy(tie_path[i]))
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_tie_off(stitches):
|
2018-09-16 01:33:44 +00:00
|
|
|
add_tie(stitches, stitches[-1:-3:-1])
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_tie_in(stitches, upcoming_stitches):
|
|
|
|
add_tie(stitches, upcoming_stitches)
|
|
|
|
|
|
|
|
|
|
|
|
def add_ties(stitch_plan):
|
|
|
|
"""Add tie-off before and after trims, jumps, and color changes."""
|
|
|
|
|
2018-07-16 02:53:18 +00:00
|
|
|
need_tie_in = True
|
2018-03-31 00:37:11 +00:00
|
|
|
for color_block in stitch_plan:
|
|
|
|
new_stitches = []
|
|
|
|
for i, stitch in enumerate(color_block.stitches):
|
2018-07-16 02:53:18 +00:00
|
|
|
is_special = stitch.trim or stitch.jump or stitch.color_change or stitch.stop
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2018-09-16 17:09:00 +00:00
|
|
|
if is_special and not need_tie_in:
|
2018-03-31 00:37:11 +00:00
|
|
|
add_tie_off(new_stitches)
|
|
|
|
new_stitches.append(stitch)
|
|
|
|
need_tie_in = True
|
|
|
|
elif need_tie_in and not is_special:
|
|
|
|
new_stitches.append(stitch)
|
|
|
|
add_tie_in(new_stitches, upcoming_stitches=color_block.stitches[i:])
|
|
|
|
need_tie_in = False
|
|
|
|
else:
|
|
|
|
new_stitches.append(stitch)
|
|
|
|
|
|
|
|
color_block.replace_stitches(new_stitches)
|
2018-07-16 02:53:18 +00:00
|
|
|
|
|
|
|
if not need_tie_in:
|
|
|
|
# tie off at the end if we haven't already
|
|
|
|
add_tie_off(color_block.stitches)
|