-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathformula.py
131 lines (105 loc) · 4.88 KB
/
formula.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
from RFEM.initModel import Model, clearAttributes, deleteEmptyAttributes
from RFEM.enums import ObjectTypes, FormulaParameter
class Formula():
def __init__(self,
object_type = ObjectTypes.E_OBJECT_TYPE_NODE,
object_no: int = 1,
attribute: str = 'coordinate_1',
formula: str = '2*3',
model = Model):
'''
Args:
object_type (enum): Type of object for which parameters are searched where formulas are allowed
object_no (int): Object number
attribute (str): Attribute identificator
formula (str): Formula in text format
model (RFEM Class, optional): Model to be edited
'''
# Client model | Object Location
lc = model.clientModel.factory.create('ns0:object_location')
opl = model.clientModel.factory.create('ns0:object_parameter_location')
lstOfAttributes = self.GetListOfParametersFormulaAllowedFor(object_type, object_no, model)
# Check if attribute is valid
if attribute not in lstOfAttributes:
raise ValueError('WARNING: It is not allowed to insert a formula for attribute:"'+attribute+'.')
# Clears object attributes | Sets all attributes to None
clearAttributes(lc)
clearAttributes(opl)
lc.type = object_type.name
lc.no = object_no
lc.parent_no = 0
opl.attribute = attribute
# Delete None attributes for improved performance
deleteEmptyAttributes(lc)
deleteEmptyAttributes(opl)
# Add Formula to client model
model.clientModel.service.set_formula(lc, opl, formula)
@staticmethod
def Get(object_type = ObjectTypes.E_OBJECT_TYPE_NODE,
object_no: int = 1,
attribute: str = 'coordinate_1',
formula_param = FormulaParameter.FORMULA,
model = Model):
'''
Args:
object_type (enum): Type of object for which parameters are searched where formulas are allowed
object_no (int): Object number
attribute (str): Attribute identificator
formula_param (enum): Formula parameters. You can choose from ALL, FORMULA, IS_VALID, CALCULATED_VALUE.
model (RFEM Class, optional): Model to be edited
Returns:
Formula for given type, number and attribute according to chosen formula_param.
'''
# Client model | Object Location
lc = model.clientModel.factory.create('ns0:object_location')
opl = model.clientModel.factory.create('ns0:object_parameter_location')
lstOfAttributes = Formula.GetListOfParametersFormulaAllowedFor(object_type, object_no, model)
# Check if attribute is valid
if attribute not in lstOfAttributes:
raise ValueError('WARNING: For the attribute "'+attribute+'" the formula is not allowed.')
# Clears object attributes | Sets all attributes to None
clearAttributes(lc)
clearAttributes(opl)
lc.type = object_type.name
lc.no = object_no
lc.parent_no = 0
opl.attribute = attribute
formulaDict = model.clientModel.service.get_formula(lc, opl)
resultFormula = None
# Return specific parameter of formula
if formula_param == FormulaParameter.ALL:
resultFormula = formulaDict
elif formula_param == FormulaParameter.FORMULA:
resultFormula = formulaDict['formula']
elif formula_param == FormulaParameter.IS_VALID:
resultFormula = formulaDict['is_valid']
elif formula_param == FormulaParameter.CALCULATED_VALUE:
resultFormula = formulaDict['calculated_value']
else:
raise ValueError("WARNING: Chosen formula_param doesn't exist.")
return resultFormula
@staticmethod
def GetListOfParametersFormulaAllowedFor(
object_type = ObjectTypes.E_OBJECT_TYPE_NODE,
object_no: int = 1,
model = Model):
'''
Args:
object_type (enum): Type of object for which parameters are searched where formulas are allowed
object_no (int): Object number
model (RFEM Class, optional): Model to be edited
Returns:
attr_lst (list): List of attriburtes for which formulas are allowed
'''
# Client model | Object Location
clientObject = model.clientModel.factory.create('ns0:object_location')
# Clears object attributes | Sets all attributes to None
clearAttributes(clientObject)
clientObject.type = object_type.name
clientObject.no = object_no
clientObject.parent_no = 0
lst = model.clientModel.service.get_list_of_parameters_formula_allowed_for(clientObject)
attr_lst = []
for i in lst[0]:
attr_lst.append(i.attribute)
return attr_lst