diff options
Diffstat (limited to 'lib/stitch_plan')
| -rw-r--r-- | lib/stitch_plan/color_block.py | 23 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch.py | 10 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch_plan.py | 9 |
3 files changed, 39 insertions, 3 deletions
diff --git a/lib/stitch_plan/color_block.py b/lib/stitch_plan/color_block.py index 9d474f80..fdef5eb8 100644 --- a/lib/stitch_plan/color_block.py +++ b/lib/stitch_plan/color_block.py @@ -3,10 +3,12 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. -from .stitch import Stitch +from typing import List + +from ..svg import PIXELS_PER_MM from ..threads import ThreadColor from ..utils.geometry import Point -from ..svg import PIXELS_PER_MM +from .stitch import Stitch class ColorBlock(object): @@ -155,3 +157,20 @@ class ColorBlock(object): maxy = max(stitch.y for stitch in self) return minx, miny, maxx, maxy + + def make_offsets(self, offsets: List[Point]): + first_final_stitch = len(self.stitches) + while (first_final_stitch > 0 and self.stitches[first_final_stitch-1].is_terminator): + first_final_stitch -= 1 + if first_final_stitch == 0: + return self + final_stitches = self.stitches[first_final_stitch:] + block_stitches = self.stitches[:first_final_stitch] + + out = ColorBlock(self.color) + for i, offset in enumerate(offsets): + out.add_stitches([s.offset(offset) for s in block_stitches]) + if i != len(offsets) - 1: + out.add_stitch(trim=True) + out.add_stitches(final_stitches) + return out diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py index 90af58c0..8ad699c7 100644 --- a/lib/stitch_plan/stitch.py +++ b/lib/stitch_plan/stitch.py @@ -68,6 +68,10 @@ class Stitch(Point): if value or base_stitch is None: setattr(self, attribute, value) + @property + def is_terminator(self) -> bool: + return self.trim or self.stop or self.color_change + def add_tags(self, tags): for tag in tags: self.add_tag(tag) @@ -93,6 +97,12 @@ class Stitch(Point): def copy(self): return Stitch(self.x, self.y, self.color, self.jump, self.stop, self.trim, self.color_change, self.tags) + def offset(self, offset: Point): + out = self.copy() + out.x += offset.x + out.y += offset.y + return out + def __json__(self): attributes = dict(vars(self)) attributes['tags'] = list(attributes['tags']) diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 25571578..caea9c09 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -4,13 +4,15 @@ # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. from sys import exit +from typing import List from inkex import errormsg from ..i18n import _ from ..svg import PIXELS_PER_MM -from .color_block import ColorBlock +from ..utils.geometry import Point from ..utils.threading import check_stop_flag +from .color_block import ColorBlock def stitch_groups_to_stitch_plan(stitch_groups, collapse_len=None, min_stitch_len=0.1, disable_ties=False): # noqa: C901 @@ -207,3 +209,8 @@ class StitchPlan(object): return self.color_blocks[-1] else: return None + + def make_offsets(self, offsets: List[Point]): + out = StitchPlan() + out.color_blocks = [block.make_offsets(offsets) for block in self] + return out |
