r/pythonhelp • u/[deleted] • Nov 07 '21
SOLVED I'm looking to speed up my g-code editor
I am currently writing a program to edit my g-code for my 3D printer I'm wanting the printer to move the print head out of the way of the print at the completion of every layer and then move back to position. The code that I have written works but it's taking a really long time for the code to go through the entire file. I've attached my g-code file with the code.
Any tips on better commenting code are appreciated as well.
file_name = "test_goproholder.gcode" # put your filename here z=0 newline=['\n']
with open(file_name, 'r+') as f:
new_code = "" # where the new modified code will be put
content = f.readlines() # gcode as a list where each element is a line
for line in content:
gcode = line.strip('\n').split(" ") # seperates the gcode from the comments
try: #searches for a Z layer height callout and sets our operator equal to the callout
if 'Z'== gcode[4][0]:
z = gcode[4][1:]
except:
if line.startswith(";LAYER:"): #searches for a layer height callout
insert1=[f'\nG0 X1 Y290 Z{float(z)+10}\n']
insert2=['G4 P5000']
gcode= gcode+insert1+insert2
gcode=gcode+newline
new_code+=''.join([str(element) for element in gcode])
print(new_code)
print(z)
f.seek(0)
f.write(new_code)Here's and example of my test code:
G1X66.43Y39.519E218.93118
F600 E-1
;LAYER_COUNT:56
;LAYER:0
M107
G0 F2625 X36.605 Y37.768 Z0.425
;TYPE:SKIRT
G1 F600 E0
G1 F900 X37.439 Y37.1 E0.03559
G1 X37.875 Y36.806 E0.05311
G1 X39.165 Y36.079 E0.10243
G1 X39.616 Y35.868 E0.11902
G1 X40.498 Y35.536 E0.15041
G1 X40.996 Y35.393 E0.16767
G0 X64.468 Y39.131
G0 X64.477 Y39.032
;TIME_ELAPSED:466.669759
;LAYER:1
M140 S60
M221 S100 T0
M106 S68
;MESH:
G0 F6562.5 X64.477 Y39.032 Z0.675
;TYPE:WALL-INNER
G1 F600 E218.89162
G1 F1800 X65.145 Y39.131 E218.90485
G1 X65.806 Y39.296 E218.9182
G1 X66.43 Y39.519 E218.93118
1
u/CraigAT Nov 08 '21
How cool, I have not used a 3d printer before, so it's the first time I have come across this file format.
A quick Google suggests using G28 for moving the head away:
https://www.simplify3d.com/support/articles/3d-printing-gcode-tutorial/#G28
Is the "try / except" necessary? Would an "if / elif / else" do what you want?
Like u/Goobyalus pointed out, unless it has be the same file, then I would concur that going through the input line by line and outputting your edited lines to a new output file would be a good approach.
1
Nov 09 '21
I'm using the "try/except" to compensate for the fact that when I separate the lines the lists are not all the same size. So when I try to find the Z variable in a line that isn't the same shape as my Z line I get a traceback. The "try/except" allows the program to ignore this expected error.
G28 is a specific command for the printer to home and zero it's axis' which you only want to happen during the beginning of the print. I just am needing the printer head to move out of the way of the print and dwell for 5 seconds.
1
u/CraigAT Nov 09 '21
Re: Try - I understand. As an alternative, when you have the line split into a list, you could use a "for" loop to cycle through the list items and "if' you find one item that starts with a Z then you could use that, otherwise do nothing.
Re: G28 - Okay! Out of curiosity what location are you defining as out of the way?
2
Nov 11 '21
I am homing it to X1 Y290 Z(current height+10) then I'm telling the machine to pause there for 5 seconds.
2
2
u/Goobyalus Nov 08 '21
Strings are immutable in Python so every time you reassign with
new_code += something, you are creating an entirely new, slightly bigger string, and throwing away the old object. I'm guessing this is probably your bottleneck.Maintain a list of lines or objects and then join them at the end, or simply write output as you go instead of constructing a large string in memory.
You can also avoid reading the entire file into memory: