default.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Matthieu Berthomé #
  5. # Date: 5/26/2017 #
  6. # MIT Licence #
  7. ############################################################
  8. from FlatCAMPostProc import *
  9. class default(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' + '\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 += 'G94\n'
  50. return gcode
  51. def startz_code(self, p):
  52. if p.startz is not None:
  53. return 'G00 Z' + self.coordinate_format%(p.coords_decimals, p.startz)
  54. else:
  55. return ''
  56. def lift_code(self, p):
  57. return 'G00 Z' + self.coordinate_format%(p.coords_decimals, p.z_move)
  58. def down_code(self, p):
  59. return 'G01 Z' + self.coordinate_format%(p.coords_decimals, p.z_cut)
  60. def toolchange_code(self, p):
  61. z_toolchange = p.z_toolchange
  62. toolchangexy = p.xy_toolchange
  63. f_plunge = p.f_plunge
  64. gcode = ''
  65. if toolchangexy is not None:
  66. x_toolchange = toolchangexy[0]
  67. y_toolchange = toolchangexy[1]
  68. no_drills = 1
  69. if int(p.tool) == 1 and p.startz is not None:
  70. z_toolchange = p.startz
  71. if p.units.upper() == 'MM':
  72. toolC_formatted = format(p.toolC, '.2f')
  73. else:
  74. toolC_formatted = format(p.toolC, '.4f')
  75. if str(p['options']['type']) == 'Excellon':
  76. for i in p['options']['Tools_in_use']:
  77. if i[0] == p.tool:
  78. no_drills = i[2]
  79. if toolchangexy is not None:
  80. gcode = """
  81. M5
  82. G00 Z{z_toolchange}
  83. G00 X{x_toolchange} Y{y_toolchange}
  84. T{tool}
  85. M6
  86. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  87. M0
  88. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  89. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  90. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  91. tool=int(p.tool),
  92. t_drills=no_drills,
  93. toolC=toolC_formatted)
  94. else:
  95. gcode = """
  96. M5
  97. G00 Z{z_toolchange}
  98. T{tool}
  99. M6
  100. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  101. M0""".format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  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 = """
  111. M5
  112. G00 Z{z_toolchange}
  113. G00 X{x_toolchange} Y{y_toolchange}
  114. T{tool}
  115. M6
  116. (MSG, Change to Tool Dia = {toolC})
  117. M0""".format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  118. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  119. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  120. tool=int(p.tool),
  121. toolC=toolC_formatted)
  122. else:
  123. gcode = """
  124. M5
  125. G00 Z{z_toolchange}
  126. T{tool}
  127. M6
  128. (MSG, Change to Tool Dia = {toolC})
  129. M0""".format(z_toolchange=self.coordinate_format%(p.coords_decimals, z_toolchange),
  130. tool=int(p.tool),
  131. toolC=toolC_formatted)
  132. if f_plunge is True:
  133. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  134. return gcode
  135. def up_to_zero_code(self, p):
  136. return 'G01 Z0'
  137. def position_code(self, p):
  138. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  139. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  140. def rapid_code(self, p):
  141. return ('G00 ' + self.position_code(p)).format(**p)
  142. def linear_code(self, p):
  143. return ('G01 ' + self.position_code(p)).format(**p)
  144. def end_code(self, p):
  145. coords_xy = p['xy_toolchange']
  146. gcode = ('G00 Z' + self.feedrate_format %(p.fr_decimals, p.z_end) + "\n")
  147. if coords_xy is not None:
  148. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  149. return gcode
  150. def feedrate_code(self, p):
  151. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate))
  152. def z_feedrate_code(self, p):
  153. return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.z_feedrate))
  154. def spindle_code(self, p):
  155. if p.spindlespeed:
  156. return 'M03 S' + str(p.spindlespeed)
  157. else:
  158. return 'M03'
  159. def dwell_code(self, p):
  160. if p.dwelltime:
  161. return 'G4 P' + str(p.dwelltime)
  162. def spindle_stop_code(self,p):
  163. return 'M05'