default.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 appPreProcessor import *
  9. class default(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 = '(This preprocessor is the default preprocessor used by FlatCAM.)\n'
  17. gcode += '(It is made to work with MACH3 compatible motion controllers.)\n\n'
  18. xmin = '%.*f' % (p.coords_decimals, p['options']['xmin'])
  19. xmax = '%.*f' % (p.coords_decimals, p['options']['xmax'])
  20. ymin = '%.*f' % (p.coords_decimals, p['options']['ymin'])
  21. ymax = '%.*f' % (p.coords_decimals, p['options']['ymax'])
  22. if str(p['options']['type']) == 'Geometry':
  23. gcode += '(TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + ')\n'
  24. gcode += '(Feedrate_XY: ' + str(p['feedrate']) + units + '/min' + ')\n'
  25. gcode += '(Feedrate_Z: ' + str(p['z_feedrate']) + units + '/min' + ')\n'
  26. gcode += '(Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + ')\n' + '\n'
  27. gcode += '(Z_Cut: ' + str(p['z_cut']) + units + ')\n'
  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. elif str(p['options']['type']) == 'Excellon' and p['use_ui'] is True:
  33. gcode += '\n(TOOLS DIAMETER: )\n'
  34. for tool, val in p['exc_tools'].items():
  35. gcode += '(Tool: %s -> ' % str(tool) + 'Dia: %s' % str(val["C"]) + ')\n'
  36. gcode += '\n(FEEDRATE Z: )\n'
  37. for tool, val in p['exc_tools'].items():
  38. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate: %s' % str(val['data']["feedrate_z"]) + ')\n'
  39. gcode += '\n(FEEDRATE RAPIDS: )\n'
  40. for tool, val in p['exc_tools'].items():
  41. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate Rapids: %s' % \
  42. str(val['data']["feedrate_rapid"]) + ')\n'
  43. gcode += '\n(Z_CUT: )\n'
  44. for tool, val in p['exc_tools'].items():
  45. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Cut: %s' % str(val['data']["cutz"]) + ')\n'
  46. gcode += '\n(Tools Offset: )\n'
  47. for tool, val in p['exc_cnc_tools'].items():
  48. gcode += '(Tool: %s -> ' % str(val['tool']) + 'Offset Z: %s' % str(val['offset_z']) + ')\n'
  49. if p['multidepth'] is True:
  50. gcode += '\n(DEPTH_PER_CUT: )\n'
  51. for tool, val in p['exc_tools'].items():
  52. gcode += '(Tool: %s -> ' % str(tool) + 'DeptPerCut: %s' % \
  53. str(val['data']["depthperpass"]) + ')\n'
  54. gcode += '\n(Z_MOVE: )\n'
  55. for tool, val in p['exc_tools'].items():
  56. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Move: %s' % str(val['data']["travelz"]) + ')\n'
  57. gcode += '\n'
  58. if p['toolchange'] is True:
  59. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  60. if coords_xy is not None:
  61. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  62. p.decimals, coords_xy[1]) + units + ')\n'
  63. else:
  64. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  65. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  66. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  67. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  68. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  69. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + '\n'
  70. else:
  71. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  72. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  73. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  74. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  75. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  76. gcode += 'G90\n'
  77. gcode += 'G94'
  78. return gcode
  79. def startz_code(self, p):
  80. if p.startz is not None:
  81. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  82. else:
  83. return ''
  84. def lift_code(self, p):
  85. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  86. def down_code(self, p):
  87. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  88. def toolchange_code(self, p):
  89. z_toolchange = p.z_toolchange
  90. toolchangexy = p.xy_toolchange
  91. f_plunge = p.f_plunge
  92. if toolchangexy is not None:
  93. x_toolchange = toolchangexy[0]
  94. y_toolchange = toolchangexy[1]
  95. else:
  96. x_toolchange = 0.0
  97. y_toolchange = 0.0
  98. no_drills = 1
  99. if int(p.tool) == 1 and p.startz is not None:
  100. z_toolchange = p.startz
  101. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  102. if str(p['options']['type']) == 'Excellon':
  103. for i in p['options']['Tools_in_use']:
  104. if i[0] == p.tool:
  105. no_drills = i[2]
  106. if toolchangexy is not None:
  107. gcode = """
  108. M5
  109. G00 Z{z_toolchange}
  110. T{tool}
  111. G00 X{x_toolchange} Y{y_toolchange}
  112. M6
  113. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  114. M0
  115. G00 Z{z_toolchange}
  116. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  117. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  118. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  119. tool=int(p.tool),
  120. t_drills=no_drills,
  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} ||| Total drills for tool T{tool} = {t_drills})
  129. M0
  130. G00 Z{z_toolchange}
  131. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  132. tool=int(p.tool),
  133. t_drills=no_drills,
  134. toolC=toolC_formatted)
  135. if f_plunge is True:
  136. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  137. return gcode
  138. else:
  139. if toolchangexy is not None:
  140. gcode = """
  141. M5
  142. G00 Z{z_toolchange}
  143. G00 X{x_toolchange} Y{y_toolchange}
  144. T{tool}
  145. M6
  146. (MSG, Change to Tool Dia = {toolC})
  147. M0
  148. G00 Z{z_toolchange}
  149. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  150. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  151. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  152. tool=int(p.tool),
  153. toolC=toolC_formatted)
  154. else:
  155. gcode = """
  156. M5
  157. G00 Z{z_toolchange}
  158. T{tool}
  159. M6
  160. (MSG, Change to Tool Dia = {toolC})
  161. M0
  162. G00 Z{z_toolchange}
  163. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  164. tool=int(p.tool),
  165. toolC=toolC_formatted)
  166. if f_plunge is True:
  167. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  168. return gcode
  169. def up_to_zero_code(self, p):
  170. return 'G01 Z0'
  171. def position_code(self, p):
  172. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  173. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  174. def rapid_code(self, p):
  175. return ('G00 ' + self.position_code(p)).format(**p)
  176. def linear_code(self, p):
  177. return ('G01 ' + self.position_code(p)).format(**p)
  178. def end_code(self, p):
  179. end_coords_xy = p['xy_end']
  180. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  181. if end_coords_xy and end_coords_xy != '':
  182. gcode += 'G00 X{x} Y{y}'.format(x=end_coords_xy[0], y=end_coords_xy[1]) + "\n"
  183. return gcode
  184. def feedrate_code(self, p):
  185. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  186. def z_feedrate_code(self, p):
  187. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  188. def spindle_code(self, p):
  189. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  190. if p.spindlespeed:
  191. return '%s S%s' % (sdir, str(p.spindlespeed))
  192. else:
  193. return sdir
  194. def dwell_code(self, p):
  195. if p.dwelltime:
  196. return 'G4 P' + str(p.dwelltime)
  197. def spindle_stop_code(self, p):
  198. return 'M05'