summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2021-08-07 12:00:56 -0400
committerLex Neva <github.com@lexneva.name>2021-08-07 12:01:07 -0400
commit28e394b2ae7f4dabcc331d456103d4b3d0efae84 (patch)
tree5234cf44b4d445d19683d7e7426fa033c105ec44
parent8fc42628e285160f8f747772b6d5674a1bf23a09 (diff)
StitchGroups now contain only Stitches
-rw-r--r--lib/stitch_plan/color_block.py6
-rw-r--r--lib/stitch_plan/stitch.py20
-rw-r--r--lib/stitch_plan/stitch_group.py18
3 files changed, 30 insertions, 14 deletions
diff --git a/lib/stitch_plan/color_block.py b/lib/stitch_plan/color_block.py
index 1cff8aa4..4ff33cdf 100644
--- a/lib/stitch_plan/color_block.py
+++ b/lib/stitch_plan/color_block.py
@@ -112,15 +112,11 @@ class ColorBlock(object):
args = (self.last_stitch.x, self.last_stitch.y)
else:
raise ValueError("internal error: can't add a command to an empty stitch block")
-
+ self.stitches.append(Stitch(*args, **kwargs))
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):
for stitch in stitches:
diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py
index ea4423fa..f163d09c 100644
--- a/lib/stitch_plan/stitch.py
+++ b/lib/stitch_plan/stitch.py
@@ -4,13 +4,25 @@
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
from ..utils.geometry import Point
+from copy import deepcopy
class Stitch(Point):
"""A stitch is a Point with extra information telling how to sew it."""
def __init__(self, x, y=None, color=None, jump=False, stop=False, trim=False, color_change=False, tie_modus=0, no_ties=False, tags=None):
- Point.__init__(self, x, y)
+ if isinstance(x, Stitch):
+ # Allow creating a Stitch from another Stitch. Attributes passed as
+ # arguments will override any existing attributes.
+ vars(self).update(deepcopy(vars(x)))
+ elif isinstance(x, Point):
+ # Allow creating a Stitch from a Point
+ point = x
+ self.x = point.x
+ self.y = point.y
+ else:
+ Point.__init__(self, x, y)
+
self.color = color
self.jump = jump
self.trim = trim
@@ -22,12 +34,6 @@ class Stitch(Point):
self.add_tags(tags or [])
- # Allow creating a Stitch from a Point
- if isinstance(x, Point):
- point = x
- self.x = point.x
- self.y = point.y
-
def __repr__(self):
return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.x,
self.y,
diff --git a/lib/stitch_plan/stitch_group.py b/lib/stitch_plan/stitch_group.py
index d1e6bae7..548ad892 100644
--- a/lib/stitch_plan/stitch_group.py
+++ b/lib/stitch_plan/stitch_group.py
@@ -1,5 +1,8 @@
+from .stitch import Stitch
+
+
class StitchGroup:
- """A collection of Stitch objects with attached instructions.
+ """A collection of Stitch objects with attached instructions and attributes.
StitchGroups will later be combined to make ColorBlocks, which in turn are
combined to make a StitchPlan. Jump stitches are allowed between
@@ -11,11 +14,14 @@ class StitchGroup:
def __init__(self, color=None, stitches=None, trim_after=False, stop_after=False, tie_modus=0, stitch_as_is=False):
self.color = color
- self.stitches = stitches or []
self.trim_after = trim_after
self.stop_after = stop_after
self.tie_modus = tie_modus
self.stitch_as_is = stitch_as_is
+ self.stitches = []
+
+ if stitches:
+ self.add_stitches(stitches)
def __add__(self, other):
if isinstance(other, StitchGroup):
@@ -27,7 +33,15 @@ class StitchGroup:
# This method allows `len(patch)` and `if patch:
return len(self.stitches)
+ def add_stitches(self, stitches):
+ for stitch in stitches:
+ self.add_stitch(stitch)
+
def add_stitch(self, stitch):
+ if not isinstance(stitch, Stitch):
+ # probably a Point
+ stitch = Stitch(stitch)
+
self.stitches.append(stitch)
def reverse(self):