Skip to main content
. 2022 Dec 26;15(1):96. doi: 10.3390/polym15010096
„„„
x0,y0
_________________
________________| ^
|________________ |
________________| | L
|________________ |
________________|v
<----------------------->
L
„„„
# Import numpy library (numerical computation) and matpotlib (generate plots from data in lists and vectors)
import numpy as np
import matplotlib.pyplot as plt

# Definition of variables
# independent variables
L = 30 # scaffold length (mm)
D = 1.75 # filament diameter (mm)
d = 0.400 # extruder nozzle diameter (mm)
dist1= 1 # distance between lines of the first layer (mm)
dist2 = 2 # line spacing of second layer (mm)
nz = 4 # number of layers
ts = 1200 # traverse speed (mm/min)
fr = 800 # printing speed (mm/min)
flow = 1 # extrusion multiplier
E_extruded = 0 # initialization of extruded length (mm)
h = 0.2 # layer height (mm)

# Dependent variables
n1 = int(L/dist1) # number of turns per horizontal layer
n2 = int(L/dist2) # number of turns per vertical layer
w = 2*h # extrusion width
A= (w-h)*h + pi*((h/2)**2) # cross section of extruded filament

# EXTRUSION FUNCTION
# extrusion volume calculation for a given length (L)
def extrusion(L):
ext = L*(A*4)/(pi*(D**2)) #source: Slic3r
return ext

# FIRST LAYER
# assign vector (x, y, z), initial position
P0 = [100-L/2, 100-L/2, h]
xpos1 = [] #1D array—storage of x values for g-code
ypos1 = [] #1D array—storage of x values for the g-code
extr_l = [] #1D array—storage of the extrusion values for the g-code

# GENERATION OF XY POSITIONS
ypos1.append(P0[1]) # initial position Y
for j in range (0,int(n1/2)): # generate positions in X for n1 laps
xpos1.append(P0[0])
xpos1.append(P0[0] + L)
xpos1.append(P0[0] + L)
xpos1.append(P0[0])

for k in range (1,2*n1-1): #generate Y positions for n1 turns
if (k % 2) == 0:
ypos1.append(ypos1[-1]) # Y position (k= even number).
else:
ypos1.append(ypos1[-2] + dist1) # Y position (k= odd number).
# G-Code generation and text file creation
# Write a .txt file with the values obtained and the G-code format for each position:
file = open(“gcode.txt”, “w+”)
extr_l.append(0)
print(‘G0 F{:.0f} X{:.3f} Y{:.3f} Z{:.3f}’.format(ts, P0[0], P0[1], P0[2]))

for o in range(1,len(xpos1)):
extr_l.append(np.abs((xpos1[o]-xpos1[o-1]) + (ypos1[o]-ypos1[o-1])))
E_extruded = E_extruded + extrusion(extr_l[o])*flow
print(‘G1 F{:.0f} X{:.3f} Y{:.3f} E{:.3f}’.format(fr, xpos1[o], ypos1[o], E_extruded))
file.write(‘G1 F{:.0f} X{:.3f} Y{:.3f} E{:.3f} E{:.3f}’.format(fr, xpos1[o], ypos1[o], E_extruded))

# Save and close file
file.close

# Generate XY positions for a scaffold with ‘nz’ layers
for i in range (1,nz):
# Generate XY positions for even layers.
if (i % 2) = = 0:
P2 = [100-L/2, 100-L/2, (i + 1)*h]
xpos2 = []
ypos2 = []
ypos2.append(P2[1])
ypos2.append(P2[1])

for j in range (0,int(n1/2)):
xpos2.append(P2[0])
xpos2.append(P2[0] + L)
xpos2.append(P2[0] + L)
xpos2.append(P2[0])

for k in range (1,2*n1-1):
if (k % 2) == 0:
ypos2.append(ypos2[-1])
else:
ypos2.append(ypos2[-2] + dist1)

file = open(„gcode.txt”,”w+”) # ‘w’ for write, the ‘ + ’ to create the file if it does not exists
extr_l.append(0)
print(‘G0 F{:.0f} X{:.3f} Y{:.3f} Z{:.3f}’.format(ts, P2[0], P2[1], P2[2]))

for o in range(1,len(xpos2)):
extr_l.append(np.abs((xpos2[o]-xpos2[o-1]) + (ypos2[o]-ypos2[o-1])))
E_extruded = E_extruded + extrusion(extr_l[o])*flow2
print(‘G1 F{:.0f} X{:.3f} Y{:.3f} E{:.3f}’.format(fr, xpos2[o], ypos2[o], E_extruded))
file.write(‘G1 F{:.0f} X{:.3f} Y{:.3f} E{:.3f}\n’.format(fr, xpos2[o], ypos2[o], E_extruded))
file.close

# Generate XY positions for odd layers
else:
P3 = [100-L/2, 100-L/2, (i + 1)*h] #100-L/2.5
xpos3 = []
ypos3 = []
xpos3.append(P3[0])

for j in range (0,2*n2-1)
if (j % 2) == 0:
xpos3.append(xpos3[-1])
else:
xpos3.append(xpos3[-2] + dist2)

for k in range (0,int(n2/2)):
ypos3.append(P3[1])
ypos3.append(P3[1] + L)
ypos3.append(P3[1] + L)
ypos3.append(P3[1])

file = open(„gcode.txt”,”w+”) # ‘w’ for write, the ‘+’ to create the file if it does not exists
extr_l.append(0)
print(‘G0 F{:.0f} X{:.3f} Y{:.3f} Z{:.3f}’.format(ts, P3[0], P3[1], P3[2])

for o in range(1,len(xpos3)):
extr_l.append(np.abs((xpos3[o]-xpos3[o-1]) + (ypos3[o]-ypos3[o-1])))
E_extruded = E_extruded + extrusion(extr_l[o])*flow2
print(‘G1 F{:.0f} X{:.3f} Y{:.3f} E{:.3f}’.format(fr, xpos3[o], ypos3[o], E_extruded))
file.write(‘G1 F{:.0f} X{:.3f} Y{:.3f} E{:.3f}\n’.format(fr, xpos3[o], ypos3[o], E_extruded))
file.close

# GENERATE PLOT
ax = plt.figure().gca()
plt.axis(‘square’)
plt.ylim(80, 120)
plt.xlim(80, 120)
#plot the final g-code values (green, red, blue))
ax.plot(xpos3,ypos3,’g’)
ax.plot(xpos1,ypos1,’b’)
plt.show()