See #5972. You can now remove boolean operations in the stack without deleting operands.

This commit is contained in:
Dion Moult
2025-01-19 01:08:47 +11:00
parent 8c746b1a2c
commit e40644c4f6
6 changed files with 150 additions and 30 deletions
@@ -20,40 +20,42 @@ import ifcopenshell.util.element
def remove_boolean(file: ifcopenshell.file, item: ifcopenshell.entity_instance) -> None:
usecase = Usecase()
usecase.file = file
usecase.settings = {"item": item}
return usecase.execute()
"""Removes a boolean operation without deleting the operands
The first operand will replace the boolean result itself, and the second
operand will be reset as a top level representation item.
class Usecase:
def execute(self):
item = None
for inverse in self.file.get_inverse(self.settings["item"]):
This may affect the Items of IfcShapeRepresentation, so it is recommended
to run :func:`ifcopenshell.api.geometry.validate_type` after all boolean
modifications are complete.
:param item: This may either be an IfcBooleanResult or an
IfcRepresentationItem that is participating in one or more boolean
results (in which case all are removed).
"""
if not item.is_a("IfcBooleanResult"):
for inverse in file.get_inverse(item):
if inverse.is_a("IfcBooleanResult"):
item = inverse
break
remove_boolean(file, inverse)
return
representation = self.get_representation(item)
representations = []
queue = list(file.get_inverse(item))
while queue:
inverse = queue.pop()
if inverse.is_a("IfcShapeRepresentation"):
representations.append(inverse)
elif inverse.is_a("IfcBooleanResult"):
queue.extend(file.get_inverse(inverse))
elif inverse.is_a("IfcCsgSolid"):
queue.extend(file.get_inverse(inverse))
first_operand = item.FirstOperand
second_operand = item.SecondOperand
for inverse in self.file.get_inverse(item):
ifcopenshell.util.element.replace_attribute(inverse, item, first_operand)
self.file.remove(item)
ifcopenshell.util.element.remove_deep2(self.file, second_operand)
first = item.FirstOperand
second = item.SecondOperand
for inverse in file.get_inverse(item):
ifcopenshell.util.element.replace_attribute(inverse, item, first)
item_classes = {i.is_a() for i in representation.Items}
if "IfcBooleanResult" in item_classes:
representation.RepresentationType = "CSG"
elif "IfcBooleanClippingResult" in item_classes:
representation.RepresentationType = "Clipping"
else:
representation.RepresentationType = "SweptSolid"
for representation in set(representations):
representation.Items = list(representation.Items) + [second]
def get_representation(self, item):
for inverse in self.file.get_inverse(item):
if inverse.is_a("IfcShapeRepresentation"):
return inverse
elif inverse.is_a("IfcRepresentationItem"):
return self.get_representation(inverse)
file.remove(item)
@@ -0,0 +1,87 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2023 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 test.bootstrap
import ifcopenshell.api.root
import ifcopenshell.api.context
import ifcopenshell.api.geometry
class TestRemoveBoolean(test.bootstrap.IFC4):
def test_removing_a_single_top_level_boolean(self):
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
model = ifcopenshell.api.context.add_context(self.file, context_type="Model")
body = ifcopenshell.api.context.add_context(
self.file, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=model
)
builder = ifcopenshell.util.shape_builder.ShapeBuilder(self.file)
first = builder.sphere()
second = builder.block()
rep = builder.get_representation(body, [first, second])
booleans = ifcopenshell.api.geometry.add_boolean(self.file, first, [second])
ifcopenshell.api.geometry.remove_boolean(self.file, booleans[0])
assert set(rep.Items) == {first, second}
assert not self.file.by_type("IfcBooleanResult")
def test_removing_a_top_level_nested_boolean(self):
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
model = ifcopenshell.api.context.add_context(self.file, context_type="Model")
body = ifcopenshell.api.context.add_context(
self.file, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=model
)
builder = ifcopenshell.util.shape_builder.ShapeBuilder(self.file)
first = builder.sphere()
second1 = builder.block()
second2 = builder.block()
rep = builder.get_representation(body, [first])
ifcopenshell.api.geometry.add_boolean(self.file, first, [second1, second2])
ifcopenshell.api.geometry.remove_boolean(self.file, second2)
assert len(rep.Items) == 2
assert second2 in rep.Items
boolean = self.file.by_type("IfcBooleanResult")[0]
assert boolean in rep.Items
assert boolean.FirstOperand == first
assert boolean.SecondOperand == second1
def test_removing_a_nested_boolean(self):
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
model = ifcopenshell.api.context.add_context(self.file, context_type="Model")
body = ifcopenshell.api.context.add_context(
self.file, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=model
)
builder = ifcopenshell.util.shape_builder.ShapeBuilder(self.file)
first = builder.sphere()
second1 = builder.block()
second2 = builder.block()
rep = builder.get_representation(body, [first])
ifcopenshell.api.geometry.add_boolean(self.file, first, [second1, second2])
ifcopenshell.api.geometry.remove_boolean(self.file, second1)
assert len(rep.Items) == 2
assert second1 in rep.Items
boolean = self.file.by_type("IfcBooleanResult")[0]
assert boolean in rep.Items
assert boolean.FirstOperand == first
assert boolean.SecondOperand == second2
class TestRemoveBooleanIFC2X3(test.bootstrap.IFC2X3, TestRemoveBoolean):
pass