From 9e310318d43e11339d133d50746e18f9a79be593 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Fri, 12 Apr 2024 10:57:35 +0500 Subject: [PATCH] ifcpatch - add ifc2x3 tests and test for MergeDuplicateTypes --- .../test/api/type/test_assign_type.py | 2 +- .../ifcpatch/recipes/MergeDuplicateTypes.py | 58 ++++---------- src/ifcpatch/test/bootstrap.py | 52 +++++++++++++ src/ifcpatch/test/test_ExtractElements.py | 48 ++++++------ src/ifcpatch/test/test_MergeDuplicateTypes.py | 77 +++++++++++++++++++ src/ifcpatch/test/test_RegenerateGlobalIds.py | 23 +++--- 6 files changed, 181 insertions(+), 79 deletions(-) create mode 100644 src/ifcpatch/test/bootstrap.py create mode 100644 src/ifcpatch/test/test_MergeDuplicateTypes.py diff --git a/src/ifcopenshell-python/test/api/type/test_assign_type.py b/src/ifcopenshell-python/test/api/type/test_assign_type.py index e1728747ee..994146a16d 100644 --- a/src/ifcopenshell-python/test/api/type/test_assign_type.py +++ b/src/ifcopenshell-python/test/api/type/test_assign_type.py @@ -97,7 +97,7 @@ class TestAssignType(test.bootstrap.IFC4): assert mapped_rep.RepresentationType == "MappedRepresentation" assert mapped_rep.Items[0].MappingSource.MappedRepresentation == rep - def test_map_representation(self): + def test_do_not_map_representation_if_type_was_assigned_previously(self): element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType") context = self.file.createIfcGeometricRepresentationContext() rep = self.file.createIfcShapeRepresentation(ContextOfItems=context) diff --git a/src/ifcpatch/ifcpatch/recipes/MergeDuplicateTypes.py b/src/ifcpatch/ifcpatch/recipes/MergeDuplicateTypes.py index d30966d409..49c80bf813 100644 --- a/src/ifcpatch/ifcpatch/recipes/MergeDuplicateTypes.py +++ b/src/ifcpatch/ifcpatch/recipes/MergeDuplicateTypes.py @@ -71,8 +71,9 @@ class Patcher: for element_type in self.file.by_type("IfcTypeObject"): original_type = keys.get(getattr(element_type, key), None) if original_type: - for element in ifcopenshell.util.element.get_types(element_type): - self.assign_type(element, original_type) + elements = ifcopenshell.util.element.get_types(element_type) + if elements: + self.assign_type(elements, original_type) for inverse in self.file.get_inverse(element_type): ifcopenshell.util.element.replace_attribute(inverse, element_type, original_type) self.file.remove(element_type) @@ -80,49 +81,16 @@ class Patcher: keys[getattr(element_type, key)] = element_type def assign_type( - self, related_object: ifcopenshell.entity_instance, relating_type: ifcopenshell.entity_instance + self, related_objects: list[ifcopenshell.entity_instance], relating_type: ifcopenshell.entity_instance ) -> None: - # This is basically a portion of the type.assign_type API which only - # affects the IfcRelDefinesByType relationship. To be conservative, we - # don't use the API directly since that would do other things like + # To be conservative, we disable `should_map_representations` + # since that would do other things like # map type representations or recalculate material set usages which is # risky when we're patching an existing dataset. - if self.file.schema == "IFC2X3": - is_typed_by = None - is_defined_by = related_object.IsDefinedBy - for rel in is_defined_by: - if rel.is_a("IfcRelDefinesByType"): - is_typed_by = [rel] - break - types = relating_type.ObjectTypeOf - else: - is_typed_by = related_object.IsTypedBy - types = relating_type.Types - - if types and is_typed_by == types: - return - - if is_typed_by: - related_objects = list(is_typed_by[0].RelatedObjects) - related_objects.remove(related_object) - if related_objects: - is_typed_by[0].RelatedObjects = related_objects - ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": is_typed_by[0]}) - else: - self.file.remove(is_typed_by[0]) - - if types: - related_objects = list(types[0].RelatedObjects) - related_objects.append(related_object) - types[0].RelatedObjects = related_objects - ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": types[0]}) - else: - types = self.file.create_entity( - "IfcRelDefinesByType", - **{ - "GlobalId": ifcopenshell.guid.new(), - "OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file), - "RelatedObjects": [related_object], - "RelatingType": relating_type, - } - ) + ifcopenshell.api.run( + "type.assign_type", + self.file, + relating_type=relating_type, + related_objects=related_objects, + should_map_representations=False, + ) diff --git a/src/ifcpatch/test/bootstrap.py b/src/ifcpatch/test/bootstrap.py new file mode 100644 index 0000000000..1603fcdb26 --- /dev/null +++ b/src/ifcpatch/test/bootstrap.py @@ -0,0 +1,52 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2021 Thomas Krijnen +# +# This file is part of IfcOpenShell. +# +# IfcOpenShell is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# IfcOpenShell is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with IfcOpenShell. If not, see . + +import pytest +import ifcopenshell +import ifcopenshell.api +import ifcopenshell.api.owner.settings + + +class IFC4X3: + @pytest.fixture(autouse=True) + def setup(self): + self.file: ifcopenshell.file = ifcopenshell.api.run("project.create_file", version="IFC4X3") + ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0] + ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0] + ifcopenshell.api.pre_listeners = {} + ifcopenshell.api.post_listeners = {} + + +class IFC4: + @pytest.fixture(autouse=True) + def setup(self): + self.file: ifcopenshell.file = ifcopenshell.api.run("project.create_file") + ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0] + ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0] + ifcopenshell.api.pre_listeners = {} + ifcopenshell.api.post_listeners = {} + + +class IFC2X3: + @pytest.fixture(autouse=True) + def setup(self): + self.file: ifcopenshell.file = ifcopenshell.api.run("project.create_file", version="IFC2X3") + ifcopenshell.api.owner.settings.get_user = lambda ifc: ifc.createIfcPersonAndOrganization() + ifcopenshell.api.owner.settings.get_application = lambda ifc: ifc.createIfcApplication() + ifcopenshell.api.pre_listeners = {} + ifcopenshell.api.post_listeners = {} diff --git a/src/ifcpatch/test/test_ExtractElements.py b/src/ifcpatch/test/test_ExtractElements.py index a6b239ee1a..10b55bad09 100644 --- a/src/ifcpatch/test/test_ExtractElements.py +++ b/src/ifcpatch/test/test_ExtractElements.py @@ -22,31 +22,30 @@ import ifcpatch import ifcopenshell import ifcopenshell.api import ifcopenshell.util.element +import test.bootstrap -class TestExtractElements: +class TestExtractElements(test.bootstrap.IFC4): def test_basic(self): - ifc_file = ifcopenshell.file() - project = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcProject") - wall = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcWall") - output = ifcpatch.execute({"file": ifc_file, "recipe": "ExtractElements", "arguments": ["IfcWall"]}) + project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + output = ifcpatch.execute({"file": self.file, "recipe": "ExtractElements", "arguments": ["IfcWall"]}) assert output.by_type("IfcProject")[0].GlobalId == project.GlobalId assert output.by_type("IfcWall")[0].GlobalId == wall.GlobalId def test_keep_spatial_structure(self): - ifc_file = ifcopenshell.file() - project = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcProject") + project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") - site = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcSite") - building = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcBuilding") - storey = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcBuildingStorey") - wall = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcWall") - ifcopenshell.api.run("aggregate.assign_object", ifc_file, products=[building], relating_object=site) - ifcopenshell.api.run("aggregate.assign_object", ifc_file, products=[storey], relating_object=building) - ifcopenshell.api.run("spatial.assign_container", ifc_file, products=[wall], relating_structure=storey) + site = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSite") + building = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding") + storey = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuildingStorey") + wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + ifcopenshell.api.run("aggregate.assign_object", self.file, products=[building], relating_object=site) + ifcopenshell.api.run("aggregate.assign_object", self.file, products=[storey], relating_object=building) + ifcopenshell.api.run("spatial.assign_container", self.file, products=[wall], relating_structure=storey) - output = ifcpatch.execute({"file": ifc_file, "recipe": "ExtractElements", "arguments": ["IfcWall"]}) + output = ifcpatch.execute({"file": self.file, "recipe": "ExtractElements", "arguments": ["IfcWall"]}) wall_new = output.by_type("IfcWall")[0] assert (storey_new := ifcopenshell.util.element.get_container(wall_new)).GlobalId == storey.GlobalId @@ -54,16 +53,15 @@ class TestExtractElements: assert (site_new := ifcopenshell.util.element.get_aggregate(building_new)).GlobalId == site.GlobalId def test_keep_aggregate_in_spatial_structure(self): - ifc_file = ifcopenshell.file() - project = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcProject") + project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") - element = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcElementAssembly") - container = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcBuildingStorey") - subelement = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcWall") - ifcopenshell.api.run("spatial.assign_container", ifc_file, products=[element], relating_structure=container) - ifcopenshell.api.run("aggregate.assign_object", ifc_file, products=[subelement], relating_object=element) + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcElementAssembly") + container = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuildingStorey") + subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + ifcopenshell.api.run("spatial.assign_container", self.file, products=[element], relating_structure=container) + ifcopenshell.api.run("aggregate.assign_object", self.file, products=[subelement], relating_object=element) - output = ifcpatch.execute({"file": ifc_file, "recipe": "ExtractElements", "arguments": ["IfcWall"]}) + output = ifcpatch.execute({"file": self.file, "recipe": "ExtractElements", "arguments": ["IfcWall"]}) wall_new = output.by_type("IfcWall")[0] assembly = output.by_type("IfcElementAssembly")[0] @@ -76,3 +74,7 @@ class TestExtractElements: output = ifcpatch.execute({"file": ifc, "recipe": "ExtractElements", "arguments": ["IfcWall"]}) assert output.by_type("IfcWall") assert not output.by_type("IfcSlab") + + +class TestExtractElementsIFC2X3(test.bootstrap.IFC2X3, TestExtractElements): + pass diff --git a/src/ifcpatch/test/test_MergeDuplicateTypes.py b/src/ifcpatch/test/test_MergeDuplicateTypes.py new file mode 100644 index 0000000000..1b2be456d6 --- /dev/null +++ b/src/ifcpatch/test/test_MergeDuplicateTypes.py @@ -0,0 +1,77 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2022 Dion Moult +# +# This file is part of IfcOpenShell. +# +# IfcOpenShell is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# IfcOpenShell is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with IfcOpenShell. If not, see . + +import os +import pytest +import ifcpatch +import ifcopenshell +import ifcopenshell.api +import ifcopenshell.util.element +import ifcopenshell.util.representation +import test.bootstrap + + +class TestMergeDuplicateTypes(test.bootstrap.IFC4): + def test_run(self): + context = self.file.createIfcGeometricRepresentationContext() + + wall1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + wall_type1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType", name="WallType") + rep = self.file.createIfcShapeRepresentation(ContextOfItems=context) + ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall_type1, representation=rep) + ifcopenshell.api.run("material.assign_material", self.file, product=wall_type1, type="IfcMaterialLayerSet") + ifcopenshell.api.run( + "type.assign_type", + self.file, + related_objects=[wall1], + relating_type=wall_type1, + should_map_representations=False, + ) + + wall2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + wall_type2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType", name="WallType") + rep = self.file.createIfcShapeRepresentation(ContextOfItems=context) + ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall_type2, representation=rep) + ifcopenshell.api.run("material.assign_material", self.file, product=wall_type2, type="IfcMaterialLayerSet") + ifcopenshell.api.run( + "type.assign_type", + self.file, + related_objects=[wall2], + relating_type=wall_type2, + should_map_representations=False, + ) + + output = ifcpatch.execute({"file": self.file, "recipe": "MergeDuplicateTypes", "arguments": ["Name"]}) + + assert len(output.by_type("IfcWall")) == 2 + wall_types = output.by_type("IfcWallType") + assert len(wall_types) == 1 + wall_type = wall_types[0] + assert set(ifcopenshell.util.element.get_types(wall_type)) == set(output.by_type("IfcWall")) + + # test not to remap representation + assert ifcopenshell.util.representation.get_representation(wall1, context=context) == None + assert ifcopenshell.util.representation.get_representation(wall2, context=context) == None + + # and not to create material usages + assert ifcopenshell.util.element.get_material(wall1, should_inherit=False) == None + assert ifcopenshell.util.element.get_material(wall2, should_inherit=False) == None + + +class TestMergeDuplicateTypesIFC2X3(test.bootstrap.IFC2X3, TestMergeDuplicateTypes): + pass diff --git a/src/ifcpatch/test/test_RegenerateGlobalIds.py b/src/ifcpatch/test/test_RegenerateGlobalIds.py index a8e6fe4c23..ae33b60747 100644 --- a/src/ifcpatch/test/test_RegenerateGlobalIds.py +++ b/src/ifcpatch/test/test_RegenerateGlobalIds.py @@ -22,27 +22,30 @@ import ifcpatch import ifcopenshell import ifcopenshell.api import ifcopenshell.util.element +import test.bootstrap -class TestRegenerateGlobalIds: +class TestRegenerateGlobalIds(test.bootstrap.IFC4): def test_run(self): - ifc_file = ifcopenshell.file() - project = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcProject") - wall = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcWall") + project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") used_guids = {project.GlobalId, wall.GlobalId} - ifcpatch.execute({"file": ifc_file, "recipe": "RegenerateGlobalIds", "arguments": [False]}) + ifcpatch.execute({"file": self.file, "recipe": "RegenerateGlobalIds", "arguments": [False]}) new_guids = {project.GlobalId, wall.GlobalId} assert not new_guids.intersection(used_guids) def test_regenerate_guids_for_duplicates(self): - ifc_file = ifcopenshell.file() - project = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcProject") - wall1 = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcWall") - wall2 = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcWall") + project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + wall1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + wall2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") wall1.GlobalId = wall2.GlobalId used_guids = {project, wall1.GlobalId, wall2.GlobalId} - ifcpatch.execute({"file": ifc_file, "recipe": "RegenerateGlobalIds", "arguments": [True]}) + ifcpatch.execute({"file": self.file, "recipe": "RegenerateGlobalIds", "arguments": [True]}) new_guids = {project, wall1.GlobalId, wall2.GlobalId} assert len(new_guids) == 3 assert len(new_guids.intersection(used_guids)) == 2 + + +class TestRegenerateGlobalIdsIFC2X3(test.bootstrap.IFC2X3, TestRegenerateGlobalIds): + pass