manual_toolchange.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. from FlatCAMPostProc import *
  2. class manual_toolchange(FlatCAMPostProc):
  3. coordinate_format = "%.*f"
  4. feedrate_format = '%.*f'
  5. def start_code(self, p):
  6. units = ' ' + str(p['units']).lower()
  7. coords_xy = p['toolchange_xy']
  8. gcode = ''
  9. if str(p['options']['type']) == 'Geometry':
  10. gcode += '(TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + ')\n'
  11. gcode += '(Feedrate: ' + str(p['feedrate']) + units + '/min' + ')\n'
  12. if str(p['options']['type']) == 'Geometry':
  13. gcode += '(Feedrate_Z: ' + str(p['feedrate_z']) + units + '/min' + ')\n'
  14. gcode += '(Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + ')\n' + '\n'
  15. gcode += '(Z_Cut: ' + str(p['z_cut']) + units + ')\n'
  16. if str(p['options']['type']) == 'Geometry':
  17. if p['multidepth'] is True:
  18. gcode += '(DepthPerCut: ' + str(p['depthpercut']) + units + ' <=>' + \
  19. str(math.ceil(abs(p['z_cut']) / p['depthpercut'])) + ' passes' + ')\n'
  20. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  21. gcode += '(Z Toolchange: ' + str(p['toolchangez']) + units + ')\n'
  22. if coords_xy is not None:
  23. gcode += '(X,Y Toolchange: ' + "%.4f, %.4f" % (coords_xy[0], coords_xy[1]) + units + ')\n'
  24. else:
  25. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  26. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  27. gcode += '(Z End: ' + str(p['endz']) + units + ')\n'
  28. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  29. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  30. gcode += '(Postprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n'
  31. else:
  32. gcode += '(Postprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  33. gcode += '(X min: ' + '%.*f' % (p.coords_decimals, p['options']['xmin']) + units + ')\n'
  34. gcode += '(Y min: ' + '%.*f' % (p.coords_decimals, p['options']['ymin']) + units + ')\n'
  35. gcode += '(X max: ' + '%.*f' % (p.coords_decimals, p['options']['xmax']) + units + ')\n'
  36. gcode += '(Y max: ' + '%.*f' % (p.coords_decimals, p['options']['ymax']) + units + ')\n\n'
  37. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  38. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  39. gcode += 'G90\n'
  40. gcode += 'G94\n'
  41. return gcode
  42. def startz_code(self, p):
  43. if p.startz is not None:
  44. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  45. else:
  46. return ''
  47. def lift_code(self, p):
  48. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  49. def down_code(self, p):
  50. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  51. def toolchange_code(self, p):
  52. toolchangez = p.toolchangez
  53. toolchangexy = p.toolchange_xy
  54. f_plunge = p.f_plunge
  55. gcode = ''
  56. if toolchangexy is not None:
  57. toolchangex = toolchangexy[0]
  58. toolchangey = toolchangexy[1]
  59. # else:
  60. # toolchangex = p.oldx
  61. # toolchangey = p.oldy
  62. no_drills = 1
  63. if int(p.tool) == 1 and p.startz is not None:
  64. toolchangez = p.startz
  65. if p.units.upper() == 'MM':
  66. toolC_formatted = format(p.toolC, '.2f')
  67. else:
  68. toolC_formatted = format(p.toolC, '.4f')
  69. if str(p['options']['type']) == 'Excellon':
  70. for i in p['options']['Tools_in_use']:
  71. if i[0] == p.tool:
  72. no_drills = i[2]
  73. if toolchangexy is not None:
  74. gcode = """G00 Z{toolchangez}
  75. T{tool}
  76. M5
  77. G00 X{toolchangex} Y{toolchangey}
  78. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  79. M0
  80. G01 Z0
  81. M0
  82. G00 Z{toolchangez}
  83. M0
  84. """.format(toolchangex=self.coordinate_format%(p.coords_decimals, toolchangex),
  85. toolchangey=self.coordinate_format%(p.coords_decimals, toolchangey),
  86. toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  87. tool=int(p.tool),
  88. t_drills=no_drills,
  89. toolC=toolC_formatted)
  90. else:
  91. gcode = """G00 Z{toolchangez}
  92. T{tool}
  93. M5
  94. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  95. M0
  96. G01 Z0
  97. M0
  98. G00 Z{toolchangez}
  99. M0
  100. """.format(
  101. toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  102. tool=int(p.tool),
  103. t_drills=no_drills,
  104. toolC=toolC_formatted)
  105. if f_plunge is True:
  106. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  107. return gcode
  108. else:
  109. if toolchangexy is not None:
  110. gcode = """G00 Z{toolchangez}
  111. T{tool}
  112. M5
  113. G00 X{toolchangex}Y{toolchangey}
  114. (MSG, Change to Tool Dia = {toolC})
  115. M0
  116. G01 Z0
  117. M0
  118. G00 Z{toolchangez}
  119. M0
  120. """.format(toolchangex=self.coordinate_format%(p.coords_decimals, toolchangex),
  121. toolchangey=self.coordinate_format%(p.coords_decimals, toolchangey),
  122. toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  123. tool=int(p.tool),
  124. toolC=toolC_formatted)
  125. else:
  126. gcode = """G00 Z{toolchangez}
  127. T{tool}
  128. M5
  129. (MSG, Change to Tool Dia = {toolC})
  130. M0
  131. G01 Z0
  132. M0
  133. G00 Z{toolchangez}
  134. M0
  135. """.format(toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  136. tool=int(p.tool),
  137. toolC=toolC_formatted)
  138. if f_plunge is True:
  139. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  140. return gcode
  141. def up_to_zero_code(self, p):
  142. return 'G01 Z0'
  143. def position_code(self, p):
  144. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  145. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  146. def rapid_code(self, p):
  147. return ('G00 ' + self.position_code(p)).format(**p)
  148. def linear_code(self, p):
  149. return ('G01 ' + self.position_code(p)).format(**p)
  150. def end_code(self, p):
  151. coords_xy = p['toolchange_xy']
  152. gcode = ('G00 Z' + self.feedrate_format %(p.fr_decimals, p.endz) + "\n")
  153. if coords_xy is not None:
  154. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  155. else:
  156. gcode += 'G00 X0 Y0' + "\n"
  157. return gcode
  158. def feedrate_code(self, p):
  159. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate))
  160. def feedrate_z_code(self, p):
  161. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate_z))
  162. def spindle_code(self,p):
  163. if p.spindlespeed:
  164. return 'M03 S' + str(p.spindlespeed)
  165. else:
  166. return 'M03'
  167. def dwell_code(self, p):
  168. if p.dwelltime:
  169. return 'G4 P' + str(p.dwelltime)
  170. def spindle_stop_code(self,p):
  171. return 'M05'