line_xyz.py 8.3 KB

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