G-code Generation for Creating Shape

by AnzuK1 in Craft > Cards

1194 Views, 2 Favorites, 0 Comments

G-code Generation for Creating Shape

Introduction.png

In this project, I create a 3D printed object with GCode generated in Grasshopper.

Test Output

3Output.png
2Test.png
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 = v
lines = rs.PolylineVertices(lines)
#move to start position
point0 = 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 position
distance = rs.Distance(lines[i],lines[i+1])
#calculate the amount of filament to extrude for distance
E = distance*extrudeRate
file.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
# vertices
vertices = []
poly = []
#vertices.append( ( 50, 50, 0 ) )

layerHeight = .2
layers = 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 = poly

a = 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
4_1output.png

I tried to get the pattern from GCode. Therefore, this time, I put the coordinated information like this.

# draw square
# vertices
OriginXY = Origin

vertices = []
poly = []

layerHeight = .2
layers = int(height/layerHeight)

detX = Horizon/Dense
detY = Vertical/Dense

for i in range (0,layers):
#draw rectangle
new_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 = poly

a = 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

This is the output pattern.