-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertToPropertySet.py
More file actions
127 lines (110 loc) · 5.21 KB
/
ConvertToPropertySet.py
File metadata and controls
127 lines (110 loc) · 5.21 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
125
126
127
import ifcopenshell
import ifcopenshell.util.date
import ifcopenshell.util.element
# Convert IfcClassification to IfcPropertySet
def convert_to_property_set(file, model, pSetNameType):
try:
version = model.schema
owner_history = model.by_type("IfcOwnerHistory")[0]
# find all IfcClassificationReferences
references = model.by_type("IfcClassificationReference")
removable = set()
changedEntities = []
# for each IfcClassificationReference create a new IfcPropertySet
for reference in references:
pSetName = pSetNameType
properties = {}
attr = reference.__dict__
# get all attributes of the IfcClassificationReference
for key in attr:
if key == 'id' or key == 'type' or key == 'GlobalId' or key == 'OwnerHistory':
continue
elif key == 'ReferencedSource':
classification = attr.get(key)
if classification is not None and classification.is_a("IfcClassification"):
properties['ClassificationReference'+key] = classification.Name
catalogAttr = classification.__dict__
# get all attributes of the IfcClassification
for k in catalogAttr:
if k == 'id' or k == 'type' or k == 'GlobalId' or k == 'OwnerHistory':
continue
elif k == 'EditionDate' and version == "IFC2X3":
date = ifcopenshell.util.date.ifc2datetime(catalogAttr.get(k))
if date is not None:
properties['Classification'+k] = date.strftime("%Y-%m-%d")
removable.add(catalogAttr.get(k))
else:
val = catalogAttr.get(k)
if val is not None:
properties['Classification'+k] = val
removable.add(classification)
if version != "IFC2X3":
removable.add(classification.ClassificationForObjects[0])
else:
value = attr.get(key)
if key == 'Location' and pSetName == 'GenerateNames' and value is not None:
val = value.split('/')
l = len(val)
try:
pSetName = 'bSD_' + val[l-4] + '_' + val[l-3]
except:
pSetName = 'bSD_' + attr.get("Name")
elif key == 'Location' and pSetName == 'GenerateNames' and value is None:
pSetName = 'bSD_' + attr.get("Name")
if value is not None:
properties['ClassificationReference'+key] = value
removable.add(reference)
# find all related entities for IfcClassificationReference
entities = []
relAssoc = []
if version == "IFC2X3":
rels = model.by_type('IfcRelAssociatesClassification')
for rel in rels:
if rel.RelatingClassification == reference:
relAssoc.append(rel)
else:
relAssoc = reference.ClassificationRefForObjects
for rel in relAssoc:
entities.extend(rel.RelatedObjects)
removable.add(rel)
# check if the property set name already exists for an entity
count = 0
for e in entities:
psets = ifcopenshell.util.element.get_psets(e, psets_only=True)
c = 0
for pset in psets:
if pset == pSetName:
c += 1
if c > count:
count = c
ifcPsetName = pSetName
if count > 0:
ifcPsetName = pSetName + '_' + str(count)
# create IfcPropertySingleValues and IfcPropertySet
ifcProperties = []
for key in properties:
ifcProperties.append(model.createIfcPropertySingleValue(key, None, model.createIfcText(properties[key]), None))
pSet = model.createIfcPropertySet(
ifcopenshell.guid.new(),
owner_history,
ifcPsetName,
None,
ifcProperties
)
# create IfcRelDefinesByProperties
model.createIfcRelDefinesByProperties(
ifcopenshell.guid.new(),
owner_history,
None,
None,
entities,
pSet
)
changedEntities.extend(entities)
# remove all IfcClassificationReferences
# for obj in removable:
# model.remove(obj)
model.write(file.replace('.ifc','') + "_with_propertyset.ifc")
return "Conversion successful!"
except Exception as e:
return str(e)