Toolchange_Probe_MACH3.py 10 KB

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