2024-08-13 15:47:27 +10:00
|
|
|
# Bonsai - OpenBIM Blender Add-on
|
2021-10-19 19:14:05 +11:00
|
|
|
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
|
|
|
|
|
#
|
2024-08-13 15:47:27 +10:00
|
|
|
# This file is part of Bonsai.
|
2021-10-19 19:14:05 +11:00
|
|
|
#
|
2024-08-13 15:47:27 +10:00
|
|
|
# Bonsai is free software: you can redistribute it and/or modify
|
2021-10-19 19:14:05 +11:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
2024-08-13 15:47:27 +10:00
|
|
|
# Bonsai is distributed in the hope that it will be useful,
|
2021-10-19 19:14:05 +11:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2024-08-13 15:47:27 +10:00
|
|
|
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
|
2021-10-19 19:14:05 +11:00
|
|
|
|
|
|
|
|
import bpy
|
|
|
|
|
import ifcopenshell
|
2024-04-10 12:08:09 +05:00
|
|
|
import ifcopenshell.api
|
2025-06-09 10:59:22 +05:00
|
|
|
import ifcopenshell.api.feature
|
|
|
|
|
import ifcopenshell.api.type
|
2024-08-14 13:56:39 +05:00
|
|
|
import bonsai.core.tool
|
|
|
|
|
import bonsai.tool as tool
|
2021-10-19 19:14:05 +11:00
|
|
|
from test.bim.bootstrap import NewFile
|
2024-08-14 13:56:39 +05:00
|
|
|
from bonsai.tool.root import Root as subject
|
2021-10-19 19:14:05 +11:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestImplementsTool(NewFile):
|
|
|
|
|
def test_run(self):
|
2024-08-14 13:56:39 +05:00
|
|
|
assert isinstance(subject(), bonsai.core.tool.Root)
|
2021-10-19 19:14:05 +11:00
|
|
|
|
|
|
|
|
|
2022-10-05 20:04:19 +11:00
|
|
|
class TestAddTrackedOpening(NewFile):
|
2021-10-19 19:14:05 +11:00
|
|
|
def test_run(self):
|
2022-10-05 20:04:19 +11:00
|
|
|
obj = bpy.data.objects.new("Object", None)
|
2025-01-15 15:58:13 +05:00
|
|
|
subject.add_tracked_opening(obj, "OPENING")
|
2025-02-10 13:31:50 +05:00
|
|
|
props = tool.Model.get_model_props()
|
2022-10-05 20:04:19 +11:00
|
|
|
assert props.openings[0].obj == obj
|
2025-01-15 15:58:13 +05:00
|
|
|
assert props.openings[0].name == "OPENING"
|
2021-10-19 19:14:05 +11:00
|
|
|
|
|
|
|
|
|
2022-10-09 18:09:22 +11:00
|
|
|
class TestCopyRepresentation(NewFile):
|
|
|
|
|
def test_copying_a_product(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
tool.Ifc.set(ifc)
|
|
|
|
|
source = ifc.createIfcWall(Representation=ifc.createIfcProductDefinitionShape())
|
|
|
|
|
dest = ifc.createIfcWall()
|
|
|
|
|
subject.copy_representation(source, dest)
|
|
|
|
|
assert dest.Representation.is_a("IfcProductDefinitionShape")
|
|
|
|
|
|
|
|
|
|
def test_copying_a_type_product(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
tool.Ifc.set(ifc)
|
|
|
|
|
source = ifc.createIfcWallType(RepresentationMaps=[ifc.createIfcRepresentationMap()])
|
|
|
|
|
dest = ifc.createIfcWallType()
|
|
|
|
|
subject.copy_representation(source, dest)
|
|
|
|
|
assert dest.RepresentationMaps[0].is_a("IfcRepresentationMap")
|
|
|
|
|
|
|
|
|
|
|
2021-10-19 19:14:05 +11:00
|
|
|
class TestDoesTypeHaveRepresentations(NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
element = ifc.createIfcWallType()
|
|
|
|
|
assert subject.does_type_have_representations(element) is False
|
|
|
|
|
element.RepresentationMaps = [ifc.createIfcRepresentationMap()]
|
|
|
|
|
assert subject.does_type_have_representations(element) is True
|
|
|
|
|
|
|
|
|
|
|
2022-10-09 22:25:55 +11:00
|
|
|
class TestGetDecompositionRelationships(NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
tool.Ifc.set(ifc)
|
|
|
|
|
|
|
|
|
|
element = ifc.createIfcWall()
|
|
|
|
|
opening = ifc.createIfcOpeningElement()
|
|
|
|
|
fill = ifc.createIfcWindow()
|
2025-06-09 10:59:22 +05:00
|
|
|
ifcopenshell.api.feature.add_feature(ifc, feature=opening, element=element)
|
|
|
|
|
ifcopenshell.api.feature.add_filling(ifc, opening=opening, element=fill)
|
2022-10-09 22:25:55 +11:00
|
|
|
|
|
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
tool.Ifc.link(fill, obj)
|
|
|
|
|
|
|
|
|
|
assert subject.get_decomposition_relationships([obj]) == {fill: {"type": "fill", "element": element}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestGetElementRepresentation(NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
tool.Ifc.set(ifc)
|
|
|
|
|
context = ifc.createIfcGeometricRepresentationContext(ContextType="Model")
|
|
|
|
|
representation = ifc.createIfcShapeRepresentation(ContextOfItems=context)
|
|
|
|
|
wall = ifc.createIfcWall(Representation=ifc.createIfcProductDefinitionShape(Representations=[representation]))
|
|
|
|
|
assert subject.get_element_representation(wall, context) == representation
|
|
|
|
|
|
|
|
|
|
|
2021-10-19 19:14:05 +11:00
|
|
|
class TestGetElementType(NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
bpy.ops.bim.create_project()
|
|
|
|
|
ifc = tool.Ifc.get()
|
|
|
|
|
element = ifc.createIfcWall()
|
|
|
|
|
type = ifc.createIfcWallType()
|
2025-06-09 10:59:22 +05:00
|
|
|
ifcopenshell.api.type.assign_type(ifc, related_objects=[element], relating_type=type)
|
2021-10-19 19:14:05 +11:00
|
|
|
assert subject.get_element_type(element) == type
|
|
|
|
|
|
|
|
|
|
|
2022-01-30 15:08:32 +11:00
|
|
|
class TestGetObjectName(NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
assert subject.get_object_name(obj) == "Object"
|
|
|
|
|
|
|
|
|
|
def test_blender_number_suffixes_are_ignored(self):
|
|
|
|
|
obj = bpy.data.objects.new("Object.001", None)
|
|
|
|
|
assert subject.get_object_name(obj) == "Object"
|
|
|
|
|
obj = bpy.data.objects.new("Object.foo.123", None)
|
|
|
|
|
assert subject.get_object_name(obj) == "Object.foo"
|
|
|
|
|
|
|
|
|
|
|
2021-10-22 18:07:34 +11:00
|
|
|
class TestGetObjectRepresentation(NewFile):
|
2021-10-19 19:14:05 +11:00
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
tool.Ifc.set(ifc)
|
2021-10-22 18:07:34 +11:00
|
|
|
representation = ifc.createIfcShapeRepresentation()
|
2025-02-19 11:06:03 +05:00
|
|
|
obj = bpy.data.objects.new("Object", (mesh := bpy.data.meshes.new("Mesh")))
|
|
|
|
|
tool.Geometry.get_mesh_props(mesh).ifc_definition_id = representation.id()
|
2021-10-22 18:07:34 +11:00
|
|
|
assert subject.get_object_representation(obj) == representation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestGetRepresentationContext(NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
tool.Ifc.set(ifc)
|
|
|
|
|
context = ifc.createIfcGeometricRepresentationContext()
|
|
|
|
|
representation = ifc.createIfcShapeRepresentation(ContextOfItems=context)
|
|
|
|
|
assert subject.get_representation_context(representation) == context
|
2021-10-19 19:14:05 +11:00
|
|
|
|
|
|
|
|
|
2023-05-03 20:37:59 +10:00
|
|
|
class TestIsElementA(NewFile):
|
2021-10-19 19:14:05 +11:00
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
2023-05-06 11:05:13 +10:00
|
|
|
assert subject.is_element_a(ifc.createIfcWall(), "IfcSlab") is False
|
|
|
|
|
assert subject.is_element_a(ifc.createIfcOpeningElement(), "IfcOpeningElement") is True
|
2021-10-19 19:14:05 +11:00
|
|
|
|
|
|
|
|
|
2021-11-03 19:03:48 +11:00
|
|
|
class TestLinkObjectData(NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
data = bpy.data.meshes.new("Mesh")
|
|
|
|
|
source = bpy.data.objects.new("Object", data)
|
|
|
|
|
destination = bpy.data.objects.new("Object", bpy.data.meshes.new("Mesh"))
|
|
|
|
|
subject.link_object_data(source, destination)
|
|
|
|
|
assert source.data == data
|
|
|
|
|
assert source.data == destination.data
|
|
|
|
|
|
|
|
|
|
|
2021-10-19 19:14:05 +11:00
|
|
|
class TestRunGeometryAddRepresntation(NewFile):
|
|
|
|
|
def test_nothing(self):
|
|
|
|
|
pass
|
2022-01-30 15:08:32 +11:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSetObjectName(NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
obj = bpy.data.objects.new("Object", bpy.data.meshes.new("Mesh"))
|
|
|
|
|
element = ifc.createIfcWall()
|
|
|
|
|
subject.set_object_name(obj, element)
|
2023-08-24 23:03:36 +10:00
|
|
|
assert obj.name == "IfcWall/Unnamed"
|
2022-01-30 15:08:32 +11:00
|
|
|
|
2023-08-24 23:03:36 +10:00
|
|
|
def test_existing_blender_names_are_ignored(self):
|
2022-01-30 15:08:32 +11:00
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
obj = bpy.data.objects.new("IfcSlab/Object", bpy.data.meshes.new("Mesh"))
|
|
|
|
|
element = ifc.createIfcWall()
|
2023-08-24 23:03:36 +10:00
|
|
|
element.Name = "Foobar"
|
2022-01-30 15:08:32 +11:00
|
|
|
subject.set_object_name(obj, element)
|
2023-08-24 23:03:36 +10:00
|
|
|
assert obj.name == "IfcWall/Foobar"
|
2024-02-28 16:53:08 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestReassignClass(NewFile):
|
2025-06-06 13:10:15 +05:00
|
|
|
def test_reassign_type_to_reassign_occurrences(self):
|
|
|
|
|
tool.Project.get_project_props().template_file = "IFC4 Demo Template.ifc"
|
|
|
|
|
bpy.ops.bim.create_project()
|
|
|
|
|
ifc_file = tool.Ifc.get()
|
|
|
|
|
context = bpy.context
|
|
|
|
|
|
|
|
|
|
n_wall_types = len(ifc_file.by_type("IfcWallType"))
|
|
|
|
|
n_slab_types = len(ifc_file.by_type("IfcSlabType"))
|
|
|
|
|
relating_type = ifc_file.by_type("IfcSlabType")[0]
|
|
|
|
|
relating_type_obj = tool.Ifc.get_object(relating_type)
|
|
|
|
|
assert isinstance(relating_type_obj, bpy.types.Object)
|
|
|
|
|
relating_type_id = relating_type.id()
|
|
|
|
|
|
|
|
|
|
# Add occurrences.
|
|
|
|
|
bpy.ops.bim.add_occurrence(relating_type_id=relating_type_id)
|
|
|
|
|
bpy.ops.bim.add_occurrence(relating_type_id=relating_type_id)
|
|
|
|
|
bpy.ops.bim.add_occurrence(relating_type_id=relating_type_id)
|
|
|
|
|
|
|
|
|
|
# Run operator.
|
|
|
|
|
tool.Blender.set_objects_selection(context, relating_type_obj, selected_objects=(relating_type_obj,))
|
|
|
|
|
props = tool.Root.get_root_props()
|
|
|
|
|
props.ifc_product = "IfcElementType"
|
|
|
|
|
props.ifc_class = "IfcWallType"
|
|
|
|
|
bpy.ops.bim.reassign_class()
|
|
|
|
|
|
|
|
|
|
assert len(ifc_file.by_type("IfcWall")) == 3
|
|
|
|
|
assert len(ifc_file.by_type("IfcSlab")) == 0
|
|
|
|
|
assert len(ifc_file.by_type("IfcWallType")) == n_wall_types + 1
|
|
|
|
|
assert len(ifc_file.by_type("IfcSlabType")) == n_slab_types - 1
|
|
|
|
|
for wall in ifc_file.by_type("IfcWall"):
|
|
|
|
|
assert isinstance(obj := tool.Ifc.get_object(wall), bpy.types.Object)
|
|
|
|
|
assert obj.name.startswith("IfcWall/")
|
|
|
|
|
|
2024-08-30 16:25:34 +05:00
|
|
|
def test_reassigning_multiple_occurrences_of_the_same_type(self):
|
2025-02-19 11:06:03 +05:00
|
|
|
tool.Project.get_project_props().template_file = "IFC4 Demo Template.ifc"
|
2024-02-28 16:53:08 +05:00
|
|
|
bpy.ops.bim.create_project()
|
|
|
|
|
ifc_file = tool.Ifc.get()
|
|
|
|
|
context = bpy.context
|
|
|
|
|
relating_type_id = ifc_file.by_type("IfcSlabType")[0].id()
|
|
|
|
|
n_wall_types = len(ifc_file.by_type("IfcWallType"))
|
|
|
|
|
n_slab_types = len(ifc_file.by_type("IfcSlabType"))
|
|
|
|
|
|
|
|
|
|
# create 3 slabs
|
2025-03-10 08:23:50 +11:00
|
|
|
bpy.ops.bim.add_occurrence(relating_type_id=relating_type_id)
|
|
|
|
|
bpy.ops.bim.add_occurrence(relating_type_id=relating_type_id)
|
|
|
|
|
bpy.ops.bim.add_occurrence(relating_type_id=relating_type_id)
|
2024-02-28 16:53:08 +05:00
|
|
|
|
2025-06-06 12:40:08 +05:00
|
|
|
slabs = [
|
|
|
|
|
obj for e in ifc_file.by_type("IfcSlab") if isinstance(obj := tool.Ifc.get_object(e), bpy.types.Object)
|
|
|
|
|
]
|
2024-02-28 16:53:08 +05:00
|
|
|
assert len(slabs) == 3
|
|
|
|
|
tool.Blender.set_objects_selection(context, slabs[0], (slabs[1],))
|
2025-02-24 11:37:19 +05:00
|
|
|
|
|
|
|
|
props = tool.Root.get_root_props()
|
|
|
|
|
props.ifc_product = "IfcElement"
|
|
|
|
|
props.ifc_class = "IfcWall"
|
2024-02-28 16:53:08 +05:00
|
|
|
bpy.ops.bim.reassign_class()
|
|
|
|
|
|
|
|
|
|
assert len(ifc_file.by_type("IfcWall")) == 3
|
|
|
|
|
assert len(ifc_file.by_type("IfcSlab")) == 0
|
|
|
|
|
assert len(ifc_file.by_type("IfcWallType")) == n_wall_types + 1
|
|
|
|
|
assert len(ifc_file.by_type("IfcSlabType")) == n_slab_types - 1
|