Toolchange_Probe_MACH3.py 8.7 KB

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