diff --git a/src/bonsai/bonsai/core/spatial.py b/src/bonsai/bonsai/core/spatial.py index 47616eff24..7c66e8b075 100644 --- a/src/bonsai/bonsai/core/spatial.py +++ b/src/bonsai/bonsai/core/spatial.py @@ -20,9 +20,10 @@ from __future__ import annotations from typing import TYPE_CHECKING, Optional, Union +import ifcopenshell + if TYPE_CHECKING: import bpy - import ifcopenshell import bonsai.tool as tool @@ -186,9 +187,6 @@ def generate_space( """ :return: None if successful, error message string if not. """ - if not root.get_default_container(): - raise SpaceGenerationError("Please set a default container to create the space in.") - active_obj = spatial.get_active_obj() selected_objects = spatial.get_selected_objects() element = None @@ -206,7 +204,14 @@ def generate_space( else: x, y, z, h, mat = spatial.get_x_y_z_h_mat_from_cursor() - space_polygon, bounding_walls = spatial.get_space_polygon_from_context_visible_objects(x, y) + if element and element.is_a("IfcSpace"): + container = ifcopenshell.util.element.get_parent(element) or root.get_default_container() + else: + container = root.get_default_container() + if not container: + raise SpaceGenerationError("Please set a default container to create the space in.") + + space_polygon, bounding_walls = spatial.get_space_polygon_from_context_visible_objects(x, y, container=container) if isinstance(space_polygon, str): if space_polygon == "NO POLYGONS FOUND": diff --git a/src/bonsai/bonsai/tool/spatial.py b/src/bonsai/bonsai/tool/spatial.py index 162ba789d9..37e0302f8f 100644 --- a/src/bonsai/bonsai/tool/spatial.py +++ b/src/bonsai/bonsai/tool/spatial.py @@ -874,13 +874,14 @@ class Spatial(bonsai.core.tool.Spatial): return ifcopenshell.util.space.get_boundary_lines(tool.Ifc.get(), cache["shapes"], cut_z) @classmethod - def get_space_polygon_from_context_visible_objects(cls, x: float, y: float) -> tuple[ + def get_space_polygon_from_context_visible_objects(cls, x: float, y: float, container: Optional[ifcopenshell.entity_instance] = None) -> tuple[ Union[shapely.Polygon, Literal["NO POLYGONS FOUND", "NO POLYGON FOR POINT"]], list[ifcopenshell.entity_instance], ]: props = tool.Model.get_model_props() calculation_rl = props.rl3 - container = tool.Root.get_default_container() + if container is None: + container = tool.Root.get_default_container() container_obj = tool.Ifc.get_object(container) cut_z = container_obj.matrix_world.translation.z + calculation_rl