marlin.py 7.3 KB

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