Created
October 23, 2019 23:28
-
-
Save jlewin/01dd311dcbf2fea9afbb5626c554bec6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(directory, csv_stream): | |
# 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()) | |
some_directories = [os.path.expandvars(r'%localappdata%\MatterControl\data\temp\gcode'), os.path.expandvars('some_other_path')] | |
# 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 directories, writing each to active csv stream | |
for directory in some_directories: | |
# Kick off import on load | |
import_mc_gcode(directory, csv_stream) | |
csv_stream.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment