1818# https://github.com/IFCJSON-Team
1919
2020import os
21- import argparse
22- import json
2321import uuid
2422import ifcopenshell
2523import ifcopenshell .guid as guid
2624import ifcjson .common as common
2725
28- # ifc_file = ifcopenshell.open('../samples/7m900_tue_hello_wall_with_door.ifc')
29- # schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(ifc_file.schema)
30- id_objects = {}
31-
32- # Create dictionary of OwnerHistory objects
33- ownerHistories = {}
34-
35- # Create dictionary of IfcGeometricRepresentationContext objects
36- representationContexts = {}
37-
38- def entityToDict (entity ):
39- attr_dict = entity .__dict__
40-
41- ref = {
42- "type" : entity .is_a ()
43- }
44-
45- # Add missing GlobalId to OwnerHistory
46- if entity .is_a () == 'IfcOwnerHistory' :
47- if not entity .id () in ownerHistories :
48- ownerHistories [entity .id ()] = guid .new ()
49- attr_dict ["GlobalId" ] = ownerHistories [entity .id ()]
50-
51- # Add missing GlobalId to IfcGeometricRepresentationContext
52- if entity .is_a () == 'IfcGeometricRepresentationContext' :
53- if not entity .id () in representationContexts :
54- representationContexts [entity .id ()] = guid .new ()
55- attr_dict ["GlobalId" ] = representationContexts [entity .id ()]
56-
57- # check for globalid
58- if "GlobalId" in attr_dict :
59- uuid = guid .split (guid .expand (attr_dict ["GlobalId" ]))[1 :- 1 ]
60- ref ["ref" ] = uuid
61- if not attr_dict ["GlobalId" ] in id_objects :
26+ class IFC2JSON4 :
27+ def __init__ (self , ifcFilePath ):
28+ self .ifcFilePath = ifcFilePath
29+ self .ifcModel = ifcopenshell .open (ifcFilePath )
30+
31+ # Dictionary referencing all objects with a GlobalId that are already created
32+ self .id_objects = {}
33+
34+ # Create dictionary of OwnerHistory objects
35+ self .ownerHistories = {}
36+
37+ # Create dictionary of IfcGeometricRepresentationContext objects
38+ self .representationContexts = {}
39+
40+ def spf2Json (self ):
41+ jsonObjects = []
42+ entityIter = iter (self .ifcModel )
43+ for entity in entityIter :
44+ self .entityToDict (entity )
45+ for key in self .id_objects :
46+ jsonObjects .append (self .id_objects [key ])
47+ return jsonObjects
48+
49+ def entityToDict (self , entity ):
50+ attr_dict = entity .__dict__
51+
52+ ref = {
53+ "type" : entity .is_a ()
54+ }
55+
56+ # Add missing GlobalId to OwnerHistory
57+ if entity .is_a () == 'IfcOwnerHistory' :
58+ if not entity .id () in self .ownerHistories :
59+ self .ownerHistories [entity .id ()] = guid .new ()
60+ attr_dict ["GlobalId" ] = self .ownerHistories [entity .id ()]
61+
62+ # Add missing GlobalId to IfcGeometricRepresentationContext
63+ if entity .is_a () == 'IfcGeometricRepresentationContext' :
64+ if not entity .id () in self .representationContexts :
65+ self .representationContexts [entity .id ()] = guid .new ()
66+ attr_dict ["GlobalId" ] = self .representationContexts [entity .id ()]
67+
68+ # check for globalid
69+ if "GlobalId" in attr_dict :
70+ uuid = guid .split (guid .expand (attr_dict ["GlobalId" ]))[1 :- 1 ]
71+ ref ["ref" ] = uuid
72+ if not attr_dict ["GlobalId" ] in self .id_objects :
73+ d = {
74+ "type" : entity .is_a ()
75+ }
76+
77+ # Add missing GlobalId to OwnerHistory
78+ if entity .is_a () == 'IfcOwnerHistory' :
79+ d ["GlobalId" ] = guid .split (guid .expand (self .ownerHistories [entity .id ()]))[1 :- 1 ]
80+
81+ # Add missing GlobalId to IfcGeometricRepresentationContext
82+ if entity .is_a () == 'IfcGeometricRepresentationContext' :
83+ d ["GlobalId" ] = guid .split (guid .expand (self .representationContexts [entity .id ()]))[1 :- 1 ]
84+
85+ for i in range (0 ,len (entity )):
86+ attr = entity .attribute_name (i )
87+ attrKey = common .toLowerCamelcase (attr )
88+ if attr == "GlobalId" :
89+ d [attrKey ] = uuid
90+ else :
91+ if attr in attr_dict :
92+ jsonValue = self .getEntityValue (attr_dict [attr ])
93+ if jsonValue :
94+ if ((entity .is_a () == 'IfcOwnerHistory' ) and (attr == "GlobalId" )):
95+ pass
96+ else :
97+ d [attrKey ] = jsonValue
98+ if attr_dict [attr ] == None :
99+ continue
100+ elif isinstance (attr_dict [attr ], ifcopenshell .entity_instance ):
101+ d [attrKey ] = self .entityToDict (attr_dict [attr ])
102+ elif isinstance (attr_dict [attr ], tuple ):
103+ subEnts = []
104+ for subEntity in attr_dict [attr ]:
105+ if isinstance (subEntity , ifcopenshell .entity_instance ):
106+ subEntJson = self .entityToDict (subEntity )
107+ if subEntJson :
108+ subEnts .append (subEntJson )
109+ else :
110+ subEnts .append (subEntity )
111+ if len (subEnts ) > 0 :
112+ d [attrKey ] = subEnts
113+ else :
114+ d [attrKey ] = attr_dict [attr ]
115+ self .id_objects [attr_dict ["GlobalId" ]] = d
116+ return ref
117+ else :
62118 d = {
63119 "type" : entity .is_a ()
64120 }
65121
66- # Add missing GlobalId to OwnerHistory
67- if entity .is_a () == 'IfcOwnerHistory' :
68- d ["GlobalId" ] = guid .split (guid .expand (ownerHistories [entity .id ()]))[1 :- 1 ]
69-
70- # Add missing GlobalId to IfcGeometricRepresentationContext
71- if entity .is_a () == 'IfcGeometricRepresentationContext' :
72- d ["GlobalId" ] = guid .split (guid .expand (representationContexts [entity .id ()]))[1 :- 1 ]
73-
74122 for i in range (0 ,len (entity )):
75123 attr = entity .attribute_name (i )
76124 attrKey = common .toLowerCamelcase (attr )
77- if attr == "GlobalId" :
78- d [attrKey ] = uuid
79- else :
80- if attr in attr_dict :
81- jsonValue = getEntityValue (attr_dict [attr ])
125+ if attr in attr_dict :
126+ if not attr == "OwnerHistory" :
127+ jsonValue = self .getEntityValue (attr_dict [attr ])
82128 if jsonValue :
83- if ((entity .is_a () == 'IfcOwnerHistory' ) and (attr == "GlobalId" )):
84- pass
85- else :
86- d [attrKey ] = jsonValue
129+ d [attrKey ] = jsonValue
87130 if attr_dict [attr ] == None :
88131 continue
89132 elif isinstance (attr_dict [attr ], ifcopenshell .entity_instance ):
90- d [attrKey ] = entityToDict (attr_dict [attr ])
133+ d [attrKey ] = self . entityToDict (attr_dict [attr ])
91134 elif isinstance (attr_dict [attr ], tuple ):
92135 subEnts = []
93136 for subEntity in attr_dict [attr ]:
94137 if isinstance (subEntity , ifcopenshell .entity_instance ):
95- subEntJson = entityToDict (subEntity )
138+ # subEnts.append(None)
139+ subEntJson = self .entityToDict (subEntity )
96140 if subEntJson :
97141 subEnts .append (subEntJson )
98142 else :
@@ -101,74 +145,19 @@ def entityToDict(entity):
101145 d [attrKey ] = subEnts
102146 else :
103147 d [attrKey ] = attr_dict [attr ]
104- id_objects [attr_dict ["GlobalId" ]] = d
105- return ref
106- else :
107- d = {
108- "type" : entity .is_a ()
109- }
110-
111- for i in range (0 ,len (entity )):
112- attr = entity .attribute_name (i )
113- attrKey = common .toLowerCamelcase (attr )
114- if attr in attr_dict :
115- if not attr == "OwnerHistory" :
116- jsonValue = getEntityValue (attr_dict [attr ])
117- if jsonValue :
118- d [attrKey ] = jsonValue
119- if attr_dict [attr ] == None :
120- continue
121- elif isinstance (attr_dict [attr ], ifcopenshell .entity_instance ):
122- d [attrKey ] = entityToDict (attr_dict [attr ])
123- elif isinstance (attr_dict [attr ], tuple ):
124- subEnts = []
125- for subEntity in attr_dict [attr ]:
126- if isinstance (subEntity , ifcopenshell .entity_instance ):
127- # subEnts.append(None)
128- subEntJson = entityToDict (subEntity )
129- if subEntJson :
130- subEnts .append (subEntJson )
131- else :
132- subEnts .append (subEntity )
133- if len (subEnts ) > 0 :
134- d [attrKey ] = subEnts
135- else :
136- d [attrKey ] = attr_dict [attr ]
137- return d
138-
139- def getEntityValue (value ):
140- if value == None :
141- jsonValue = None
142- elif isinstance (value , ifcopenshell .entity_instance ):
143- jsonValue = entityToDict (value )
144- elif isinstance (value , tuple ):
145- jsonValue = None
146- subEnts = []
147- for subEntity in value :
148- subEnts .append (getEntityValue (subEntity ))
149- jsonValue = subEnts
150- else :
151- jsonValue = value
152- return jsonValue
153-
154-
155- # jsonObjects= []
156- # entityIter = iter(ifc_file)
157- # for entity in entityIter:
158- # entityToDict(entity)
159- # for key in id_objects:
160- # jsonObjects.append(id_objects[key])
161- # with open('../7m900_tue_hello_wall_with_door.json', 'w') as outfile:
162- # json.dump(jsonObjects, outfile, indent=4)
163-
164- def spf2Json (ifcFilePath , jsonFilePath ):
165- ifc_file = ifcopenshell .open (ifcFilePath )
166-
167- jsonObjects = []
168- entityIter = iter (ifc_file )
169- for entity in entityIter :
170- entityToDict (entity )
171- for key in id_objects :
172- jsonObjects .append (id_objects [key ])
173- with open (jsonFilePath , 'w' ) as outfile :
174- json .dump (jsonObjects , outfile , indent = 4 )
148+ return d
149+
150+ def getEntityValue (self , value ):
151+ if value == None :
152+ jsonValue = None
153+ elif isinstance (value , ifcopenshell .entity_instance ):
154+ jsonValue = self .entityToDict (value )
155+ elif isinstance (value , tuple ):
156+ jsonValue = None
157+ subEnts = []
158+ for subEntity in value :
159+ subEnts .append (self .getEntityValue (subEntity ))
160+ jsonValue = subEnts
161+ else :
162+ jsonValue = value
163+ return jsonValue
0 commit comments