-
Notifications
You must be signed in to change notification settings - Fork 17
/
parameters.py
151 lines (121 loc) · 5.36 KB
/
parameters.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 19 19:52:29 2015
Contains all of the print parameters
@author: lvanhulle
"""
""" Version Numbers: Main_Version.sub-version.Feature_Added.Bug_Fixed """
__version__ = '0.3.4.0'
from collections import namedtuple
import itertools
import math
import os
import inspect
import constants as c
import doneshapes as ds
paramDict = {}
def makeParamObj(param_data, dropdown_data, layerParamLabels, partParamLabels):
for key, value in param_data.items():
paramDict[key] = value
currPath = os.path.dirname(os.path.realpath(__file__))
paramDict['startEndSubDirectory'] = currPath + '\\Start_End_Gcode'
for data in dropdown_data: #x in range(len(dropdown_data)):
the_label = data[c.THE_LABEL]
del data[c.THE_LABEL]
paramDict[the_label] = getattr(ds, paramDict[the_label])(**data)
return Parameters(layerParamLabels, partParamLabels)
class Parameters:
def __init__(self, layerParamLabels, partParamLabels):
self.layerParamLabels = layerParamLabels
self.partParamLabels = partParamLabels
self.globalParamLabels = []
nonGlobalLabels = set(layerParamLabels + partParamLabels)
for key, value in paramDict.items():
if key not in nonGlobalLabels:
setattr(self, key, value)
self.globalParamLabels.append(key)
self.globalParamLabels.sort()
self.partLists = [paramDict[label] for label in partParamLabels]
self.layerLists = [paramDict[label] for label in layerParamLabels]
self.layerParamGen = None
self.outline_gen = None
self.currHeight = 0
self.LayerParams = namedtuple('LayerParams', self.layerParamLabels)
self.PartParams = namedtuple('PartParams', self.partParamLabels)
self.GlobalParams = namedtuple('GlobalParams', self.globalParamLabels)
self.Params = namedtuple('Params', self.globalParamLabels +
self.partParamLabels +
self.layerParamLabels)
@property
def layerParams(self):
return self.LayerParams(*[self.__dict__[label] for label in self.layerParamLabels])
@property
def partParams(self):
return self.PartParams(*[self.__dict__[label] for label in self.partParamLabels])
@property
def globalParams(self):
return self.GlobalParams(*[self.__dict__[label] for label in self.globalParamLabels])
@property
def params(self):
return self.Params(*[self.__dict__[label] for label in self.Params._fields])
def parts(self):
partParamGen = zipVariables_gen(self.partLists)
partNum = 1
for nextPartParams in partParamGen:
self.currHeight = 0
self.layerParamGen = zipVariables_gen(self.layerLists, repeat=True)
for label, param in zip(self.partParamLabels, nextPartParams):
setattr(self, label, param)
yield self.layers()
partNum += 1
def layers(self):
self.outline_gen = self.outline()
next(self.outline_gen)
layerNum = 1
for nextLayerParam in self.layerParamGen:
for label, param in zip(self.layerParamLabels, nextLayerParam):
setattr(self, label, param)
yield self.regions(self.outline_gen.send(self.params))
layerNum += 1
def regions(self, regions):
global_params = self.params
try:
outlines, regionParams, self.currHeight = regions
except Exception:
outlines, regionParams = regions
self.currHeight += self.layerHeight
for outline, params in zip(outlines, regionParams):
self._updateAttributes(params)
yield outline
self._updateAttributes(global_params)
def _updateAttributes(self, namedTuple):
for key, value in namedTuple._asdict().items():
setattr(self, key, value)
def zipVariables_gen(inputLists, repeat=False):
if iter(inputLists) is iter(inputLists):
# Tests if inputLists is a generator
iterType = tuple
else:
iterType = type(inputLists)
variableGenerators = list(map(itertools.cycle, inputLists))
while True:
for _ in max(inputLists, key=len):
varList = list(map(next, variableGenerators))
try:
""" This is the logic for a namedtuple. """
yield iterType(*varList)
# except TypeError:
# """ If we get a TypeError then then we had a namedtuple but the fields
# are inforrect for some reason.
# """
# message = 'In parameters.py namedtuple ' + iterType.__name__ + ' could not be created.'
# if '' in inputLists:
# missing = iterType._fields[inputLists.index('')]
# message += ' Variable ' + missing + ' does not contain a value.'
# else:
# message += ' Check spelling of field names.'
# raise Exception(message)
except Exception:
yield iterType(varList)
if not repeat:
break