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. 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. if toolchangexy is not None:
  70. x_toolchange = toolchangexy[0]
  71. y_toolchange = toolchangexy[1]
  72. else:
  73. x_toolchange = 0.0
  74. y_toolchange = 0.0
  75. no_drills = 1
  76. if int(p.tool) == 1 and p.startz is not None:
  77. z_toolchange = p.startz
  78. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  79. if str(p['options']['type']) == 'Excellon':
  80. for i in p['options']['Tools_in_use']:
  81. if i[0] == p.tool:
  82. no_drills = i[2]
  83. if toolchangexy is not None:
  84. gcode = """
  85. M5
  86. T{tool}
  87. M6
  88. G00 Z{z_toolchange}
  89. G00 X{x_toolchange} Y{y_toolchange}
  90. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE ||| Drills for this tool = {t_drills})
  91. M0
  92. F{feedrate_probe}
  93. G31 Z{z_pdepth}
  94. G92 Z0
  95. G00 Z{z_in_between}
  96. F{feedrate_probe_slow}
  97. G31 Z{z_pdepth}
  98. G92 Z0
  99. G00 Z{z_move}
  100. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  101. M0
  102. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  103. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  104. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  105. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  106. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  107. feedrate_probe=str(self.feedrate_format % (p.fr_decimals, p.feedrate_probe)),
  108. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  109. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  110. tool=int(p.tool),
  111. t_drills=no_drills,
  112. toolC=toolC_formatted)
  113. else:
  114. gcode = """
  115. M5
  116. T{tool}
  117. M6
  118. G00 Z{z_toolchange}
  119. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE ||| Drills for this tool = {t_drills})
  120. M0
  121. F{feedrate_probe}
  122. G31 Z{z_pdepth}
  123. G92 Z0
  124. G00 Z{z_in_between}
  125. F{feedrate_probe_slow}
  126. G31 Z{z_pdepth}
  127. G92 Z0
  128. G00 Z{z_move}
  129. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  130. M0
  131. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  132. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  133. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  134. feedrate_probe=str(self.feedrate_format % (p.fr_decimals, p.feedrate_probe)),
  135. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  136. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  137. tool=int(p.tool),
  138. t_drills=no_drills,
  139. toolC=toolC_formatted)
  140. # if f_plunge is True:
  141. # gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  142. return gcode
  143. else:
  144. if toolchangexy is not None:
  145. gcode = """
  146. M5
  147. T{tool}
  148. M6
  149. G00 Z{z_toolchange}
  150. G00 X{x_toolchange} Y{y_toolchange}
  151. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE)
  152. M0
  153. F{feedrate_probe}
  154. G31 Z{z_pdepth}
  155. G92 Z0
  156. G00 Z{z_in_between}
  157. F{feedrate_probe_slow}
  158. G31 Z{z_pdepth}
  159. G92 Z0
  160. G00 Z{z_move}
  161. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  162. M0
  163. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  164. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  165. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  166. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  167. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  168. feedrate_probe=str(self.feedrate_format % (p.fr_decimals, p.feedrate_probe)),
  169. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  170. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  171. tool=int(p.tool),
  172. toolC=toolC_formatted)
  173. else:
  174. gcode = """
  175. M5
  176. T{tool}
  177. M6
  178. G00 Z{z_toolchange}
  179. (MSG, Change to Tool Dia = {toolC} ||| CONNECT THE PROBE)
  180. M0
  181. F{feedrate_probe}
  182. G31 Z{z_pdepth}
  183. G92 Z0
  184. G00 Z{z_in_between}
  185. F{feedrate_probe_slow}
  186. G31 Z{z_pdepth}
  187. G92 Z0
  188. G00 Z{z_move}
  189. (MSG, Remove any clips or other devices used for probing. CNC work is resuming ...)
  190. M0
  191. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  192. z_move=self.coordinate_format % (p.coords_decimals, p.z_move),
  193. z_in_between=self.coordinate_format % (p.coords_decimals, p.z_move / 2),
  194. feedrate_probe=str(self.feedrate_format % (p.fr_decimals, p.feedrate_probe)),
  195. feedrate_probe_slow=str(self.feedrate_format % (p.fr_decimals, (p.feedrate_probe / 2))),
  196. z_pdepth=self.coordinate_format % (p.coords_decimals, p.z_pdepth),
  197. tool=int(p.tool),
  198. toolC=toolC_formatted)
  199. # if f_plunge is True:
  200. # gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  201. return gcode
  202. def up_to_zero_code(self, p):
  203. return 'G01 Z0'
  204. def position_code(self, p):
  205. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  206. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  207. def rapid_code(self, p):
  208. return ('G00 ' + self.position_code(p)).format(**p)
  209. def linear_code(self, p):
  210. return ('G01 ' + self.position_code(p)).format(**p)
  211. def end_code(self, p):
  212. coords_xy = p['xy_toolchange']
  213. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  214. if coords_xy is not None:
  215. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  216. return gcode
  217. def feedrate_code(self, p):
  218. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  219. def z_feedrate_code(self, p):
  220. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  221. def spindle_code(self, p):
  222. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  223. if p.spindlespeed:
  224. return '%s S%s' % (sdir, str(p.spindlespeed))
  225. else:
  226. return sdir
  227. def dwell_code(self, p):
  228. if p.dwelltime:
  229. return 'G4 P' + str(p.dwelltime)
  230. def spindle_stop_code(self, p):
  231. return 'M05'