G-code Generation for Creating Shape
In this project, I create a 3D printed object with GCode generated in Grasshopper.
Test Output
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
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
This is the output pattern.