#title: Sheet All-Needle Cast-On (python with knitout module)
# Casts on a sheet by knitting all needles and then stacking front and back stitches to produce a decorative welt.

import knitout
K = knitout.Writer('1 2 3 4 5 6 7 8 9 10');
K.addHeader('Machine', 'SWGN2');

#Parameters:
min = 1 #needle number of left edge
max = 20 #needle number of right edge
carrier = "3" #carrier name

#Bring in carrier:
K.inhook(carrier)
#Set stitch table entry for cast-on:
K.stitchNumber(101)

#On SWGN2 machines, carriers start on the right,
#so will start by tucking all needles right-to-left,
#and will be sure to tuck the leftmost back-bed needle last.

#Need to use quarter-pitch racking to tuck all needles in one pass:
K.rack(-0.75)

for n in range(max, min-1, -1):
	K.tuck("-", 'f' + str(n), carrier)
	K.tuck("-", 'b' + str(n), carrier)

#Return to aligned racking:
K.rack(0)

#Set stitch table entry for knitting:
K.stitchNumber(105)

#Do a rows of plain knitting on the front and back:
for n in range(min, max+1, 1):
	K.knit("+", 'f' + str(n), carrier)
for n in range(max, min-1, -1):
	K.knit("-", 'b' + str(n), carrier)
#One can knit more on the front or back beds here to make a larger "welt" at the edge.

#Stack back-bed stitches with front-bed stitches:
for n in range(max, min-1, -1):
	K.xfer('b' + str(n), 'f' + str(n))

#knit a plain row through the stacked stitches and return carrier to the right edge:
for n in range(min, max+1, 1):
	K.knit("+", 'f' + str(n), carrier)

#send out yarn inserting hook; it is no longer needed to hold the yarn in place:
K.releasehook(carrier)

#...now can knit on [min,max].

K.write('sheet-all-needle-cast-on.swgn2.k')
