line_xyz.py 10 KB

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