Toolchange_manual.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_manual(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 += '(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. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  33. if coords_xy is not None:
  34. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  35. p.decimals, coords_xy[1]) + units + ')\n'
  36. else:
  37. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  38. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  39. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  40. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  41. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  42. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + '\n'
  43. else:
  44. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  45. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  46. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  47. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  48. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  49. gcode += 'G90\n'
  50. gcode += 'G17\n'
  51. gcode += 'G94\n'
  52. return gcode
  53. def startz_code(self, p):
  54. if p.startz is not None:
  55. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  56. else:
  57. return ''
  58. def lift_code(self, p):
  59. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  60. def down_code(self, p):
  61. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  62. def toolchange_code(self, p):
  63. z_toolchange = p.z_toolchange
  64. toolchangexy = p.xy_toolchange
  65. f_plunge = p.f_plunge
  66. if toolchangexy is not None:
  67. x_toolchange = toolchangexy[0]
  68. y_toolchange = toolchangexy[1]
  69. else:
  70. x_toolchange = 0.0
  71. y_toolchange = 0.0
  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. G00 Z{z_toolchange}
  84. T{tool}
  85. G00 X{x_toolchange} Y{y_toolchange}
  86. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  87. M0
  88. G01 Z0
  89. (MSG, Adjust the tool T{tool} to touch the material and then tighten it slightly.)
  90. M0
  91. G00 Z{z_toolchange}
  92. (MSG, Now the tool can be tightened more securely.)
  93. M0
  94. (MSG, Drilling with Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  95. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  96. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  97. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  98. tool=int(p.tool),
  99. t_drills=no_drills,
  100. toolC=toolC_formatted)
  101. else:
  102. gcode = """
  103. M5
  104. G00 Z{z_toolchange}
  105. T{tool}
  106. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  107. M0
  108. G01 Z0
  109. (MSG, Adjust the tool T{tool} to touch the material and then tighten it slightly.)
  110. M0
  111. G00 Z{z_toolchange}
  112. (MSG, Now the tool can be tightened more securely.)
  113. M0
  114. (MSG, Milling with Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  115. """.format(
  116. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  117. tool=int(p.tool),
  118. t_drills=no_drills,
  119. toolC=toolC_formatted)
  120. if f_plunge is True:
  121. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  122. return gcode
  123. else:
  124. if toolchangexy is not None:
  125. gcode = """
  126. M5
  127. G00 Z{z_toolchange}
  128. T{tool}
  129. G00 X{x_toolchange}Y{y_toolchange}
  130. (MSG, Change to Tool Dia = {toolC})
  131. M0
  132. G01 Z0
  133. (MSG, Adjust the tool T{tool} to touch the material and then tighten it slightly.)
  134. M0
  135. G00 Z{z_toolchange}
  136. (MSG, Now the tool can be tightened more securely.)
  137. M0
  138. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  139. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  140. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  141. tool=int(p.tool),
  142. toolC=toolC_formatted)
  143. else:
  144. gcode = """
  145. M5
  146. G00 Z{z_toolchange}
  147. T{tool}
  148. (MSG, Change to Tool Dia = {toolC})
  149. M0
  150. G01 Z0
  151. (MSG, Adjust the tool T{tool} to touch the material and then tighten it slightly.)
  152. M0
  153. G00 Z{z_toolchange}
  154. (MSG, Now the tool can be tightened more securely.)
  155. M0
  156. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange), tool=int(p.tool),
  157. toolC=toolC_formatted)
  158. if f_plunge is True:
  159. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  160. return gcode
  161. def up_to_zero_code(self, p):
  162. return 'G01 Z0'
  163. def position_code(self, p):
  164. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  165. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  166. def rapid_code(self, p):
  167. return ('G00 ' + self.position_code(p)).format(**p)
  168. def linear_code(self, p):
  169. return ('G01 ' + self.position_code(p)).format(**p)
  170. def end_code(self, p):
  171. coords_xy = p['xy_toolchange']
  172. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  173. if coords_xy is not None:
  174. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  175. else:
  176. gcode += 'G00 X0 Y0' + "\n"
  177. return gcode
  178. def feedrate_code(self, p):
  179. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  180. def z_feedrate_code(self, p):
  181. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  182. def spindle_code(self, p):
  183. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  184. if p.spindlespeed:
  185. return '%s S%s' % (sdir, str(p.spindlespeed))
  186. else:
  187. return sdir
  188. def dwell_code(self, p):
  189. if p.dwelltime:
  190. return 'G4 P' + str(p.dwelltime)
  191. def spindle_stop_code(self, p):
  192. return 'M05'