default.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Matthieu Berthomé #
  5. # Date: 5/26/2017 #
  6. # MIT Licence #
  7. # ##########################################################
  8. from FlatCAMPostProc import *
  9. class default(FlatCAMPostProc):
  10. include_header = True
  11. coordinate_format = "%.*f"
  12. feedrate_format = '%.*f'
  13. def start_code(self, p):
  14. units = ' ' + str(p['units']).lower()
  15. coords_xy = p['xy_toolchange']
  16. gcode = '(This preprocessor is the default preprocessor used by FlatCAM.)\n'
  17. gcode += '(It is made to work with MACH3 compatible motion controllers.)\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: ' + str(p['feedrate']) + units + '/min' + ')\n'
  25. if str(p['options']['type']) == 'Geometry':
  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 str(p['options']['type']) == 'Excellon':
  30. gcode += '\n(Tools Offset: )\n'
  31. for tool, val in p['exc_cnc_tools'].items():
  32. gcode += '(Tool: %s -> ' % str(val['tool']) + 'Dia: %s -> ' % str(tool) + \
  33. 'Offset Z: %s' % str(val['offset_z']) + ')\n'
  34. gcode += '\n'
  35. if p['multidepth'] is True:
  36. gcode += '(DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  37. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + ')\n'
  38. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  39. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  40. if coords_xy is not None:
  41. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  42. p.decimals, coords_xy[1]) + units + ')\n'
  43. else:
  44. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  45. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  46. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  47. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  48. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  49. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + '\n'
  50. else:
  51. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  52. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  53. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  54. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  55. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  56. gcode += 'G90\n'
  57. gcode += 'G94'
  58. return gcode
  59. def startz_code(self, p):
  60. if p.startz is not None:
  61. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  62. else:
  63. return ''
  64. def lift_code(self, p):
  65. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  66. def down_code(self, p):
  67. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  68. def toolchange_code(self, p):
  69. z_toolchange = p.z_toolchange
  70. toolchangexy = p.xy_toolchange
  71. f_plunge = p.f_plunge
  72. if toolchangexy is not None:
  73. x_toolchange = toolchangexy[0]
  74. y_toolchange = toolchangexy[1]
  75. else:
  76. x_toolchange = 0.0
  77. y_toolchange = 0.0
  78. no_drills = 1
  79. if int(p.tool) == 1 and p.startz is not None:
  80. z_toolchange = p.startz
  81. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  82. if str(p['options']['type']) == 'Excellon':
  83. for i in p['options']['Tools_in_use']:
  84. if i[0] == p.tool:
  85. no_drills = i[2]
  86. if toolchangexy is not None:
  87. gcode = """
  88. M5
  89. G00 Z{z_toolchange}
  90. T{tool}
  91. G00 X{x_toolchange} Y{y_toolchange}
  92. M6
  93. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  94. M0
  95. G00 Z{z_toolchange}
  96. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  97. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  98. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  99. tool=int(p.tool),
  100. t_drills=no_drills,
  101. toolC=toolC_formatted)
  102. else:
  103. gcode = """
  104. M5
  105. G00 Z{z_toolchange}
  106. T{tool}
  107. M6
  108. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  109. M0
  110. G00 Z{z_toolchange}
  111. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  112. tool=int(p.tool),
  113. t_drills=no_drills,
  114. toolC=toolC_formatted)
  115. if f_plunge is True:
  116. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  117. return gcode
  118. else:
  119. if toolchangexy is not None:
  120. gcode = """
  121. M5
  122. G00 Z{z_toolchange}
  123. G00 X{x_toolchange} Y{y_toolchange}
  124. T{tool}
  125. M6
  126. (MSG, Change to Tool Dia = {toolC})
  127. M0
  128. G00 Z{z_toolchange}
  129. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  130. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  131. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  132. tool=int(p.tool),
  133. toolC=toolC_formatted)
  134. else:
  135. gcode = """
  136. M5
  137. G00 Z{z_toolchange}
  138. T{tool}
  139. M6
  140. (MSG, Change to Tool Dia = {toolC})
  141. M0
  142. G00 Z{z_toolchange}
  143. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  144. tool=int(p.tool),
  145. toolC=toolC_formatted)
  146. if f_plunge is True:
  147. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  148. return gcode
  149. def up_to_zero_code(self, p):
  150. return 'G01 Z0'
  151. def position_code(self, p):
  152. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  153. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  154. def rapid_code(self, p):
  155. return ('G00 ' + self.position_code(p)).format(**p)
  156. def linear_code(self, p):
  157. return ('G01 ' + self.position_code(p)).format(**p)
  158. def end_code(self, p):
  159. coords_xy = p['xy_toolchange']
  160. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  161. if coords_xy is not None:
  162. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  163. return gcode
  164. def feedrate_code(self, p):
  165. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  166. def z_feedrate_code(self, p):
  167. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  168. def spindle_code(self, p):
  169. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  170. if p.spindlespeed:
  171. return '%s S%s' % (sdir, str(p.spindlespeed))
  172. else:
  173. return sdir
  174. def dwell_code(self, p):
  175. if p.dwelltime:
  176. return 'G4 P' + str(p.dwelltime)
  177. def spindle_stop_code(self, p):
  178. return 'M05'