-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathknitout.py
executable file
·418 lines (346 loc) · 13.2 KB
/
knitout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env python3
# python3
# see example() for usage
import re
validHeaders = ['Carriers', 'Machine', 'Position', 'Yarn', 'Gauge']
############### helpers ######################################################
reg = re.compile("([a-zA-Z]+)([\+\-]?[0-9]+)")
def shiftCarrierSet(args, carriers):
if len(args) == 0:
raise AssertionError("No carriers specified")
args = args[0].split()
for c in args:
if not str(c) in carriers:
raise ValueError("Carrier not specified in initial set", c)
cs = ' '.join(str(c) for c in args)
return cs
def shiftBedNeedle(args):
if len(args) == 0:
raise AssertionError("No needles specified")
bn = args.pop(0)
if not (type(bn) == str or type(bn) == list or type(bn) == tuple):
raise AssertionError("Invalid BedNeedle type")
bed = None
needle = None
if type(bn) == str:
m = reg.match(bn)
if not m and (bn == 'f' or bn == 'b' or bn == 'fs' or bn == 'bs'):
bed = bn
if not len(args):
raise ValueError("Needle not specified")
if isinstance(args[0],int) or args[0].isdgit():
needle = int(args.pop(0))
else:
if (m.group(1) == 'f' or m.group(1) == 'b' or m.group(1) == 'fs' or m.group(1) == 'bs'):
bed = m.group(1)
else:
raise ValueError("Invalid bed type. Must be 'f' 'b' 'fs' 'bs'.")
if m.group(2):
needle = m.group(2)
else:
raise ValueError("Invalid needle. Must be numeric.", m.group(2))
else:
if len(bn) != 2:
raise ValueError("Bed and Needle need to be supplied.")
if (bn[0] == 'f' or bn[0] == 'b' or bn[0] == 'fs' or bn[0] == 'bs'):
bed = bn[0]
else:
raise ValueError("Invalid bed type. Must be 'f' 'b' 'fs' 'bs'.")
if type(bn[1]) == int or bn[1].isdigit():
needle = int(bn[1])
else:
raise ValueError("2.Invalid needle. Must be numeric.")
return bed + str(needle)
def shiftDirection(args):
if len(args) == 0:
raise AssertionError("No direction specified")
direction = args.pop(0)
if direction != '+' and direction != '-':
raise ValueError("Invalid direction: " + direction)
return direction
##############################################################################
class Writer:
def __init__(self, cs):
# array of carrier names, front-to-back
self.carriers = list()
# array of operations, strings
self.operations = list()
# array of headers, strings
self.headers = list()
self.carriers = cs.split()
self.addHeader('Carriers', cs);
def addHeader(self, name, value):
if not name in validHeaders:
raise ValueError("Unknown header, must be " + ' '.join(validHeaders) + "; " + name)
self.headers.append(';;' + name + ': ' + value)
def addRawOperation(self, op):
self.operations.append(op)
def ingripper(self, *args):
argl = list(args)
self.operations.append('in ' + shiftCarrierSet(argl, self.carriers))
def inhook(self, *args):
argl = list(args)
self.operations.append('inhook ' + shiftCarrierSet(argl, self.carriers))
def incarrier(self, *args): #NOTE: can't name func `in` since that is a keyword in python
argl = list(args)
self.operations.append('in ' + shiftCarrierSet(argl, self.carriers))
def outgripper(self, *args):
argl = list(args)
self.operations.append('out ' + shiftCarrierSet(argl, self.carriers))
def outhook(self, *args):
argl = list(args)
self.operations.append('outhook ' + shiftCarrierSet(argl, self.carriers))
def outcarrier(self, *args):
argl = list(args)
self.operations.append('out ' + shiftCarrierSet(argl, self.carriers))
def releasehook(self, *args):
argl = list(args)
self.operations.append('releasehook ' + shiftCarrierSet(argl, self.carriers))
def rack(self, r):
if not (type(r) == int or type(r) == float or (type(r) == str and r.isdigit())):
raise ValueError("Rack is not an integer or fraction")
#TODO only certain values make sense
self.operations.append('rack ' + str(r))
def knit(self, *args):
argl = list(args)
direction = shiftDirection(argl)
bn = shiftBedNeedle(argl)
cs = shiftCarrierSet(argl, self.carriers)
self.operations.append('knit ' + direction + ' ' + bn + ' ' + cs)
def tuck(self, *args):
argl = list(args)
direction = shiftDirection(argl)
bn = shiftBedNeedle(argl)
cs = shiftCarrierSet(argl, self.carriers)
self.operations.append('tuck ' + direction + ' ' + bn + ' ' + cs)
def xfer(self, *args):
argl = list(args)
bn_from = shiftBedNeedle(argl)
bn_to = shiftBedNeedle(argl)
self.operations.append('xfer ' + bn_from + ' ' + bn_to)
def split(self, *args):
argl = list(args)
direction = shiftDirection(argl)
bn_from = shiftBedNeedle(argl)
bn_to = shiftBedNeedle(argl)
cs = shiftCarrierSet(argl, self.carriers)
self.operations.append('split '+ direction + ' ' + bn_from + ' ' + bn_to + ' ' + cs)
def miss(self, *args):
argl = list(args)
direction = shiftDirection(argl)
bn = shiftBedNeedle(argl)
cs = shiftCarrierSet(argl, self.carriers)
self.operations.append('miss ' + direction + ' ' + bn + ' ' + cs)
def drop(self, *args):
argl = list(args)
bn = shiftBedNeedle(argl)
self.operations.append('drop ' + bn)
def amiss(self, *args):
argl = list(args)
bn = shiftBedNeedle(argl)
self.operations.append('amiss ' + bn)
def pause(self, message=''):
self.operations.append(f'pause {message}')
def comment(self, commentString):
if type(commentString) != str:
raise ValueError('comment has to be string')
self.operations.append(';' + commentString)
def kcodecomment(self, commentString):
if type(commentString) != str:
raise ValueError('comment has to be string')
self.operations.append(';kniterate ' + commentString)
#Extensions:
def stitchNumber(self, val):
self.operations.append('x-stitch-number ' + str(val))
def speedNumber(self, val):
self.operations.append('x-speed-number ' + str(val))
def subRollerNumber(self, val):
self.operations.append('x-sub-roller-number ' + str(val))
def fabricPresser(self, mode):
if not (mode == 'auto' or mode == 'on' or mode == 'off'):
raise ValueError("Mode must be one of 'auto','on','off' : "+ str(mode))
self.operations.append('x-presser-mode ' + mode)
#for kniterate:
def rollerAdvance(self, val):
self.operations.append('x-roller-advance ' + str(val))
def addRollerAdvance(self,val):
self.operations.append('x-add-roller-advance ' + str(val))
def stoppingDistance(self,val):
self.operations.append('x-carrier-stopping-distance ' + str(val))
def xferStitchNumber(self, val):
self.operations.append('x-xfer-stitch-number ' + str(val))
def clear(self):
#clear buffers
self.headers = list()
self.operations = list()
def write(self, filename):
version = ';!knitout-2\n'
content = version + '\n'.join(self.headers) + '\n' + '\n'.join(self.operations)
try:
with open(filename, "w") as out:
print(content, file=out)
print('wrote file ' + filename)
except IOError as error:
print('Could not write to file ' + filename)
def example():
stockinette_rectangle()
garter_rectangle()
rib_rectangle()
cable_rectangle()
stockinette_band()
def stockinette_rectangle(width=10, height=20):
writer = Writer('1 2 3 4')
writer.addHeader('Machine', 'swg')
carrier = '1'
writer.inhook(carrier)
for i in range(width-1, 0, -2):
writer.tuck('-', ('f',i), carrier)
writer.releasehook(carrier)
for i in range(0, width, 2):
writer.tuck('+', ('f', i), carrier)
for j in range(0, height):
if j%2 == 0:
for i in range(width, 0,-1):
writer.knit('-', ('f', i-1), carrier)
else:
for i in range(0, width):
writer.knit('+', ('f', i), carrier)
writer.outhook(carrier)
for i in range(0, width):
writer.drop(('f', i))
writer.write('stockinette-'+str(width)+'x'+str(height)+'.k')
def garter_rectangle(width=10, height=20):
writer = Writer('1 2 3 4')
writer.addHeader('Machine', 'swg')
carrier = '1'
writer.inhook(carrier)
for i in range(width-1, 0, -2):
writer.tuck('-', ('f',i), carrier)
writer.releasehook(carrier)
for i in range(0, width, 2):
writer.tuck('+', ('f', i), carrier)
for j in range(0, height):
if j%2 == 0:
for i in range(width, 0,-1):
writer.knit('-', ('f', i-1), carrier)
else:
for i in range(0, width):
writer.xfer(('f',i), ('b',i))
for i in range(0, width):
writer.knit('+', ('b', i), carrier)
for i in range(0, width):
writer.xfer(('b',i), ('f',i))
writer.outhook(carrier)
for i in range(0, width):
writer.drop(('f', i))
writer.write('garter-'+str(width)+'x'+str(height)+'.k')
def rib_rectangle(width=10, height=20):
writer = Writer('1 2 3 4')
writer.addHeader('Machine', 'swg')
carrier = '1'
writer.inhook(carrier)
for i in range(width-1, 0, -2):
bed = 'f'
if i%2:
bed = 'b'
writer.tuck('-', (bed,i), carrier)
writer.releasehook(carrier)
for i in range(0, width, 2):
bed = 'f'
if i%2:
bed = 'b'
writer.tuck('+', (bed, i), carrier)
for j in range(0, height):
if j%2 == 0:
for i in range(width, 0,-1):
bed = 'f'
if (i-1)%2:
bed = 'b'
writer.knit('-', (bed, i-1), carrier)
else:
for i in range(0, width):
bed = 'f'
if i%2:
bed = 'b'
writer.knit('+', (bed, i), carrier)
writer.outhook(carrier)
for i in range(0, width):
writer.drop(('f', i))
writer.drop(('b', i))
writer.write('rib-'+str(width)+'x'+str(height)+'.k')
#to test xfers, rack:
def cable_rectangle(width=30, height=40):
writer = Writer('1 2 3 4')
writer.addHeader('Machine', 'swg')
carrier = '1'
mid = int(width/2)
writer.inhook(carrier)
for i in range(width-1, 0, -2):
writer.tuck('-', ('f',i), carrier)
writer.releasehook(carrier)
for i in range(0, width, 2):
writer.tuck('+', ('f', i), carrier)
for j in range(0, height):
if j%2 == 0:
for i in range(width, 0,-1):
writer.knit('-', ('f', i-1), carrier)
else:
for i in range(0, width):
writer.knit('+', ('f', i), carrier)
if j > 2 and j < height-2 and j%4==0: #every 4th row
writer.comment("cable op at row " + str(j))
writer.xfer('f', mid-2, 'b', mid-2)
writer.xfer('f', mid-1, 'b', mid-1)
writer.xfer('f', mid, 'b', mid)
writer.xfer('f', mid+1, 'b', mid+1)
writer.rack(2)
writer.xfer('b',mid-2, 'f', mid)
writer.xfer('b',mid-1, 'f', mid+1)
writer.rack(-2)
writer.xfer('b',mid, 'f', mid-2)
writer.xfer('b',mid+1, 'f', mid-1)
writer.rack(0)
writer.outhook(carrier)
for i in range(0, width):
writer.drop(('f', i))
writer.write('cable-'+str(width)+'x'+str(height)+'.k')
#to test split
def stockinette_band(width=20, height=40):
writer = Writer('1 2 3 4')
writer.addHeader('Machine', 'swg')
carrier = '1'
writer.inhook(carrier)
for i in range(width-1, 0, -2):
writer.tuck('-', ('f',i), carrier)
writer.releasehook(carrier)
for i in range(0, width, 2):
writer.tuck('+', ('f', i), carrier)
for j in range(0, height):
if j%2 == 0:
for i in range(width, 0,-1):
writer.knit('-', ('f', i-1), carrier)
else:
if j == 1:
for i in range(0, width):
writer.split('+','f',i,'b',i, carrier)
else:
for i in range(0, width):
writer.knit('+', ('f', i), carrier)
for i in range(0, width):
writer.xfer('b',i,'f',i)
if height%2 == 0:
for i in range(width, 0,-1):
writer.knit('-', ('f', i-1), carrier)
for i in range(0, width):
writer.knit('+', ('f', i), carrier)
else:
for i in range(0, width):
writer.knit('+', ('f', i), carrier)
for i in range(width, 0,-1):
writer.knit('-', ('f', i-1), carrier)
#todo bindoff
writer.outhook(carrier)
for i in range(0, width):
writer.drop(('f', i))
writer.write('band-'+str(width)+'x'+str(height)+'.k')
#todo tubes, castons, bindoffs