import re import sys XADD = 150 YADD = 300 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" fp.write(f"{prefix} X{x+XADD} Y{y+YADD}{postfix}\n") fp.close() if __name__ == '__main__': rewrite_gcode(sys.argv[1], sys.argv[2])