New feature to select the container of an object

This commit is contained in:
Dion Moult
2021-11-07 15:32:33 +11:00
parent de561b78e0
commit 05068754b9
10 changed files with 58 additions and 9 deletions
-6
View File
@@ -237,7 +237,6 @@ class IfcStore:
is_top_level_operator = not bool(IfcStore.current_transaction)
if is_top_level_operator:
active_object = context.active_object
IfcStore.begin_transaction(operator)
IfcStore.get_file().begin_transaction()
# This empty transaction ensures that each operator has at least one transaction
@@ -253,11 +252,6 @@ class IfcStore:
operator, rollback=lambda d: IfcStore.get_file().undo(), commit=lambda d: IfcStore.get_file().redo()
)
IfcStore.end_transaction(operator)
try:
active_object.name
context.view_layer.objects.active = active_object
except:
pass
return result
@@ -21,11 +21,12 @@ from . import ui, prop, operator
classes = (
operator.AssignContainer,
operator.RemoveContainer,
operator.EnableEditingContainer,
operator.DisableEditingContainer,
operator.ChangeSpatialLevel,
operator.CopyToContainer,
operator.DisableEditingContainer,
operator.EnableEditingContainer,
operator.RemoveContainer,
operator.SelectContainer,
prop.SpatialElement,
prop.BIMSpatialProperties,
prop.BIMObjectSpatialProperties,
@@ -103,3 +103,12 @@ class CopyToContainer(bpy.types.Operator, Operator):
for obj in context.selected_objects:
core.copy_to_container(tool.Ifc, tool.Spatial, obj=obj, containers=containers)
blenderbim.bim.handler.purge_module_data()
class SelectContainer(bpy.types.Operator, Operator):
bl_idname = "bim.select_container"
bl_label = "Select Container"
bl_options = {"REGISTER", "UNDO"}
def _execute(self, context):
core.select_container(tool.Ifc, tool.Spatial, obj=context.active_object)
@@ -62,6 +62,7 @@ class BIM_PT_spatial(Panel):
row = self.layout.row(align=True)
if SpatialData.data["is_contained"]:
row.label(text=SpatialData.data["label"])
row.operator("bim.select_container", icon="TRACKER", text="")
row.operator("bim.enable_editing_container", icon="GREASEPENCIL", text="")
if SpatialData.data["is_directly_contained"]:
row.operator("bim.remove_container", icon="X", text="")
@@ -66,3 +66,10 @@ def copy_to_container(ifc, spatial, obj=None, containers=None):
spatial.run_root_copy_class(obj=copied_obj)
spatial.run_spatial_assign_container(structure_obj=to_container_obj, element_obj=copied_obj)
spatial.disable_editing(obj)
def select_container(ifc, spatial, obj=None):
element = ifc.get_entity(obj)
if not element:
return
spatial.set_active_object(ifc.get_object(spatial.get_container(element)))
+1
View File
@@ -196,6 +196,7 @@ class Spatial:
def import_containers(cls, parent=None): pass
def run_root_copy_class(cls, obj=None): pass
def run_spatial_assign_container(cls, structure_obj=None, element_obj=None): pass
def set_active_object(cls, obj): pass
def set_relative_object_matrix(cls, target_obj, relative_to_obj, matrix): pass
@@ -101,6 +101,11 @@ class Spatial(blenderbim.core.tool.Spatial):
tool.Ifc, tool.Collector, tool.Spatial, structure_obj=structure_obj, element_obj=element_obj
)
@classmethod
def set_active_object(cls, obj):
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
@classmethod
def set_relative_object_matrix(cls, target_obj, relative_to_obj, matrix):
target_obj.matrix_world = relative_to_obj.matrix_world @ matrix
@@ -40,3 +40,16 @@ Scenario: Copy to container
When I set "scene.BIMSpatialProperties.containers[0].is_selected" to "True"
And I press "bim.copy_to_container"
Then the object "IfcWall/Cube.001" is in the collection "IfcSite/My Site"
Scenario: Select container
Given an empty IFC project
And I add a cube
And the object "Cube" is selected
And I set "scene.BIMRootProperties.ifc_class" to "IfcWall"
And I press "bim.assign_class"
And the object "IfcWall/Cube" is selected
And I press "bim.enable_editing_container"
And the variable "site" is "tool.Ifc.get().by_type('IfcSite')[0].id()"
And I press "bim.assign_container(structure={site})"
When I press "bim.select_container"
Then nothing happens
+9
View File
@@ -94,3 +94,12 @@ class TestCopyToContainer:
spatial.disable_editing("obj").should_be_called()
subject.copy_to_container(ifc, spatial, obj="obj", containers=["to_container"])
class TestSelectContainer:
def test_run(self, ifc, spatial):
ifc.get_entity("obj").should_be_called().will_return("element")
spatial.get_container("element").should_be_called().will_return("container")
ifc.get_object("container").should_be_called().will_return("container_obj")
spatial.set_active_object("container_obj").should_be_called()
subject.select_container(ifc, spatial, obj="obj")
+9
View File
@@ -192,6 +192,15 @@ class TestRunSpatialAssignContainer(NewFile):
pass
class TestSetActiveObject(NewFile):
def test_run(self):
obj = bpy.data.objects.new("Object", None)
bpy.context.scene.collection.objects.link(obj)
subject.set_active_object(obj)
assert bpy.context.view_layer.objects.active == obj
assert obj in bpy.context.selected_objects
class TestSetRelativeObjectMatrix(NewFile):
def test_run(self):
obj = bpy.data.objects.new("Object", None)