Toolchange_Probe_MACH3.py 10 KB

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