Fix bug where removing material set items weren't consistent in how they purged materials and profiles

Previously remove_profile would purge unused materials and profiles, but
remove_layer and remove_constituent wouldn't. This was as subtle
inconsitency. Now everything by default consistently retains materials
and profiles, and has options to change this default.
This commit is contained in:
Dion Moult
2025-02-11 20:00:17 +11:00
parent 1f83b4f7a0
commit eeac0a2e30
4 changed files with 34 additions and 27 deletions
@@ -15,19 +15,19 @@
#
# 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
import ifcopenshell.util.element
def remove_layer(file: ifcopenshell.file, layer: ifcopenshell.entity_instance) -> None:
def remove_layer(
file: ifcopenshell.file, layer: ifcopenshell.entity_instance, should_remove_material: bool = False
) -> None:
"""Removes a layer from a layer set
Note that it is invalid to have zero items in a set, so you should leave
at least one layer to ensure a valid IFC dataset.
:param layer: The IfcMaterialLayer entity you want to remove
:type layer: ifcopenshell.entity_instance
:return: None
:rtype: None
:param should_remove_material: If true, materials with no users will be removed
Example:
@@ -54,6 +54,7 @@ def remove_layer(file: ifcopenshell.file, layer: ifcopenshell.entity_instance) -
# one one side such as to line a services riser.
ifcopenshell.api.material.remove_layer(model, layer=layer3)
"""
settings = {"layer": layer}
file.remove(settings["layer"])
material = layer.Material
file.remove(layer)
if material and should_remove_material:
ifcopenshell.util.element.remove_deep2(file, subelement)