Guest User

gcode layer sep by chatgpt

a guest
May 5th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. # Set the path to your G-code file
  2. gcode_file_path = 'path/to/gcode/file.gcode'
  3.  
  4. # Open the G-code file for reading
  5. with open(gcode_file_path, 'r') as gcode_file:
  6.     # Initialize variables for tracking layer information
  7.     layer_number = None
  8.     layer_lines = []
  9.  
  10.     # Loop through each line in the file
  11.     for line in gcode_file:
  12.         # Check if the line contains a layer number
  13.         if line.startswith(';LAYER:'):
  14.             # If we have previous layer data, save it to a file
  15.             if layer_number is not None:
  16.                 # Construct the output file name
  17.                 output_file_name = f'layer_{layer_number}.gcode'
  18.  
  19.                 # Open the output file for writing
  20.                 with open(output_file_name, 'w') as output_file:
  21.                     # Write the layer's G-code to the output file
  22.                     output_file.writelines(layer_lines)
  23.  
  24.                 # Clear the layer data for the next iteration
  25.                 layer_number = None
  26.                 layer_lines = []
  27.  
  28.             # Parse the layer number from the line
  29.             layer_number = int(line.split(':')[1])
  30.  
  31.         # Add the line to the current layer's data
  32.         layer_lines.append(line)
  33.  
  34.     # If there is still layer data remaining after the loop, save it to a file
  35.     if layer_number is not None:
  36.         # Construct the output file name
  37.         output_file_name = f'layer_{layer_number}.gcode'
  38.  
  39.         # Open the output file for writing
  40.         with open(output_file_name, 'w') as output_file:
  41.             # Write the layer's G-code to the output file
  42.             output_file.writelines(layer_lines)
  43.  
Add Comment
Please, Sign In to add comment