You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

import re
import sys
XADD = 150
YADD = 300
ZDOWN = 62.4
ZUP = ZDOWN + 5
def get_xy(line):
xmatch = re.search(r"X(\d*\.\d*)", line)
ymatch = re.search(r"Y(\d*\.\d*)", line)
if xmatch is None or ymatch is None:
return None, None
xval = float(xmatch.group().strip("X"))
yval = float(ymatch.group().strip("Y"))
return xval, yval
def rewrite_gcode(infile, outfile):
with open(infile) as fp:
lines = fp.read().splitlines()
fp = open(outfile, 'w')
# Home the board after bed is setup
fp.write("G28\n")
for line in lines:
x, y = get_xy(line)
prefix = line[:2]
if x is None or y is None:
if prefix != "M2":
fp.write(f"{line}\n")
continue
postfix = "" if prefix == "G0" else " F1500"
lines = f"{prefix} X{x+XADD} Y{y+YADD}{postfix}\n"
# Raise and lower the pen before moving on fresh lines
if prefix == "G0":
lines = f"G0 Z{ZUP}\n{lines}G0 Z{ZDOWN}\n"
fp.write(lines)
# Raise pen as final control
fp.write(f"G0 Z{ZUP}\n")
fp.close()
if __name__ == '__main__':
rewrite_gcode(sys.argv[1], sys.argv[2])