Skip to content

Instantly share code, notes, and snippets.

@jlewin
Last active October 22, 2019 18:25
Show Gist options
  • Save jlewin/a293aafa13e90c0984af298794bdd761 to your computer and use it in GitHub Desktop.
Save jlewin/a293aafa13e90c0984af298794bdd761 to your computer and use it in GitHub Desktop.
import os
from os import path
def import_settings(file_path):
with open(file_path) as gcode_file:
all_lines = gcode_file.readlines()
# Filter to comments with equals
comments = [comment for comment in all_lines if comment.startswith('; ') and len(comment.split('=')) == 2]
# Extract settings from gcode comments into dictionary
settings = {}
for comment in comments:
segments = [x.strip() for x in comment.replace(';', '').strip().split('=')]
settings[segments[0]] = segments[1]
return (settings, all_lines)
def import_mc_gcode():
# MatterControl GCode directory
directory = os.path.expandvars(r'%localappdata%\MatterControl\data\temp\gcode')
# Open output stream
csv_stream = open('c:/temp/some.csv', 'w')
# Write headers
csv_stream.write('Name,Layer Thickness, Bottom Infill Speed, Air Gap Speed\n')
# Loop over all files in gcode folder
for file_name in os.listdir(directory):
file_name = file_name.lower()
if file_name.endswith(".gcode"):
# Collect the name without extension
segments = os.path.splitext(file_name)
name_without_extension = segments[0]
print name_without_extension
full_path = os.path.join(directory, file_name)
# Extract settings
(settings, all_lines) = import_settings(full_path)
if len(settings) > 40:
# Create a line in the CSV for this file
csv_stream.write("%s,%s,%s,%s" % (file_name, settings['layerThickness'], settings['bottomInfillSpeed'], settings['airGapSpeed']))
temp_line = [l for l in all_lines if l.startswith("M109")][0]
segments = temp_line.split(' ')
if len(segments) == 2:
nozzle_temp = segments[1].replace('S', '')
else:
nozzle_temp = segments[2].replace('S', '')
csv_stream.write(',%s\n' % nozzle_temp.strip())
csv_stream.close()
# Kick off import on load
import_mc_gcode();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment