Toolchange_manual.py 7.0 KB

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