manual_toolchange.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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'
  33. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  34. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  35. gcode += 'G90\n'
  36. gcode += 'G94\n'
  37. return gcode
  38. def startz_code(self, p):
  39. if p.startz is not None:
  40. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  41. else:
  42. return ''
  43. def lift_code(self, p):
  44. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  45. def down_code(self, p):
  46. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  47. def toolchange_code(self, p):
  48. toolchangez = p.toolchangez
  49. toolchangexy = p.toolchange_xy
  50. f_plunge = p.f_plunge
  51. gcode = ''
  52. if toolchangexy is not None:
  53. toolchangex = toolchangexy[0]
  54. toolchangey = toolchangexy[1]
  55. # else:
  56. # toolchangex = p.oldx
  57. # toolchangey = p.oldy
  58. no_drills = 1
  59. if int(p.tool) == 1 and p.startz is not None:
  60. toolchangez = p.startz
  61. if p.units.upper() == 'MM':
  62. toolC_formatted = format(p.toolC, '.2f')
  63. else:
  64. toolC_formatted = format(p.toolC, '.4f')
  65. if str(p['options']['type']) == 'Excellon':
  66. for i in p['options']['Tools_in_use']:
  67. if i[0] == p.tool:
  68. no_drills = i[2]
  69. if toolchangexy is not None:
  70. gcode = """G00 Z{toolchangez}
  71. T{tool}
  72. M5
  73. G00 X{toolchangex} Y{toolchangey}
  74. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  75. M0
  76. G01 Z0
  77. M0
  78. G00 Z{toolchangez}
  79. M0
  80. """.format(toolchangex=self.coordinate_format%(p.coords_decimals, toolchangex),
  81. toolchangey=self.coordinate_format%(p.coords_decimals, toolchangey),
  82. toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  83. tool=int(p.tool),
  84. t_drills=no_drills,
  85. toolC=toolC_formatted)
  86. else:
  87. gcode = """G00 Z{toolchangez}
  88. T{tool}
  89. M5
  90. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  91. M0
  92. G01 Z0
  93. M0
  94. G00 Z{toolchangez}
  95. M0
  96. """.format(
  97. toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  98. tool=int(p.tool),
  99. t_drills=no_drills,
  100. toolC=toolC_formatted)
  101. if f_plunge is True:
  102. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  103. return gcode
  104. else:
  105. if toolchangexy is not None:
  106. gcode = """G00 Z{toolchangez}
  107. T{tool}
  108. M5
  109. G00 X{toolchangex}Y{toolchangey}
  110. (MSG, Change to Tool Dia = {toolC})
  111. M0
  112. G01 Z0
  113. M0
  114. G00 Z{toolchangez}
  115. M0
  116. """.format(toolchangex=self.coordinate_format%(p.coords_decimals, toolchangex),
  117. toolchangey=self.coordinate_format%(p.coords_decimals, toolchangey),
  118. toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  119. tool=int(p.tool),
  120. toolC=toolC_formatted)
  121. else:
  122. gcode = """G00 Z{toolchangez}
  123. T{tool}
  124. M5
  125. (MSG, Change to Tool Dia = {toolC})
  126. M0
  127. G01 Z0
  128. M0
  129. G00 Z{toolchangez}
  130. M0
  131. """.format(toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  132. tool=int(p.tool),
  133. toolC=toolC_formatted)
  134. if f_plunge is True:
  135. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  136. return gcode
  137. def up_to_zero_code(self, p):
  138. return 'G01 Z0'
  139. def position_code(self, p):
  140. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  141. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  142. def rapid_code(self, p):
  143. return ('G00 ' + self.position_code(p)).format(**p)
  144. def linear_code(self, p):
  145. return ('G01 ' + self.position_code(p)).format(**p)
  146. def end_code(self, p):
  147. coords_xy = p['toolchange_xy']
  148. gcode = ('G00 Z' + self.feedrate_format %(p.fr_decimals, p.endz) + "\n")
  149. if coords_xy is not None:
  150. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  151. else:
  152. gcode += 'G00 X0 Y0' + "\n"
  153. return gcode
  154. def feedrate_code(self, p):
  155. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate))
  156. def feedrate_z_code(self, p):
  157. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate_z))
  158. def spindle_code(self,p):
  159. if p.spindlespeed:
  160. return 'M03 S' + str(p.spindlespeed)
  161. else:
  162. return 'M03'
  163. def dwell_code(self, p):
  164. if p.dwelltime:
  165. return 'G4 P' + str(p.dwelltime)
  166. def spindle_stop_code(self,p):
  167. return 'M05'