replace api.run with static methods

This commit is contained in:
Andrej
2025-06-09 10:59:22 +05:00
parent 02d359d0e6
commit 8b4683aef1
122 changed files with 1334 additions and 1370 deletions
+4 -3
View File
@@ -19,13 +19,14 @@
import pytest
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.api.project
import ifcopenshell.api.owner.settings
class IFC4X3:
@pytest.fixture(autouse=True)
def setup(self):
self.file: ifcopenshell.file = ifcopenshell.api.run("project.create_file", version="IFC4X3")
self.file: ifcopenshell.file = ifcopenshell.api.project.create_file(version="IFC4X3")
ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0]
ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0]
ifcopenshell.api.pre_listeners = {}
@@ -35,7 +36,7 @@ class IFC4X3:
class IFC4:
@pytest.fixture(autouse=True)
def setup(self):
self.file: ifcopenshell.file = ifcopenshell.api.run("project.create_file")
self.file: ifcopenshell.file = ifcopenshell.api.project.create_file()
ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0]
ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0]
ifcopenshell.api.pre_listeners = {}
@@ -45,7 +46,7 @@ class IFC4:
class IFC2X3:
@pytest.fixture(autouse=True)
def setup(self):
self.file: ifcopenshell.file = ifcopenshell.api.run("project.create_file", version="IFC2X3")
self.file: ifcopenshell.file = ifcopenshell.api.project.create_file(version="IFC2X3")
def get_user(ifc_file: ifcopenshell.file) -> ifcopenshell.entity_instance:
person = ifcopenshell.api.owner.add_person(ifc_file)
+21 -18
View File
@@ -21,29 +21,32 @@ import pytest
import ifcpatch
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.api.aggregate
import ifcopenshell.api.root
import ifcopenshell.api.spatial
import ifcopenshell.util.element
import test.bootstrap
class TestExtractElements(test.bootstrap.IFC4):
def test_basic(self):
project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
project = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
wall = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
output = ifcpatch.execute({"file": self.file, "recipe": "ExtractElements", "arguments": ["IfcWall"]})
assert output.by_type("IfcProject")[0].GlobalId == project.GlobalId
assert output.by_type("IfcWall")[0].GlobalId == wall.GlobalId
def test_keep_spatial_structure(self):
project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
project = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
site = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSite")
building = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
storey = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuildingStorey")
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("aggregate.assign_object", self.file, products=[building], relating_object=site)
ifcopenshell.api.run("aggregate.assign_object", self.file, products=[storey], relating_object=building)
ifcopenshell.api.run("spatial.assign_container", self.file, products=[wall], relating_structure=storey)
site = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcSite")
building = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
storey = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuildingStorey")
wall = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.aggregate.assign_object(self.file, products=[building], relating_object=site)
ifcopenshell.api.aggregate.assign_object(self.file, products=[storey], relating_object=building)
ifcopenshell.api.spatial.assign_container(self.file, products=[wall], relating_structure=storey)
output = ifcpatch.execute({"file": self.file, "recipe": "ExtractElements", "arguments": ["IfcWall"]})
@@ -53,13 +56,13 @@ class TestExtractElements(test.bootstrap.IFC4):
assert (site_new := ifcopenshell.util.element.get_aggregate(building_new)).GlobalId == site.GlobalId
def test_keep_aggregate_in_spatial_structure(self):
project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
project = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcElementAssembly")
container = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuildingStorey")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[element], relating_structure=container)
ifcopenshell.api.run("aggregate.assign_object", self.file, products=[subelement], relating_object=element)
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcElementAssembly")
container = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuildingStorey")
subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.spatial.assign_container(self.file, products=[element], relating_structure=container)
ifcopenshell.api.aggregate.assign_object(self.file, products=[subelement], relating_object=element)
output = ifcpatch.execute({"file": self.file, "recipe": "ExtractElements", "arguments": ["IfcWall"]})
@@ -84,8 +87,8 @@ class TestExtractElements(test.bootstrap.IFC4):
)
def test_extracting_non_standard_schema_version(self):
self.file = ifcopenshell.file(schema_version=(4, 3, 0, 0))
project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
project = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
wall = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
output = ifcpatch.execute({"file": self.file, "recipe": "ExtractElements", "arguments": ["IfcWall"]})
assert output.by_type("IfcProject")[0].GlobalId == project.GlobalId
assert output.by_type("IfcWall")[0].GlobalId == wall.GlobalId
+14 -12
View File
@@ -21,6 +21,10 @@ import pytest
import ifcpatch
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.api.geometry
import ifcopenshell.api.material
import ifcopenshell.api.root
import ifcopenshell.api.type
import ifcopenshell.util.element
import ifcopenshell.util.representation
import test.bootstrap
@@ -30,26 +34,24 @@ class TestMergeDuplicateTypes(test.bootstrap.IFC4):
def test_run(self):
context = self.file.createIfcGeometricRepresentationContext()
wall1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
wall_type1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType", name="WallType")
wall1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
wall_type1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType", name="WallType")
rep = self.file.createIfcShapeRepresentation(ContextOfItems=context)
ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall_type1, representation=rep)
ifcopenshell.api.run("material.assign_material", self.file, products=[wall_type1], type="IfcMaterialLayerSet")
ifcopenshell.api.run(
"type.assign_type",
ifcopenshell.api.geometry.assign_representation(self.file, product=wall_type1, representation=rep)
ifcopenshell.api.material.assign_material(self.file, products=[wall_type1], type="IfcMaterialLayerSet")
ifcopenshell.api.type.assign_type(
self.file,
related_objects=[wall1],
relating_type=wall_type1,
should_map_representations=False,
)
wall2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
wall_type2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType", name="WallType")
wall2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
wall_type2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType", name="WallType")
rep = self.file.createIfcShapeRepresentation(ContextOfItems=context)
ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall_type2, representation=rep)
ifcopenshell.api.run("material.assign_material", self.file, products=[wall_type2], type="IfcMaterialLayerSet")
ifcopenshell.api.run(
"type.assign_type",
ifcopenshell.api.geometry.assign_representation(self.file, product=wall_type2, representation=rep)
ifcopenshell.api.material.assign_material(self.file, products=[wall_type2], type="IfcMaterialLayerSet")
ifcopenshell.api.type.assign_type(
self.file,
related_objects=[wall2],
relating_type=wall_type2,
+7 -5
View File
@@ -21,6 +21,8 @@ import ifcopenshell
import ifcopenshell.api.context
import ifcopenshell.api.geometry
import ifcopenshell.api.georeference
import ifcopenshell.api.root
import ifcopenshell.api.unit
import ifcopenshell.geom
import ifcopenshell.util.geolocation
import ifcopenshell.util.placement
@@ -40,16 +42,16 @@ class TestMergeProjects(test.bootstrap.IFC4):
if ifc_file is None:
ifc_file = ifcopenshell.file(schema=self.file.schema)
ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcProject")
unit = ifcopenshell.api.run("unit.add_si_unit", ifc_file, unit_type="LENGTHUNIT", prefix=prefix)
ifcopenshell.api.run("unit.assign_unit", ifc_file, units=[unit])
ifcopenshell.api.root.create_entity(ifc_file, ifc_class="IfcProject")
unit = ifcopenshell.api.unit.add_si_unit(ifc_file, unit_type="LENGTHUNIT", prefix=prefix)
ifcopenshell.api.unit.assign_unit(ifc_file, units=[unit])
model = ifcopenshell.api.context.add_context(ifc_file, "Model")
ifcopenshell.api.context.add_context(ifc_file, "Model", "Body", "MODEL_VIEW", parent=model)
matrix = np.eye(4)
matrix[:, 3] = (1, 2, 3, 1)
wall = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcWall")
ifcopenshell.api.run("geometry.edit_object_placement", ifc_file, product=wall, matrix=matrix, is_si=True)
wall = ifcopenshell.api.root.create_entity(ifc_file, ifc_class="IfcWall")
ifcopenshell.api.geometry.edit_object_placement(ifc_file, product=wall, matrix=matrix, is_si=True)
return ifc_file
def test_run(self):
@@ -21,23 +21,24 @@ import pytest
import ifcpatch
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.api.root
import ifcopenshell.util.element
import test.bootstrap
class TestRegenerateGlobalIds(test.bootstrap.IFC4):
def test_run(self):
project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
project = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
wall = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
used_guids = {project.GlobalId, wall.GlobalId}
ifcpatch.execute({"file": self.file, "recipe": "RegenerateGlobalIds", "arguments": [False]})
new_guids = {project.GlobalId, wall.GlobalId}
assert not new_guids.intersection(used_guids)
def test_regenerate_guids_for_duplicates(self):
project = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
wall1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
wall2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
project = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
wall1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
wall2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
wall1.GlobalId = wall2.GlobalId
used_guids = {project, wall1.GlobalId, wall2.GlobalId}