| 1 |
# Bonsai - OpenBIM Blender Add-on |
| 2 |
# Copyright (C) 2023 Dion Moult <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Bonsai. |
| 5 |
# |
| 6 |
# Bonsai is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Bonsai is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
import bpy |
| 20 |
import bonsai.core.tool |
| 21 |
import bonsai.tool as tool |
| 22 |
import ifcopenshell.util.element |
| 23 |
|
| 24 |
|
| 25 |
class Nest(bonsai.core.tool.Nest): |
| 26 |
@classmethod |
| 27 |
def can_nest(cls, relating_obj, related_obj): |
| 28 |
relating_object = tool.Ifc.get_entity(relating_obj) |
| 29 |
related_object = tool.Ifc.get_entity(related_obj) |
| 30 |
if not relating_object or not related_object: |
| 31 |
return False |
| 32 |
if relating_object.is_a("IfcElement") and related_object.is_a("IfcElement"): |
| 33 |
return True |
| 34 |
return False |
| 35 |
|
| 36 |
@classmethod |
| 37 |
def disable_editing(cls, obj): |
| 38 |
obj.BIMObjectNestProperties.is_editing = False |
| 39 |
|
| 40 |
@classmethod |
| 41 |
def enable_editing(cls, obj): |
| 42 |
obj.BIMObjectNestProperties.is_editing = True |
| 43 |
|
| 44 |
@classmethod |
| 45 |
def get_container(cls, element): |
| 46 |
return ifcopenshell.util.element.get_container(element) |
| 47 |
|
| 48 |
@classmethod |
| 49 |
def get_relating_object(cls, related_element): |
| 50 |
return ifcopenshell.util.element.get_nest(related_element) |
| 51 |
|
| 52 |
@classmethod |
| 53 |
def get_components_recursively(cls, element: ifcopenshell.entity_instance) -> set[ifcopenshell.entity_instance]: |
| 54 |
"""Get elements components recursively, resulting set includes `element`.""" |
| 55 |
components = set() |
| 56 |
queue = {element} |
| 57 |
while queue: |
| 58 |
element = queue.pop() |
| 59 |
queue.update(new_components := set(ifcopenshell.util.element.get_components(element))) |
| 60 |
components.update(new_components) |
| 61 |
return components |
| 62 |
|
| 63 |
@classmethod |
| 64 |
def get_nest_mode(cls): |
| 65 |
return bpy.context.scene.BIMNestProperties.in_nest_mode |
| 66 |
|
| 67 |
@classmethod |
| 68 |
def enable_nest_mode(cls, active_object: bpy.types.Object): |
| 69 |
context = bpy.context |
| 70 |
props = context.scene.BIMNestProperties |
| 71 |
|
| 72 |
element = tool.Ifc.get_entity(active_object) |
| 73 |
if not element: |
| 74 |
return {"FINISHED"} |
| 75 |
nest = ifcopenshell.util.element.get_nest(element) |
| 76 |
components = ifcopenshell.util.element.get_components(element) |
| 77 |
if not nest and not components: |
| 78 |
return {"FINISHED"} |
| 79 |
if not components: |
| 80 |
components = ifcopenshell.util.element.get_components(nest) |
| 81 |
if components: |
| 82 |
props.editing_nest = tool.Ifc.get_object(nest) if nest else tool.Ifc.get_object(element) |
| 83 |
components_objs = [tool.Ifc.get_object(component) for component in components] |
| 84 |
objs = [] |
| 85 |
visible_objects = tool.Raycast.get_visible_objects(context) |
| 86 |
for obj in visible_objects: |
| 87 |
if obj.visible_in_viewport_get(context.space_data): |
| 88 |
objs.append(obj.original) |
| 89 |
for obj in objs: |
| 90 |
if obj.original not in components_objs: |
| 91 |
if obj == props.editing_nest: |
| 92 |
continue |
| 93 |
not_editing_obj = props.not_editing_objects.add() |
| 94 |
not_editing_obj.obj = obj.original |
| 95 |
not_editing_obj.previous_display_type = obj.original.display_type |
| 96 |
obj.original.display_type = "WIRE" |
| 97 |
else: |
| 98 |
editing_obj = props.editing_objects.add() |
| 99 |
editing_obj.obj = obj.original |
| 100 |
|
| 101 |
props.in_nest_mode = True |
| 102 |
return {"FINISHED"} |
| 103 |
|
| 104 |
@classmethod |
| 105 |
def disable_nest_mode(cls): |
| 106 |
context = bpy.context |
| 107 |
props = context.scene.BIMNestProperties |
| 108 |
for obj_prop in props.not_editing_objects: |
| 109 |
obj = obj_prop.obj |
| 110 |
obj.original.display_type = obj_prop.previous_display_type |
| 111 |
element = tool.Ifc.get_entity(obj) |
| 112 |
if not element: |
| 113 |
continue |
| 114 |
|
| 115 |
components = ifcopenshell.util.element.get_components(tool.Ifc.get_entity(props.editing_nest)) |
| 116 |
objs = [tool.Ifc.get_object(component) for component in components] |
| 117 |
if context.space_data.local_view: |
| 118 |
bpy.ops.view3d.localview() |
| 119 |
|
| 120 |
props.in_nest_mode = False |
| 121 |
props.not_editing_objects.clear() |
| 122 |
props.editing_objects.clear() |
| 123 |
props.editing_nest = None |