Toolchange_Custom.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_Custom(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'
  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. return ''
  54. def lift_code(self, p):
  55. return 'G00 Z' + self.coordinate_format%(p.coords_decimals, p.z_move)
  56. def down_code(self, p):
  57. return 'G01 Z' + self.coordinate_format%(p.coords_decimals, p.z_cut)
  58. def toolchange_code(self, p):
  59. z_toolchange = p.z_toolchange
  60. toolchangexy = p.xy_toolchange
  61. f_plunge = p.f_plunge
  62. gcode = ''
  63. if toolchangexy is not None:
  64. x_toolchange = toolchangexy[0]
  65. y_toolchange = toolchangexy[1]
  66. no_drills = 1
  67. if int(p.tool) == 1 and p.startz is not None:
  68. z_toolchange = p.startz
  69. if p.units.upper() == 'MM':
  70. toolC_formatted = format(p.toolC, '.2f')
  71. else:
  72. toolC_formatted = format(p.toolC, '.4f')
  73. if str(p['options']['type']) == 'Excellon':
  74. for i in p['options']['Tools_in_use']:
  75. if i[0] == p.tool:
  76. no_drills = i[2]
  77. if toolchangexy is not None:
  78. gcode = """
  79. M6
  80. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  81. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  82. tool=int(p.tool),
  83. t_drills=no_drills,
  84. toolC=toolC_formatted)
  85. if f_plunge is True:
  86. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  87. return gcode
  88. else:
  89. if toolchangexy is not None:
  90. gcode = """
  91. M6
  92. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  93. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  94. tool=int(p.tool),
  95. toolC=toolC_formatted)
  96. if f_plunge is True:
  97. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  98. return gcode
  99. def up_to_zero_code(self, p):
  100. return 'G01 Z0'
  101. def position_code(self, p):
  102. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  103. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  104. def rapid_code(self, p):
  105. return ('G00 ' + self.position_code(p)).format(**p)
  106. def linear_code(self, p):
  107. return ('G01 ' + self.position_code(p)).format(**p)
  108. def end_code(self, p):
  109. coords_xy = p['xy_toolchange']
  110. gcode = ('G00 Z' + self.feedrate_format %(p.fr_decimals, p.z_end) + "\n")
  111. if coords_xy is not None:
  112. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  113. return gcode
  114. def feedrate_code(self, p):
  115. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate))
  116. def z_feedrate_code(self, p):
  117. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.z_feedrate))
  118. def spindle_code(self, p):
  119. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  120. if p.spindlespeed:
  121. return '%s S%s' % (sdir, str(p.spindlespeed))
  122. else:
  123. return sdir
  124. def dwell_code(self, p):
  125. if p.dwelltime:
  126. return 'G4 P' + str(p.dwelltime)
  127. def spindle_stop_code(self,p):
  128. return 'M05'