From 037fd22b518f52cf9a296aa4156523e3dc10bee3 Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Mon, 4 Aug 2025 08:53:08 +0200 Subject: [PATCH] util/unit.py::calculate_unit_scale() Check for valid unit_type --- src/ifcopenshell-python/ifcopenshell/util/unit.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ifcopenshell-python/ifcopenshell/util/unit.py b/src/ifcopenshell-python/ifcopenshell/util/unit.py index 8d2b2d8306..d2273ec129 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/unit.py +++ b/src/ifcopenshell-python/ifcopenshell/util/unit.py @@ -691,6 +691,14 @@ def calculate_unit_scale(ifc_file: ifcopenshell.file, unit_type: str = "LENGTHUN :param unit_type: The type of SI unit, defaults to "LENGTHUNIT" :returns: The scale factor """ + if ( + unit_type + not in ifcopenshell.ifcopenshell_wrapper.schema_by_name(ifc_file.schema_identifier) + .declaration_by_name("IfcUnitEnum") + .enumeration_items() + ): + raise ValueError(f"Unit type {unit_type!r} does not name a valid type") + # Currently we assume that all ifc projects must have IfcProject. if not (projects := ifc_file.by_type("IfcProject")) or not (units := projects[0].UnitsInContext): return 1