Repetier.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. gcode = ';This preprocessor is used with a motion controller loaded with REPETIER firmware.\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["tooldia"]) + '\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' if p.units.upper() == 'IN' else 'G21') + "\n"
  76. gcode += 'G90\n'
  77. gcode += 'G94\n'
  78. return gcode
  79. def startz_code(self, p):
  80. if p.startz is not None:
  81. return 'G0 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  82. else:
  83. return ''
  84. def lift_code(self, p):
  85. return 'G0 Z' + self.coordinate_format % (p.coords_decimals, p.z_move) + " " + self.feedrate_rapid_code(p)
  86. def down_code(self, p):
  87. return 'G1 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut) + " " + self.inline_z_feedrate_code(p)
  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. G0 Z{z_toolchange}
  109. G0 X{x_toolchange} Y{y_toolchange}
  110. M84
  111. @pause Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  112. G0 Z{z_toolchange}
  113. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  114. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  115. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  116. tool=int(p.tool),
  117. t_drills=no_drills,
  118. toolC=toolC_formatted)
  119. else:
  120. gcode = """
  121. G0 Z{z_toolchange}
  122. M84
  123. @pause Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  124. G0 Z{z_toolchange}
  125. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  126. tool=int(p.tool),
  127. t_drills=no_drills,
  128. toolC=toolC_formatted)
  129. if f_plunge is True:
  130. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  131. return gcode
  132. else:
  133. if toolchangexy is not None:
  134. gcode = """
  135. G0 Z{z_toolchange}
  136. G0 X{x_toolchange} Y{y_toolchange}
  137. M84
  138. @pause Change to tool T{tool} with Tool Dia = {toolC}
  139. G0 Z{z_toolchange}
  140. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  141. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  142. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  143. tool=int(p.tool),
  144. toolC=toolC_formatted)
  145. else:
  146. gcode = """
  147. G0 Z{z_toolchange}
  148. M84
  149. @pause Change to tool T{tool} with Tool Dia = {toolC}
  150. G0 Z{z_toolchange}
  151. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  152. tool=int(p.tool),
  153. toolC=toolC_formatted)
  154. if f_plunge is True:
  155. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  156. return gcode
  157. def up_to_zero_code(self, p):
  158. return 'G1 Z0' + " " + self.feedrate_code(p)
  159. def position_code(self, p):
  160. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  161. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  162. def rapid_code(self, p):
  163. return ('G0 ' + self.position_code(p)).format(**p) + " " + self.feedrate_rapid_code(p)
  164. def linear_code(self, p):
  165. return ('G1 ' + self.position_code(p)).format(**p) + " " + self.inline_feedrate_code(p)
  166. def end_code(self, p):
  167. coords_xy = p['xy_end']
  168. gcode = ('G0 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n")
  169. if coords_xy and coords_xy != '':
  170. gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n"
  171. return gcode
  172. def feedrate_code(self, p):
  173. return 'G1 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  174. def inline_feedrate_code(self, p):
  175. return 'F' + self.feedrate_format % (p.fr_decimals, p.feedrate)
  176. def inline_z_feedrate_code(self, p):
  177. return 'F' + self.feedrate_format % (p.fr_decimals, p.z_feedrate)
  178. def z_feedrate_code(self, p):
  179. return 'G1 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  180. def feedrate_rapid_code(self, p):
  181. return 'F' + self.feedrate_rapid_format % (p.fr_decimals, p.feedrate_rapid)
  182. def spindle_code(self, p):
  183. if p.spindlespeed:
  184. return 'M106 S%d' % p.spindlespeed
  185. else:
  186. return 'M106'
  187. def dwell_code(self, p):
  188. if p.dwelltime:
  189. return 'G4 P' + str(p.dwelltime)
  190. def spindle_stop_code(self, p):
  191. return 'M107'