line_xyz.py 8.3 KB

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