thr_to_gcode.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from math import pi
  2. class State:
  3. def __init__(self):
  4. self.current_theta = None # detect first move
  5. self.current_rho = 0.0
  6. self.machine_x = 0.0
  7. self.machine_y = 0.0
  8. self.x_steps_per_mm = 320.0
  9. self.y_steps_per_mm = 530.0
  10. self.gear_ratio = 10.0
  11. self.table_type = 'dune_weaver'
  12. self.speed = 200.0
  13. state = State()
  14. def convert_thr_to_gcode(thr_file_path, output_file_path):
  15. with open(thr_file_path, 'r') as f:
  16. lines = f.readlines()
  17. gcode_lines = ["G21 ; Set units to mm", "G90 ; Absolute positioning"]
  18. for line in lines:
  19. stripped = line.strip()
  20. if not stripped or stripped.startswith("#"):
  21. continue
  22. try:
  23. theta, rho = map(float, stripped.split())
  24. except ValueError:
  25. continue
  26. x_scaling_factor = 2
  27. y_scaling_factor = 5
  28. if state.current_theta is None:
  29. state.current_theta = theta
  30. delta_theta = 0
  31. else:
  32. delta_theta = theta - state.current_theta
  33. delta_rho = rho - state.current_rho
  34. x_increment = delta_theta * 100 / (2 * pi * x_scaling_factor)
  35. y_increment = delta_rho * 100 / y_scaling_factor
  36. x_total_steps = state.x_steps_per_mm * (100 / x_scaling_factor)
  37. y_total_steps = state.y_steps_per_mm * (100 / y_scaling_factor)
  38. offset = x_increment * (x_total_steps * x_scaling_factor / (state.gear_ratio * y_total_steps * y_scaling_factor))
  39. y_increment += offset
  40. new_x = state.machine_x + x_increment
  41. new_y = state.machine_y + y_increment
  42. gcode_lines.append(f"G1 X{round(new_x, 5)} Y{round(new_y, 5)} F{state.speed}")
  43. state.machine_x = new_x
  44. state.machine_y = new_y
  45. state.current_theta = theta
  46. state.current_rho = rho
  47. # Add G92 reset to align with final polar position
  48. final_rho_mm = state.current_rho * 100 / y_scaling_factor
  49. gcode_lines.append(f"G92 X0 Y{round(final_rho_mm, 5)}")
  50. with open(output_file_path, 'w') as f:
  51. for line in gcode_lines:
  52. f.write(line + '\n')
  53. print(f"G-code saved to: {output_file_path}")
  54. convert_thr_to_gcode('patterns/clear_from_in_pro.thr', 'patterns/clear_from_in_pro.nc')