-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFC-checker.py
294 lines (243 loc) · 10.5 KB
/
IFC-checker.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import ifcopenshell
def clean_ifc(ifc_file_path, printout=False):
"""
Bereinigt ein IFC, indem es nur eine Instanz pro Typ zulässt. alle Psets und Attribute bleiben dem Typ zugeordnet.
Parameters:
ifc_file_path (str): Pfad zum IFC-file
printout (bool): Debug Informationen ausgeben oder nicht
Returns:
ifcopenshell.file: neues IFC model welches nur eine Instanz von jedem Typ hat.
"""
try:
ifc_model = ifcopenshell.open(ifc_file_path)
new_ifc_model = ifcopenshell.file()
# header
for entity in ifc_model.by_type('IfcProject'):
new_ifc_model.createIfcProject(
entity.GlobalId, entity.OwnerHistory, entity.Name, entity.Description,
entity.ObjectType, entity.LongName, entity.Phase
)
# Typen (anschiessend alle)
types_to_keep = [
'IfcWall', 'IfcDoor', 'IfcWindow', 'IfcSlab', 'IfcColumn', 'IfcBeam',
'IfcRoof', 'IfcStair', 'IfcRamp', 'IfcSpace', 'IfcZone', 'IfcCovering'
]
added_types = {type_name: False for type_name in types_to_keep}
for type_name in types_to_keep:
instances = ifc_model.by_type(type_name)
if instances:
instance = instances[0]
new_instance = new_ifc_model.add(instance)
added_types[type_name] = True
# alle attribute der zu behaltenden Instanz hinzugügen.
for attribute in instance.__dict__:
setattr(new_instance, attribute, getattr(instance, attribute))
for pset in ifcopenshell.util.element.get_psets(instance):
ifcopenshell.util.element.add_pset(new_ifc_model, new_instance, pset)
if printout:
print(f"IFC bereinigt, entahltene typen: {list(added_types.keys())}")
return new_ifc_model
except FileNotFoundError:
print(f"File not found: {ifc_file_path}")
return ifcopenshell.file()
except Exception as e:
print(f"Error cleaning IFC file: {e}")
return ifcopenshell.file()
def get_element_types(ifc_path, printout=False) -> list:
"""
Listet alle elemelnt typen in IFC auf.
Parameters:
ifc_file_path (str): Pfad zum IFC-file
printout (bool): Debug Informationen ausgeben oder nicht
Returns:
list of element types.
"""
try:
ifc_file = ifcopenshell.open(ifc_path)
element_types = set()
for entity in ifc_file:
element_types.add(entity.is_a())
if printout:
print(f"Element types: {element_types}")
return list(element_types)
except FileNotFoundError:
print(f"File not found: {ifc_path}")
return []
except Exception as e:
print(f"Error parsing IFC file: {e}")
return []
def get_psets_for_entity(ifc_path, entity_type, printout=False):
"""
Listet alle Psets für einen entity-typen in IFC auf.
Parameters:
ifc_file_path (str): Pfad zum IFC-file
entity_type (str): IFC entity typ, (z.B. IfcSlab, IfcWall)
printout (bool): Debug Informationen ausgeben oder nicht
Returns:
list of element types.
"""
try:
ifc_file = ifcopenshell.open(ifc_path)
psets = set()
for entity in ifc_file.by_type(entity_type):
if hasattr(entity, 'IsDefinedBy'):
for definition in entity.IsDefinedBy:
if definition.is_a('IfcRelDefinesByProperties'):
psets.add(definition.RelatingPropertyDefinition.Name)
if printout:
print(f"Psets for {entity_type}: {psets}")
return list(psets)
except FileNotFoundError:
print(f"File not found: {ifc_path}")
return []
except Exception as e:
print(f"Error retrieving psets for {entity_type}: {e}")
return []
def get_properties_in_pset(ifc_path, pset_name, printout=False):
"""
Liste von Attributen in einem PSET.
Parameters:
ifc_file_path (str): Pfad zum IFC-file
pset_name (str): PSET
printout (bool): Debug Informationen ausgeben oder nicht
Returns:
list der properties des PSETS
"""
try:
ifc_file = ifcopenshell.open(ifc_path)
for entity in ifc_file.by_type('IfcPropertySet'):
if entity.Name == pset_name:
properties = [prop.Name for prop in entity.HasProperties]
if printout:
print(f"Properties in {pset_name}: {properties}")
return properties
return []
except FileNotFoundError:
print(f"File not found: {ifc_path}")
return []
except Exception as e:
print(f"Error retrieving properties in pset {pset_name}: {e}")
return []
def get_property_value(ifc_path, entity_type, pset_name, property_name, printout=False):
"""
Eigenschaft abrufen.
Parameters:
ifc_file_path (str): Pfad zum IFC-file
entity_type (str): IFC entity typ, (z.B. IfcSlab, IfcWall)
pset_name (str): PSET
property_name (str): Name des Property.
printout (bool): Debug Informationen ausgeben oder nicht
Returns:
Any: den Wert der Property.
"""
try:
ifc_file = ifcopenshell.open(ifc_path)
for entity in ifc_file.by_type(entity_type):
if hasattr(entity, 'IsDefinedBy'):
for definition in entity.IsDefinedBy:
if definition.is_a('IfcRelDefinesByProperties'):
pset = definition.RelatingPropertyDefinition
if pset.Name == pset_name:
for prop in pset.HasProperties:
if prop.Name == property_name:
if printout:
print(f"Value of {property_name} in {pset_name} "
f"for {entity_type}: {prop.NominalValue}")
return prop.NominalValue
raise ValueError(f"Property '{property_name}' not found in pset '{pset_name}' "
f"for entity type '{entity_type}'")
except FileNotFoundError:
print(f"File not found: {ifc_path}")
raise ValueError(f"File not found: {ifc_path}")
except Exception as e:
print(f"Error retrieving property value: {e}")
raise ValueError(f"Error retrieving property value: {e}")
def compare_ifcs(ifc_path1, ifc_path2, printout=False):
"""
Zwei IFC's vergleichen und einen similiarity-score in Prozent berechnen.
Parameters:
ifc_path1 (str): Pfad zum ersten IFC-file.
ifc_path2 (str): Pfad zum zweiten IFC-file
printout (bool): Debug Informationen ausgeben oder nicht
Returns:
dict: Anzahl Abfragen (int), Anzahl Matches(int), similarity score in %.
"""
element_types = get_element_types(ifc_path1, printout)
all_psets = {}
for entity_type in element_types:
psets = get_psets_for_entity(ifc_path1, entity_type, printout)
all_psets[entity_type] = psets
all_properties = {}
for entity_type, psets in all_psets.items():
for pset in psets:
properties = get_properties_in_pset(ifc_path1, pset, printout)
all_properties[(entity_type, pset)] = properties
all_values_ifc1 = []
for (entity_type, pset), properties in all_properties.items():
for prop in properties:
try:
value = get_property_value(ifc_path1, entity_type, pset, prop, printout)
all_values_ifc1.append(value)
except ValueError as e:
if printout:
print(e)
request_count = 0
matched_count = 0
for (entity_type, pset), properties in all_properties.items():
for prop in properties:
try:
value_ifc1 = get_property_value(ifc_path1, entity_type, pset, prop, printout)
value_ifc2 = get_property_value(ifc_path2, entity_type, pset, prop, printout)
request_count += 1
if isinstance(value_ifc2, type(value_ifc1)):
matched_count += 1
except ValueError:
continue
similarity_score = (matched_count / request_count) * 100 if request_count > 0 else 0
if printout:
print(f"Requests made: {request_count}")
print(f"Requests matched: {matched_count}")
print(f"Similarity score: {similarity_score:.2f}%")
return {
"requests_made": request_count,
"requests_matched": matched_count,
"similarity_score": similarity_score
}
def load_ifc(file_path):
""" Lädt eine IFC-Datei und gibt das Modell zurück. """
return ifcopenshell.open(file_path)
def extract_structure(ifc_model):
""" Extrahiert relevante Strukturen und Daten aus einem IFC-Modell. """
elements = {}
for element in ifc_model.by_type('IfcProduct'):
element_type = element.is_a()
if element_type not in elements:
elements[element_type] = []
elements[element_type].append(element)
return elements
def compare_ifc_models(model1, model2):
""" Vergleicht zwei IFC-Modelle auf ihre Ähnlichkeit basierend auf ihrer Struktur. """
structure1 = extract_structure(model1)
structure2 = extract_structure(model2)
# Ähnlichkeit basierend auf der Anzahl der gleichen Typen
score = 0
total_types = set(structure1.keys()).union(set(structure2.keys()))
for type_name in total_types:
if type_name in structure1 and type_name in structure2:
score += min(len(structure1[type_name]), len(structure2[type_name]))
elif type_name in structure1 or type_name in structure2:
score -= 1
max_score = sum(max(len(structure1.get(t, [])), len(structure2.get(t, []))) for t in total_types)
similarity = score / max_score if max_score > 0 else 0
return similarity
def main():
ifc_file1 = 'pathfirst.ifc'
ifc_file2 = 'pathsecond.ifc'
model1 = load_ifc(ifc_file1)
model2 = load_ifc(ifc_file2)
similarity_score = compare_ifc_models(model1, model2)
print(f"Ähnlichkeit: {similarity_score:.2f}")
result = compare_ifcs(ifc_file1, ifc_file2, printout=True)
print(result)
if __name__ == "__main__":
main()