-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcode_parse.py
More file actions
110 lines (92 loc) · 4.44 KB
/
gcode_parse.py
File metadata and controls
110 lines (92 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import re # for searching
import datetime # for time formatting
def run(fileName, short):
parameters = {"name": None,
"email": None,
"filename": None,
"time_taken": None,
"filament_used": {},
"project_type": None,
"date_added": None,
"rep_check": None,
"status": None,
"printer_type": None,
"path": None,
"order": ["name",
"email",
"filename",
"time_taken",
"filament_used",
"project_type",
"date_added",
"rep_check",
"status",
"printer_type",
"path"]
}
date = datetime.datetime.now()
parameters["date_added"] = str(date.strftime("%d/%m/%Y %H:%M:%S"))
# times in hh:mm:ss
# lengths in metres
# volumes in cm^3
# dates in dd/mm/yyyy
parameters["path"] = fileName
parameters["filename"] = short
increment = 0
with open(fileName, "r", encoding="utf8") as myfile: # open file that was handed to function
for line in myfile:
increment += 1
print(increment)
with open(fileName, "r", encoding="utf8") as myfile: # open file that was handed to function
for line in myfile: # Read gcode line by line checking for things
if parameters["printer_type"] and parameters["time_taken"] and len(parameters["filament_used"]) == 3:
break
if not parameters["printer_type"]:
if re.search("iForge Ultimaker", line): # this whole section could be cleaner
# if parameters["printer_type"]=="printer_type" and re.search("UltiG",line) : #this whole section could be cleaner
print("Ultimaker")
parameters["printer_type"] = "Ultimaker"
elif re.search("iForge PETG", line):
print("Prusa PETG")
parameters["printer_type"] = "Exotic_Prusa"
elif re.search("iForge TPU", line):
print("iForge TPU")
parameters["printer_type"] = "Exotic_Prusa"
elif re.search("iForge PLA", line):
print("Prusa PLA")
parameters["printer_type"] = "Exotic_Prusa"
elif re.search("iForge C1 PLA", line):
print("Prusa PLA")
parameters["printer_type"] = "Prusa"
if not parameters["time_taken"] and re.search("(normal mode)", line):
times = {'d': 0, 'h': 0, 'm': 0, 's': 0}
for elem in times.keys():
pattern = rf"\d+(?={elem}\s)"
tmp = re.findall(pattern, line)
if tmp:
times[elem] = int(tmp[0])
time = f"{times['d'] * 24 + times['h']:02d}:{times['m']:02d}:{times['s']:02d}" # Formatting to ensure two digits in minutes and seconds
parameters["time_taken"] = time
if len(parameters["filament_used"]) != 3 and re.search(r"(?<=; filament used \[)\w+(?=\])", line):
# some regex optimisation :)
# get mm/cm3/g string from line
key = re.findall(r"(?<=; filament used \[)\w+(?=\])", line)[0]
# get value from line
parameters["filament_used"][key] = float(re.findall(r"(?<=\s=\s)\d+\.*\d*", line)[0])
# if re.search("mm", line):
# print(12)
# search = list(map(float, re.findall('[-+]?\d*\.\d+|\d+', line)))
# print(search)
# length = round(search[0] / 1000, 2)
# print(length)
# parameters["filament_used"] = length
# volume = round(length * 3.1415 * (1.75 / 2) ** 2, 2)
# print(volume)
parameters["status"] = "Uploading"
error = False
if not (parameters["printer_type"] and parameters["time_taken"] and len(parameters["filament_used"]) == 3):
print("Malformed Gcode, please reslice. parameters:")
print(parameters)
parameters["printer_type"] = "non_iforge"
error = True
return parameters, error