3
3
4
4
def convert_to_classification (file , model , pSetName ):
5
5
6
- version = model .schema
7
- owner_history = model .by_type ("IfcOwnerHistory" )[0 ]
8
- project = model .by_type ("IfcProject" )[0 ]
6
+ try :
7
+ version = model .schema
8
+ owner_history = model .by_type ("IfcOwnerHistory" )[0 ]
9
+ project = model .by_type ("IfcProject" )[0 ]
9
10
10
- # Get all IfcPropertySet objects
11
- property_sets = model .by_type ("IfcPropertySet" )
11
+ # Get all IfcPropertySet objects
12
+ property_sets = model .by_type ("IfcPropertySet" )
12
13
13
- property_sets_classification = []
14
- removable = set ()
15
- for property_set in property_sets :
16
- # if property_set.Name.startswith(pSetName):
17
- if property_set .Name in pSetName :
18
- property_sets_classification .append (property_set )
19
-
20
-
21
- for property_set in property_sets_classification :
22
- entities = []
23
- catalogAttr = {}
24
- classAttr = {}
25
- relDefinesByProperties = []
26
-
27
- if version == "IFC2X3" :
28
- relDefinesByProperties = property_set .PropertyDefinitionOf
29
- else :
30
- relDefinesByProperties = property_set .DefinesOccurrence
31
- for relDefinesByProperty in relDefinesByProperties :
32
- objects = relDefinesByProperty .RelatedObjects
33
- for obj in objects :
34
- entities .append (obj )
35
- removable .add (relDefinesByProperty )
36
-
37
- properties = property_set .HasProperties
38
- for prop in properties :
39
- if prop .is_a ("IfcPropertySingleValue" ):
40
- if prop .Name .startswith ("Catalog" ):
41
- catName = prop .Name .replace ("Catalog" , "" )
42
- catalogAttr [catName ] = prop .NominalValue .wrappedValue
43
- elif prop .Name .startswith ("Class" ):
44
- className = prop .Name .replace ("Class" , "" )
45
- classAttr [className ] = prop .NominalValue .wrappedValue
46
- else :
47
- print ("Unknown property: " , prop .Name )
48
- removable .add (prop )
49
- removable .add (property_set )
14
+ property_sets_classification = []
15
+ removable = set ()
50
16
51
- if version == "IFC2X3" :
52
- dates = ifcopenshell .util .date .datetime2ifc (catalogAttr .get ('EditionDate' , None ), 'IfcCalendarDate' )
53
- caledarDate = model .createIfcCalendarDate (dates ['DayComponent' ], dates ['MonthComponent' ], dates ['YearComponent' ])
54
- classification = model .createIfcClassification (
55
- catalogAttr .get ('Source' , None ),
56
- catalogAttr .get ('Edition' , None ),
57
- caledarDate ,
58
- catalogAttr .get ('Name' )
59
- )
60
- reference = model .createIfcClassificationReference (
61
- classAttr .get ('Location' , None ),
62
- classAttr .get ('Identification' , None ),
63
- classAttr .get ('Name' , None ),
64
- classification
65
- )
66
- else :
67
- classification = model .createIfcClassification (
68
- catalogAttr .get ('Source' , None ),
69
- catalogAttr .get ('Edition' , None ),
70
- catalogAttr .get ('EditionDate' , None ),
71
- catalogAttr .get ('Name' ),
72
- catalogAttr .get ('Description' , None ),
73
- catalogAttr .get ('Specification' , None ),
74
- catalogAttr .get ('ReferenceToken' , None )
75
- )
76
- reference = model .createIfcClassificationReference (
77
- classAttr .get ('Location' , None ),
78
- classAttr .get ('Identification' , None ),
79
- classAttr .get ('Name' , None ),
80
- classification ,
81
- classAttr .get ('Description' , None ),
82
- classAttr .get ('Sort' , None )
17
+ # find all IfcPropertySets with the given names
18
+ for property_set in property_sets :
19
+ if property_set .Name in pSetName :
20
+ property_sets_classification .append (property_set )
21
+
22
+ # for each IfcPropertySet create a new IfcClassification and IfcClassificationReference
23
+ for property_set in property_sets_classification :
24
+ entities = []
25
+ catalogAttr = {}
26
+ classAttr = {}
27
+ relDefinesByProperties = []
28
+
29
+ # get all related entities
30
+ if version == "IFC2X3" :
31
+ relDefinesByProperties = property_set .PropertyDefinitionOf
32
+ else :
33
+ relDefinesByProperties = property_set .DefinesOccurrence
34
+ for relDefinesByProperty in relDefinesByProperties :
35
+ objects = relDefinesByProperty .RelatedObjects
36
+ for obj in objects :
37
+ entities .append (obj )
38
+ removable .add (relDefinesByProperty )
39
+
40
+ # get all properties of the IfcPropertySet
41
+ properties = property_set .HasProperties
42
+ for prop in properties :
43
+ if prop .is_a ("IfcPropertySingleValue" ):
44
+ if prop .Name .startswith ("Catalog" ):
45
+ catName = prop .Name .replace ("Catalog" , "" )
46
+ catalogAttr [catName ] = prop .NominalValue .wrappedValue
47
+ elif prop .Name .startswith ("Class" ):
48
+ className = prop .Name .replace ("Class" , "" )
49
+ classAttr [className ] = prop .NominalValue .wrappedValue
50
+ else :
51
+ print ("Unknown property: " , prop .Name )
52
+ removable .add (prop )
53
+ removable .add (property_set )
54
+
55
+ # create IfcClassification and IfcClassificationReference
56
+ if version == "IFC2X3" :
57
+ dates = ifcopenshell .util .date .datetime2ifc (catalogAttr .get ('EditionDate' , None ), 'IfcCalendarDate' )
58
+ caledarDate = model .createIfcCalendarDate (dates ['DayComponent' ], dates ['MonthComponent' ], dates ['YearComponent' ])
59
+ classification = model .createIfcClassification (
60
+ catalogAttr .get ('Source' , None ),
61
+ catalogAttr .get ('Edition' , None ),
62
+ caledarDate ,
63
+ catalogAttr .get ('Name' )
64
+ )
65
+ reference = model .createIfcClassificationReference (
66
+ classAttr .get ('Location' , None ),
67
+ classAttr .get ('Identification' , None ),
68
+ classAttr .get ('Name' , None ),
69
+ classification
83
70
)
71
+ else :
72
+ classification = model .createIfcClassification (
73
+ catalogAttr .get ('Source' , None ),
74
+ catalogAttr .get ('Edition' , None ),
75
+ catalogAttr .get ('EditionDate' , None ),
76
+ catalogAttr .get ('Name' ),
77
+ catalogAttr .get ('Description' , None ),
78
+ catalogAttr .get ('Specification' , None ),
79
+ catalogAttr .get ('ReferenceToken' , None )
80
+ )
81
+ reference = model .createIfcClassificationReference (
82
+ classAttr .get ('Location' , None ),
83
+ classAttr .get ('Identification' , None ),
84
+ classAttr .get ('Name' , None ),
85
+ classification ,
86
+ classAttr .get ('Description' , None ),
87
+ classAttr .get ('Sort' , None )
88
+ )
89
+
90
+ # create IfcRelAssociatesClassification between IfcProject and IfcClassification, not in IFC 2x3 specified
91
+ model .createIfcRelAssociatesClassification (
92
+ ifcopenshell .guid .new (),
93
+ owner_history ,
94
+ None ,
95
+ None ,
96
+ [project ],
97
+ classification
98
+ )
84
99
85
- relClassClass = model .createIfcRelAssociatesClassification (
100
+ # create IfcRelAssociatesClassification between IfcClassificationReference and related entities
101
+ model .createIfcRelAssociatesClassification (
86
102
ifcopenshell .guid .new (),
87
103
owner_history ,
88
104
None ,
89
105
None ,
90
- [ project ] ,
91
- classification
106
+ entities ,
107
+ reference
92
108
)
93
-
94
- relClassRef = model .createIfcRelAssociatesClassification (
95
- ifcopenshell .guid .new (),
96
- owner_history ,
97
- None ,
98
- None ,
99
- entities ,
100
- reference
101
- )
102
- for obj in removable :
103
- model .remove (obj )
104
-
105
- model .write (file .replace ('.ifc' ,'' ) + "_with_classification.ifc" )
106
- return "Conversion successful!"
109
+
110
+ # remove all IfcPropertySets
111
+ for obj in removable :
112
+ model .remove (obj )
113
+
114
+ model .write (file .replace ('.ifc' ,'' ) + "_with_classification.ifc" )
115
+ return "Conversion successful!"
116
+
117
+ except Exception as e :
118
+ return str (e )
0 commit comments