diff --git a/src/ifcblenderexport/blenderbim/bim/import_ifc.py b/src/ifcblenderexport/blenderbim/bim/import_ifc.py index 6380a68a1b..02e3c20a83 100644 --- a/src/ifcblenderexport/blenderbim/bim/import_ifc.py +++ b/src/ifcblenderexport/blenderbim/bim/import_ifc.py @@ -223,6 +223,7 @@ class IfcImporter(): self.auto_set_workarounds() self.calculate_unit_scale() self.create_project() + self.create_classifications() self.create_spatial_hierarchy() self.purge_diff() self.create_type_products() @@ -742,6 +743,40 @@ class IfcImporter(): self.project['blender'].objects.link(obj) del self.added_data[self.project['ifc'].GlobalId] + def create_classifications(self): + for classification in self.file.by_type('IfcClassification'): + data = bpy.context.scene.BIMProperties.classifications.add() + data_map = { + 'name': 'Name', 'source': 'Source', + 'edition': 'Edition', 'edition_date': 'EditionDate', + 'description': 'Description', 'location': 'Location', + 'reference_tokens': 'ReferenceTokens' + } + for key, value in data_map.items(): + if getattr(classification, value): + setattr(data, key, str(getattr(classification, value))) + classification_file = ifcopenshell.file(schema=self.file.wrapped_data.schema) + references = [classification] + while references: + entities_to_add = references + references = self.get_classification_references(references) + classification_file.add(classification) + for entity in entities_to_add: + classification_file.add(entity) + classification_filename = '{}-{}.ifc'.format( + Path(os.path.basename(self.ifc_import_settings.input_file)).stem, classification.Name) + classification_file.write(os.path.join( + bpy.context.scene.BIMProperties.schema_dir, 'classifications', classification_filename)) + + destination = os.path.join(bpy.context.scene.BIMProperties.schema_dir, + 'classifications', '{}-{}.ifc') + + def get_classification_references(self, references): + results = [] + for reference in references: + results.extend(self.file.get_inverse(reference)) + return results + def create_spatial_hierarchy(self): if self.project['ifc'].IsDecomposedBy: for rel_aggregate in self.project['ifc'].IsDecomposedBy: diff --git a/src/ifcblenderexport/blenderbim/bim/operator.py b/src/ifcblenderexport/blenderbim/bim/operator.py index 2e5e7a2141..64517f64ee 100644 --- a/src/ifcblenderexport/blenderbim/bim/operator.py +++ b/src/ifcblenderexport/blenderbim/bim/operator.py @@ -1263,13 +1263,15 @@ class AddClassification(bpy.types.Operator): if context.scene.BIMProperties.classification in schema.ifc.classifications: data = schema.ifc.classifications[context.scene.BIMProperties.classification] classification = context.scene.BIMProperties.classifications.add() - classification.source = data.Source - classification.edition = data.Edition - classification.edition_date = data.EditionDate - classification.name = data.Name - classification.description = data.Description - classification.location = data.Location - classification.reference_tokens = str(data.ReferenceTokens) + data_map = { + 'name': 'Name', 'source': 'Source', + 'edition': 'Edition', 'edition_date': 'EditionDate', + 'description': 'Description', 'location': 'Location', + 'reference_tokens': 'ReferenceTokens' + } + for key, value in data_map.items(): + if getattr(data, value): + setattr(classification, key, str(getattr(data, value))) return {'FINISHED'} diff --git a/src/ifcblenderexport/blenderbim/bim/schema.py b/src/ifcblenderexport/blenderbim/bim/schema.py index efcb8c99b2..cdd11ad6dd 100644 --- a/src/ifcblenderexport/blenderbim/bim/schema.py +++ b/src/ifcblenderexport/blenderbim/bim/schema.py @@ -31,10 +31,6 @@ class IfcSchema(): self.property_files.append(ifcopenshell.open(os.path.join(self.schema_dir, 'Pset_IFC4_ADD2.ifc'))) self.classification_files = {} - classification_paths = Path(self.schema_dir).glob('classifications/*.ifc') - for path in classification_paths: - self.classification_files[os.path.basename(path).split('.')[0]] = ifcopenshell.open(path) - self.psets = {} self.qtos = {} self.applicable_psets = {} @@ -64,11 +60,11 @@ class IfcSchema(): entity = prop.ApplicableEntity if prop.ApplicableEntity else 'IfcRoot' self.applicable_psets.setdefault(entity, []).append(prop.Name) - for filename, classification_file in self.classification_files.items(): - classification = classification_file.by_type('IfcClassification')[0] - self.classifications[filename] = classification - def load_classification(self, filename): + if filename not in self.classifications: + classification_path = os.path.join(self.schema_dir, 'classifications', '{}.ifc'.format(filename)) + self.classification_files[filename] = ifcopenshell.open(classification_path) + self.classifications[filename] = self.classification_files[filename].by_type('IfcClassification')[0] classification = self.classifications[filename] return { 'name': '',