default.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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']) == 'Geometry':
  30. if p['multidepth'] is True:
  31. gcode += '(DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  32. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + ')\n'
  33. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  34. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  35. if coords_xy is not None:
  36. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  37. p.decimals, coords_xy[1]) + units + ')\n'
  38. else:
  39. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  40. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  41. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  42. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  43. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  44. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + '\n'
  45. else:
  46. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  47. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  48. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  49. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  50. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  51. gcode += 'G90\n'
  52. gcode += 'G94'
  53. return gcode
  54. def startz_code(self, p):
  55. if p.startz is not None:
  56. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  57. else:
  58. return ''
  59. def lift_code(self, p):
  60. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  61. def down_code(self, p):
  62. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  63. def toolchange_code(self, p):
  64. z_toolchange = p.z_toolchange
  65. toolchangexy = p.xy_toolchange
  66. f_plunge = p.f_plunge
  67. if toolchangexy is not None:
  68. x_toolchange = toolchangexy[0]
  69. y_toolchange = toolchangexy[1]
  70. else:
  71. x_toolchange = 0.0
  72. y_toolchange = 0.0
  73. no_drills = 1
  74. if int(p.tool) == 1 and p.startz is not None:
  75. z_toolchange = p.startz
  76. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  77. if str(p['options']['type']) == 'Excellon':
  78. for i in p['options']['Tools_in_use']:
  79. if i[0] == p.tool:
  80. no_drills = i[2]
  81. if toolchangexy is not None:
  82. gcode = """
  83. M5
  84. G00 Z{z_toolchange}
  85. T{tool}
  86. G00 X{x_toolchange} Y{y_toolchange}
  87. M6
  88. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  89. M0
  90. G00 Z{z_toolchange}
  91. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  92. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  93. 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. else:
  98. gcode = """
  99. M5
  100. G00 Z{z_toolchange}
  101. T{tool}
  102. M6
  103. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  104. M0
  105. G00 Z{z_toolchange}
  106. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  107. tool=int(p.tool),
  108. t_drills=no_drills,
  109. toolC=toolC_formatted)
  110. if f_plunge is True:
  111. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  112. return gcode
  113. else:
  114. if toolchangexy is not None:
  115. gcode = """
  116. M5
  117. G00 Z{z_toolchange}
  118. G00 X{x_toolchange} Y{y_toolchange}
  119. T{tool}
  120. M6
  121. (MSG, Change to Tool Dia = {toolC})
  122. M0
  123. G00 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. toolC=toolC_formatted)
  129. else:
  130. gcode = """
  131. M5
  132. G00 Z{z_toolchange}
  133. T{tool}
  134. M6
  135. (MSG, Change to Tool Dia = {toolC})
  136. M0
  137. G00 Z{z_toolchange}
  138. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  139. tool=int(p.tool),
  140. toolC=toolC_formatted)
  141. if f_plunge is True:
  142. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  143. return gcode
  144. def up_to_zero_code(self, p):
  145. return 'G01 Z0'
  146. def position_code(self, p):
  147. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  148. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  149. def rapid_code(self, p):
  150. return ('G00 ' + self.position_code(p)).format(**p)
  151. def linear_code(self, p):
  152. return ('G01 ' + self.position_code(p)).format(**p)
  153. def end_code(self, p):
  154. coords_xy = p['xy_toolchange']
  155. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  156. if coords_xy is not None:
  157. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  158. return gcode
  159. def feedrate_code(self, p):
  160. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  161. def z_feedrate_code(self, p):
  162. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  163. def spindle_code(self, p):
  164. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  165. if p.spindlespeed:
  166. return '%s S%s' % (sdir, str(p.spindlespeed))
  167. else:
  168. return sdir
  169. def dwell_code(self, p):
  170. if p.dwelltime:
  171. return 'G4 P' + str(p.dwelltime)
  172. def spindle_stop_code(self, p):
  173. return 'M05'