Replace all ifcopenshell.api.run with ifcopenshell.api static functions.

This commit is contained in:
Dion Moult
2024-06-28 12:36:31 +10:00
parent b5d613989e
commit 07060fc769
432 changed files with 4633 additions and 4856 deletions
@@ -19,60 +19,64 @@
import numpy
import pytest
import test.bootstrap
import ifcopenshell.api
import ifcopenshell.api.root
import ifcopenshell.api.unit
import ifcopenshell.api.spatial
import ifcopenshell.api.geometry
import ifcopenshell.api.aggregate
import ifcopenshell.util.element
import ifcopenshell.util.placement
class TestAssignContainer(test.bootstrap.IFC4):
def test_assigning_a_container(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
rel = ifcopenshell.api.run(
"spatial.assign_container", self.file, products=[subelement, subelement2], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
rel = ifcopenshell.api.spatial.assign_container(
self.file, products=[subelement, subelement2], relating_structure=element
)
assert ifcopenshell.util.element.get_container(subelement) == element
assert ifcopenshell.util.element.get_container(subelement2) == element
assert rel.is_a("IfcRelContainedInSpatialStructure")
def test_doing_nothing_if_the_container_is_already_assigned(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element)
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement], relating_structure=element)
total_elements = len([e for e in self.file])
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element)
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement], relating_structure=element)
assert len([e for e in self.file]) == total_elements
def test_that_old_containment_relationships_are_updated_if_they_still_contain_elements(self):
element1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement1], relating_structure=element1)
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement2], relating_structure=element1)
element1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
element2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement1], relating_structure=element1)
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement2], relating_structure=element1)
rel = subelement1.ContainedInStructure[0]
assert len(rel.RelatedElements) == 2
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement1], relating_structure=element2)
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement1], relating_structure=element2)
assert len(rel.RelatedElements) == 1
def test_that_old_containment_relationships_are_purged_if_no_more_elements_are_contained(self):
element1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement1], relating_structure=element1)
element1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
element2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement1], relating_structure=element1)
rel_id = subelement1.ContainedInStructure[0].id()
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement1], relating_structure=element2)
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement1], relating_structure=element2)
with pytest.raises(RuntimeError):
self.file.by_id(rel_id)
def test_assigning_a_container_does_not_shift_object_placements(self):
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
ifcopenshell.api.run("unit.assign_unit", self.file)
element1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element1)
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
ifcopenshell.api.unit.assign_unit(self.file)
element1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
element2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement], relating_structure=element1)
matrix1 = numpy.array(
(
(1.0, 0.0, 0.0, 1.0),
@@ -89,37 +93,37 @@ class TestAssignContainer(test.bootstrap.IFC4):
(0.0, 0.0, 0.0, 1.0),
)
)
ifcopenshell.api.run(
"geometry.edit_object_placement", self.file, product=element1, matrix=matrix1.copy(), is_si=False
ifcopenshell.api.geometry.edit_object_placement(
self.file, product=element1, matrix=matrix1.copy(), is_si=False
)
ifcopenshell.api.run(
"geometry.edit_object_placement", self.file, product=element2, matrix=matrix2.copy(), is_si=False
ifcopenshell.api.geometry.edit_object_placement(
self.file, product=element2, matrix=matrix2.copy(), is_si=False
)
ifcopenshell.api.run(
"geometry.edit_object_placement", self.file, product=subelement, matrix=matrix1.copy(), is_si=False
ifcopenshell.api.geometry.edit_object_placement(
self.file, product=subelement, matrix=matrix1.copy(), is_si=False
)
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element2)
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement], relating_structure=element2)
assert subelement.ObjectPlacement.PlacementRelTo.PlacesObject[0] == element2
assert numpy.array_equal(ifcopenshell.util.placement.get_local_placement(subelement.ObjectPlacement), matrix1)
def test_not_updating_placement_if_placement_is_not_relative(self):
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
ifcopenshell.api.run("unit.assign_unit", self.file)
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
ifcopenshell.api.unit.assign_unit(self.file)
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
placement = self.file.createIfcGridPlacement()
subelement.ObjectPlacement = placement
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element)
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement], relating_structure=element)
assert subelement.ObjectPlacement == placement
def test_removing_aggregation_if_it_exists(self):
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
ifcopenshell.api.run("unit.assign_unit", self.file)
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
aggregate = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcElementAssembly")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("aggregate.assign_object", self.file, products=[subelement], relating_object=aggregate)
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element)
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
ifcopenshell.api.unit.assign_unit(self.file)
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
aggregate = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcElementAssembly")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.aggregate.assign_object(self.file, products=[subelement], relating_object=aggregate)
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement], relating_structure=element)
assert not ifcopenshell.util.element.get_aggregate(subelement)
@@ -16,49 +16,49 @@
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import pytest
import test.bootstrap
import ifcopenshell.api
import ifcopenshell.api.root
import ifcopenshell.api.spatial
import ifcopenshell.util.element
class TestDereferenceStructure(test.bootstrap.IFC4):
def test_removing_a_container(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.reference_structure", self.file, products=[subelement, subelement2], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.reference_structure(
self.file, products=[subelement, subelement2], relating_structure=element
)
ifcopenshell.api.run(
"spatial.dereference_structure", self.file, products=[subelement, subelement2], relating_structure=element
ifcopenshell.api.spatial.dereference_structure(
self.file, products=[subelement, subelement2], relating_structure=element
)
assert ifcopenshell.util.element.get_referenced_structures(subelement) == []
assert len(self.file.by_type("IfcRelReferencedInSpatialStructure")) == 0
def test_doing_nothing_if_no_container(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.dereference_structure", self.file, products=[subelement, subelement2], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.dereference_structure(
self.file, products=[subelement, subelement2], relating_structure=element
)
assert ifcopenshell.util.element.get_referenced_structures(subelement) == []
assert ifcopenshell.util.element.get_referenced_structures(subelement2) == []
def test_updating_the_rel_when_a_reference_is_removed_with_multipled_elements(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.reference_structure", self.file, products=[subelement1], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement3 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.reference_structure(
self.file, products=[subelement1], relating_structure=element
)
ifcopenshell.api.run(
"spatial.reference_structure", self.file, products=[subelement2, subelement3], relating_structure=element
ifcopenshell.api.spatial.reference_structure(
self.file, products=[subelement2, subelement3], relating_structure=element
)
ifcopenshell.api.run(
"spatial.dereference_structure", self.file, products=[subelement1, subelement2], relating_structure=element
ifcopenshell.api.spatial.dereference_structure(
self.file, products=[subelement1, subelement2], relating_structure=element
)
assert element.ReferencesElements[0].RelatedElements == (subelement3,)
@@ -16,45 +16,43 @@
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import pytest
import test.bootstrap
import ifcopenshell.api
import ifcopenshell.api.root
import ifcopenshell.api.spatial
import ifcopenshell.util.element
class TestReferenceStructure(test.bootstrap.IFC4):
def test_referencing_a_structure(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.reference_structure", self.file, products=[subelement, subelement2], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.reference_structure(
self.file, products=[subelement, subelement2], relating_structure=element
)
assert ifcopenshell.util.element.get_structure_referenced_elements(element) == {subelement, subelement2}
def test_doing_nothing_if_the_structure_is_already_referenced(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.reference_structure", self.file, products=[subelement, subelement2], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.reference_structure(
self.file, products=[subelement, subelement2], relating_structure=element
)
total_elements = len([e for e in self.file])
ifcopenshell.api.run(
"spatial.reference_structure", self.file, products=[subelement, subelement2], relating_structure=element
ifcopenshell.api.spatial.reference_structure(
self.file, products=[subelement, subelement2], relating_structure=element
)
assert len([e for e in self.file]) == total_elements
def test_that_old_relationships_are_updated_if_they_still_contain_elements(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.reference_structure", self.file, products=[subelement1], relating_structure=element
)
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.reference_structure", self.file, products=[subelement2, subelement3], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.reference_structure(self.file, products=[subelement1], relating_structure=element)
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement3 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.reference_structure(
self.file, products=[subelement2, subelement3], relating_structure=element
)
rel = subelement1.ReferencedInStructures[0]
assert len(rel.RelatedElements) == 3
@@ -16,46 +16,45 @@
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import numpy
import pytest
import test.bootstrap
import ifcopenshell.api
import ifcopenshell.api.root
import ifcopenshell.api.spatial
import ifcopenshell.util.element
import ifcopenshell.util.placement
class TestUnassignContainer(test.bootstrap.IFC4):
def test_unassigning_a_container(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.assign_container", self.file, products=[subelement, subelement2], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.assign_container(
self.file, products=[subelement, subelement2], relating_structure=element
)
ifcopenshell.api.run("spatial.unassign_container", self.file, products=[subelement, subelement2])
ifcopenshell.api.spatial.unassign_container(self.file, products=[subelement, subelement2])
assert not self.file.by_type("IfcRelContainedInSpatialStructure")
def test_doing_nothing_if_no_container(self):
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.unassign_container", self.file, products=[subelement])
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.unassign_container(self.file, products=[subelement])
assert ifcopenshell.util.element.get_container(subelement) is None
def test_updating_the_rel_when_a_container_is_removed_with_multipled_elements(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run(
"spatial.assign_container", self.file, products=[subelement, subelement2], relating_structure=element
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
subelement2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.assign_container(
self.file, products=[subelement, subelement2], relating_structure=element
)
ifcopenshell.api.run("spatial.unassign_container", self.file, products=[subelement])
ifcopenshell.api.spatial.unassign_container(self.file, products=[subelement])
rel = self.file.by_type("IfcRelContainedInSpatialStructure")[0]
assert list(rel.RelatedElements) == [subelement2]
def test_deleting_the_rel_when_a_container_is_removed_with_no_elements(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element)
ifcopenshell.api.run("spatial.unassign_container", self.file, products=[subelement])
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.assign_container(self.file, products=[subelement], relating_structure=element)
ifcopenshell.api.spatial.unassign_container(self.file, products=[subelement])
assert len(self.file.by_type("IfcRelContainedInSpatialStructure")) == 0