marlin.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. from FlatCAMPostProc import *
  2. class marlin(FlatCAMPostProc):
  3. coordinate_format = "%.*f"
  4. feedrate_format = '%.*f'
  5. feedrate_rapid_format = feedrate_format
  6. def start_code(self, p):
  7. units = ' ' + str(p['units']).lower()
  8. coords_xy = p['toolchange_xy']
  9. gcode = ''
  10. if str(p['options']['type']) == 'Geometry':
  11. gcode += ';TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + '\n' + '\n'
  12. gcode += ';Feedrate: ' + str(p['feedrate']) + units + '/min' + '\n'
  13. if str(p['options']['type']) == 'Geometry':
  14. gcode += ';Feedrate_Z: ' + str(p['feedrate_z']) + units + '/min' + '\n'
  15. gcode += ';Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + '\n' + '\n'
  16. gcode += ';Z_Cut: ' + str(p['z_cut']) + units + '\n'
  17. if str(p['options']['type']) == 'Geometry':
  18. if p['multidepth'] is True:
  19. gcode += ';DepthPerCut: ' + str(p['depthpercut']) + units + ' <=>' + \
  20. str(math.ceil(abs(p['z_cut']) / p['depthpercut'])) + ' passes' + '\n'
  21. gcode += ';Z_Move: ' + str(p['z_move']) + units + '\n'
  22. gcode += ';Z Toolchange: ' + str(p['toolchangez']) + units + '\n'
  23. if coords_xy is not None:
  24. gcode += ';X,Y Toolchange: ' + "%.4f, %.4f" % (coords_xy[0], coords_xy[1]) + units + '\n'
  25. else:
  26. gcode += ';X,Y Toolchange: ' + "None" + units + '\n'
  27. gcode += ';Z Start: ' + str(p['startz']) + units + '\n'
  28. gcode += ';Z End: ' + str(p['endz']) + units + '\n'
  29. gcode += ';Steps per circle: ' + str(p['steps_per_circle']) + '\n'
  30. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  31. gcode += ';Postprocessor Excellon: ' + str(p['pp_excellon_name']) + '\n'
  32. else:
  33. gcode += ';Postprocessor Geometry: ' + str(p['pp_geometry_name']) + '\n' + '\n'
  34. gcode += ';X min: ' + '%.*f' % (p.coords_decimals, p['options']['xmin']) + units + '\n'
  35. gcode += ';Y min: ' + '%.*f' % (p.coords_decimals, p['options']['ymin']) + units + '\n'
  36. gcode += ';X max: ' + '%.*f' % (p.coords_decimals, p['options']['xmax']) + units + '\n'
  37. gcode += ';Y max: ' + '%.*f' % (p.coords_decimals, p['options']['ymax']) + units + '\n\n'
  38. gcode += ';Spindle Speed: ' + str(p['spindlespeed']) + ' RPM' + '\n' + '\n'
  39. gcode += ('G20' if p.units.upper() == 'IN' else 'G21') + "\n"
  40. gcode += 'G90\n'
  41. return gcode
  42. def startz_code(self, p):
  43. if p.startz is not None:
  44. return 'G0 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  45. else:
  46. return ''
  47. def lift_code(self, p):
  48. return 'G0 Z' + self.coordinate_format%(p.coords_decimals, p.z_move) + " " + self.feedrate_rapid_code(p)
  49. def down_code(self, p):
  50. return 'G1 Z' + self.coordinate_format%(p.coords_decimals, p.z_cut) + " " + self.end_feedrate_code(p)
  51. def toolchange_code(self, p):
  52. toolchangez = p.toolchangez
  53. toolchangexy = p.toolchange_xy
  54. f_plunge = p.f_plunge
  55. gcode = ''
  56. if toolchangexy is not None:
  57. toolchangex = toolchangexy[0]
  58. toolchangey = toolchangexy[1]
  59. no_drills = 1
  60. if int(p.tool) == 1 and p.startz is not None:
  61. toolchangez = p.startz
  62. if p.units.upper() == 'MM':
  63. toolC_formatted = format(p.toolC, '.2f')
  64. else:
  65. toolC_formatted = format(p.toolC, '.4f')
  66. if str(p['options']['type']) == 'Excellon':
  67. for i in p['options']['Tools_in_use']:
  68. if i[0] == p.tool:
  69. no_drills = i[2]
  70. if toolchangexy is not None:
  71. gcode = """G0 Z{toolchangez}
  72. G0 X{toolchangex} Y{toolchangey}
  73. T{tool}
  74. M5
  75. M6
  76. (MSG, Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills})
  77. M0""".format(toolchangex=self.coordinate_format % (p.coords_decimals, toolchangex),
  78. toolchangey=self.coordinate_format % (p.coords_decimals, toolchangey),
  79. toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez),
  80. tool=int(p.tool),
  81. t_drills=no_drills,
  82. toolC=toolC_formatted)
  83. else:
  84. gcode = """G0 Z{toolchangez}
  85. T{tool}
  86. M5
  87. M6
  88. (MSG, Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills})
  89. M0""".format(toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez),
  90. tool=int(p.tool),
  91. t_drills=no_drills,
  92. toolC=toolC_formatted)
  93. if f_plunge is True:
  94. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  95. return gcode
  96. else:
  97. if toolchangexy is not None:
  98. gcode = """G0 Z{toolchangez}
  99. G0 X{toolchangex} Y{toolchangey}
  100. T{tool}
  101. M5
  102. M6
  103. (MSG, Change to Tool Dia = {toolC})
  104. M0""".format(toolchangex=self.coordinate_format % (p.coords_decimals, toolchangex),
  105. toolchangey=self.coordinate_format % (p.coords_decimals, toolchangey),
  106. toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez),
  107. tool=int(p.tool),
  108. toolC=toolC_formatted)
  109. else:
  110. gcode = """G0 Z{toolchangez}
  111. T{tool}
  112. M5
  113. M6
  114. (MSG, Change to Tool Dia = {toolC})
  115. M0""".format(toolchangez=self.coordinate_format%(p.coords_decimals, toolchangez),
  116. tool=int(p.tool),
  117. toolC=toolC_formatted)
  118. if f_plunge is True:
  119. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  120. return gcode
  121. def up_to_zero_code(self, p):
  122. return 'G1 Z0' + " " + self.feedrate_code(p)
  123. def position_code(self, p):
  124. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  125. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  126. def rapid_code(self, p):
  127. return ('G0 ' + self.position_code(p)).format(**p) + " " + self.feedrate_rapid_code(p)
  128. def linear_code(self, p):
  129. return ('G1 ' + self.position_code(p)).format(**p) + " " + self.end_feedrate_code(p)
  130. def end_code(self, p):
  131. coords_xy = p['toolchange_xy']
  132. gcode = ('G0 Z' + self.feedrate_format %(p.fr_decimals, p.endz) + " " + self.feedrate_rapid_code(p) + "\n")
  133. if coords_xy is not None:
  134. gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n"
  135. return gcode
  136. def feedrate_code(self, p):
  137. return 'G1 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate))
  138. def end_feedrate_code(self, p):
  139. return 'F' + self.feedrate_format %(p.fr_decimals, p.feedrate)
  140. def feedrate_z_code(self, p):
  141. return 'G1 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate_z))
  142. def feedrate_rapid_code(self, p):
  143. return 'F' + self.feedrate_rapid_format % (p.fr_decimals, p.feedrate_rapid)
  144. def spindle_code(self,p):
  145. if p.spindlespeed:
  146. return 'M3 S%d' % p.spindlespeed
  147. else:
  148. return 'M3'
  149. def dwell_code(self, p):
  150. if p.dwelltime:
  151. return 'G4 P' + str(p.dwelltime)
  152. def spindle_stop_code(self,p):
  153. return 'M5'