Repetier.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 Repetier(PreProc):
  10. include_header = True
  11. coordinate_format = "%.*f"
  12. feedrate_format = '%.*f'
  13. feedrate_rapid_format = feedrate_format
  14. def start_code(self, p):
  15. units = ' ' + str(p['units']).lower()
  16. coords_xy = p['xy_toolchange']
  17. end_coords_xy = p['xy_end']
  18. gcode = ';This preprocessor is used with a motion controller loaded with REPETIER firmware.\n\n'
  19. xmin = '%.*f' % (p.coords_decimals, p['options']['xmin'])
  20. xmax = '%.*f' % (p.coords_decimals, p['options']['xmax'])
  21. ymin = '%.*f' % (p.coords_decimals, p['options']['ymin'])
  22. ymax = '%.*f' % (p.coords_decimals, p['options']['ymax'])
  23. if str(p['options']['type']) == 'Geometry':
  24. gcode += ';TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + '\n'
  25. gcode += ';Feedrate_XY: ' + str(p['feedrate']) + units + '/min' + '\n'
  26. gcode += ';Feedrate_Z: ' + str(p['z_feedrate']) + units + '/min' + '\n'
  27. gcode += ';Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + '\n' + '\n'
  28. gcode += ';Z_Cut: ' + str(p['z_cut']) + units + '\n'
  29. if p['multidepth'] is True:
  30. gcode += ';DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  31. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + '\n'
  32. gcode += ';Z_Move: ' + str(p['z_move']) + units + '\n'
  33. elif str(p['options']['type']) == 'Excellon' and p['use_ui'] is True:
  34. gcode += '\n;TOOLS DIAMETER: \n'
  35. for tool, val in p['exc_tools'].items():
  36. gcode += ';Tool: %s -> ' % str(tool) + 'Dia: %s' % str(val["tooldia"]) + '\n'
  37. gcode += '\n;FEEDRATE Z: \n'
  38. for tool, val in p['exc_tools'].items():
  39. gcode += ';Tool: %s -> ' % str(tool) + 'Feedrate: %s' % \
  40. str(val['data']["tools_drill_feedrate_z"]) + '\n'
  41. gcode += '\n;FEEDRATE RAPIDS: \n'
  42. for tool, val in p['exc_tools'].items():
  43. gcode += ';Tool: %s -> ' % str(tool) + 'Feedrate Rapids: %s' % \
  44. str(val['data']["tools_drill_feedrate_rapid"]) + '\n'
  45. gcode += '\n;Z_CUT: \n'
  46. for tool, val in p['exc_tools'].items():
  47. gcode += ';Tool: %s -> ' % str(tool) + 'Z_Cut: %s' % str(val['data']["tools_drill_cutz"]) + '\n'
  48. gcode += '\n;Tools Offset: \n'
  49. for tool, val in p['exc_cnc_tools'].items():
  50. gcode += ';Tool: %s -> ' % str(val['tool']) + 'Offset Z: %s' % \
  51. str(val['data']["tools_drill_offset"]) + '\n'
  52. if p['multidepth'] is True:
  53. gcode += '\n;DEPTH_PER_CUT: \n'
  54. for tool, val in p['exc_tools'].items():
  55. gcode += ';Tool: %s -> ' % str(tool) + 'DeptPerCut: %s' % \
  56. str(val['data']["tools_drill_depthperpass"]) + '\n'
  57. gcode += '\n;Z_MOVE: \n'
  58. for tool, val in p['exc_tools'].items():
  59. gcode += ';Tool: %s -> ' % str(tool) + 'Z_Move: %s' % str(val['data']["tools_drill_travelz"]) + '\n'
  60. gcode += '\n'
  61. if p['toolchange'] is True:
  62. gcode += ';Z Toolchange: ' + str(p['z_toolchange']) + units + '\n'
  63. if coords_xy is not None:
  64. gcode += ';X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  65. p.decimals, coords_xy[1]) + units + '\n'
  66. else:
  67. gcode += ';X,Y Toolchange: ' + "None" + units + '\n'
  68. gcode += ';Z Start: ' + str(p['startz']) + units + '\n'
  69. gcode += ';Z End: ' + str(p['z_end']) + units + '\n'
  70. if end_coords_xy is not None:
  71. gcode += ';X,Y End: ' + "%.*f, %.*f" % (p.decimals, end_coords_xy[0],
  72. p.decimals, end_coords_xy[1]) + units + '\n'
  73. else:
  74. gcode += ';X,Y End: ' + "None" + units + '\n'
  75. gcode += ';Steps per circle: ' + str(p['steps_per_circle']) + '\n'
  76. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  77. gcode += ';Preprocessor Excellon: ' + str(p['pp_excellon_name']) + '\n' + '\n'
  78. else:
  79. gcode += ';Preprocessor Geometry: ' + str(p['pp_geometry_name']) + '\n' + '\n'
  80. gcode += ';X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + '\n'
  81. gcode += ';Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + '\n\n'
  82. gcode += ';Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  83. gcode += ('G20' if p.units.upper() == 'IN' else 'G21') + "\n"
  84. gcode += 'G90\n'
  85. gcode += 'G94\n'
  86. return gcode
  87. def startz_code(self, p):
  88. if p.startz is not None:
  89. return 'G0 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  90. else:
  91. return ''
  92. def lift_code(self, p):
  93. return 'G0 Z' + self.coordinate_format % (p.coords_decimals, p.z_move) + " " + self.feedrate_rapid_code(p)
  94. def down_code(self, p):
  95. return 'G1 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut) + " " + self.inline_z_feedrate_code(p)
  96. def toolchange_code(self, p):
  97. z_toolchange = p.z_toolchange
  98. toolchangexy = p.xy_toolchange
  99. f_plunge = p.f_plunge
  100. if toolchangexy is not None:
  101. x_toolchange = toolchangexy[0]
  102. y_toolchange = toolchangexy[1]
  103. else:
  104. x_toolchange = 0.0
  105. y_toolchange = 0.0
  106. no_drills = 1
  107. if int(p.tool) == 1 and p.startz is not None:
  108. z_toolchange = p.startz
  109. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  110. if str(p['options']['type']) == 'Excellon':
  111. for i in p['options']['Tools_in_use']:
  112. if i[0] == p.tool:
  113. no_drills = i[2]
  114. if toolchangexy is not None:
  115. gcode = """
  116. G0 Z{z_toolchange}
  117. G0 X{x_toolchange} Y{y_toolchange}
  118. M84
  119. @pause Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  120. G0 Z{z_toolchange}
  121. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  122. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  123. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  124. tool=int(p.tool),
  125. t_drills=no_drills,
  126. toolC=toolC_formatted)
  127. else:
  128. gcode = """
  129. G0 Z{z_toolchange}
  130. M84
  131. @pause Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  132. G0 Z{z_toolchange}
  133. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  134. tool=int(p.tool),
  135. t_drills=no_drills,
  136. toolC=toolC_formatted)
  137. if f_plunge is True:
  138. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  139. return gcode
  140. else:
  141. if toolchangexy is not None:
  142. gcode = """
  143. G0 Z{z_toolchange}
  144. G0 X{x_toolchange} Y{y_toolchange}
  145. M84
  146. @pause Change to tool T{tool} with Tool Dia = {toolC}
  147. G0 Z{z_toolchange}
  148. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  149. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  150. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  151. tool=int(p.tool),
  152. toolC=toolC_formatted)
  153. else:
  154. gcode = """
  155. G0 Z{z_toolchange}
  156. M84
  157. @pause Change to tool T{tool} with Tool Dia = {toolC}
  158. G0 Z{z_toolchange}
  159. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  160. tool=int(p.tool),
  161. toolC=toolC_formatted)
  162. if f_plunge is True:
  163. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  164. return gcode
  165. def up_to_zero_code(self, p):
  166. return 'G1 Z0' + " " + self.feedrate_code(p)
  167. def position_code(self, p):
  168. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  169. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  170. def rapid_code(self, p):
  171. return ('G0 ' + self.position_code(p)).format(**p) + " " + self.feedrate_rapid_code(p)
  172. def linear_code(self, p):
  173. return ('G1 ' + self.position_code(p)).format(**p) + " " + self.inline_feedrate_code(p)
  174. def end_code(self, p):
  175. coords_xy = p['xy_end']
  176. gcode = ('G0 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n")
  177. if coords_xy and coords_xy != '':
  178. gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n"
  179. return gcode
  180. def feedrate_code(self, p):
  181. return 'G1 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  182. def inline_feedrate_code(self, p):
  183. return 'F' + self.feedrate_format % (p.fr_decimals, p.feedrate)
  184. def inline_z_feedrate_code(self, p):
  185. return 'F' + self.feedrate_format % (p.fr_decimals, p.z_feedrate)
  186. def z_feedrate_code(self, p):
  187. return 'G1 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  188. def feedrate_rapid_code(self, p):
  189. return 'F' + self.feedrate_rapid_format % (p.fr_decimals, p.feedrate_rapid)
  190. def spindle_code(self, p):
  191. if p.spindlespeed:
  192. return 'M106 S%d' % p.spindlespeed
  193. else:
  194. return 'M106'
  195. def dwell_code(self, p):
  196. if p.dwelltime:
  197. return 'G4 P' + str(p.dwelltime)
  198. def spindle_stop_code(self, p):
  199. return 'M107'