diff options
Diffstat (limited to 'lib/stitch_plan')
| -rw-r--r-- | lib/stitch_plan/__init__.py | 3 | ||||
| -rw-r--r-- | lib/stitch_plan/read_file.py | 21 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch_plan.py | 6 |
3 files changed, 29 insertions, 1 deletions
diff --git a/lib/stitch_plan/__init__.py b/lib/stitch_plan/__init__.py index 791a5f20..2aaa0ab9 100644 --- a/lib/stitch_plan/__init__.py +++ b/lib/stitch_plan/__init__.py @@ -1,2 +1,3 @@ -from stitch_plan import patches_to_stitch_plan, StitchPlan, ColorBlock +from .stitch_plan import patches_to_stitch_plan, StitchPlan, ColorBlock from .stitch import Stitch +from .read_file import stitch_plan_from_file diff --git a/lib/stitch_plan/read_file.py b/lib/stitch_plan/read_file.py new file mode 100644 index 00000000..ff5a68ac --- /dev/null +++ b/lib/stitch_plan/read_file.py @@ -0,0 +1,21 @@ +import pyembroidery +from .stitch_plan import StitchPlan + +from ..svg import PIXELS_PER_MM + + +def stitch_plan_from_file(embroidery_file): + """Read a machine embroidery file in any supported format and return a stitch plan.""" + pattern = pyembroidery.read(embroidery_file) + + stitch_plan = StitchPlan() + color_block = None + + 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)) + + return stitch_plan diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index c713b42e..808fa626 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -36,6 +36,12 @@ def patches_to_stitch_plan(patches, collapse_len=3.0 * PIXELS_PER_MM): # make a new block of our color color_block = stitch_plan.new_color_block(color=patch.color) + # always start a color with a JUMP to the first stitch position + color_block.add_stitch(patch.stitches[0], jump=True) + else: + if len(color_block) and (patch.stitches[0] - color_block.stitches[-1]).length() > collapse_len: + color_block.add_stitch(patch.stitches[0], jump=True) + color_block.add_stitches(patch.stitches, no_ties=patch.stitch_as_is) if patch.trim_after: |
