Advertisement
Guest User

gcode layer sep and conversion to svg by chatgpt

a guest
May 5th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. import subprocess
  2.  
  3. # Set the path to your G-code file
  4. gcode_file_path = 'path/to/gcode/file.gcode'
  5.  
  6. # Set the path to the OpenSCAD script that generates the SVG
  7. openscad_script_path = 'path/to/openscad/script.scad'
  8.  
  9. # Open the G-code file for reading
  10. with open(gcode_file_path, 'r') as gcode_file:
  11.     # Initialize variables for tracking layer information
  12.     layer_number = None
  13.     layer_lines = []
  14.  
  15.     # Loop through each line in the file
  16.     for line in gcode_file:
  17.         # Check if the line contains a layer number
  18.         if line.startswith(';LAYER:'):
  19.             # If we have previous layer data, convert it to an SVG
  20.             if layer_number is not None:
  21.                 # Construct the output file name
  22.                 output_file_name = f'layer_{layer_number}.svg'
  23.  
  24.                 # Join the layer's G-code lines into a single string
  25.                 layer_code = ''.join(layer_lines)
  26.  
  27.                 # Create the OpenSCAD command to generate the SVG
  28.                 openscad_command = f'openscad -o {output_file_name} -D "gcode=\'{layer_code}\'" {openscad_script_path}'
  29.  
  30.                 # Run the OpenSCAD command
  31.                 subprocess.run(openscad_command, shell=True)
  32.  
  33.                 # Clear the layer data for the next iteration
  34.                 layer_number = None
  35.                 layer_lines = []
  36.  
  37.             # Parse the layer number from the line
  38.             layer_number = int(line.split(':')[1])
  39.  
  40.         # Add the line to the current layer's data
  41.         layer_lines.append(line)
  42.  
  43.     # If there is still layer data remaining after the loop, convert it to an SVG
  44.     if layer_number is not None:
  45.         # Construct the output file name
  46.         output_file_name = f'layer_{layer_number}.svg'
  47.  
  48.         # Join the layer's G-code lines into a single string
  49.         layer_code = ''.join(layer_lines)
  50.  
  51.         # Create the OpenSCAD command to generate the SVG
  52.         openscad_command = f'openscad -o {output_file_name} -D "gcode=\'{layer_code}\'" {openscad_script_path}'
  53.  
  54.         # Run the OpenSCAD command
  55.         subprocess.run(openscad_command, shell=True)
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement