Skip to content

Commit be72eb4

Browse files
Fixed OBJ vertex count + camelCase for entities
1 parent aa74c1b commit be72eb4

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

ifc2json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
else:
5858
print('Version ' + args.v + ' is not supported')
5959
else:
60-
print(args.i + ' is not a valid file')
60+
print(str(args.i) + ' is not a valid file')
6161
t1_stop = perf_counter()
6262
print("Conversion took ", t1_stop-t1_start, " seconds")

ifcjson/ifc2json4.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,43 @@ def spf2Json(self):
5757

5858
@functools.lru_cache(maxsize=maxCache)
5959
def entityToDict(self, entity):
60+
61+
# Entitie names must be in camelCase
62+
entityType = entity.is_a()
63+
entityType = entityType[0].lower() + entityType[1:]
6064
entityAttributes = entity.__dict__
6165

6266
ref = {
63-
"type": entity.is_a()
67+
"type": entityType
6468
}
6569

6670
# Add missing GlobalId to OwnerHistory
67-
if entity.is_a() == 'IfcOwnerHistory':
71+
if entityType == 'ifcOwnerHistory':
6872
if not entity.id() in self.ownerHistories:
6973
self.ownerHistories[entity.id()] = guid.new()
70-
entityAttributes["GlobalId"] = self.ownerHistories[entity.id()]
74+
entityAttributes["globalId"] = self.ownerHistories[entity.id()]
7175

7276
# Add missing GlobalId to IfcGeometricRepresentationContext
73-
if entity.is_a() == 'IfcGeometricRepresentationContext':
77+
if entityType == 'ifcGeometricRepresentationContext':
7478
if not entity.id() in self.representationContexts:
7579
self.representationContexts[entity.id()] = guid.new()
76-
entityAttributes["GlobalId"] = self.representationContexts[entity.id()]
80+
entityAttributes["globalId"] = self.representationContexts[entity.id()]
7781

7882
# check for globalid
7983
if "GlobalId" in entityAttributes:
8084
uuid = guid.split(guid.expand(entityAttributes["GlobalId"]))[1:-1]
8185
ref["ref"] = uuid
8286
if not entityAttributes["GlobalId"] in self.id_objects:
8387
d = {
84-
"type": entity.is_a()
88+
"type": entityType
8589
}
8690

8791
# Add missing GlobalId to OwnerHistory
88-
if entity.is_a() == 'IfcOwnerHistory':
92+
if entityType == 'ifcOwnerHistory':
8993
d["globalId"] = guid.split(guid.expand(self.ownerHistories[entity.id()]))[1:-1]
9094

9195
# Add missing GlobalId to IfcGeometricRepresentationContext
92-
if entity.is_a() == 'IfcGeometricRepresentationContext':
96+
if entityType == 'ifcGeometricRepresentationContext':
9397
d["globalId"] = guid.split(guid.expand(self.representationContexts[entity.id()]))[1:-1]
9498

9599
for i in range(0,len(entity)):
@@ -101,7 +105,7 @@ def entityToDict(self, entity):
101105
if attr in entityAttributes:
102106
jsonValue = self.getEntityValue(entityAttributes[attr])
103107
if jsonValue:
104-
if ((entity.is_a() == 'IfcOwnerHistory') and (attr == "GlobalId")):
108+
if ((entityType == 'ifcOwnerHistory') and (attr == "GlobalId")):
105109
pass
106110
else:
107111
d[attrKey] = jsonValue
@@ -126,7 +130,7 @@ def entityToDict(self, entity):
126130
return ref
127131
else:
128132
d = {
129-
"type": entity.is_a()
133+
"type": entityType
130134
}
131135

132136
for i in range(0,len(entity)):

ifcjson/ifc2json5a.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import ifcopenshell
3232
import ifcopenshell.guid as guid
3333
import ifcjson.common as common
34+
import copy
3435

3536

3637
explicitInverseAttributes = {
@@ -75,6 +76,9 @@ def entityToDict(self, entity, objData, parent=None):
7576
entityType = entity.is_a()
7677
entityType = entityType[3:]
7778

79+
# Entitie names must be in camelCase
80+
entityType = entityType[0].lower() + entityType[1:]
81+
7882
ref = {
7983
'type': entityType
8084
}
@@ -230,12 +234,12 @@ def entityToDict(self, entity, objData, parent=None):
230234
id = guid.split(guid.expand(guid.new()))[1:-1]
231235
d['representations'] = [
232236
{
233-
"type": "ShapeRepresentation",
237+
"type": "shapeRepresentation",
234238
"ref": id
235239
}
236240
]
237241
self.representations[id] = {
238-
"type": "ShapeRepresentation",
242+
"type": "shapeRepresentation",
239243
"globalId": id,
240244
"representationIdentifier": "Body",
241245
"representationType": "OBJ",
@@ -323,6 +327,7 @@ def getEntityValue(self, value, objData):
323327
# convert IFC SPF file into OBJ using IfcConvert and extract OBJ objects
324328
def getObjData(self, ifcFilePath):
325329
objFilePath = NamedTemporaryFile(suffix='.obj', delete=True).name
330+
print(objFilePath)
326331

327332
# Convert IFC to OBJ using IfcConvert (could also be done for glTF or Collada)
328333
subprocess.run([
@@ -336,21 +341,39 @@ def getObjData(self, ifcFilePath):
336341
header = True
337342
groupId = ''
338343
groupData = []
344+
vertexIterator = 0
345+
vertexCount = 0
339346
f = open(objFilePath, 'r')
347+
lc = 0
340348
for line in f:
349+
lc +=1
341350

342351
# find group
343352
if line[0] == 'g':
344353
header = False
354+
vertexCount = copy.deepcopy(vertexIterator)
345355
objData[groupId] = ''.join(groupData)
346356
groupId = line.split()[1]
347357
groupData = []
348358
else:
349359
if header:
350360
pass
351361
else:
352-
if line[0] == 'v' or line[0] == 'f':
362+
if line[0:2] == 'v ':
353363
groupData.append(line)
364+
vertexIterator += 1
365+
elif line[0] == 'f':
366+
fl = []
367+
face = line[2:].rstrip().split(' ')
368+
for fp in face:
369+
p = fp.split('//')
370+
ps0 = int(p[0]) - vertexCount
371+
ps1 = int(p[1]) - vertexCount
372+
ps = str(ps0) + '//' + str(ps1)
373+
fl.append(ps)
374+
375+
fs = 'f ' + ' '.join(fl) + '\n'
376+
groupData.append(fs)
354377
return objData
355378
else:
356379
print('Creating intermediate OBJ failed')

0 commit comments

Comments
 (0)