No Description
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.

gcode_center_outline.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. from outline_gcode import file_bounding_box, write_outline
  5. from convert_lasergrbl import convert_gcode
  6. machine_size = ( 208, 287 )
  7. def patch_offset(fi, fo, zp):
  8. found_off = False
  9. found_home = False
  10. found_abs = False
  11. for line in fi:
  12. if line.startswith("G92"):
  13. found_off = True
  14. elif line.startswith("G28"):
  15. found_home = True
  16. elif line.startswith("G90"):
  17. found_abs = True
  18. fi.seek(0)
  19. sx = "{:.3f}".format(zp[0])
  20. sy = "{:.3f}".format(zp[1])
  21. if found_off:
  22. for line in fi:
  23. if line.startswith("G92"):
  24. fo.write("G92 X" + sx + " Y" + sy + "\n")
  25. else:
  26. fo.write(line)
  27. elif found_home:
  28. for line in fi:
  29. fo.write(line)
  30. if line.startswith("G28"):
  31. fo.write("G92 X" + sx + " Y" + sy + "\n")
  32. elif found_abs:
  33. for line in fi:
  34. fo.write(line)
  35. if line.startswith("G90"):
  36. fo.write("G92 X" + sx + " Y" + sy + "\n")
  37. else:
  38. fo.write("G92 X" + sx + " Y" + sy + "\n")
  39. for line in fi:
  40. fo.write(line)
  41. def main():
  42. if len(sys.argv) < 2:
  43. print("Usage:")
  44. print(" " + sys.argv[0] + " input.nc")
  45. sys.exit(1)
  46. in_file = sys.argv[1]
  47. out_tmp_file = os.path.splitext(in_file)[0] + '_tmp.gcode'
  48. out_file = os.path.splitext(in_file)[0] + '.gcode'
  49. outline_tmp_file = os.path.splitext(in_file)[0] + '_bb_tmp.gcode'
  50. outline_file = os.path.splitext(in_file)[0] + '_bb.gcode'
  51. for f in [ out_tmp_file, out_file, outline_tmp_file, outline_file ]:
  52. if os.path.exists(f):
  53. print("File exists: " + f)
  54. sys.exit(1)
  55. with open(in_file, 'r') as fi, open(out_tmp_file, 'w') as fo:
  56. convert_gcode(fi, fo)
  57. bb = ( None, None, None, None )
  58. with open(out_tmp_file, 'r') as f:
  59. bb = file_bounding_box(f)
  60. x_min, x_max, y_min, y_max = bb
  61. size = ( x_max - x_min, y_max - y_min )
  62. off = ( -x_min + (machine_size[0] - size[0]) / 2.0, -y_min + (machine_size[1] - size[1]) / 2.0 )
  63. zero_point = ( -off[0], -off[1] )
  64. with open(out_tmp_file, 'r') as fi, open(out_file, 'w') as fo:
  65. patch_offset(fi, fo, zero_point)
  66. with open(outline_tmp_file, 'w') as f:
  67. write_outline(f, bb)
  68. with open(outline_tmp_file, 'r') as fi, open(outline_file, 'w') as fo:
  69. patch_offset(fi, fo, zero_point)
  70. os.remove(out_tmp_file)
  71. os.remove(outline_tmp_file)
  72. if __name__ == '__main__':
  73. main()