Toolchange_Probe_MACH3.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 appPreProcessor import *
  9. class Toolchange_Probe_MACH3(PreProc):
  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. end_coords_xy = p['xy_end']
  17. gcode = '(This preprocessor is used with MACH3 with probing height.)\n\n'
  18. xmin = '%.*f' % (p.coords_decimals, p['options']['xmin'])
  19. xmax = '%.*f' % (p.coords_decimals, p['options']['xmax'])
  20. ymin = '%.*f' % (p.coords_decimals, p['options']['ymin'])
  21. ymax = '%.*f' % (p.coords_decimals, p['options']['ymax'])
  22. if str(p['options']['type']) == 'Geometry':
  23. gcode += '(TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + ')\n'
  24. gcode += '(Feedrate_XY: ' + str(p['feedrate']) + units + '/min' + ')\n'
  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 += '(Z_Cut: ' + str(p['z_cut']) + units + ')\n'
  28. if p['multidepth'] is True:
  29. gcode += '(DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  30. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + ')\n'
  31. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  32. elif str(p['options']['type']) == 'Excellon' and p['use_ui'] is True:
  33. gcode += '\n(TOOLS DIAMETER: )\n'
  34. for tool, val in p['exc_tools'].items():
  35. gcode += '(Tool: %s -> ' % str(tool) + 'Dia: %s' % str(val["tooldia"]) + ')\n'
  36. gcode += '\n(FEEDRATE Z: )\n'
  37. for tool, val in p['exc_tools'].items():
  38. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate: %s' % \
  39. str(val['data']["tools_drill_feedrate_z"]) + ')\n'
  40. gcode += '\n(FEEDRATE RAPIDS: )\n'
  41. for tool, val in p['exc_tools'].items():
  42. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate Rapids: %s' % \
  43. str(val['data']["tools_drill_feedrate_rapid"]) + ')\n'
  44. gcode += '\n(Z_CUT: )\n'
  45. for tool, val in p['exc_tools'].items():
  46. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Cut: %s' % str(val['data']["tools_drill_cutz"]) + ')\n'
  47. gcode += '\n(Tools Offset: )\n'
  48. for tool, val in p['exc_cnc_tools'].items():
  49. gcode += '(Tool: %s -> ' % str(val['tool']) + 'Offset Z: %s' % \
  50. str(val['data']["tools_drill_offset"]) + ')\n'
  51. if p['multidepth'] is True:
  52. gcode += '\n(DEPTH_PER_CUT: )\n'
  53. for tool, val in p['exc_tools'].items():
  54. gcode += '(Tool: %s -> ' % str(tool) + 'DeptPerCut: %s' % \
  55. str(val['data']["tools_drill_depthperpass"]) + ')\n'
  56. gcode += '\n(Z_MOVE: )\n'
  57. for tool, val in p['exc_tools'].items():
  58. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Move: %s' % str(val['data']["tools_drill_travelz"]) + ')\n'
  59. gcode += '\n'
  60. if p['toolchange'] is True:
  61. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  62. if coords_xy is not None:
  63. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  64. p.decimals, coords_xy[1]) + units + ')\n'
  65. else:
  66. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  67. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  68. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  69. if end_coords_xy is not None:
  70. gcode += '(X,Y End: ' + "%.*f, %.*f" % (p.decimals, end_coords_xy[0],
  71. p.decimals, end_coords_xy[1]) + units + ')\n'
  72. else:
  73. gcode += '(X,Y End: ' + "None" + units + ')\n'
  74. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  75. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  76. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + '\n'
  77. else:
  78. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  79. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  80. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  81. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  82. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  83. gcode += 'G90\n'
  84. gcode += 'G17\n'
  85. gcode += 'G94\n'
  86. return gcode
  87. def startz_code(self, p):
  88. g = '(MSG, WARNING: Make sure you do zero on all axis. ' \
  89. 'For Z axis, since it will be probed, make a rough estimate and do a zero.)\n'
  90. g += 'M0'
  91. return g
  92. def lift_code(self, p):
  93. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  94. def down_code(self, p):
  95. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  96. def toolchange_code(self, p):
  97. z_toolchange = p.z_toolchange
  98. toolchangexy = p.xy_toolchange
  99. f_plunge = p.f_plunge
  100. if toolchangexy is not None:
  101. x_toolchange = toolchangexy[0]
  102. y_toolchange = toolchangexy[1]
  103. else:
  104. x_toolchange = 0.0
  105. y_toolchange = 0.0
  106. no_drills = 1
  107. if int(p.tool) == 1 and p.startz is not None:
  108. z_toolchange = p.startz
  109. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  110. if str(p['options']['type']) == 'Excellon':
  111. for i in p['options']['Tools_in_use']:
  112. if i[0] == p.tool:
  113. no_drills = i[2]
  114. if toolchangexy is not None:
  115. gcode = """
  116. M5
  117. T{tool}
  118. M6
  119. G00 Z{z_toolchange}
  120. G00 X{x_toolchange} Y{y_toolchange}
  121. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE ||| Drills for this tool = {t_drills})
  122. M0
  123. F{feedrate_probe}
  124. G31 Z{z_pdepth}
  125. G92 Z0
  126. G00 Z{z_in_between}
  127. F{feedrate_probe_slow}
  128. G31 Z{z_pdepth}
  129. G92 Z0
  130. G00 Z{z_move}
  131. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  132. M0
  133. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  134. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  135. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  136. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  137. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  138. feedrate_probe=str(self.feedrate_format % (p.fr_decimals, p.feedrate_probe)),
  139. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  140. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  141. tool=int(p.tool),
  142. t_drills=no_drills,
  143. toolC=toolC_formatted)
  144. else:
  145. gcode = """
  146. M5
  147. T{tool}
  148. M6
  149. G00 Z{z_toolchange}
  150. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE ||| Drills for this tool = {t_drills})
  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(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  163. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  164. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  165. feedrate_probe=str(self.feedrate_format % (p.fr_decimals, p.feedrate_probe)),
  166. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  167. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  168. tool=int(p.tool),
  169. t_drills=no_drills,
  170. toolC=toolC_formatted)
  171. # if f_plunge is True:
  172. # gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  173. return gcode
  174. else:
  175. if toolchangexy is not None:
  176. gcode = """
  177. M5
  178. T{tool}
  179. M6
  180. G00 Z{z_toolchange}
  181. G00 X{x_toolchange} Y{y_toolchange}
  182. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE)
  183. M0
  184. F{feedrate_probe}
  185. G31 Z{z_pdepth}
  186. G92 Z0
  187. G00 Z{z_in_between}
  188. F{feedrate_probe_slow}
  189. G31 Z{z_pdepth}
  190. G92 Z0
  191. G00 Z{z_move}
  192. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  193. M0
  194. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  195. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  196. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  197. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  198. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  199. feedrate_probe=str(self.feedrate_format % (p.fr_decimals, p.feedrate_probe)),
  200. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  201. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  202. tool=int(p.tool),
  203. toolC=toolC_formatted)
  204. else:
  205. gcode = """
  206. M5
  207. T{tool}
  208. M6
  209. G00 Z{z_toolchange}
  210. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE)
  211. M0
  212. F{feedrate_probe}
  213. G31 Z{z_pdepth}
  214. G92 Z0
  215. G00 Z{z_in_between}
  216. F{feedrate_probe_slow}
  217. G31 Z{z_pdepth}
  218. G92 Z0
  219. G00 Z{z_move}
  220. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  221. M0
  222. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  223. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  224. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  225. feedrate_probe=str(self.feedrate_format % (p.fr_decimals, p.feedrate_probe)),
  226. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  227. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  228. tool=int(p.tool),
  229. toolC=toolC_formatted)
  230. # if f_plunge is True:
  231. # gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  232. return gcode
  233. def up_to_zero_code(self, p):
  234. return 'G01 Z0'
  235. def position_code(self, p):
  236. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  237. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  238. def rapid_code(self, p):
  239. return ('G00 ' + self.position_code(p)).format(**p)
  240. def linear_code(self, p):
  241. return ('G01 ' + self.position_code(p)).format(**p)
  242. def end_code(self, p):
  243. coords_xy = p['xy_end']
  244. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  245. if coords_xy and coords_xy != '':
  246. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  247. return gcode
  248. def feedrate_code(self, p):
  249. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  250. def z_feedrate_code(self, p):
  251. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  252. def spindle_code(self, p):
  253. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  254. if p.spindlespeed:
  255. return '%s S%s' % (sdir, str(p.spindlespeed))
  256. else:
  257. return sdir
  258. def dwell_code(self, p):
  259. if p.dwelltime:
  260. return 'G4 P' + str(p.dwelltime)
  261. def spindle_stop_code(self, p):
  262. return 'M05'