Skip to content

Commit 8752c81

Browse files
authored
Moved InterpolateTrim into EmbPattern
1 parent 42c52e7 commit 8752c81

File tree

4 files changed

+81
-82
lines changed

4 files changed

+81
-82
lines changed

pyembroidery/DstReader.py

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,7 @@ def dst_read_header(f, out):
5959

6060

6161
def dst_read_stitches(f, out, settings=None):
62-
jump_count_max = 3
63-
clipping = True
64-
if settings is not None:
65-
jump_count_max = settings.get('trim_at', jump_count_max)
66-
clipping = settings.get('clipping', clipping)
6762
sequin_mode = False
68-
jump_count = 0
69-
jump_start = 0
70-
jump_dx = 0
71-
jump_dy = 0
72-
jumping = False
73-
trimmed = True
7463
while True:
7564
byte = bytearray(f.read(3))
7665
if len(byte) != 3:
@@ -81,38 +70,29 @@ def dst_read_stitches(f, out, settings=None):
8170
break
8271
elif byte[2] & 0b11000011 == 0b11000011:
8372
out.color_change(dx, dy)
84-
trimmed = True
85-
jumping = False
8673
elif byte[2] & 0b01000011 == 0b01000011:
8774
out.sequin_mode(dx, dy)
8875
sequin_mode = not sequin_mode
89-
jumping = False
9076
elif byte[2] & 0b10000011 == 0b10000011:
9177
if sequin_mode:
9278
out.sequin_eject(dx, dy)
9379
else:
9480
out.move(dx, dy)
95-
if not jumping:
96-
jump_dx = 0
97-
jump_dy = 0
98-
jump_count = 0
99-
jump_start = len(out.stitches) - 1
100-
jumping = True
101-
jump_count += 1
102-
jump_dx += dx
103-
jump_dy += dy
104-
if not trimmed and jump_count == jump_count_max:
105-
out.trim(position=jump_start)
106-
jump_start += 1 # We inserted a position, start jump has moved.
107-
trimmed = True
108-
if clipping and jump_dx == 0 and jump_dy == 0: # jump displacement is 0, clip trim command.
109-
out.stitches = out.stitches[:jump_start]
11081
else:
11182
out.stitch(dx, dy)
112-
trimmed = False
113-
jumping = False
11483
out.end()
11584

85+
count_max = 3
86+
clipping = True
87+
trim_distance = None
88+
if settings is not None:
89+
count_max = settings.get('trim_at', count_max)
90+
trim_distance = settings.get("trim_distance", trim_distance)
91+
clipping = settings.get('clipping', clipping)
92+
if trim_distance is not None:
93+
trim_distance *= 10 # Pixels per mm. Native units are 1/10 mm.
94+
out.interpolate_trims(count_max, trim_distance, clipping)
95+
11696

11797
def read(f, out, settings=None):
11898
dst_read_header(f, out)

pyembroidery/EmbPattern.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,60 @@ def add_pattern(self, pattern):
501501
self.stitches[i][2] = NO_COMMAND
502502
self.extras.update(pattern.extras)
503503

504+
def interpolate_trims(self, jumps_to_require_trim=None, distance_to_require_trim=None, clipping=True):
505+
"""Processes a pattern adding trims according to the given criteria."""
506+
i = -1
507+
ie = len(self.stitches) - 1
508+
509+
x = 0
510+
y = 0
511+
jump_count = 0
512+
jump_start = 0
513+
jump_dx = 0
514+
jump_dy = 0
515+
jumping = False
516+
trimmed = True
517+
while i < ie:
518+
i += 1
519+
stitch = self.stitches[i]
520+
dx = stitch[0] - x
521+
dy = stitch[1] - y
522+
x = stitch[0]
523+
y = stitch[1]
524+
command = stitch[2] & COMMAND_MASK
525+
if command == STITCH or command == SEQUIN_EJECT:
526+
trimmed = False
527+
jumping = False
528+
elif command == COLOR_CHANGE or command == NEEDLE_SET or command == TRIM:
529+
trimmed = True
530+
jumping = False
531+
if command == JUMP:
532+
if not jumping:
533+
jump_dx = 0
534+
jump_dy = 0
535+
jump_count = 0
536+
jump_start = i
537+
jumping = True
538+
jump_count += 1
539+
jump_dx += dx
540+
jump_dy += dy
541+
if not trimmed:
542+
if jump_count == jumps_to_require_trim or\
543+
distance_to_require_trim is not None and\
544+
(
545+
abs(jump_dy) > distance_to_require_trim or\
546+
abs(jump_dx) > distance_to_require_trim
547+
):
548+
self.trim(position=jump_start)
549+
jump_start += 1 # We inserted a position, start jump has moved.
550+
i += 1
551+
ie += 1
552+
trimmed = True
553+
if clipping and jump_dx == 0 and jump_dy == 0: # jump displacement is 0, clip trim command.
554+
del self.stitches[jump_start:i+1]
555+
i = jump_start - 1
556+
ie = len(self.stitches) - 1
557+
504558
def get_pattern_interpolate_trim(self, jumps_to_require_trim):
505559
"""Gets a processed pattern with untrimmed jumps merged
506560
and trims added if merged jumps are beyond the given value.

pyembroidery/JefReader.py

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,6 @@
33

44

55
def read_jef_stitches(f, out, settings=None):
6-
clipping = True
7-
trims = False
8-
command_count_max = 3
9-
trim_distance = 3.0
10-
if settings is not None:
11-
command_count_max = settings.get('trim_at', command_count_max)
12-
trims = settings.get("trims", trims)
13-
trim_distance = settings.get("trim_distance", trim_distance)
14-
clipping = settings.get('clipping', clipping)
15-
if trim_distance is not None:
16-
trim_distance *= 10 # Pixels per mm. Native units are 1/10 mm.
17-
jump_count = 0
18-
jump_start = 0
19-
jump_dx = 0
20-
jump_dy = 0
21-
jumping = False
22-
trimmed = True
236
while True:
247
b = bytearray(f.read(2))
258
if len(b) != 2:
@@ -28,8 +11,6 @@ def read_jef_stitches(f, out, settings=None):
2811
x = signed8(b[0])
2912
y = -signed8(b[1])
3013
out.stitch(x, y)
31-
trimmed = False
32-
jumping = False
3314
continue
3415
ctrl = b[1]
3516
b = bytearray(f.read(2))
@@ -39,47 +20,30 @@ def read_jef_stitches(f, out, settings=None):
3920
y = -signed8(b[1])
4021
if ctrl == 0x02:
4122
out.move(x, y)
42-
if not jumping:
43-
jump_dx = 0
44-
jump_dy = 0
45-
jump_count = 0
46-
jump_start = len(out.stitches) - 1
47-
jumping = True
48-
jump_count += 1
49-
jump_dx += x
50-
jump_dy += y
51-
if not trimmed and\
52-
(
53-
(
54-
trims
55-
and
56-
jump_count == command_count_max
57-
)
58-
or
59-
(
60-
trim_distance is not None
61-
and
62-
(
63-
abs(jump_dy) > trim_distance or abs(jump_dx) > trim_distance
64-
)
65-
)
66-
):
67-
out.trim(position=jump_start)
68-
jump_start += 1 # We inserted a position, start jump has moved.
69-
trimmed = True
70-
if clipping and jump_dx == 0 and jump_dy == 0: # jump displacement is 0, clip trim command.
71-
out.stitches = out.stitches[:jump_start]
7223
continue
7324
if ctrl == 0x01:
7425
out.color_change(0, 0)
75-
trimmed = True
76-
jumping = False
7726
continue
7827
if ctrl == 0x10:
7928
break
8029
break # Uncaught Control
8130
out.end(0, 0)
8231

32+
clipping = True
33+
trims = False
34+
count_max = None
35+
trim_distance = 3.0
36+
if settings is not None:
37+
count_max = settings.get('trim_at', count_max)
38+
trims = settings.get("trims", trims)
39+
trim_distance = settings.get("trim_distance", trim_distance)
40+
clipping = settings.get('clipping', clipping)
41+
if trims and count_max is None:
42+
count_max = 3
43+
if trim_distance is not None:
44+
trim_distance *= 10 # Pixels per mm. Native units are 1/10 mm.
45+
out.interpolate_trims(count_max, trim_distance, clipping)
46+
8347

8448
def read(f, out, settings=None):
8549
jef_threads = get_thread_set()

pyembroidery/PyEmbroidery.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def supported_formats():
107107
"reader": DstReader,
108108
"writer": DstWriter,
109109
"read_options": {
110+
"trim_distance": (None, 3.0, 50.0),
110111
"trim_at": (2, 3, 4, 5, 6, 7, 8),
111112
"clipping": (True, False)
112113
},

0 commit comments

Comments
 (0)