From dbf4a2a5ed1f7db7fd3de5186b3601ef54cc0662 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 12 Mar 2025 16:48:51 +0500 Subject: [PATCH] Fix reassign_class on IFC4X3, use ValueError Use ValueError to make it more specific --- src/ifcopenshell-python/ifcopenshell/util/schema.py | 10 +++++++--- .../test/api/root/test_reassign_class.py | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/util/schema.py b/src/ifcopenshell-python/ifcopenshell/util/schema.py index edffea2b2f..65a12b230e 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/schema.py +++ b/src/ifcopenshell-python/ifcopenshell/util/schema.py @@ -162,16 +162,20 @@ def reassign_class( (such as IfcRelNests) It's unlikely that this affects real-world usage of this function. + + :raises ValueError: If ``new_class`` does not exist in the provided file schema. """ if not ifc_file: ifc_file = element.file - schema: ifcopenshell_wrapper.schema_definition = ifcopenshell_wrapper.schema_by_name(ifc_file.schema) + schema: ifcopenshell_wrapper.schema_definition = ifcopenshell_wrapper.schema_by_name(ifc_file.schema_identifier) try: declaration = schema.declaration_by_name(new_class) - except: - raise Exception(f"Class of {element} could not be changed to {new_class} as the class does not exist") + except RuntimeError: + raise ValueError( + f"Class of {element} could not be changed to {new_class} as the class does not exist in schema {ifc_file.schema_identifier}." + ) info = element.get_info() diff --git a/src/ifcopenshell-python/test/api/root/test_reassign_class.py b/src/ifcopenshell-python/test/api/root/test_reassign_class.py index f8053e13b5..f8be13225e 100644 --- a/src/ifcopenshell-python/test/api/root/test_reassign_class.py +++ b/src/ifcopenshell-python/test/api/root/test_reassign_class.py @@ -197,6 +197,10 @@ class TestReassignClass(test.bootstrap.IFC4): assert len(self.file.by_type("IfcSlab")) == 1 +class TestReassignClassIFC4X3(test.bootstrap.IFC4X3, TestReassignClass): + pass + + class TestReassignClassIFC2X3(test.bootstrap.IFC2X3, TestReassignClass): def test_providing_occurrence_class(self): element_type = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType")