Python Scripted GCode: The Basics

This tutorial series will show off ways of making your own GCode files directly with python scripting. In a lot of applications, it can be more efficient to work directly on GCode than to build a 3D model. Further, directly modeling a shape can be less fiddly than skeinforge, particularly ones with single walls.
This tutorial will explain the file para_01.py found in the GCode Starter Kit. If you’ve never programmed before, you might want to study a few tutorials on the Python language, but this program is simple enough that if you’ve got the basics down, it shouldn’t be too hard to deal with this one.
The first few lines in this file are a “class definition” for an object called G1Code. In the case of this file, this is just a handy way of storing the variables X, Y, Z, and F (F is feedrate) and a good place to put the function that makes a text GCode out of those variables:
#G1 Code Object
class G1Code:
def __init__(self, X=0, Y=0, Z=0, F=0):
self.X =X
self.Y =Y
self.Z = Z
self.F = F
def __str__(self):
string = "G1 X" + str(self.X) + " Y" + str(self.Y) + " Z" + str(self.Z) + " F" + str(self.F)
return string
With the above definitions in place, we can now access the coordinates in an object of type G1Code by adding a .X, .Y, .Z, or .F to the end of its name. We can also print a gcode just by calling str(G1Code). Next we’ve got some variable declarations:
filename = "test.gcode"
Zsteps = 0
ThisGCode = G1Code(X=0, Y=0, Z=0, F=1000)
This program only has one G1Code object– we just change its value repeatedly and write it out to the file after each change. Next, we’ll take that file name, open a text file, and write out headers. Thes GCode and MCode references explain how these settings tell the printer to operate in absolute coordinates, milimeters, and set up the extruder.
FILE = open(filename,"w")
FILE.writelines("G21\n")
FILE.writelines("G90\n")
FILE.writelines("M103\n")
FILE.writelines("M105\n")
FILE.writelines("M104 S220.0\n")
FILE.writelines("M101\n")
Now that we’ve got a header, it’s just a matter of using a loop in the program to lay down each layer of the shape. In the case of this program each layer is the simplest shape possible: a triangle.
for Zsteps in range(30):
#Hop up to the next level, staying put
ThisGCode.Z = 0.4 * Zsteps
FILE.writelines(str(ThisGCode)+ "\n")
#Trace out the prism
ThisGCode.X = 0
ThisGCode.Y = 0
FILE.writelines(str(ThisGCode)+ "\n")
ThisGCode.X = 0
ThisGCode.Y = -10
FILE.writelines(str(ThisGCode)+ "\n")
ThisGCode.X = -10
ThisGCode.Y = 0
FILE.writelines(str(ThisGCode)+ "\n")
(WordPress isn’t being friendly to my tabs, but they’re in the original file, and you do need them, because Python uses tabs instead of brackets to indicate loops and subroutines.)
Note that inside the loop there are four print commands. The first one contains the value of the last GCode on the previous layer (or just zero if there is no previous layer) plus a slight vertical shift. The other three move the print head first to the origin, then back ten milimeters, then diagonally down to a position ten milimeters left of the origin. From that position, the next pass of the loop will move the print head up again, then around the triangle.
The print generated by this code isn’t very tall, or very large, or very tall, so you won’t waste a lot of plastic on messing around with the settings and trying new things. Leave any questions in comments, and I’ll use that to gague whether the next one of these needs more or less of anything.

D.K. Said,
March 31, 2011 @ 6:08 am
How I can better consider the mill geometry with python scripting?
Please prompt me any good solutions.
Thanks.