Migrate remove_deep to remove_deep2 across API modules

remove_deep is deprecated and can silently delete elements still in use.
remove_deep2 requires zero inverses before removal, making it safer.
Also fixes a double-removal bug in remove_grid_axis and prevents
removing the last prop template from a pset template.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dion Moult
2026-03-21 15:56:12 +11:00
parent c026dd3b6e
commit bcfad8d96d
13 changed files with 264 additions and 17 deletions
@@ -0,0 +1,72 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# 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 <http://www.gnu.org/licenses/>.
import ifcopenshell.api.cost
import ifcopenshell.api.unit
import test.bootstrap
class TestEditCostValue(test.bootstrap.IFC4):
def test_editing_applied_value(self):
schedule = ifcopenshell.api.cost.add_cost_schedule(self.file)
item = ifcopenshell.api.cost.add_cost_item(self.file, cost_schedule=schedule)
value = ifcopenshell.api.cost.add_cost_value(self.file, parent=item)
ifcopenshell.api.cost.edit_cost_value(self.file, cost_value=value, attributes={"AppliedValue": 42.0})
assert value.AppliedValue.wrappedValue == 42.0
def test_editing_unit_basis_removes_old_deeply(self):
schedule = ifcopenshell.api.cost.add_cost_schedule(self.file)
item = ifcopenshell.api.cost.add_cost_item(self.file, cost_schedule=schedule)
value = ifcopenshell.api.cost.add_cost_value(self.file, parent=item)
unit = ifcopenshell.api.unit.add_si_unit(self.file, unit_type="LENGTHUNIT")
ifcopenshell.api.cost.edit_cost_value(
self.file,
cost_value=value,
attributes={"UnitBasis": {"ValueComponent": 1.0, "UnitComponent": unit}},
)
old_basis = value.UnitBasis
assert old_basis is not None
old_basis_id = old_basis.id()
# Now change to a new unit basis — the old one should be deeply removed.
ifcopenshell.api.cost.edit_cost_value(
self.file,
cost_value=value,
attributes={"UnitBasis": {"ValueComponent": 2.0, "UnitComponent": unit}},
)
assert value.UnitBasis is not None
assert value.UnitBasis.id() != old_basis_id
def test_clearing_unit_basis(self):
schedule = ifcopenshell.api.cost.add_cost_schedule(self.file)
item = ifcopenshell.api.cost.add_cost_item(self.file, cost_schedule=schedule)
value = ifcopenshell.api.cost.add_cost_value(self.file, parent=item)
unit = ifcopenshell.api.unit.add_si_unit(self.file, unit_type="LENGTHUNIT")
ifcopenshell.api.cost.edit_cost_value(
self.file,
cost_value=value,
attributes={"UnitBasis": {"ValueComponent": 1.0, "UnitComponent": unit}},
)
assert value.UnitBasis is not None
ifcopenshell.api.cost.edit_cost_value(
self.file, cost_value=value, attributes={"UnitBasis": None}
)
assert value.UnitBasis is None
class TestEditCostValueIFC4X3(test.bootstrap.IFC4X3, TestEditCostValue):
pass
@@ -0,0 +1,59 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# 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 <http://www.gnu.org/licenses/>.
import ifcopenshell.api.grid
import test.bootstrap
class TestRemoveGridAxis(test.bootstrap.IFC4):
def test_removing_an_axis_removes_its_curve(self):
grid = self.file.createIfcGrid()
axis = ifcopenshell.api.grid.create_grid_axis(
self.file, axis_tag="A", same_sense=True, uvw_axes="UAxes", grid=grid
)
axis.AxisCurve = self.file.createIfcPolyline([self.file.createIfcCartesianPoint((0.0, 0.0, 0.0))])
axis2 = ifcopenshell.api.grid.create_grid_axis(
self.file, axis_tag="B", same_sense=True, uvw_axes="UAxes", grid=grid
)
axis2.AxisCurve = self.file.createIfcPolyline([self.file.createIfcCartesianPoint((1.0, 0.0, 0.0))])
ifcopenshell.api.grid.remove_grid_axis(self.file, axis=axis2)
assert grid.UAxes == (axis,)
assert len(self.file.by_type("IfcGridAxis")) == 1
# The curve should be removed since it was only used by the removed axis.
assert len(self.file.by_type("IfcPolyline")) == 1
def test_removing_an_axis_preserves_shared_curve(self):
grid = self.file.createIfcGrid()
shared_curve = self.file.createIfcPolyline([self.file.createIfcCartesianPoint((0.0, 0.0, 0.0))])
axis = ifcopenshell.api.grid.create_grid_axis(
self.file, axis_tag="A", same_sense=True, uvw_axes="UAxes", grid=grid
)
axis.AxisCurve = shared_curve
axis2 = ifcopenshell.api.grid.create_grid_axis(
self.file, axis_tag="B", same_sense=True, uvw_axes="UAxes", grid=grid
)
axis2.AxisCurve = shared_curve
ifcopenshell.api.grid.remove_grid_axis(self.file, axis=axis2)
assert grid.UAxes == (axis,)
# The shared curve should be preserved since it's still used by axis.
assert shared_curve in self.file
assert axis.AxisCurve == shared_curve
class TestRemoveGridAxisIFC2X3(test.bootstrap.IFC2X3, TestRemoveGridAxis):
pass
@@ -0,0 +1,38 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# 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 <http://www.gnu.org/licenses/>.
import ifcopenshell.api.pset_template
import test.bootstrap
class TestRemovePropTemplate(test.bootstrap.IFC4):
def test_removing_a_prop_template(self):
template = ifcopenshell.api.pset_template.add_pset_template(self.file, name="ABC_RiskFactors")
prop1 = ifcopenshell.api.pset_template.add_prop_template(self.file, pset_template=template)
prop2 = ifcopenshell.api.pset_template.add_prop_template(self.file, pset_template=template)
ifcopenshell.api.pset_template.remove_prop_template(self.file, prop_template=prop2)
assert len(self.file.by_type("IfcSimplePropertyTemplate")) == 1
assert template.HasPropertyTemplates == (prop1,)
def test_not_removing_the_last_prop_template(self):
template = ifcopenshell.api.pset_template.add_pset_template(self.file, name="ABC_RiskFactors")
prop = ifcopenshell.api.pset_template.add_prop_template(self.file, pset_template=template)
ifcopenshell.api.pset_template.remove_prop_template(self.file, prop_template=prop)
# The last prop template should not be removed to keep the pset template valid.
assert len(self.file.by_type("IfcSimplePropertyTemplate")) == 1
assert template.HasPropertyTemplates == (prop,)
@@ -0,0 +1,35 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# 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 <http://www.gnu.org/licenses/>.
import ifcopenshell.api.pset_template
import test.bootstrap
class TestRemovePsetTemplate(test.bootstrap.IFC4):
def test_removing_a_pset_template(self):
template = ifcopenshell.api.pset_template.add_pset_template(self.file, name="ABC_RiskFactors")
ifcopenshell.api.pset_template.remove_pset_template(self.file, pset_template=template)
assert len(self.file.by_type("IfcPropertySetTemplate")) == 0
def test_removing_a_pset_template_with_property_templates(self):
template = ifcopenshell.api.pset_template.add_pset_template(self.file, name="ABC_RiskFactors")
prop1 = ifcopenshell.api.pset_template.add_prop_template(self.file, pset_template=template)
prop2 = ifcopenshell.api.pset_template.add_prop_template(self.file, pset_template=template)
ifcopenshell.api.pset_template.remove_pset_template(self.file, pset_template=template)
assert len(self.file.by_type("IfcPropertySetTemplate")) == 0
assert len(self.file.by_type("IfcSimplePropertyTemplate")) == 0
@@ -0,0 +1,44 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# 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 <http://www.gnu.org/licenses/>.
import ifcopenshell.api.resource
import test.bootstrap
class TestRemoveResourceQuantity(test.bootstrap.IFC4):
def test_removing_a_resource_quantity(self):
self.file.create_entity("IfcProject")
resource = ifcopenshell.api.resource.add_resource(self.file, ifc_class="IfcLaborResource")
ifcopenshell.api.resource.add_resource_quantity(
self.file, resource=resource, ifc_class="IfcQuantityTime"
)
assert resource.BaseQuantity is not None
ifcopenshell.api.resource.remove_resource_quantity(self.file, resource=resource)
assert resource.BaseQuantity is None
assert len(self.file.by_type("IfcPhysicalSimpleQuantity")) == 0
def test_removing_a_resource_quantity_when_none_exists(self):
self.file.create_entity("IfcProject")
resource = ifcopenshell.api.resource.add_resource(self.file, ifc_class="IfcLaborResource")
# Should not raise.
ifcopenshell.api.resource.remove_resource_quantity(self.file, resource=resource)
assert resource.BaseQuantity is None
class TestRemoveResourceQuantityIFC2X3(test.bootstrap.IFC2X3, TestRemoveResourceQuantity):
pass