line_xyz.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 line_xyz(PreProc):
  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 = ''
  17. xmin = '%.*f' % (p.coords_decimals, p['options']['xmin'])
  18. xmax = '%.*f' % (p.coords_decimals, p['options']['xmax'])
  19. ymin = '%.*f' % (p.coords_decimals, p['options']['ymin'])
  20. ymax = '%.*f' % (p.coords_decimals, p['options']['ymax'])
  21. if str(p['options']['type']) == 'Geometry':
  22. gcode += '(TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + ')\n'
  23. gcode += '(Feedrate_XY: ' + str(p['feedrate']) + units + '/min' + ')\n'
  24. gcode += '(Feedrate_Z: ' + str(p['z_feedrate']) + units + '/min' + ')\n'
  25. gcode += '(Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + ')\n' + '\n'
  26. gcode += '(Z_Cut: ' + str(p['z_cut']) + units + ')\n'
  27. if p['multidepth'] is True:
  28. gcode += '(DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  29. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + ')\n'
  30. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  31. elif str(p['options']['type']) == 'Excellon' and p['use_ui'] is True:
  32. gcode += '\n(TOOLS DIAMETER: )\n'
  33. for tool, val in p['exc_tools'].items():
  34. gcode += '(Tool: %s -> ' % str(tool) + 'Dia: %s' % str(val["C"]) + ')\n'
  35. gcode += '\n(FEEDRATE Z: )\n'
  36. for tool, val in p['exc_tools'].items():
  37. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate: %s' % str(val['data']["feedrate_z"]) + ')\n'
  38. gcode += '\n(FEEDRATE RAPIDS: )\n'
  39. for tool, val in p['exc_tools'].items():
  40. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate Rapids: %s' % \
  41. str(val['data']["feedrate_rapid"]) + ')\n'
  42. gcode += '\n(Z_CUT: )\n'
  43. for tool, val in p['exc_tools'].items():
  44. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Cut: %s' % str(val['data']["cutz"]) + ')\n'
  45. gcode += '\n(Tools Offset: )\n'
  46. for tool, val in p['exc_cnc_tools'].items():
  47. gcode += '(Tool: %s -> ' % str(val['tool']) + 'Offset Z: %s' % str(val['offset_z']) + ')\n'
  48. if p['multidepth'] is True:
  49. gcode += '\n(DEPTH_PER_CUT: )\n'
  50. for tool, val in p['exc_tools'].items():
  51. gcode += '(Tool: %s -> ' % str(tool) + 'DeptPerCut: %s' % \
  52. str(val['data']["depthperpass"]) + ')\n'
  53. gcode += '\n(Z_MOVE: )\n'
  54. for tool, val in p['exc_tools'].items():
  55. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Move: %s' % str(val['data']["travelz"]) + ')\n'
  56. gcode += '\n'
  57. if p['toolchange'] is True:
  58. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  59. if coords_xy is not None:
  60. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  61. p.decimals, coords_xy[1]) + units + ')\n'
  62. else:
  63. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  64. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  65. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  66. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  67. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  68. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + '\n'
  69. else:
  70. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  71. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  72. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  73. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  74. gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n')
  75. gcode += 'G90\n'
  76. gcode += 'G94\n'
  77. return gcode
  78. def startz_code(self, p):
  79. if p.startz is not None:
  80. g = 'G00 ' + 'X' + self.coordinate_format % (p.coords_decimals, p.x) + \
  81. ' Y' + self.coordinate_format % (p.coords_decimals, p.y) + \
  82. ' Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  83. return g
  84. else:
  85. return ''
  86. def lift_code(self, p):
  87. g = 'G00 ' + 'X' + self.coordinate_format % (p.coords_decimals, p.x) + \
  88. ' Y' + self.coordinate_format % (p.coords_decimals, p.y) + \
  89. ' Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  90. return g
  91. def down_code(self, p):
  92. g = 'G01 ' + 'X' + self.coordinate_format % (p.coords_decimals, p.x) + \
  93. ' Y' + self.coordinate_format % (p.coords_decimals, p.y) + \
  94. ' Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  95. return g
  96. def toolchange_code(self, p):
  97. z_toolchange = p.z_toolchange
  98. xy_toolchange = p.xy_toolchange
  99. f_plunge = p.f_plunge
  100. if xy_toolchange is not None:
  101. x_toolchange = xy_toolchange[0]
  102. y_toolchange = xy_toolchange[1]
  103. else:
  104. if str(p['options']['type']) == 'Excellon':
  105. x_toolchange = p.oldx
  106. y_toolchange = p.oldy
  107. else:
  108. x_toolchange = p.x
  109. y_toolchange = p.y
  110. no_drills = 1
  111. if int(p.tool) == 1 and p.startz is not None:
  112. z_toolchange = p.startz
  113. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  114. if str(p['options']['type']) == 'Excellon':
  115. for i in p['options']['Tools_in_use']:
  116. if i[0] == p.tool:
  117. no_drills = i[2]
  118. gcode = """
  119. M5
  120. G00 X{x_toolchange} Y{x_toolchange} Z{z_toolchange}
  121. T{tool}
  122. M6
  123. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  124. M0""".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. if f_plunge is True:
  131. gcode += """\nG00 X{x_toolchange} Y{x_toolchange} Z{z_move}""".format(
  132. x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  133. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  134. z_move=self.coordinate_format % (p.coords_decimals, p.z_move))
  135. return gcode
  136. else:
  137. gcode = """
  138. M5
  139. G00 X{x_toolchange} Y{x_toolchange} Z{z_toolchange}
  140. T{tool}
  141. M6
  142. (MSG, Change to Tool Dia = {toolC})
  143. M0""".format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  144. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  145. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  146. tool=int(p.tool),
  147. toolC=toolC_formatted)
  148. if f_plunge is True:
  149. gcode += """\nG00 X{x_toolchange} Y{x_toolchange} Z{z_move}""".format(
  150. x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  151. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  152. z_move=self.coordinate_format % (p.coords_decimals, p.z_move))
  153. return gcode
  154. def up_to_zero_code(self, p):
  155. g = 'G01 ' + 'X' + self.coordinate_format % (p.coords_decimals, p.x) + \
  156. ' Y' + self.coordinate_format % (p.coords_decimals, p.y) + \
  157. ' Z0'
  158. return g
  159. def position_code(self, p):
  160. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  161. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  162. def rapid_code(self, p):
  163. g = ('G00 ' + self.position_code(p)).format(**p)
  164. g += ' Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  165. return g
  166. def linear_code(self, p):
  167. g = ('G01 ' + self.position_code(p)).format(**p)
  168. g += ' Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  169. return g
  170. def end_code(self, p):
  171. coords_xy = p['xy_end']
  172. if coords_xy and coords_xy != '':
  173. g = 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  174. else:
  175. g = ('G00 ' + self.position_code(p)).format(**p)
  176. g += ' Z' + self.coordinate_format % (p.coords_decimals, p.z_end)
  177. return g
  178. def feedrate_code(self, p):
  179. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  180. def z_feedrate_code(self, p):
  181. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  182. def spindle_code(self, p):
  183. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  184. if p.spindlespeed:
  185. return '%s S%s' % (sdir, str(p.spindlespeed))
  186. else:
  187. return sdir
  188. def dwell_code(self, p):
  189. if p.dwelltime:
  190. return 'G4 P' + str(p.dwelltime)
  191. def spindle_stop_code(self, p):
  192. return 'M05'