Toolchange_manual.py 8.0 KB

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