-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_model.py
106 lines (95 loc) · 3.16 KB
/
pattern_model.py
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
from tkinter import *
from typing import Mapping
from PIL import ImageTk, Image
from patterns.pattern1.pattern import Pattern as Pattern1
from patterns.pattern2.pattern import Pattern as Pattern2
from patterns.pattern3.pattern import Pattern as Pattern3
from logger import log
class PatternModel:
def __init__(self, folderName: str):
self.folderName = folderName
self.attributes = self.parsePatternAttributes()
self.img = Image.open(folderName + "/image.png")
self.name = ""
self.id = self.attributes["id"]
if self.attributes["id"] == "NoID":
raise "Pattern " + self.name + " does not have an ID"
self.location = {}
self.x = 0.0
self.y = 0.0
self.rotation = 0.0
self.params = {}
for x in [i for i in self.attributes["params"].split(",") if i]:
self.params[x] = "0.0"
def getGcode(
self,
workHeight: float = 0,
freeMoveHeight: float = 1,
eFactor: float = 0,
eFactorStart: float = 0,
fFactor: float = 0,
overrunStart: float = 0,
overrunEnd: float = 0,
printOverrun: float = 0,
pause: float = 0,
cleaningX: float = None,
cleaningY: float = None,
):
values = {}
for key, val in self.params.items():
values[key] = float(val)
if self.folderName.endswith("pattern1"):
patternCalc = Pattern1
if self.folderName.endswith("pattern2"):
patternCalc = Pattern2
if self.folderName.endswith("pattern3"):
patternCalc = Pattern3
return patternCalc(
values,
workHeight,
freeMoveHeight,
eFactor,
eFactorStart,
fFactor,
overrunStart,
overrunEnd,
printOverrun,
self.x,
self.y,
self.rotation,
pause,
cleaningX,
cleaningY,
).gcode()
def setName(self, newName: str):
self.name = newName if newName else "NoName"
def setLocation(self, x: float, y: float):
self.x = x
self.y = y
def updateParams(self, mapping: Mapping):
self.params.update(mapping)
def print(self):
log("Pattern " + self.name)
log("Params: " + str(self.params))
log("Location" + self.getPosition())
log("Rotation: " + str(self.rotation))
def getPosition(self):
return str(self.x) + ", " + str(self.y)
def parsePatternAttributes(self):
pattern = open(self.folderName + "/pattern.py", "r")
mapping: Mapping = {}
mapping["id"] = "NoID"
mapping["author"] = "NoAuthor"
mapping["params"] = ""
for line in pattern:
if line.startswith("#"):
while line.startswith("#") or line.startswith(" "):
line = line[1:]
if line.endswith("\n"):
line = line[: len(line) - 1]
split = line.split("=")
if len(split) == 2:
mapping[split[0]] = split[1]
else:
break
return mapping