Toolchange_Custom.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 AppPreProcessor import *
  9. class Toolchange_Custom(PreProc):
  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_XY: ' + str(p['feedrate']) + units + '/min' + ')\n'
  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 p['multidepth'] is True:
  28. gcode += '(DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  29. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + ')\n'
  30. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  31. elif str(p['options']['type']) == 'Excellon' and p['use_ui'] is True:
  32. gcode += '\n(TOOLS DIAMETER: )\n'
  33. for tool, val in p['exc_tools'].items():
  34. gcode += '(Tool: %s -> ' % str(tool) + 'Dia: %s' % str(val["C"]) + ')\n'
  35. gcode += '\n(FEEDRATE Z: )\n'
  36. for tool, val in p['exc_tools'].items():
  37. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate: %s' % str(val['data']["feedrate_z"]) + ')\n'
  38. gcode += '\n(FEEDRATE RAPIDS: )\n'
  39. for tool, val in p['exc_tools'].items():
  40. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate Rapids: %s' % \
  41. str(val['data']["feedrate_rapid"]) + ')\n'
  42. gcode += '\n(Z_CUT: )\n'
  43. for tool, val in p['exc_tools'].items():
  44. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Cut: %s' % str(val['data']["cutz"]) + ')\n'
  45. gcode += '\n(Tools Offset: )\n'
  46. for tool, val in p['exc_cnc_tools'].items():
  47. gcode += '(Tool: %s -> ' % str(val['tool']) + 'Offset Z: %s' % str(val['offset_z']) + ')\n'
  48. if p['multidepth'] is True:
  49. gcode += '\n(DEPTH_PER_CUT: )\n'
  50. for tool, val in p['exc_tools'].items():
  51. gcode += '(Tool: %s -> ' % str(tool) + 'DeptPerCut: %s' % \
  52. str(val['data']["depthperpass"]) + ')\n'
  53. gcode += '\n(Z_MOVE: )\n'
  54. for tool, val in p['exc_tools'].items():
  55. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Move: %s' % str(val['data']["travelz"]) + ')\n'
  56. gcode += '\n'
  57. if p['toolchange'] is True:
  58. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  59. if coords_xy is not None:
  60. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  61. p.decimals, coords_xy[1]) + units + ')\n'
  62. else:
  63. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  64. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  65. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  66. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  67. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  68. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + '\n'
  69. else:
  70. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  71. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  72. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  73. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  74. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  75. gcode += 'G90\n'
  76. gcode += 'G17\n'
  77. gcode += 'G94\n'
  78. return gcode
  79. def startz_code(self, p):
  80. return ''
  81. def lift_code(self, p):
  82. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  83. def down_code(self, p):
  84. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  85. def toolchange_code(self, p):
  86. z_toolchange = p.z_toolchange
  87. toolchangexy = p.xy_toolchange
  88. f_plunge = p.f_plunge
  89. gcode = ''
  90. if toolchangexy is not None:
  91. x_toolchange = toolchangexy[0]
  92. y_toolchange = toolchangexy[1]
  93. else:
  94. x_toolchange = 0.0
  95. y_toolchange = 0.0
  96. no_drills = 1
  97. if int(p.tool) == 1 and p.startz is not None:
  98. z_toolchange = p.startz
  99. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  100. if str(p['options']['type']) == 'Excellon':
  101. for i in p['options']['Tools_in_use']:
  102. if i[0] == p.tool:
  103. no_drills = i[2]
  104. if toolchangexy is not None:
  105. gcode = """
  106. M6
  107. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  108. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  109. tool=int(p.tool),
  110. t_drills=no_drills,
  111. toolC=toolC_formatted)
  112. if f_plunge is True:
  113. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  114. return gcode
  115. else:
  116. if toolchangexy is not None:
  117. gcode = """
  118. M6
  119. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  120. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  121. tool=int(p.tool),
  122. toolC=toolC_formatted)
  123. if f_plunge is True:
  124. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  125. return gcode
  126. def up_to_zero_code(self, p):
  127. return 'G01 Z0'
  128. def position_code(self, p):
  129. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  130. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  131. def rapid_code(self, p):
  132. return ('G00 ' + self.position_code(p)).format(**p)
  133. def linear_code(self, p):
  134. return ('G01 ' + self.position_code(p)).format(**p)
  135. def end_code(self, p):
  136. coords_xy = p['xy_end']
  137. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  138. if coords_xy and coords_xy != '':
  139. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  140. return gcode
  141. def feedrate_code(self, p):
  142. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  143. def z_feedrate_code(self, p):
  144. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  145. def spindle_code(self, p):
  146. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  147. if p.spindlespeed:
  148. return '%s S%s' % (sdir, str(p.spindlespeed))
  149. else:
  150. return sdir
  151. def dwell_code(self, p):
  152. if p.dwelltime:
  153. return 'G4 P' + str(p.dwelltime)
  154. def spindle_stop_code(self, p):
  155. return 'M05'