1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #!/usr/bin/env python
-
- import sys
- import os
- from outline_gcode import file_bounding_box, write_outline
- from convert_lasergrbl import convert_gcode
-
- machine_size = ( 208, 287 )
-
- def patch_offset(fi, fo, zp):
- found_off = False
- found_home = False
- found_abs = False
- for line in fi:
- if line.startswith("G92"):
- found_off = True
- elif line.startswith("G28"):
- found_home = True
- elif line.startswith("G90"):
- found_abs = True
-
- fi.seek(0)
-
- sx = "{:.3f}".format(zp[0])
- sy = "{:.3f}".format(zp[1])
-
- if found_off:
- for line in fi:
- if line.startswith("G92"):
- fo.write("G92 X" + sx + " Y" + sy + "\n")
- else:
- fo.write(line)
- elif found_home:
- for line in fi:
- fo.write(line)
- if line.startswith("G28"):
- fo.write("G92 X" + sx + " Y" + sy + "\n")
- elif found_abs:
- for line in fi:
- fo.write(line)
- if line.startswith("G90"):
- fo.write("G92 X" + sx + " Y" + sy + "\n")
- else:
- fo.write("G92 X" + sx + " Y" + sy + "\n")
- for line in fi:
- fo.write(line)
-
- def main():
- if len(sys.argv) < 2:
- print("Usage:")
- print(" " + sys.argv[0] + " input.nc")
- sys.exit(1)
-
- in_file = sys.argv[1]
-
- out_tmp_file = os.path.splitext(in_file)[0] + '_tmp.gcode'
- out_file = os.path.splitext(in_file)[0] + '.gcode'
- outline_tmp_file = os.path.splitext(in_file)[0] + '_bb_tmp.gcode'
- outline_file = os.path.splitext(in_file)[0] + '_bb.gcode'
-
- for f in [ out_tmp_file, out_file, outline_tmp_file, outline_file ]:
- if os.path.exists(f):
- print("File exists: " + f)
- sys.exit(1)
-
- with open(in_file, 'r') as fi, open(out_tmp_file, 'w') as fo:
- convert_gcode(fi, fo)
-
- bb = ( None, None, None, None )
- with open(out_tmp_file, 'r') as f:
- bb = file_bounding_box(f)
-
- x_min, x_max, y_min, y_max = bb
-
- size = ( x_max - x_min, y_max - y_min )
- off = ( -x_min + (machine_size[0] - size[0]) / 2.0, -y_min + (machine_size[1] - size[1]) / 2.0 )
- zero_point = ( -off[0], -off[1] )
-
- with open(out_tmp_file, 'r') as fi, open(out_file, 'w') as fo:
- patch_offset(fi, fo, zero_point)
-
- with open(outline_tmp_file, 'w') as f:
- write_outline(f, bb)
-
- with open(outline_tmp_file, 'r') as fi, open(outline_file, 'w') as fo:
- patch_offset(fi, fo, zero_point)
-
- os.remove(out_tmp_file)
- os.remove(outline_tmp_file)
-
- if __name__ == '__main__':
- main()
|