Marlin.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Marlin(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 = ''
  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. gcode += ';Steps per circle: ' + str(p['steps_per_circle']) + '\n'
  77. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  78. gcode += ';Preprocessor Excellon: ' + str(p['pp_excellon_name']) + '\n' + '\n'
  79. else:
  80. gcode += ';Preprocessor Geometry: ' + str(p['pp_geometry_name']) + '\n' + '\n'
  81. gcode += ';X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + '\n'
  82. gcode += ';Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + '\n\n'
  83. gcode += ';Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  84. gcode += ('G20' if p.units.upper() == 'IN' else 'G21') + "\n"
  85. gcode += 'G90'
  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. M5
  117. G0 Z{z_toolchange}
  118. G0 X{x_toolchange} Y{y_toolchange}
  119. T{tool}
  120. M6
  121. ;MSG, Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  122. M0
  123. G0 Z{z_toolchange}
  124. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  125. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  126. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  127. tool=int(p.tool),
  128. t_drills=no_drills,
  129. toolC=toolC_formatted)
  130. else:
  131. gcode = """
  132. M5
  133. G0 Z{z_toolchange}
  134. T{tool}
  135. M6
  136. ;MSG, Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  137. M0
  138. G0 Z{z_toolchange}
  139. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  140. tool=int(p.tool),
  141. t_drills=no_drills,
  142. toolC=toolC_formatted)
  143. if f_plunge is True:
  144. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  145. return gcode
  146. else:
  147. if toolchangexy is not None:
  148. gcode = """
  149. M5
  150. G0 Z{z_toolchange}
  151. G0 X{x_toolchange} Y{y_toolchange}
  152. T{tool}
  153. M6
  154. ;MSG, Change to Tool Dia = {toolC}
  155. M0
  156. G0 Z{z_toolchange}
  157. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  158. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  159. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  160. tool=int(p.tool),
  161. toolC=toolC_formatted)
  162. else:
  163. gcode = """
  164. M5
  165. G0 Z{z_toolchange}
  166. T{tool}
  167. M6
  168. ;MSG, Change to Tool Dia = {toolC}
  169. M0
  170. G0 Z{z_toolchange}
  171. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  172. tool=int(p.tool),
  173. toolC=toolC_formatted)
  174. if f_plunge is True:
  175. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  176. return gcode
  177. def up_to_zero_code(self, p):
  178. return 'G1 Z0' + " " + self.feedrate_code(p)
  179. def position_code(self, p):
  180. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  181. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  182. def rapid_code(self, p):
  183. return ('G0 ' + self.position_code(p)).format(**p) + " " + self.feedrate_rapid_code(p)
  184. def linear_code(self, p):
  185. return ('G1 ' + self.position_code(p)).format(**p) + " " + self.inline_feedrate_code(p)
  186. def end_code(self, p):
  187. coords_xy = p['xy_end']
  188. gcode = ('G0 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n")
  189. if coords_xy and coords_xy != '':
  190. gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n"
  191. return gcode
  192. def feedrate_code(self, p):
  193. return 'G1 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  194. def inline_feedrate_code(self, p):
  195. return 'F' + self.feedrate_format % (p.fr_decimals, p.feedrate)
  196. def inline_z_feedrate_code(self, p):
  197. return 'F' + self.feedrate_format % (p.fr_decimals, p.z_feedrate)
  198. def z_feedrate_code(self, p):
  199. return 'G1 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  200. def feedrate_rapid_code(self, p):
  201. return 'F' + self.feedrate_rapid_format % (p.fr_decimals, p.feedrate_rapid)
  202. def spindle_code(self, p):
  203. sdir = {'CW': 'M3', 'CCW': 'M4'}[p.spindledir]
  204. if p.spindlespeed:
  205. return '%s S%s' % (sdir, str(p.spindlespeed))
  206. else:
  207. return sdir
  208. def dwell_code(self, p):
  209. gcode = 'G4 P' + str(p.dwelltime)
  210. if p.dwelltime:
  211. return gcode
  212. def spindle_stop_code(self, p):
  213. gcode = 'M400\n'
  214. gcode += 'M5'
  215. return gcode