Toolchange_Manual.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_Manual(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 = ''
  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. if p.startz is not None:
  81. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  82. else:
  83. return ''
  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. G00 Z{z_toolchange}
  110. T{tool}
  111. G00 X{x_toolchange} Y{y_toolchange}
  112. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  113. M0
  114. G01 Z0
  115. (MSG, Adjust the tool T{tool} to touch the material and then tighten it slightly.)
  116. M0
  117. G00 Z{z_toolchange}
  118. (MSG, Now the tool can be tightened more securely.)
  119. M0
  120. (MSG, Drilling with Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  121. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  122. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  123. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  124. tool=int(p.tool),
  125. t_drills=no_drills,
  126. toolC=toolC_formatted)
  127. else:
  128. gcode = """
  129. M5
  130. G00 Z{z_toolchange}
  131. T{tool}
  132. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  133. M0
  134. G01 Z0
  135. (MSG, Adjust the tool T{tool} to touch the material and then tighten it slightly.)
  136. M0
  137. G00 Z{z_toolchange}
  138. (MSG, Now the tool can be tightened more securely.)
  139. M0
  140. (MSG, Milling with Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  141. """.format(
  142. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  143. tool=int(p.tool),
  144. t_drills=no_drills,
  145. toolC=toolC_formatted)
  146. if f_plunge is True:
  147. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  148. return gcode
  149. else:
  150. if toolchangexy is not None:
  151. gcode = """
  152. M5
  153. G00 Z{z_toolchange}
  154. T{tool}
  155. G00 X{x_toolchange}Y{y_toolchange}
  156. (MSG, Change to Tool Dia = {toolC})
  157. M0
  158. G01 Z0
  159. (MSG, Adjust the tool T{tool} to touch the material and then tighten it slightly.)
  160. M0
  161. G00 Z{z_toolchange}
  162. (MSG, Now the tool can be tightened more securely.)
  163. M0
  164. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  165. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  166. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  167. tool=int(p.tool),
  168. toolC=toolC_formatted)
  169. else:
  170. gcode = """
  171. M5
  172. G00 Z{z_toolchange}
  173. T{tool}
  174. (MSG, Change to Tool Dia = {toolC})
  175. M0
  176. G01 Z0
  177. (MSG, Adjust the tool T{tool} to touch the material and then tighten it slightly.)
  178. M0
  179. G00 Z{z_toolchange}
  180. (MSG, Now the tool can be tightened more securely.)
  181. M0
  182. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange), tool=int(p.tool),
  183. toolC=toolC_formatted)
  184. if f_plunge is True:
  185. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  186. return gcode
  187. def up_to_zero_code(self, p):
  188. return 'G01 Z0'
  189. def position_code(self, p):
  190. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  191. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  192. def rapid_code(self, p):
  193. return ('G00 ' + self.position_code(p)).format(**p)
  194. def linear_code(self, p):
  195. return ('G01 ' + self.position_code(p)).format(**p)
  196. def end_code(self, p):
  197. coords_xy = p['xy_end']
  198. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  199. if coords_xy and coords_xy != '':
  200. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  201. return gcode
  202. def feedrate_code(self, p):
  203. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  204. def z_feedrate_code(self, p):
  205. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  206. def spindle_code(self, p):
  207. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  208. if p.spindlespeed:
  209. return '%s S%s' % (sdir, str(p.spindlespeed))
  210. else:
  211. return sdir
  212. def dwell_code(self, p):
  213. if p.dwelltime:
  214. return 'G4 P' + str(p.dwelltime)
  215. def spindle_stop_code(self, p):
  216. return 'M05'