def getAttributeValue(self, value): """Recursive method that walks through all nested objects of an attribute and returns a IFC.JSON-4 model structure Parameters: value Returns: attribute data converted to IFC.JSON-4 model structure """ if value == None or value == '': jsonValue = None elif isinstance(value, ifcopenshell.entity_instance): entity = value entityAttributes = entity.__dict__ # Remove empty properties if entity.is_a() == 'IfcPropertySingleValue': try: value = entity.NominalValue.wrappedValue if not value: return None except: return None # Add unit dimensions https://standards.buildingsmart.org/IFC/DEV/IFC4_2/FINAL/HTML/schema/ifcmeasureresource/lexical/ifcdimensionsforsiunit.htm if entity.is_a() == 'IfcSIUnit': entityAttributes['dimensions'] = self.getDimensionsForSiUnit( entity) # All objects with a GlobalId must be referenced, all others nested if entity.id() in self.rootobjects: entityAttributes["GlobalId"] = self.rootobjects[entity.id()] return self.createReferenceObject(entityAttributes, self.compact) else: if 'GlobalId' in entityAttributes: entityAttributes["GlobalId"] = guid.split( guid.expand(entity.GlobalId))[1:-1] return self.createFullObject(entityAttributes) elif isinstance(value, tuple): jsonValue = None subEnts = [] for subEntity in value: attrValue = self.getAttributeValue(subEntity) if attrValue: subEnts.append(attrValue) jsonValue = subEnts else: jsonValue = value return jsonValue
def spf2Json(self): """ Create json dictionary structure for all attributes of the objects in the root list also including inverse attributes (except for IfcGeometricRepresentationContext and IfcOwnerHistory types) # (?) Check every IFC object to see if it is used multiple times Returns: dict: IFC.JSON-4 model structure """ jsonObjects = [] for entity in self.ifcModel.by_type('IfcOwnerHistory'): self.ownerHistories[entity.id()] = str(uuid.uuid4()) for entity in self.ifcModel.by_type('IfcGeometricRepresentationContext'): self.representationContexts[entity.id()] = str(uuid.uuid4()) for entity in self.ifcModel.by_type('IfcObjectDefinition'): self.objectDefinitions[entity.id()] = guid.split( guid.expand(entity.GlobalId))[1:-1] self.rootobjects = dict(self.ownerHistories) self.rootobjects.update(self.representationContexts) self.rootobjects.update(self.objectDefinitions) for key in self.rootobjects: entity = self.ifcModel.by_id(key) entityAttributes = entity.__dict__ entityType = entityAttributes['type'] if not entityType in ['IfcGeometricRepresentationContext', 'IfcOwnerHistory']: for attr in entity.wrapped_data.get_inverse_attribute_names(): inverseAttribute = getattr(entity, attr) attrValue = self.getAttributeValue(inverseAttribute) if attrValue: entityAttributes[attr] = attrValue else: continue entityAttributes["GlobalId"] = self.rootobjects[entity.id()] jsonObjects.append(self.createFullObject(entityAttributes)) return { 'fileSchema': 'IFC.JSON-4', 'originatingSystem': 'IFC2JSON_python', 'timeStamp': datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), 'data': jsonObjects }
def getAttributeValue(self, value): """Recursive method that walks through all nested objects of an attribute and returns a IFC.JSON-4 model structure Parameters: value Returns: attribute data converted to IFC.JSON-4 model structure """ if value == None or value == '': jsonValue = None elif isinstance(value, ifcopenshell.entity_instance): entity = value entityAttributes = entity.__dict__ # Remove empty properties if entity.is_a('IfcProperty'): if not self.EMPTY_PROPERTIES: if self.empty_property(entity): return None # Add unit dimensions https://standards.buildingsmart.org/IFC/DEV/IFC4_2/FINAL/HTML/schema/ifcmeasureresource/lexical/ifcdimensionsforsiunit.htm if entity.is_a('IfcSIUnit'): entityAttributes['dimensions'] = self.getDimensionsForSiUnit( entity) # All objects with a GlobalId must be referenced, all others nested if entity.id() in self.rootObjects: entityAttributes["GlobalId"] = self.rootObjects[entity.id()] return self.createReferenceObject(entityAttributes, self.COMPACT) else: if 'GlobalId' in entityAttributes: entityAttributes["GlobalId"] = guid.split( guid.expand(entity.GlobalId))[1:-1] return self.createFullObject(entityAttributes) elif isinstance(value, tuple): jsonValue = tuple(x for x in map(self.getAttributeValue, value) if x is not None) else: jsonValue = value return jsonValue
def spf2Json(self): """ Create json dictionary structure for all attributes of the objects in the root list (?) also including inverse attributes (?) Check every IFC object to see if it is used multiple times Returns: dict: IFC.JSON-5a model structure """ jsonObjects = [] for entity in self.ifcModel.by_type('IfcObjectDefinition'): self.rootObjects[entity.id()] = guid.split( guid.expand(entity.GlobalId))[1:-1] for key in self.rootObjects: entity = self.ifcModel.by_id(key) entityAttributes = entity.__dict__ entityType = entityAttributes['type'] if not entityType in [ 'IfcGeometricRepresentationContext', 'IfcOwnerHistory' ]: for attr in entity.wrapped_data.get_inverse_attribute_names(): inverseAttribute = getattr(entity, attr) entityAttributes[attr] = self.getAttributeValue( inverseAttribute) entityAttributes["GlobalId"] = self.rootObjects[entity.id()] # Convert representations to OBJ if 'Representation' in entityAttributes: obj = self.toObj(entity) if obj: id = guid.split(guid.expand(guid.new()))[1:-1] ref = {} if not self.COMPACT: ref['type'] = "shapeRepresentation" ref['ref'] = id entityAttributes['representations'] = [ref] self.representations[id] = { "type": "shapeRepresentation", "globalId": id, "representationIdentifier": "Body", "representationType": "OBJ", "items": [obj] } # (!) delete original representation, even if OBJ generation fails del entityAttributes['Representation'] jsonObjects.append(self.createFullObject(entityAttributes)) jsonObjects = jsonObjects + list(self.representations.values()) return { 'type': 'IFC.JSON-5a', 'version': self.SCHEMA_VERSION, 'schemaIdentifier': self.ifcModel.wrapped_data.schema, 'originatingSystem': 'IFC2JSON_python Version ' + self.VERSION, 'preprocessorVersion': 'IfcOpenShell ' + ifcopenshell.version, 'timeStamp': datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), 'data': jsonObjects }
def spf2Json(self): """ Create json dictionary structure for all attributes of the objects in the root list also including inverse attributes (except for IfcGeometricRepresentationContext and IfcOwnerHistory types) # (?) Check every IFC object to see if it is used multiple times Returns: dict: IFC.JSON-4 model structure """ jsonObjects = [] relationships = [] # Collect all entity types that already have a GlobalId for entity in self.ifcModel.by_type('IfcRoot'): if entity.is_a('IfcRelationship'): relationships.append(entity) else: self.rootObjects[entity.id()] = guid.split( guid.expand(entity.GlobalId))[1:-1] # seperately collect all entity types where a GlobalId needs to be added # for entity in self.ifcModel.by_type('IfcMaterialDefinition'): # self.rootObjects[entity.id()] = str(uuid.uuid4()) for entity in self.ifcModel.by_type('IfcShapeRepresentation'): self.rootObjects[entity.id()] = str(uuid.uuid4()) for entity in self.ifcModel.by_type('IfcOwnerHistory'): self.rootObjects[entity.id()] = str(uuid.uuid4()) for entity in self.ifcModel.by_type( 'IfcGeometricRepresentationContext'): self.rootObjects[entity.id()] = str(uuid.uuid4()) # Seperately add all IfcRelationship entities so they appear at the end of the list for entity in relationships: self.rootObjects[entity.id()] = guid.split( guid.expand(entity.GlobalId))[1:-1] for key in self.rootObjects: entity = self.ifcModel.by_id(key) entityAttributes = entity.__dict__ entityType = entityAttributes['type'] if not entityType == 'IfcOwnerHistory': if not self.INCLUDE_INVERSE: for attr in entity.wrapped_data.get_inverse_attribute_names( ): inverseAttribute = getattr(entity, attr) attrValue = self.getAttributeValue(inverseAttribute) if not attrValue and attrValue is not False: continue else: entityAttributes[attr] = attrValue entityAttributes["GlobalId"] = self.rootObjects[entity.id()] jsonObjects.append(self.createFullObject(entityAttributes)) return { 'fileSchema': 'IFC.JSON-4', 'originatingSystem': 'IFC2JSON_python', 'timeStamp': datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), 'data': jsonObjects }