Toolchange_Probe_MACH3.py 12 KB

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