Repository navigation
Expand file tree
/
Copy pathgcode_offset.py
More file actions
66 lines (57 loc) · 2.29 KB
/
Copy pathgcode_offset.py
File metadata and controls
66 lines (57 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import argparse
def read_gcode(gcode_file):
with open(gcode_file) as f:
lines = f.readlines()
return lines
def delete_offset_layer(gcode_lines):
end_offset_index = gcode_lines.index("M600\n")
gcode_lines[end_offset_index]=";M600\n"
start_offset_index=None
for i,line in enumerate(gcode_lines):
if "; printing object" in line:
start_offset_index=i
break
gcode_lines=gcode_lines[0:start_offset_index]+gcode_lines[end_offset_index:]
new_end_offset_index = gcode_lines.index(";M600\n")
return gcode_lines,new_end_offset_index
def get_pause_lines():
return [';pause\n','G28 X Y\n', 'G1 Z100\n', 'M25\n']
def get_purge_lines(gcode_lines):
begin_purge_line = gcode_lines.index("G80 ; mesh bed leveling\n")+1
end_purge_line = None
for i in range(begin_purge_line, len(gcode_lines),1):
if len(gcode_lines[i].strip()) == 0:
end_purge_line = i
break
purge_lines = gcode_lines[begin_purge_line:end_purge_line]
set_y_pos_line = purge_lines[1].replace("Y-3","Y2")
purge_lines[1] = set_y_pos_line
return purge_lines
def insert_lines(gcode_lines,lines,insert_line):
gcode_lines = gcode_lines[0:insert_line+1]+lines+gcode_lines[insert_line+1:]
end_insert_line = insert_line+len(lines)
return gcode_lines,end_insert_line
def write_modified_file(path,gcode_lines):
path = path.replace(".gcode", "_edited.gcode")
with open(path, 'w') as f:
for line in gcode_lines:
f.write("%s\n" % line)
def parse_args():
parser = argparse.ArgumentParser()
# Required positional argument
parser.add_argument("gcode", help="gcode to edit")
args = parser.parse_args()
return args
def main():
args = parse_args()
path = args.gcode
gcode_lines = read_gcode(path)
purge_lines = get_purge_lines(gcode_lines)
pause_lines = get_pause_lines()
gcode_lines,end_offset_index = delete_offset_layer(gcode_lines)
gcode_lines,end_pause_line = insert_lines(gcode_lines,pause_lines,end_offset_index)
gcode_lines,end_purge_line = insert_lines(gcode_lines,purge_lines,end_pause_line)
gcode_lines,end_upline = insert_lines(gcode_lines,['G1 Z.5 F720\n'],end_purge_line)
write_modified_file(path,gcode_lines)
if __name__ == "__main__":
main()