Skip to content

Commit 85727ee

Browse files
Included ALL inverse relations in IfcRoot entities
1 parent 7ca3a30 commit 85727ee

File tree

3 files changed

+14741
-4580
lines changed

3 files changed

+14741
-4580
lines changed

ifcjson/ifc2json4.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import ifcopenshell.guid as guid
3232
import ifcjson.common as common
3333

34+
3435
class IFC2JSON4:
35-
maxCache=2048
36+
maxCache = 2048
37+
3638
def __init__(self, ifcFilePath):
3739
self.ifcFilePath = ifcFilePath
3840
self.ifcModel = ifcopenshell.open(ifcFilePath)
@@ -47,7 +49,7 @@ def __init__(self, ifcFilePath):
4749
self.representationContexts = {}
4850

4951
def spf2Json(self):
50-
jsonObjects= []
52+
jsonObjects = []
5153
entityIter = iter(self.ifcModel)
5254
for entity in entityIter:
5355
self.entityToDict(entity)
@@ -57,7 +59,7 @@ def spf2Json(self):
5759
'file_schema': 'IFC.JSON4',
5860
'originatingSystem': 'IFC2JSON_python',
5961
'data': jsonObjects
60-
}
62+
}
6163

6264
@functools.lru_cache(maxsize=maxCache)
6365
def entityToDict(self, entity):
@@ -96,11 +98,36 @@ def entityToDict(self, entity):
9698

9799
# Add missing GlobalId to OwnerHistory
98100
if entityType == 'ifcOwnerHistory':
99-
d["globalId"] = guid.split(guid.expand(self.ownerHistories[entity.id()]))[1:-1]
101+
d["globalId"] = guid.split(guid.expand(
102+
self.ownerHistories[entity.id()]))[1:-1]
100103

101104
# Add missing GlobalId to IfcGeometricRepresentationContext
102105
if entityType == 'ifcGeometricRepresentationContext':
103-
d["globalId"] = guid.split(guid.expand(self.representationContexts[entity.id()]))[1:-1]
106+
d["globalId"] = guid.split(guid.expand(
107+
self.representationContexts[entity.id()]))[1:-1]
108+
109+
# Inverse attributes must be added if not OwnerHistory or GeometricRepresentationContext
110+
if not entityType in ['ifcGeometricRepresentationContext', 'ifcOwnerHistory']:
111+
for attr in entity.wrapped_data.get_inverse_attribute_names():
112+
inverseAttribute = getattr(entity, attr)
113+
114+
# print(attr)
115+
# d[attrKey] = {
116+
# "type": attr.Name,
117+
# "ref": "guid"
118+
# }
119+
# print(inverseAttribute)
120+
if isinstance(inverseAttribute, tuple):
121+
for attribute in inverseAttribute:
122+
attrKey = common.toLowerCamelcase(attribute.is_a())
123+
# print(dir(attribute))
124+
d[attr] = {
125+
'type': attrKey
126+
}
127+
if 'GlobalId' in dir(attribute):
128+
d[attr]['ref'] = guid.split(guid.expand(attribute.GlobalId))[1:-1]
129+
else:
130+
print(entityType + ' has no GlobalId for referencing!')
104131

105132
for attr in entityAttributes:
106133
attrKey = common.toLowerCamelcase(attr)
@@ -121,7 +148,8 @@ def entityToDict(self, entity):
121148
if entityAttributes[attr] == None:
122149
continue
123150
elif isinstance(entityAttributes[attr], ifcopenshell.entity_instance):
124-
d[attrKey] = self.entityToDict(entityAttributes[attr])
151+
d[attrKey] = self.entityToDict(
152+
entityAttributes[attr])
125153
elif isinstance(entityAttributes[attr], tuple):
126154
subEnts = []
127155
for subEntity in entityAttributes[attr]:
@@ -142,7 +170,7 @@ def entityToDict(self, entity):
142170
"type": entityType
143171
}
144172

145-
for i in range(0,len(entity)):
173+
for i in range(0, len(entity)):
146174
attr = entity.attribute_name(i)
147175
attrKey = common.toLowerCamelcase(attr)
148176
if attr in entityAttributes:
@@ -153,7 +181,8 @@ def entityToDict(self, entity):
153181
if entityAttributes[attr] == None:
154182
continue
155183
elif isinstance(entityAttributes[attr], ifcopenshell.entity_instance):
156-
d[attrKey] = self.entityToDict(entityAttributes[attr])
184+
d[attrKey] = self.entityToDict(
185+
entityAttributes[attr])
157186
elif isinstance(entityAttributes[attr], tuple):
158187
subEnts = []
159188
for subEntity in entityAttributes[attr]:
@@ -184,4 +213,4 @@ def getEntityValue(self, value):
184213
jsonValue = subEnts
185214
else:
186215
jsonValue = value
187-
return jsonValue
216+
return jsonValue

samples/7m900_tue_hello_wall_with_door_4.json

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"data": [
55
{
66
"type": "IfcGeometricRepresentationContext",
7-
"globalId": "5a4b92f5-2835-4ba9-ab90-ac1a4a9b791c",
7+
"globalId": "9365fefe-7719-4058-8316-88be389552bf",
88
"contextType": "Model",
99
"coordinateSpaceDimension": 3,
1010
"precision": 1e-05,
@@ -25,7 +25,7 @@
2525
"globalId": "4c36f556-7d6e-41d3-a06a-b1e8736aaed2",
2626
"ownerHistory": {
2727
"type": "ifcOwnerHistory",
28-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
28+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
2929
},
3030
"relatingOpeningElement": {
3131
"type": "ifcOpeningElement",
@@ -38,7 +38,7 @@
3838
},
3939
{
4040
"type": "IfcOwnerHistory",
41-
"globalId": "c03161f1-2517-4576-9dc6-a12eec739057",
41+
"globalId": "8bafe821-32c4-4101-af05-adeca896ffc8",
4242
"owningUser": {
4343
"type": "ifcPersonAndOrganization",
4444
"thePerson": {
@@ -69,10 +69,18 @@
6969
},
7070
{
7171
"type": "IfcOpeningElement",
72+
"VoidsElements": {
73+
"type": "ifcRelVoidsElement",
74+
"ref": "ef6c5bb2-49d1-4153-ae27-bb01684b2778"
75+
},
76+
"HasFillings": {
77+
"type": "ifcRelFillsElement",
78+
"ref": "4c36f556-7d6e-41d3-a06a-b1e8736aaed2"
79+
},
7280
"globalId": "9598e1c0-b1a9-4505-93a3-f3193af39e1d",
7381
"ownerHistory": {
7482
"type": "ifcOwnerHistory",
75-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
83+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
7684
},
7785
"name": "Opening Element xyz",
7886
"description": "Description of Opening",
@@ -233,7 +241,7 @@
233241
"type": "ifcShapeRepresentation",
234242
"contextOfItems": {
235243
"type": "ifcGeometricRepresentationContext",
236-
"ref": "5a4b92f5-2835-4ba9-ab90-ac1a4a9b791c"
244+
"ref": "9365fefe-7719-4058-8316-88be389552bf"
237245
},
238246
"representationIdentifier": "Body",
239247
"representationType": "SweptSolid",
@@ -328,10 +336,18 @@
328336
},
329337
{
330338
"type": "IfcDoor",
339+
"FillsVoids": {
340+
"type": "ifcRelFillsElement",
341+
"ref": "4c36f556-7d6e-41d3-a06a-b1e8736aaed2"
342+
},
343+
"ContainedInStructure": {
344+
"type": "ifcRelContainedInSpatialStructure",
345+
"ref": "98fa75b8-371d-412a-be42-2326c68dfcf5"
346+
},
331347
"globalId": "157c866c-9c08-4348-a0ed-4d57cd66c9e2",
332348
"ownerHistory": {
333349
"type": "ifcOwnerHistory",
334-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
350+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
335351
},
336352
"name": "A common door",
337353
"description": "Description of a standard door",
@@ -522,7 +538,7 @@
522538
"type": "ifcShapeRepresentation",
523539
"contextOfItems": {
524540
"type": "ifcGeometricRepresentationContext",
525-
"ref": "5a4b92f5-2835-4ba9-ab90-ac1a4a9b791c"
541+
"ref": "9365fefe-7719-4058-8316-88be389552bf"
526542
},
527543
"representationIdentifier": "Body",
528544
"representationType": "SweptSolid",
@@ -622,7 +638,7 @@
622638
"globalId": "d2ecfe17-45be-4b36-959d-1be3ec8193bd",
623639
"ownerHistory": {
624640
"type": "ifcOwnerHistory",
625-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
641+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
626642
},
627643
"relatedObjects": [
628644
{
@@ -637,10 +653,26 @@
637653
},
638654
{
639655
"type": "IfcWallStandardCase",
656+
"HasAssociations": {
657+
"type": "ifcRelAssociatesMaterial",
658+
"ref": "bda2aa8b-b6e2-4a17-a9a6-0ee24c8b49d5"
659+
},
660+
"IsDefinedBy": {
661+
"type": "ifcRelDefinesByProperties",
662+
"ref": "d2ecfe17-45be-4b36-959d-1be3ec8193bd"
663+
},
664+
"HasOpenings": {
665+
"type": "ifcRelVoidsElement",
666+
"ref": "ef6c5bb2-49d1-4153-ae27-bb01684b2778"
667+
},
668+
"ContainedInStructure": {
669+
"type": "ifcRelContainedInSpatialStructure",
670+
"ref": "98fa75b8-371d-412a-be42-2326c68dfcf5"
671+
},
640672
"globalId": "f92c2898-fd68-44ef-9178-3348e340017b",
641673
"ownerHistory": {
642674
"type": "ifcOwnerHistory",
643-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
675+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
644676
},
645677
"name": "Wall xyz",
646678
"description": "Description of Wall",
@@ -771,7 +803,7 @@
771803
"type": "ifcShapeRepresentation",
772804
"contextOfItems": {
773805
"type": "ifcGeometricRepresentationContext",
774-
"ref": "5a4b92f5-2835-4ba9-ab90-ac1a4a9b791c"
806+
"ref": "9365fefe-7719-4058-8316-88be389552bf"
775807
},
776808
"representationIdentifier": "Axis",
777809
"representationType": "Curve2D",
@@ -801,7 +833,7 @@
801833
"type": "ifcShapeRepresentation",
802834
"contextOfItems": {
803835
"type": "ifcGeometricRepresentationContext",
804-
"ref": "5a4b92f5-2835-4ba9-ab90-ac1a4a9b791c"
836+
"ref": "9365fefe-7719-4058-8316-88be389552bf"
805837
},
806838
"representationIdentifier": "Body",
807839
"representationType": "SweptSolid",
@@ -896,10 +928,14 @@
896928
},
897929
{
898930
"type": "IfcPropertySet",
931+
"PropertyDefinitionOf": {
932+
"type": "ifcRelDefinesByProperties",
933+
"ref": "d2ecfe17-45be-4b36-959d-1be3ec8193bd"
934+
},
899935
"globalId": "486f7679-1a8a-4deb-8798-5a7e0c8c7d51",
900936
"ownerHistory": {
901937
"type": "ifcOwnerHistory",
902-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
938+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
903939
},
904940
"name": "Pset_WallCommon",
905941
"hasProperties": [
@@ -1000,7 +1036,7 @@
10001036
"globalId": "bda2aa8b-b6e2-4a17-a9a6-0ee24c8b49d5",
10011037
"ownerHistory": {
10021038
"type": "ifcOwnerHistory",
1003-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1039+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
10041040
},
10051041
"relatedObjects": [
10061042
{
@@ -1033,7 +1069,7 @@
10331069
"globalId": "98fa75b8-371d-412a-be42-2326c68dfcf5",
10341070
"ownerHistory": {
10351071
"type": "ifcOwnerHistory",
1036-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1072+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
10371073
},
10381074
"name": "Default Building",
10391075
"description": "Contents of Building Storey",
@@ -1054,10 +1090,18 @@
10541090
},
10551091
{
10561092
"type": "IfcBuildingStorey",
1093+
"Decomposes": {
1094+
"type": "ifcRelAggregates",
1095+
"ref": "81188789-c594-45ec-b0de-ccdefee14041"
1096+
},
1097+
"ContainsElements": {
1098+
"type": "ifcRelContainedInSpatialStructure",
1099+
"ref": "98fa75b8-371d-412a-be42-2326c68dfcf5"
1100+
},
10571101
"globalId": "0c207ba4-d0b8-4fff-bcd0-c1d8c7f7b5ff",
10581102
"ownerHistory": {
10591103
"type": "ifcOwnerHistory",
1060-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1104+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
10611105
},
10621106
"name": "Default Building Storey",
10631107
"description": "Description of Default Building Storey",
@@ -1159,7 +1203,7 @@
11591203
"globalId": "57bfe2d2-b505-4bba-8278-f867834a0be0",
11601204
"ownerHistory": {
11611205
"type": "ifcOwnerHistory",
1162-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1206+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
11631207
},
11641208
"name": "ProjectContainer",
11651209
"description": "ProjectContainer for Sites",
@@ -1176,17 +1220,21 @@
11761220
},
11771221
{
11781222
"type": "IfcProject",
1223+
"IsDecomposedBy": {
1224+
"type": "ifcRelAggregates",
1225+
"ref": "57bfe2d2-b505-4bba-8278-f867834a0be0"
1226+
},
11791227
"globalId": "22e66ddf-794d-40bb-8aa5-3dda450d8255",
11801228
"ownerHistory": {
11811229
"type": "ifcOwnerHistory",
1182-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1230+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
11831231
},
11841232
"name": "Default Project",
11851233
"description": "Description of Default Project",
11861234
"representationContexts": [
11871235
{
11881236
"type": "ifcGeometricRepresentationContext",
1189-
"ref": "5a4b92f5-2835-4ba9-ab90-ac1a4a9b791c"
1237+
"ref": "9365fefe-7719-4058-8316-88be389552bf"
11901238
}
11911239
],
11921240
"unitsInContext": {
@@ -1264,10 +1312,18 @@
12641312
},
12651313
{
12661314
"type": "IfcSite",
1315+
"IsDecomposedBy": {
1316+
"type": "ifcRelAggregates",
1317+
"ref": "d3e2bc1a-4cdb-49ed-9c4a-c60c97949121"
1318+
},
1319+
"Decomposes": {
1320+
"type": "ifcRelAggregates",
1321+
"ref": "57bfe2d2-b505-4bba-8278-f867834a0be0"
1322+
},
12671323
"globalId": "f55eaf97-145e-4431-b2f3-69f9634f244b",
12681324
"ownerHistory": {
12691325
"type": "ifcOwnerHistory",
1270-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1326+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
12711327
},
12721328
"name": "TU/e campus",
12731329
"description": "The High Tech campus of the Eindhoven University of Technology",
@@ -1318,7 +1374,7 @@
13181374
"globalId": "d3e2bc1a-4cdb-49ed-9c4a-c60c97949121",
13191375
"ownerHistory": {
13201376
"type": "ifcOwnerHistory",
1321-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1377+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
13221378
},
13231379
"name": "SiteContainer",
13241380
"description": "SiteContainer For Buildings",
@@ -1335,10 +1391,18 @@
13351391
},
13361392
{
13371393
"type": "IfcBuilding",
1394+
"IsDecomposedBy": {
1395+
"type": "ifcRelAggregates",
1396+
"ref": "81188789-c594-45ec-b0de-ccdefee14041"
1397+
},
1398+
"Decomposes": {
1399+
"type": "ifcRelAggregates",
1400+
"ref": "d3e2bc1a-4cdb-49ed-9c4a-c60c97949121"
1401+
},
13381402
"globalId": "3ca7e585-4e3e-4969-a86f-f049f4fbde52",
13391403
"ownerHistory": {
13401404
"type": "ifcOwnerHistory",
1341-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1405+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
13421406
},
13431407
"name": "Vertigo Building",
13441408
"description": "TU/e Department of the Built Environment",
@@ -1409,7 +1473,7 @@
14091473
"globalId": "81188789-c594-45ec-b0de-ccdefee14041",
14101474
"ownerHistory": {
14111475
"type": "ifcOwnerHistory",
1412-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1476+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
14131477
},
14141478
"name": "BuildingContainer",
14151479
"description": "BuildingContainer for BuildigStories",
@@ -1429,7 +1493,7 @@
14291493
"globalId": "ef6c5bb2-49d1-4153-ae27-bb01684b2778",
14301494
"ownerHistory": {
14311495
"type": "ifcOwnerHistory",
1432-
"ref": "c03161f1-2517-4576-9dc6-a12eec739057"
1496+
"ref": "8bafe821-32c4-4101-af05-adeca896ffc8"
14331497
},
14341498
"relatingBuildingElement": {
14351499
"type": "ifcWallStandardCase",

0 commit comments

Comments
 (0)