2024-08-13 15:47:27 +10:00
|
|
|
# Bonsai - OpenBIM Blender Add-on
|
2021-09-29 14:14:10 +10:00
|
|
|
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
|
|
|
|
|
#
|
2024-08-13 15:47:27 +10:00
|
|
|
# This file is part of Bonsai.
|
2021-09-29 14:14:10 +10:00
|
|
|
#
|
2024-08-13 15:47:27 +10:00
|
|
|
# Bonsai is free software: you can redistribute it and/or modify
|
2021-09-29 14:14:10 +10: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-09-29 14:14:10 +10: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-09-29 14:14:10 +10:00
|
|
|
|
|
|
|
|
import bpy
|
|
|
|
|
import ifcopenshell
|
|
|
|
|
import test.bim.bootstrap
|
2024-08-14 13:56:39 +05:00
|
|
|
import bonsai.core.tool
|
|
|
|
|
import bonsai.tool as tool
|
2024-08-02 12:14:47 +05:00
|
|
|
import pytest
|
2024-08-14 13:56:39 +05:00
|
|
|
from bonsai.tool.ifc import Ifc as subject
|
2021-09-29 14:14:10 +10:00
|
|
|
|
|
|
|
|
|
2021-10-04 13:14:38 +11:00
|
|
|
class TestImplementsTool(test.bim.bootstrap.NewFile):
|
|
|
|
|
def test_run(self):
|
2024-08-14 13:56:39 +05:00
|
|
|
assert isinstance(subject(), bonsai.core.tool.Ifc)
|
2021-10-04 13:14:38 +11:00
|
|
|
|
|
|
|
|
|
2021-09-29 14:14:10 +10:00
|
|
|
class TestSet(test.bim.bootstrap.NewFile):
|
|
|
|
|
def test_setting_an_ifc_data(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
2021-10-04 13:14:38 +11:00
|
|
|
subject.set(ifc)
|
|
|
|
|
assert subject.get() == ifc
|
2021-09-29 14:14:10 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestGet(test.bim.bootstrap.NewFile):
|
|
|
|
|
def test_getting_an_ifc_dataset_from_a_ifc_spf_filepath(self):
|
2021-10-04 13:14:38 +11:00
|
|
|
assert subject.get() is None
|
2021-09-29 14:14:10 +10:00
|
|
|
bpy.context.scene.BIMProperties.ifc_file = "test/files/basic.ifc"
|
2021-10-04 13:14:38 +11:00
|
|
|
result = subject.get()
|
2021-09-29 14:14:10 +10:00
|
|
|
assert isinstance(result, ifcopenshell.file)
|
|
|
|
|
|
|
|
|
|
def test_getting_the_active_ifc_dataset_regardless_of_ifc_path(self):
|
|
|
|
|
bpy.context.scene.BIMProperties.ifc_file = "test/files/basic.ifc"
|
|
|
|
|
ifc = ifcopenshell.file()
|
2021-10-04 13:14:38 +11:00
|
|
|
subject.set(ifc)
|
|
|
|
|
assert subject.get() == ifc
|
2021-09-29 14:14:10 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRun(test.bim.bootstrap.NewFile):
|
|
|
|
|
def test_running_a_command_on_the_active_ifc_dataset(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
2021-10-04 13:14:38 +11:00
|
|
|
subject.set(ifc)
|
|
|
|
|
wall = subject.run("root.create_entity", ifc_class="IfcWall")
|
|
|
|
|
assert subject.get().by_type("IfcWall")[0] == wall
|
2021-09-29 14:14:10 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestGetSchema(test.bim.bootstrap.NewFile):
|
|
|
|
|
def test_getting_the_schema_version_identifier(self):
|
|
|
|
|
ifc = ifcopenshell.file(schema="IFC4")
|
2021-10-04 13:14:38 +11:00
|
|
|
subject.set(ifc)
|
|
|
|
|
assert subject.get_schema() == "IFC4"
|
|
|
|
|
|
|
|
|
|
|
2023-03-24 21:59:09 +11:00
|
|
|
class TestIsMoved(test.bim.bootstrap.NewFile):
|
|
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
|
2023-03-24 21:59:09 +11:00
|
|
|
obj = bpy.data.objects.new("Object", None)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcWall")
|
2023-03-24 21:59:09 +11:00
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert subject.is_moved(obj) is True
|
2024-08-02 12:14:47 +05:00
|
|
|
|
2023-03-24 21:59:09 +11:00
|
|
|
tool.Geometry.record_object_position(obj)
|
|
|
|
|
assert subject.is_moved(obj) is False
|
2024-08-02 12:14:47 +05:00
|
|
|
|
2023-03-24 21:59:09 +11:00
|
|
|
obj.matrix_world[0][2] += 1
|
|
|
|
|
assert subject.is_moved(obj) is True
|
|
|
|
|
|
|
|
|
|
def test_that_a_type_or_project_never_moves_but_a_grid_axis_does(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
|
2023-03-24 21:59:09 +11:00
|
|
|
obj = bpy.data.objects.new("Object", None)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcWallType")
|
2023-03-24 21:59:09 +11:00
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert subject.is_moved(obj) is False
|
2024-08-02 12:14:47 +05:00
|
|
|
|
|
|
|
|
element = ifc.create_entity("IfcProject")
|
2023-03-24 21:59:09 +11:00
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert subject.is_moved(obj) is False
|
2024-08-02 12:14:47 +05:00
|
|
|
|
|
|
|
|
element = ifc.create_entity("IfcGridAxis")
|
2023-03-24 21:59:09 +11:00
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert subject.is_moved(obj) is True
|
|
|
|
|
|
|
|
|
|
|
2021-10-05 09:51:51 +11:00
|
|
|
class TestGetEntity(test.bim.bootstrap.NewFile):
|
2021-10-04 13:14:38 +11:00
|
|
|
def test_run(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
|
|
|
|
obj = bpy.data.objects.new("Object", None)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcWall")
|
|
|
|
|
subject.link(element, obj)
|
2021-10-04 13:14:38 +11:00
|
|
|
assert subject.get_entity(obj) == element
|
|
|
|
|
|
|
|
|
|
def test_attempting_to_get_an_unlinked_object(self):
|
|
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
assert subject.get_entity(obj) is None
|
|
|
|
|
|
|
|
|
|
def test_attempting_without_a_file(self):
|
|
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
obj.BIMObjectProperties.ifc_definition_id = 1
|
|
|
|
|
assert subject.get_entity(obj) is None
|
|
|
|
|
|
|
|
|
|
def test_attempting_to_get_an_invalidly_linked_object(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
|
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
obj.BIMObjectProperties.ifc_definition_id = 1
|
|
|
|
|
assert subject.get_entity(obj) is None
|
2021-10-05 09:51:51 +11:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestGetObject(test.bim.bootstrap.NewFile):
|
2024-08-02 12:14:47 +05:00
|
|
|
def test_get_object(self):
|
2021-10-05 09:51:51 +11:00
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcWall")
|
2021-10-05 09:51:51 +11:00
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert subject.get_object(element) == obj
|
|
|
|
|
|
2024-08-02 12:14:47 +05:00
|
|
|
def test_get_material(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
|
|
|
|
element = ifc.create_entity("IfcSurfaceStyle")
|
|
|
|
|
obj = bpy.data.materials.new("Style")
|
|
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert subject.get_object(element) == obj
|
|
|
|
|
|
2021-10-05 09:51:51 +11:00
|
|
|
|
|
|
|
|
class TestLink(test.bim.bootstrap.NewFile):
|
2021-11-10 18:01:10 +11:00
|
|
|
def test_link_an_object(self):
|
2021-10-05 09:51:51 +11:00
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcWall")
|
2021-10-05 09:51:51 +11:00
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert subject.get_entity(obj) == element
|
|
|
|
|
assert subject.get_object(element) == obj
|
|
|
|
|
|
2024-08-02 12:14:47 +05:00
|
|
|
def test_link_a_style(self):
|
2021-11-10 18:01:10 +11:00
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcSurfaceStyle")
|
2021-11-10 18:01:10 +11:00
|
|
|
obj = bpy.data.materials.new("Material")
|
|
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert subject.get_entity(obj) == element
|
|
|
|
|
assert subject.get_object(element) == obj
|
|
|
|
|
|
2024-08-02 12:14:47 +05:00
|
|
|
def test_link_not_a_style_to_blender_material(self):
|
2021-11-11 14:18:44 +11:00
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcMaterial")
|
2021-11-11 14:18:44 +11:00
|
|
|
obj = bpy.data.materials.new("Material")
|
2024-08-02 12:14:47 +05:00
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
|
subject.link(element, obj)
|
2021-11-11 14:18:44 +11:00
|
|
|
|
2024-08-02 12:14:47 +05:00
|
|
|
def test_link_a_mesh(self):
|
2021-11-11 14:18:44 +11:00
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcShapeRepresentation")
|
|
|
|
|
obj = bpy.data.meshes.new("Material")
|
|
|
|
|
subject.link(element, obj)
|
|
|
|
|
assert obj.BIMMeshProperties.ifc_definition_id == element.id()
|
2021-11-11 14:18:44 +11:00
|
|
|
|
2021-10-05 09:51:51 +11:00
|
|
|
|
|
|
|
|
class TestUnlink(test.bim.bootstrap.NewFile):
|
2021-11-10 18:01:10 +11:00
|
|
|
def test_unlink_an_object(self):
|
2021-10-05 09:51:51 +11:00
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcWall")
|
2021-10-05 09:51:51 +11:00
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
subject.link(element, obj)
|
2024-08-02 12:14:47 +05:00
|
|
|
subject.unlink(element)
|
2021-11-10 18:01:10 +11:00
|
|
|
assert subject.get_entity(obj) is None
|
|
|
|
|
assert subject.get_object(element) is None
|
|
|
|
|
|
2021-11-11 14:18:44 +11:00
|
|
|
def test_unlink_a_style(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcSurfaceStyle")
|
2021-11-11 14:18:44 +11:00
|
|
|
obj = bpy.data.materials.new("Material")
|
|
|
|
|
subject.link(element, obj)
|
2024-08-02 12:14:47 +05:00
|
|
|
subject.unlink(element)
|
2021-11-11 14:18:44 +11:00
|
|
|
assert subject.get_entity(obj) is None
|
2024-08-02 12:14:47 +05:00
|
|
|
assert subject.get_object(element) is None
|
2021-11-11 14:18:44 +11:00
|
|
|
|
2021-10-05 09:51:51 +11:00
|
|
|
def test_unlinking_using_an_object(self):
|
|
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcWall")
|
2021-10-05 09:51:51 +11:00
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
subject.link(element, obj)
|
|
|
|
|
subject.unlink(obj=obj)
|
|
|
|
|
assert subject.get_entity(obj) is None
|
2024-08-02 12:14:47 +05:00
|
|
|
assert subject.get_object(element) is obj
|
2021-10-05 09:51:51 +11:00
|
|
|
|
2024-08-02 12:14:47 +05:00
|
|
|
def test_unlinking_using_both_arguments(self):
|
2021-10-05 09:51:51 +11:00
|
|
|
ifc = ifcopenshell.file()
|
|
|
|
|
subject.set(ifc)
|
2024-08-02 12:14:47 +05:00
|
|
|
element = ifc.create_entity("IfcWall")
|
2021-10-05 09:51:51 +11:00
|
|
|
obj = bpy.data.objects.new("Object", None)
|
|
|
|
|
subject.link(element, obj)
|
2024-08-02 12:14:47 +05:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
|
subject.unlink(element=element, obj=obj)
|
|
|
|
|
assert subject.get_entity(obj) == element
|
|
|
|
|
assert subject.get_object(element) == obj
|