G-code Generation for Creating Shape
![Introduction.png](/proxy/?url=https://content.instructables.com/FR6/UWUL/KO01G866/FR6UWULKO01G866.png&filename=Introduction.png)
In this project, I create a 3D printed object with GCode generated in Grasshopper.
Test Output
![3Output.png](/proxy/?url=https://content.instructables.com/FAL/7DKO/KO01G869/FAL7DKOKO01G869.png&filename=3Output.png)
![2Test.png](/proxy/?url=https://content.instructables.com/F66/M7W8/KO01G867/F66M7W8KO01G867.png&filename=2Test.png)
![2_1testout.png](/proxy/?url=https://content.instructables.com/FZS/PAA9/KO01G868/FZSPAA9KO01G868.png&filename=2_1testout.png)
First, to output the GCode from Grasshopper, using the example code (GCodeTurtle.gh)
In the sample code, the Turtle3D.py is used to draw the lines in the creating object.
Therefore, I removed the part of the Turtle.py to make the object deciding the 3D point object.
In the code, I modified it like this.
def writeTurtleGCode(file,v,extrudeRate):lines = vlines = rs.PolylineVertices(lines)#move to start positionpoint0 = lines[0]file.append(" ; ############### begin shape ############## \n""G1 F300 Z2.0 ; Move Z Axis up 2mm \n" +"G1 F1000 X" +str('%.2f' % point0.X) + " Y" + str('%.2f' % point0.Y) + " ; Move to starting location \n" +"G1 F300 Z" +str('%.2f' % point0.Z) +" \n" +"G1 F300 E3 ; Extrude to get ready\n")for i in range(0,len(vertices)-2):#calculate the distance between current and next positiondistance = rs.Distance(lines[i],lines[i+1])#calculate the amount of filament to extrude for distanceE = distance*extrudeRatefile.append("G1 F1000 X" + str('%.3f' % lines[i+1].X) + " Y" + str('%.3f' % lines[i+1].Y) + " Z" + str('%.3f' % lines[i+1].Z) + " E" + str('%.3f' % E) + "\n")file.append(" ; ############### end shape ############## \n")
This is the main part.
# draw square# verticesvertices = []poly = []#vertices.append( ( 50, 50, 0 ) )layerHeight = .2layers = int(height/layerHeight)for i in range (0,layers):new_position = rs.CreatePoint(50, 50, layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(50, 100, layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(100, 100, layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(100, 50, layerHeight*i)vertices.append(new_position)poly.append(rs.AddPolyline(vertices))pt = polya = vertices
Vertices array contains the position from creating point and it is converted to a polyline.
According to the polyline, this code generates the G-Code strings.
In this time, as test code, I put the rectangle position so, I got rectangle output.
Generate Pattern With GCode From Grasshopper
![4.png](/proxy/?url=https://content.instructables.com/FKA/0KJZ/KO01G86A/FKA0KJZKO01G86A.png&filename=4.png)
![4_1output.png](/proxy/?url=https://content.instructables.com/F7P/NFRP/KO01G86B/F7PNFRPKO01G86B.png&filename=4_1output.png)
I tried to get the pattern from GCode. Therefore, this time, I put the coordinated information like this.
# draw square# verticesOriginXY = Originvertices = []poly = []layerHeight = .2layers = int(height/layerHeight)detX = Horizon/DensedetY = Vertical/Densefor i in range (0,layers):#draw rectanglenew_position = rs.CreatePoint(OriginXY, OriginXY, layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(OriginXY, OriginXY + detY * (int(Dense)-1), layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(OriginXY + detX * (int(Dense)-1), OriginXY + detY * (int(Dense)-1), layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(OriginXY + detX * (int(Dense)-1), OriginXY, layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(OriginXY, OriginXY, layerHeight*i)vertices.append(new_position)for j in range (0,int(Dense)-1):new_position = rs.CreatePoint(OriginXY + detX * (int(Dense)-1), OriginXY + detY * j, layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(OriginXY, OriginXY + detY * (j+1), layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(OriginXY + detX * (int(Dense)-1), OriginXY + detY * (int(Dense)-1), layerHeight*i)vertices.append(new_position)for k in range (0,int(Dense)-1):m = int(Dense) - (1 + k)new_position = rs.CreatePoint(OriginXY + detX * m, OriginXY, layerHeight*i)vertices.append(new_position)new_position = rs.CreatePoint(OriginXY + detX * (m-1), OriginXY + detY * (int(Dense)-1), layerHeight*i)vertices.append(new_position)poly.append(rs.AddPolyline(vertices))pt = polya = vertices
Horizon and Vertical are the horizontal sizes and vertical sizes, accordingly.
Dence decides the density of the pattern. We can change parameters of these.
Done
![5Done.png](/proxy/?url=https://content.instructables.com/FRL/EC1B/KO01G86C/FRLEC1BKO01G86C.png&filename=5Done.png)
This is the output pattern.