mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-30 03:59:55 +00:00
See #5972. You can now remove boolean operations in the stack without deleting operands.
This commit is contained in:
@@ -88,6 +88,7 @@ classes = (
|
|||||||
opening.HideOpenings,
|
opening.HideOpenings,
|
||||||
opening.PurgeUnusedOpenings,
|
opening.PurgeUnusedOpenings,
|
||||||
opening.RecalculateFill,
|
opening.RecalculateFill,
|
||||||
|
opening.RemoveBoolean,
|
||||||
opening.RemoveBooleans,
|
opening.RemoveBooleans,
|
||||||
opening.ShowBooleans,
|
opening.ShowBooleans,
|
||||||
opening.ShowOpenings,
|
opening.ShowOpenings,
|
||||||
|
|||||||
@@ -1144,6 +1144,27 @@ class PurgeUnusedOpenings(Operator, tool.Ifc.Operator):
|
|||||||
return {"FINISHED"}
|
return {"FINISHED"}
|
||||||
|
|
||||||
|
|
||||||
|
class RemoveBoolean(Operator, tool.Ifc.Operator):
|
||||||
|
bl_idname = "bim.remove_boolean"
|
||||||
|
bl_label = "Remove Boolean"
|
||||||
|
bl_options = {"REGISTER", "UNDO"}
|
||||||
|
bl_description = "Removes the actively selected boolean"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def poll(cls, context):
|
||||||
|
props = context.scene.BIMBooleanProperties
|
||||||
|
return props.active_boolean
|
||||||
|
|
||||||
|
def _execute(self, context):
|
||||||
|
props = context.scene.BIMBooleanProperties
|
||||||
|
ifcopenshell.api.geometry.remove_boolean(
|
||||||
|
tool.Ifc.get(), tool.Ifc.get().by_id(props.active_boolean.ifc_definition_id)
|
||||||
|
)
|
||||||
|
bpy.ops.bim.enable_editing_booleans()
|
||||||
|
rep_obj = bpy.context.scene.BIMGeometryProperties.representation_obj
|
||||||
|
tool.Geometry.reload_representation(rep_obj)
|
||||||
|
|
||||||
|
|
||||||
# TODO: merge with ProfileDecorator?
|
# TODO: merge with ProfileDecorator?
|
||||||
class DecorationsHandler:
|
class DecorationsHandler:
|
||||||
installed = None
|
installed = None
|
||||||
|
|||||||
@@ -45,3 +45,8 @@ class BIMBooleanProperties(PropertyGroup):
|
|||||||
name="Operator",
|
name="Operator",
|
||||||
default="DIFFERENCE",
|
default="DIFFERENCE",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def active_boolean(self):
|
||||||
|
if self.booleans and self.active_boolean_index < len(self.booleans):
|
||||||
|
return self.booleans[self.active_boolean_index]
|
||||||
|
|||||||
@@ -176,6 +176,10 @@ class BIM_PT_booleans(Panel):
|
|||||||
row.prop(props, "operator", text="")
|
row.prop(props, "operator", text="")
|
||||||
row.operator("bim.add_boolean", text="", icon="ADD")
|
row.operator("bim.add_boolean", text="", icon="ADD")
|
||||||
|
|
||||||
|
row = layout.row(align=True)
|
||||||
|
row.alignment = "RIGHT"
|
||||||
|
row.operator("bim.remove_boolean", text="", icon="X")
|
||||||
|
|
||||||
self.layout.template_list("BIM_UL_booleans", "", props, "booleans", props, "active_boolean_index")
|
self.layout.template_list("BIM_UL_booleans", "", props, "booleans", props, "active_boolean_index")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,40 +20,42 @@ import ifcopenshell.util.element
|
|||||||
|
|
||||||
|
|
||||||
def remove_boolean(file: ifcopenshell.file, item: ifcopenshell.entity_instance) -> None:
|
def remove_boolean(file: ifcopenshell.file, item: ifcopenshell.entity_instance) -> None:
|
||||||
usecase = Usecase()
|
"""Removes a boolean operation without deleting the operands
|
||||||
usecase.file = file
|
|
||||||
usecase.settings = {"item": item}
|
|
||||||
return usecase.execute()
|
|
||||||
|
|
||||||
|
The first operand will replace the boolean result itself, and the second
|
||||||
|
operand will be reset as a top level representation item.
|
||||||
|
|
||||||
class Usecase:
|
This may affect the Items of IfcShapeRepresentation, so it is recommended
|
||||||
def execute(self):
|
to run :func:`ifcopenshell.api.geometry.validate_type` after all boolean
|
||||||
item = None
|
modifications are complete.
|
||||||
for inverse in self.file.get_inverse(self.settings["item"]):
|
|
||||||
|
: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"):
|
if inverse.is_a("IfcBooleanResult"):
|
||||||
item = inverse
|
remove_boolean(file, inverse)
|
||||||
break
|
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
|
first = item.FirstOperand
|
||||||
second_operand = item.SecondOperand
|
second = item.SecondOperand
|
||||||
for inverse in self.file.get_inverse(item):
|
for inverse in file.get_inverse(item):
|
||||||
ifcopenshell.util.element.replace_attribute(inverse, item, first_operand)
|
ifcopenshell.util.element.replace_attribute(inverse, item, first)
|
||||||
self.file.remove(item)
|
|
||||||
ifcopenshell.util.element.remove_deep2(self.file, second_operand)
|
|
||||||
|
|
||||||
item_classes = {i.is_a() for i in representation.Items}
|
for representation in set(representations):
|
||||||
if "IfcBooleanResult" in item_classes:
|
representation.Items = list(representation.Items) + [second]
|
||||||
representation.RepresentationType = "CSG"
|
|
||||||
elif "IfcBooleanClippingResult" in item_classes:
|
|
||||||
representation.RepresentationType = "Clipping"
|
|
||||||
else:
|
|
||||||
representation.RepresentationType = "SweptSolid"
|
|
||||||
|
|
||||||
def get_representation(self, item):
|
file.remove(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)
|
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in New Issue
Block a user