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.

convert_lasergrbl.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. import sys
  3. def add_spaces(s):
  4. prev = ' '
  5. r = ""
  6. for c in s:
  7. if str(prev).isdigit() and str(c).isalpha():
  8. r = r + " "
  9. r = r + c
  10. prev = c
  11. return r
  12. def convert_gcode(fi, fo):
  13. power = ""
  14. speed = ""
  15. mode_g0 = True
  16. for line in fi:
  17. if line.startswith("S"):
  18. power = " " + line.rstrip()
  19. elif line.startswith("F"):
  20. speed = " " + line.rstrip()
  21. elif line.startswith("M3 S0"):
  22. fo.write(line)
  23. fo.write("G0 X0 Y0 S0 F1000\n")
  24. fo.write("G1 X1 Y0 S1 F1000\n")
  25. fo.write("G0 X0 Y0 S0 F1000\n")
  26. fo.write("G1 X0 Y1 S1 F1000\n")
  27. fo.write("G0 X0 Y0 S0 F1000\n")
  28. else:
  29. s = add_spaces(line.rstrip())
  30. if line.startswith("G1"):
  31. mode_g0 = False
  32. s += power
  33. s += speed
  34. elif line.startswith("G0"):
  35. mode_g0 = True
  36. s += power
  37. if (power != " S0") and (power != ""):
  38. print("Warning: G0 move with power not zero!" + power)
  39. elif line.startswith("X") or line.startswith("Y"):
  40. if mode_g0:
  41. s = "G0 " + s
  42. s += power
  43. if (power != " S0") and (power != ""):
  44. print("Warning: G0 move with power not zero!" + power)
  45. else:
  46. s = "G1 " + s
  47. s += power
  48. s += speed
  49. s += "\n"
  50. fo.write(s)
  51. def main():
  52. if len(sys.argv) < 3:
  53. print("Usage:")
  54. print(" " + sys.argv[0] + " input.nc output.gcode")
  55. sys.exit(1)
  56. in_file = sys.argv[1]
  57. out_file = sys.argv[2]
  58. with open(in_file, 'r') as fi, open(out_file, 'w') as fo:
  59. convert_gcode(fi, fo)
  60. if __name__ == '__main__':
  61. main()