-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_psets.py
252 lines (225 loc) · 9.86 KB
/
ifc_psets.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# ***************************************************************************
# * *
# * Copyright (c) 2023 Yorik van Havre <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License (GPL) *
# * as published by the Free Software Foundation; either version 3 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""This NativeIFC module deals with properties and property sets"""
import re
import FreeCAD
import ifc_tools
def has_psets(obj):
"""Returns True if an object has attached psets"""
element = ifc_tools.get_ifc_element(obj)
psets = getattr(element, "IsDefinedBy", [])
if psets and [p for p in psets if p.is_a("IfcRelDefinesByProperties")]:
# TODO verify too if these psets are not already there
return True
psets = getattr(element, "HasProperties", [])
if psets:
return True
return False
def get_psets(element):
"""Returns a dictionary of dictionaries representing the
properties of an element in the form:
{ pset_name : { property_name : IfcType(value), ... }, ... }"""
result = {}
psets = getattr(element, "IsDefinedBy", [])
psets = [p for p in psets if p.is_a("IfcRelDefinesByProperties")]
psets = [p.RelatingPropertyDefinition for p in psets]
if not psets:
psets = getattr(element, "HasProperties", [])
for pset in psets:
pset_dict = {}
if pset.is_a("IfcPropertySet"):
for prop in pset.HasProperties:
pset_dict[prop.Name] = str(prop.NominalValue)
if pset.is_a("IfcMaterialProperties"):
for prop in pset.Properties:
pset_dict[prop.Name] = str(prop.NominalValue)
elif pset.is_a("IfcElementQuantity"):
# TODO implement quantities
pass
if pset_dict:
result[pset.Name] = pset_dict
return result
def get_pset(psetname, element):
"""Returns an IfcPropertySet with the given name"""
psets = getattr(element, "IsDefinedBy", [])
psets = [p for p in psets if p.is_a("IfcRelDefinesByProperties")]
for p in psets:
pset = p.RelatingPropertyDefinition
if pset.Name == psetname:
return pset
return None
def show_psets(obj):
"""Shows the psets attached to the given object as properties"""
element = ifc_tools.get_ifc_element(obj)
if not element:
return
psets = get_psets(element)
for gname, pset in psets.items():
for pname, pvalue in pset.items():
oname = pname
ptype, value = pvalue.split("(", 1)
value = value.strip(")")
value = value.strip("'")
pname = re.sub("[^0-9a-zA-Z]+", "", pname)
if pname[0].isdigit():
pname = "_" + pname
ttip = (
ptype + ":" + oname
) # setting IfcType:PropName as a tooltip to desambiguate
while pname in obj.PropertiesList:
# print("DEBUG: property", pname, "(", value, ") already exists in", obj.Label)
pname += "_"
if ptype in [
"IfcPositiveLengthMeasure",
"IfcLengthMeasure",
"IfcNonNegativeLengthMeasure",
]:
obj.addProperty("App::PropertyDistance", pname, gname, ttip)
elif ptype in ["IfcVolumeMeasure"]:
obj.addProperty("App::PropertyVolume", pname, gname, ttip)
elif ptype in ["IfcPositivePlaneAngleMeasure", "IfcPlaneAngleMeasure"]:
obj.addProperty("App::PropertyAngle", pname, gname, ttip)
value = float(value)
while value > 360:
value = value - 360
elif ptype in ["IfcMassMeasure"]:
obj.addProperty("App::PropertyMass", pname, gname, ttip)
elif ptype in ["IfcAreaMeasure"]:
obj.addProperty("App::PropertyArea", pname, gname, ttip)
elif ptype in ["IfcCountMeasure", "IfcInteger"]:
obj.addProperty("App::PropertyInteger", pname, gname, ttip)
value = int(value.strip("."))
elif ptype in ["IfcReal"]:
obj.addProperty("App::PropertyFloat", pname, gname, ttip)
value = float(value)
elif ptype in ["IfcBoolean", "IfcLogical"]:
obj.addProperty("App::PropertyBool", pname, gname, ttip)
if value in [".T."]:
value = True
else:
value = False
elif ptype in [
"IfcDateTime",
"IfcDate",
"IfcTime",
"IfcDuration",
"IfcTimeStamp",
]:
obj.addProperty("App::PropertyTime", pname, gname, ttip)
else:
obj.addProperty("App::PropertyString", pname, gname, ttip)
# print("DEBUG: setting",pname, ptype, value)
setattr(obj, pname, value)
def edit_pset(obj, prop, value=None):
"""Edits the corresponding property"""
pset = obj.getGroupOfProperty(prop)
ttip = obj.getDocumentationOfProperty(prop)
if value is None:
value = getattr(obj, prop)
ifcfile = ifc_tools.get_ifcfile(obj)
element = ifc_tools.get_ifc_element(obj)
pset_exist = get_psets(element)
if ttip.startswith("Ifc") and ":" in ttip:
target_prop = ttip.split(":", 1)[-1]
else:
# no tooltip set - try to build a name
prop = prop.rstrip("_")
prop_uncamel = re.sub(r"(\w)([A-Z])", r"\1 \2", prop)
prop_unslash = re.sub(r"(\w)([A-Z])", r"\1\/\2", prop)
target_prop = None
if pset in pset_exist:
if not target_prop:
if prop in pset_exist[pset]:
target_prop = prop
elif prop_uncamel in pset_exist[pset]:
target_prop = prop_uncamel
elif prop_unslash in pset_exist[pset]:
target_prop = prop_unslash
if target_prop:
value_exist = pset_exist[pset][target_prop].split("(", 1)[1][:-1].strip("'")
if value_exist in [".F.", ".U."]:
value_exist = False
elif value_exist in [".T."]:
value_exist = True
elif isinstance(value, int):
value_exist = int(value_exist.strip("."))
elif isinstance(value, float):
value_exist = float(value_exist)
elif isinstance(value, FreeCAD.Units.Quantity):
if value.Unit.Type == "Angle":
value_exist = float(value_exist)
while value_exist > 360:
value_exist = value_exist - 360
value_exist = FreeCAD.Units.Quantity(float(value_exist), value.Unit)
if value == value_exist:
return False
else:
FreeCAD.Console.PrintLog(
"IFC: property changed for "
+ obj.Label
+ " ("
+ str(obj.StepId)
+ ") : "
+ str(target_prop)
+ " : "
+ str(value)
+ " ("
+ str(type(value))
+ ") -> "
+ str(value_exist)
+ " ("
+ str(type(value_exist))
+ ")\n"
)
pset = get_pset(pset, element)
else:
pset = ifc_tools.api_run("pset.add_pset", ifcfile, product=element, name=pset)
if not target_prop:
target_prop = prop
ifc_tools.api_run(
"pset.edit_pset", ifcfile, pset=pset, properties={target_prop: value}
)
# TODO manage quantities
return True
def load_psets(obj):
"""Recursively loads psets of child objects"""
show_psets(obj)
if isinstance(obj, FreeCAD.DocumentObject):
group = obj.Group
else:
group = obj.Objects
for child in group:
load_psets(child)
def add_pset(obj, psetname):
"""Adds a pset with the given name to the given object"""
ifcfile = ifc_tools.get_ifcfile(obj)
element = ifc_tools.get_ifc_element(obj)
if ifcfile and element:
pset = ifc_tools.api_run(
"pset.add_pset", ifcfile, product=element, name=psetname
)
return pset
def add_property(ifcfile, pset, name, value=""):
"""Adds a property with the given name to the given pset. The type is deduced from
the value: string is IfcLabel, True/False is IfcBoolean, number is IfcLengthMeasure.
To force a certain type, value can also be an IFC element such as IfcLabel"""
ifc_tools.api_run("pset.edit_pset", ifcfile, pset=pset, properties={name: value})