The Python Script below is used for reversing the toolpaths of the milling tool after the drilling process is complete. This quickly generates the proper toolpaths for the syringe, allowing for smooth transition into the bio-ink extrusion process.
2D Adaptive Converter
# Read the original G-code file
#-----------------------------------------------------
with open(" file name goes here .nc", "r") as f:
#-----------------------------------------------------
gcode = f.read()
# Find all the Z coordinate values in the Gcode
z_values = []
for line in gcode.split("\n"):
if line.startswith("G0") or line.startswith("G1") or line.startswith("Z"):
match = re.search(r"Z([\d.-]+)", line)
if match:
z_values.append(float(match.group(1)))
print(z_values)
# Reverse the order of the z_values array
#z_values.reverse()
# Replace the Z values in the original Gcode with the values from the z_values array
new_gcode = ""
for line in gcode.split("\n"):
if line.startswith("G1") or line.startswith("G0") or line.startswith("Z"):
match = re.search(r"Z([\d.-]+)", line)
if match:
z_value = z_values.pop()
line = re.sub(r"Z([\d.-]+)", f"Z{z_value:.3f}", line)
new_gcode += line + "\n"
# Write the updated Gcode to a new file
#-----------------------------------------------------------------
with open("exported file name goes here .nc", "w") as f:
f.write(new_gcode)
#------------------------------------------------------------------