Toolchange_Probe_MACH3.py 10 KB

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