Toolchange_Probe_MACH3.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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['toolchange_xy']
  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['feedrate_z']) + 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['depthpercut']) + units + ' <=>' + \
  24. str(math.ceil(abs(p['z_cut']) / p['depthpercut'])) + ' passes' + ')\n'
  25. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  26. gcode += '(Z Toolchange: ' + str(p['toolchangez']) + 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['endz']) + 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. return gcode
  47. def startz_code(self, p):
  48. return ''
  49. def lift_code(self, p):
  50. return 'G00 Z' + self.coordinate_format%(p.coords_decimals, p.z_move)
  51. def down_code(self, p):
  52. return 'G01 Z' + self.coordinate_format%(p.coords_decimals, p.z_cut)
  53. def toolchange_code(self, p):
  54. toolchangez = p.toolchangez
  55. toolchangexy = p.toolchange_xy
  56. f_plunge = p.f_plunge
  57. gcode = ''
  58. if toolchangexy is not None:
  59. toolchangex = toolchangexy[0]
  60. toolchangey = toolchangexy[1]
  61. no_drills = 1
  62. if int(p.tool) == 1 and p.startz is not None:
  63. toolchangez = p.startz
  64. if p.units.upper() == 'MM':
  65. toolC_formatted = format(p.toolC, '.2f')
  66. else:
  67. toolC_formatted = format(p.toolC, '.4f')
  68. if str(p['options']['type']) == 'Excellon':
  69. for i in p['options']['Tools_in_use']:
  70. if i[0] == p.tool:
  71. no_drills = i[2]
  72. if toolchangexy is not None:
  73. gcode = """
  74. T{tool}
  75. M5
  76. M6
  77. G00 Z{toolchangez}
  78. G00 X{toolchangex} Y{toolchangey}
  79. (MSG, Change to Tool Dia = {toolC} ||| Drills for this tool = {t_drills} ||| Tool Probing MACH3)
  80. M0
  81. G00 Z{z_move}
  82. F{feedrate_probe}
  83. G31 Z{z_pdepth}
  84. G92 Z0
  85. G00 Z{z_move}
  86. F{feedrate_probe_slow}
  87. G31 Z{z_pdepth}
  88. G92 Z0
  89. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  90. M0
  91. G00 Z{z_move}
  92. """.format(toolchangex=self.coordinate_format % (p.coords_decimals, toolchangex),
  93. toolchangey=self.coordinate_format % (p.coords_decimals, toolchangey),
  94. toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez),
  95. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  96. feedrate_probe=str(self.feedrate_format %(p.fr_decimals, p.feedrate_probe)),
  97. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  98. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  99. tool=int(p.tool),
  100. t_drills=no_drills,
  101. toolC=toolC_formatted)
  102. else:
  103. gcode = """
  104. T{tool}
  105. M5
  106. M6
  107. G00 Z{toolchangez}
  108. (MSG, Change to Tool Dia = {toolC} ||| Drills for this tool = {t_drills} ||| Tool Probing MACH3)
  109. M0
  110. G00 Z{z_move}
  111. F{feedrate_probe}
  112. G31 Z{z_pdepth}
  113. G92 Z0
  114. G00 Z{z_move}
  115. F{feedrate_probe_slow}
  116. G31 Z{z_pdepth}
  117. G92 Z0
  118. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  119. M0
  120. G00 Z{z_move}
  121. """.format(toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez),
  122. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  123. feedrate_probe=str(self.feedrate_format %(p.fr_decimals, p.feedrate_probe)),
  124. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  125. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  126. tool=int(p.tool),
  127. t_drills=no_drills,
  128. toolC=toolC_formatted)
  129. # if f_plunge is True:
  130. # gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  131. return gcode
  132. else:
  133. if toolchangexy is not None:
  134. gcode = """
  135. T{tool}
  136. M5
  137. M6
  138. G00 Z{toolchangez}
  139. G00 X{toolchangex} Y{toolchangey}
  140. (MSG, Change to Tool Dia = {toolC} ||| Tool Probing MACH3)
  141. M0
  142. G00 Z{z_move}
  143. F{feedrate_probe}
  144. G31 Z{z_pdepth}
  145. G92 Z0
  146. G00 Z{z_move}
  147. F{feedrate_probe_slow}
  148. G31 Z{z_pdepth}
  149. G92 Z0
  150. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  151. M0
  152. G00 Z{z_move}
  153. """.format(toolchangex=self.coordinate_format % (p.coords_decimals, toolchangex),
  154. toolchangey=self.coordinate_format % (p.coords_decimals, toolchangey),
  155. toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez),
  156. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  157. feedrate_probe=str(self.feedrate_format %(p.fr_decimals, p.feedrate_probe)),
  158. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  159. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  160. tool=int(p.tool),
  161. toolC=toolC_formatted)
  162. else:
  163. gcode = """
  164. T{tool}
  165. M5
  166. M6
  167. G00 Z{toolchangez}
  168. (MSG, Change to Tool Dia = {toolC} ||| Tool Probing MACH3)
  169. M0
  170. G00 Z{z_move}
  171. F{feedrate_probe}
  172. G31 Z{z_pdepth}
  173. G92 Z0
  174. G00 Z{z_move}
  175. F{feedrate_probe_slow}
  176. G31 Z{z_pdepth}
  177. G92 Z0
  178. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  179. M0
  180. G00 Z{z_move}
  181. """.format(toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez),
  182. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  183. feedrate_probe=str(self.feedrate_format %(p.fr_decimals, p.feedrate_probe)),
  184. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  185. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  186. tool=int(p.tool),
  187. toolC=toolC_formatted)
  188. # if f_plunge is True:
  189. # gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  190. return gcode
  191. def up_to_zero_code(self, p):
  192. return 'G01 Z0'
  193. def position_code(self, p):
  194. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  195. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  196. def rapid_code(self, p):
  197. return ('G00 ' + self.position_code(p)).format(**p)
  198. def linear_code(self, p):
  199. return ('G01 ' + self.position_code(p)).format(**p)
  200. def end_code(self, p):
  201. coords_xy = p['toolchange_xy']
  202. gcode = ('G00 Z' + self.feedrate_format %(p.fr_decimals, p.endz) + "\n")
  203. if coords_xy is not None:
  204. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  205. return gcode
  206. def feedrate_code(self, p):
  207. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate))
  208. def feedrate_z_code(self, p):
  209. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate_z))
  210. def spindle_code(self, p):
  211. if p.spindlespeed:
  212. return 'M03 S' + str(p.spindlespeed)
  213. else:
  214. return 'M03'
  215. def dwell_code(self, p):
  216. if p.dwelltime:
  217. return 'G4 P' + str(p.dwelltime)
  218. def spindle_stop_code(self,p):
  219. return 'M05'