Toolchange_Probe_MACH3.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from FlatCAMPostProc import *
  2. class Toolchange_Probe_MACH3(FlatCAMPostProc):
  3. coordinate_format = "%.*f"
  4. feedrate_format = '%.*f'
  5. def start_code(self, p):
  6. units = ' ' + str(p['units']).lower()
  7. coords_xy = p['xy_toolchange']
  8. gcode = ''
  9. xmin = '%.*f' % (p.coords_decimals, p['options']['xmin'])
  10. xmax = '%.*f' % (p.coords_decimals, p['options']['xmax'])
  11. ymin = '%.*f' % (p.coords_decimals, p['options']['ymin'])
  12. ymax = '%.*f' % (p.coords_decimals, p['options']['ymax'])
  13. if str(p['options']['type']) == 'Geometry':
  14. gcode += '(TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + ')\n'
  15. gcode += '(Feedrate: ' + str(p['feedrate']) + units + '/min' + ')\n'
  16. if str(p['options']['type']) == 'Geometry':
  17. gcode += '(Feedrate_Z: ' + str(p['z_feedrate']) + units + '/min' + ')\n'
  18. gcode += '(Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + ')\n' + '\n'
  19. gcode += '(Feedrate Probe ' + str(p['feedrate_probe']) + units + '/min' + ')\n' + '\n'
  20. gcode += '(Z_Cut: ' + str(p['z_cut']) + units + ')\n'
  21. if str(p['options']['type']) == 'Geometry':
  22. if p['multidepth'] is True:
  23. gcode += '(DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  24. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + ')\n'
  25. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  26. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  27. if coords_xy is not None:
  28. gcode += '(X,Y Toolchange: ' + "%.4f, %.4f" % (coords_xy[0], coords_xy[1]) + units + ')\n'
  29. else:
  30. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  31. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  32. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  33. gcode += '(Z Probe Depth: ' + str(p['z_pdepth']) + units + ')\n'
  34. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  35. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  36. gcode += '(Postprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n'
  37. else:
  38. gcode += '(Postprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  39. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  40. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  41. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  42. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  43. gcode += 'G90\n'
  44. gcode += 'G17\n'
  45. gcode += 'G94\n'
  46. gcode += '(MSG, WARNING: Make sure you do zero on all axis. ' \
  47. 'For Z axis, since it will be probed, make a rough estimate and do a zero.)\n'
  48. gcode += 'M0'
  49. return gcode
  50. def startz_code(self, p):
  51. return ''
  52. def lift_code(self, p):
  53. return 'G00 Z' + self.coordinate_format%(p.coords_decimals, p.z_move)
  54. def down_code(self, p):
  55. return 'G01 Z' + self.coordinate_format%(p.coords_decimals, p.z_cut)
  56. def toolchange_code(self, p):
  57. z_toolchange = p.z_toolchange
  58. toolchangexy = p.xy_toolchange
  59. f_plunge = p.f_plunge
  60. gcode = ''
  61. if toolchangexy is not None:
  62. x_toolchange = toolchangexy[0]
  63. y_toolchange = toolchangexy[1]
  64. no_drills = 1
  65. if int(p.tool) == 1 and p.startz is not None:
  66. z_toolchange = p.startz
  67. if p.units.upper() == 'MM':
  68. toolC_formatted = format(p.toolC, '.2f')
  69. else:
  70. toolC_formatted = format(p.toolC, '.4f')
  71. if str(p['options']['type']) == 'Excellon':
  72. for i in p['options']['Tools_in_use']:
  73. if i[0] == p.tool:
  74. no_drills = i[2]
  75. if toolchangexy is not None:
  76. gcode = """
  77. M5
  78. T{tool}
  79. M6
  80. G00 Z{z_toolchange}
  81. G00 X{x_toolchange} Y{y_toolchange}
  82. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE ||| Drills for this tool = {t_drills})
  83. M0
  84. F{feedrate_probe}
  85. G31 Z{z_pdepth}
  86. G92 Z0
  87. G00 Z{z_in_between}
  88. F{feedrate_probe_slow}
  89. G31 Z{z_pdepth}
  90. G92 Z0
  91. G00 Z{z_move}
  92. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  93. M0
  94. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  95. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  96. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  97. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  98. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  99. feedrate_probe=str(self.feedrate_format %(p.fr_decimals, p.feedrate_probe)),
  100. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  101. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  102. tool=int(p.tool),
  103. t_drills=no_drills,
  104. toolC=toolC_formatted)
  105. else:
  106. gcode = """
  107. M5
  108. T{tool}
  109. M6
  110. G00 Z{z_toolchange}
  111. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE ||| Drills for this tool = {t_drills})
  112. M0
  113. F{feedrate_probe}
  114. G31 Z{z_pdepth}
  115. G92 Z0
  116. G00 Z{z_in_between}
  117. F{feedrate_probe_slow}
  118. G31 Z{z_pdepth}
  119. G92 Z0
  120. G00 Z{z_move}
  121. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  122. M0
  123. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  124. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  125. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  126. feedrate_probe=str(self.feedrate_format %(p.fr_decimals, p.feedrate_probe)),
  127. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  128. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  129. tool=int(p.tool),
  130. t_drills=no_drills,
  131. toolC=toolC_formatted)
  132. # if f_plunge is True:
  133. # gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  134. return gcode
  135. else:
  136. if toolchangexy is not None:
  137. gcode = """
  138. M5
  139. T{tool}
  140. M6
  141. G00 Z{z_toolchange}
  142. G00 X{x_toolchange} Y{y_toolchange}
  143. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE)
  144. M0
  145. F{feedrate_probe}
  146. G31 Z{z_pdepth}
  147. G92 Z0
  148. G00 Z{z_in_between}
  149. F{feedrate_probe_slow}
  150. G31 Z{z_pdepth}
  151. G92 Z0
  152. G00 Z{z_move}
  153. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  154. M0
  155. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  156. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  157. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  158. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  159. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  160. feedrate_probe=str(self.feedrate_format %(p.fr_decimals, p.feedrate_probe)),
  161. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  162. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  163. tool=int(p.tool),
  164. toolC=toolC_formatted)
  165. else:
  166. gcode = """
  167. M5
  168. T{tool}
  169. M6
  170. G00 Z{z_toolchange}
  171. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE)
  172. M0
  173. F{feedrate_probe}
  174. G31 Z{z_pdepth}
  175. G92 Z0
  176. G00 Z{z_in_between}
  177. F{feedrate_probe_slow}
  178. G31 Z{z_pdepth}
  179. G92 Z0
  180. G00 Z{z_move}
  181. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  182. M0
  183. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  184. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  185. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  186. feedrate_probe=str(self.feedrate_format %(p.fr_decimals, p.feedrate_probe)),
  187. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  188. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  189. tool=int(p.tool),
  190. toolC=toolC_formatted)
  191. # if f_plunge is True:
  192. # gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  193. return gcode
  194. def up_to_zero_code(self, p):
  195. return 'G01 Z0'
  196. def position_code(self, p):
  197. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  198. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  199. def rapid_code(self, p):
  200. return ('G00 ' + self.position_code(p)).format(**p)
  201. def linear_code(self, p):
  202. return ('G01 ' + self.position_code(p)).format(**p)
  203. def end_code(self, p):
  204. coords_xy = p['xy_toolchange']
  205. gcode = ('G00 Z' + self.feedrate_format %(p.fr_decimals, p.z_end) + "\n")
  206. if coords_xy is not None:
  207. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  208. return gcode
  209. def feedrate_code(self, p):
  210. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate))
  211. def z_feedrate_code(self, p):
  212. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.z_feedrate))
  213. def spindle_code(self, p):
  214. if p.spindlespeed:
  215. return 'M03 S' + str(p.spindlespeed)
  216. else:
  217. return 'M03'
  218. def dwell_code(self, p):
  219. if p.dwelltime:
  220. return 'G4 P' + str(p.dwelltime)
  221. def spindle_stop_code(self,p):
  222. return 'M05'