diff --git a/src/ifcopenshell-python/ifcopenshell/util/unit.py b/src/ifcopenshell-python/ifcopenshell/util/unit.py index f729c047cc..041021d5b9 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/unit.py +++ b/src/ifcopenshell-python/ifcopenshell/util/unit.py @@ -853,8 +853,9 @@ def convert_file_length_units(ifc_file: ifcopenshell.file, target_units: str = " }, ) - file_patched.remove(old_length) unit_assignment = get_unit_assignment(file_patched) - unit_assignment.Units = tuple([new_length, *unit_assignment.Units]) + unit_assignment.Units = [new_length, *(u for u in unit_assignment.Units if u.UnitType != new_length.UnitType)] + if not file_patched.get_total_inverses(old_length): + ifcopenshell.util.element.remove_deep2(file_patched, old_length) return file_patched diff --git a/src/ifcopenshell-python/test/util/test_unit.py b/src/ifcopenshell-python/test/util/test_unit.py index 229435da73..1ee5d8b293 100644 --- a/src/ifcopenshell-python/test/util/test_unit.py +++ b/src/ifcopenshell-python/test/util/test_unit.py @@ -132,6 +132,24 @@ class TestConvertFileLengthUnits(test.bootstrap.IFC4): assert output.by_type("IfcMapConversion")[0].Scale == 1 assert subject.get_full_unit_name(output.by_type("IfcProjectedCRS")[0].MapUnit) == "METRE" + def test_preserving_enh_if_there_is_a_map_unit_which_is_also_the_project_default(self): + ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject") + meter = ifcopenshell.api.unit.add_si_unit(self.file, unit_type="LENGTHUNIT") + ifcopenshell.api.context.add_context(self.file, "Model") + ifcopenshell.api.georeference.add_georeferencing(self.file) + ifcopenshell.api.georeference.edit_georeferencing( + self.file, projected_crs={"MapUnit": meter}, coordinate_operation={"Eastings": 10, "Scale": 1} + ) + ifcopenshell.api.unit.assign_unit(self.file, units=[meter]) + output = subject.convert_file_length_units(self.file, target_units="MILLIMETER") + assert subject.get_full_unit_name(subject.get_project_unit(output, "LENGTHUNIT")) == "MILLIMETRE" + assert output.by_type("IfcMapConversion")[0].Eastings == 10 + assert output.by_type("IfcMapConversion")[0].Northings == 0 + assert output.by_type("IfcMapConversion")[0].Scale == 0.001 + assert subject.get_full_unit_name(output.by_type("IfcProjectedCRS")[0].MapUnit) == "METRE" + + unit_assignment = subject.get_unit_assignment(output) + assert len(unit_assignment.Units) == 1 class TestConvertFileLengthUnitsIFC2X3(test.bootstrap.IFC2X3): def test_converting_map_conversion_if_there_is_no_map_unit(self):