-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertToClassification.py
More file actions
124 lines (110 loc) · 5.37 KB
/
ConvertToClassification.py
File metadata and controls
124 lines (110 loc) · 5.37 KB
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
import ifcopenshell
import ifcopenshell.util.date
def convert_to_classification(file, model, pSetName, delete_psets):
try:
version = model.schema
owner_history = model.by_type("IfcOwnerHistory")[0]
project = model.by_type("IfcProject")[0]
# Get all IfcPropertySet objects
property_sets = model.by_type("IfcPropertySet")
property_sets_classification = []
removable = set()
# find all IfcPropertySets with the given names
for property_set in property_sets:
if property_set.Name in pSetName:
property_sets_classification.append(property_set)
# for each IfcPropertySet create a new IfcClassification and IfcClassificationReference
for property_set in property_sets_classification:
entities = []
catalogAttr = {}
classAttr = {}
relDefinesByProperties = []
# get all related entities
if version == "IFC2X3":
relDefinesByProperties = property_set.PropertyDefinitionOf
else:
relDefinesByProperties = property_set.DefinesOccurrence
for relDefinesByProperty in relDefinesByProperties:
objects = relDefinesByProperty.RelatedObjects
for obj in objects:
entities.append(obj)
removable.add(relDefinesByProperty)
# get all properties of the IfcPropertySet
properties = property_set.HasProperties
for prop in properties:
if prop.is_a("IfcPropertySingleValue"):
if prop.Name=="ClassificationReferenceToken" or (prop.Name.startswith("Classification") and not prop.Name.startswith("ClassificationReference")):
catName = prop.Name.replace("Classification", "")
catalogAttr[catName] = prop.NominalValue.wrappedValue
elif prop.Name.startswith("ClassificationReference"):
className = prop.Name.replace("ClassificationReference", "")
classAttr[className] = prop.NominalValue.wrappedValue
else:
print("Unknown property: ", prop.Name)
removable.add(prop)
removable.add(property_set)
# create IfcClassification and IfcClassificationReference
if version == "IFC2X3":
dates = ifcopenshell.util.date.datetime2ifc(catalogAttr.get('EditionDate', None), 'IfcCalendarDate')
caledarDate = model.createIfcCalendarDate(dates['DayComponent'], dates['MonthComponent'], dates['YearComponent'])
classification = model.createIfcClassification(
catalogAttr.get('Source', None),
catalogAttr.get('Edition', None),
caledarDate,
catalogAttr.get('Name')
)
reference = model.createIfcClassificationReference(
classAttr.get('Location', None),
classAttr.get('Identification', None),
classAttr.get('Name', None),
classification
)
else:
spec = None
if 'Specification' in catalogAttr:
spec = catalogAttr['Specification'] # IFC4.3
elif 'Location' in catalogAttr:
spec = catalogAttr['Location'] # IFC4
classification = model.createIfcClassification(
catalogAttr.get('Source', None),
catalogAttr.get('Edition', None),
catalogAttr.get('EditionDate', None),
catalogAttr.get('Name'),
catalogAttr.get('Description', None),
spec,
catalogAttr.get('ReferenceToken', None)
)
reference = model.createIfcClassificationReference(
classAttr.get('Location', None),
classAttr.get('Identification', None),
classAttr.get('Name', None),
classification,
classAttr.get('Description', None),
classAttr.get('Sort', None)
)
# create IfcRelAssociatesClassification between IfcProject and IfcClassification, not in IFC 2x3 specified
model.createIfcRelAssociatesClassification(
ifcopenshell.guid.new(),
owner_history,
None,
None,
[project],
classification
)
# create IfcRelAssociatesClassification between IfcClassificationReference and related entities
model.createIfcRelAssociatesClassification(
ifcopenshell.guid.new(),
owner_history,
None,
None,
entities,
reference
)
# remove all IfcPropertySets
if delete_psets:
for obj in removable:
model.remove(obj)
model.write(file.replace('.ifc','') + "_with_classification.ifc")
return "Conversion successful!"
except Exception as e:
return str(e)