1+ from pathlib import Path
2+ from pystitch import EmbPattern , CONTINGENCY_TIE_ON_THREE_SMALL , CONTINGENCY_TIE_OFF_THREE_SMALL , CONTINGENCY_LONG_STITCH_SEW_TO
3+ from typing import List , Tuple
4+ from utils import parse_args , UNITS_PER_MM
5+ import math
6+ import pystitch
7+
8+ args = parse_args (default_filename = f'out/{ Path (__file__ ).parent .name } .jef' )
9+
10+ # Create and encode pattern
11+ SIDE_WIDTH = args .side_width * UNITS_PER_MM
12+ CURVE_ORDER = args .curve_order
13+ print (f'Curve order: { CURVE_ORDER } ' )
14+
15+
16+ def build_arrowhead_curve (order : int , segment_length : float ) -> List [Tuple [float , float ]]:
17+ path = [(0.0 , 0.0 )]
18+ angle = 60 if order % 2 == 1 else 0
19+
20+ def expand (symbol : str , depth : int ):
21+ if depth == 0 :
22+ return symbol
23+ result = ''
24+ for char in symbol :
25+ if char == 'A' :
26+ result += 'B-A-B'
27+ elif char == 'B' :
28+ result += 'A+B+A'
29+ else :
30+ result += char
31+ return expand (result , depth - 1 )
32+
33+
34+ instructions = expand ('A' , order )
35+ for cmd in instructions :
36+ if cmd in 'AB' :
37+ x , y = path [- 1 ]
38+ dx = segment_length * math .cos (math .radians (angle ))
39+ dy = segment_length * math .sin (math .radians (angle ))
40+ path .append ((x + dx , y - dy ))
41+ elif cmd == '+' :
42+ angle += 60
43+ elif cmd == '-' :
44+ angle -= 60
45+ return path
46+
47+
48+ segment_length = SIDE_WIDTH / pow (2 , CURVE_ORDER )
49+ print (f'Stitch length: { segment_length / UNITS_PER_MM } ' )
50+ if (segment_length > 6 * UNITS_PER_MM ) or (1 * UNITS_PER_MM > segment_length ):
51+ print (f'Computed stitch length of { segment_length / UNITS_PER_MM } mm is out of recommended range 1-6 mm.' )
52+
53+ pattern = EmbPattern ()
54+ pattern .add_block (build_arrowhead_curve (CURVE_ORDER , segment_length ), "red" )
55+ pattern .move_center_to_origin ()
56+ pystitch .write (pattern , args .output , settings = {
57+ 'max_stitch' : segment_length + 1 , # Forces acceptable stitch length, the value might be outside of range supported by the machine,
58+ 'long_stitch_contingency' : CONTINGENCY_LONG_STITCH_SEW_TO ,
59+ 'tie_on' : CONTINGENCY_TIE_ON_THREE_SMALL , # Tie-on stitches before the pattern
60+ 'tie_off' : CONTINGENCY_TIE_OFF_THREE_SMALL , # Tie-off stitches after the pattern
61+ })
0 commit comments