1+ import ifcopenshell
2+ import json
3+
4+ ifc_file = ifcopenshell .open ('./samples/7m900_tue_hello_wall_with_door.ifc' )
5+ schema = ifcopenshell .ifcopenshell_wrapper .schema_by_name (ifc_file .schema )
6+ id_objects = {}
7+
8+ def entityToDict (entity ):
9+ ref = {
10+ "Type" : entity .is_a ()
11+ }
12+ attr_dict = entity .__dict__
13+
14+ # check for globalid
15+ if "GlobalId" in attr_dict :
16+ ref ["ref" ] = attr_dict ["GlobalId" ]
17+ if not attr_dict ["GlobalId" ] in id_objects :
18+ d = {
19+ "Type" : entity .is_a ()
20+ }
21+
22+ for i in range (0 ,len (entity )):
23+ attr = entity .attribute_name (i )
24+ if attr in attr_dict :
25+ if not attr == "OwnerHistory" :
26+ jsonValue = getEntityValue (attr_dict [attr ])
27+ if jsonValue :
28+ d [attr ] = jsonValue
29+ if attr_dict [attr ] == None :
30+ continue
31+ elif isinstance (attr_dict [attr ], ifcopenshell .entity_instance ):
32+ d [attr ] = entityToDict (attr_dict [attr ])
33+ elif isinstance (attr_dict [attr ], tuple ):
34+ subEnts = []
35+ for subEntity in attr_dict [attr ]:
36+ if isinstance (subEntity , ifcopenshell .entity_instance ):
37+ subEntJson = entityToDict (subEntity )
38+ if subEntJson :
39+ subEnts .append (subEntJson )
40+ else :
41+ subEnts .append (subEntity )
42+ if len (subEnts ) > 0 :
43+ d [attr ] = subEnts
44+ else :
45+ d [attr ] = attr_dict [attr ]
46+ id_objects [attr_dict ["GlobalId" ]] = d
47+ return ref
48+ else :
49+ d = {
50+ "Type" : entity .is_a ()
51+ }
52+
53+ for i in range (0 ,len (entity )):
54+ attr = entity .attribute_name (i )
55+ if attr in attr_dict :
56+ if not attr == "OwnerHistory" :
57+ jsonValue = getEntityValue (attr_dict [attr ])
58+ if jsonValue :
59+ d [attr ] = jsonValue
60+ if attr_dict [attr ] == None :
61+ continue
62+ elif isinstance (attr_dict [attr ], ifcopenshell .entity_instance ):
63+ d [attr ] = entityToDict (attr_dict [attr ])
64+ elif isinstance (attr_dict [attr ], tuple ):
65+ subEnts = []
66+ for subEntity in attr_dict [attr ]:
67+ if isinstance (subEntity , ifcopenshell .entity_instance ):
68+ # subEnts.append(None)
69+ subEntJson = entityToDict (subEntity )
70+ if subEntJson :
71+ subEnts .append (subEntJson )
72+ else :
73+ subEnts .append (subEntity )
74+ if len (subEnts ) > 0 :
75+ d [attr ] = subEnts
76+ else :
77+ d [attr ] = attr_dict [attr ]
78+ return d
79+
80+ def getEntityValue (value ):
81+ if value == None :
82+ jsonValue = None
83+ elif isinstance (value , ifcopenshell .entity_instance ):
84+ jsonValue = entityToDict (value )
85+ elif isinstance (value , tuple ):
86+ jsonValue = None
87+ subEnts = []
88+ for subEntity in value :
89+ subEnts .append (getEntityValue (subEntity ))
90+ jsonValue = subEnts
91+ else :
92+ jsonValue = value
93+ return jsonValue
94+
95+
96+ jsonObjects = []
97+ entityIter = iter (ifc_file )
98+ for entity in entityIter :
99+ entityToDict (entity )
100+ for key in id_objects :
101+ jsonObjects .append (id_objects [key ])
102+ with open ('7m900_tue_hello_wall_with_door.json' , 'w' ) as outfile :
103+ json .dump (jsonObjects , outfile , indent = 4 )
0 commit comments