mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 01:41:57 +00:00
classification.add_reference - support batching #4474
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
# 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.util.classification
|
||||
@@ -23,85 +24,127 @@ import ifcopenshell.util.classification
|
||||
|
||||
class TestAddReference(test.bootstrap.IFC4):
|
||||
def test_adding_a_reference(self):
|
||||
is_ifc2x3 = self.file.schema == "IFC2X3"
|
||||
|
||||
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
|
||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
result = ifcopenshell.api.run("classification.add_classification", self.file, classification="Name")
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element, element2],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
)
|
||||
references = list(ifcopenshell.util.classification.get_references(element))
|
||||
assert len(references) == 1
|
||||
assert references[0].Identification == "X"
|
||||
assert getattr(references[0], "ItemReference" if is_ifc2x3 else "Identification") == "X"
|
||||
assert references[0].Name == "Foobar"
|
||||
assert references[0].ReferencedSource == self.file.by_type("IfcClassification")[0]
|
||||
|
||||
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element2,
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
references2 = list(ifcopenshell.util.classification.get_references(element))
|
||||
assert len(references2) == 1
|
||||
assert getattr(references2[0], "ItemReference" if is_ifc2x3 else "Identification") == "X"
|
||||
assert references2[0].Name == "Foobar"
|
||||
assert references2[0] == references[0]
|
||||
|
||||
rel = next(
|
||||
rel
|
||||
for rel in self.file.by_type("IfcRelAssociatesClassification")
|
||||
if rel.RelatingClassification == references[0]
|
||||
)
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0].Identification == "X"
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0].Name == "Foobar"
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0] == references[0]
|
||||
assert len(rel.RelatedObjects) == 2
|
||||
|
||||
def test_adding_a_library_based_reference(self):
|
||||
is_ifc2x3 = self.file.schema == "IFC2X3"
|
||||
|
||||
library = ifcopenshell.file()
|
||||
classification = library.createIfcClassification(Name="Name")
|
||||
reference = library.createIfcClassificationReference(Identification="1", ReferencedSource=classification)
|
||||
|
||||
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
|
||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
result = ifcopenshell.api.run("classification.add_classification", self.file, classification=classification)
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element, element2],
|
||||
reference=reference,
|
||||
classification=result,
|
||||
)
|
||||
references = list(ifcopenshell.util.classification.get_references(element))
|
||||
assert len(references) == 1
|
||||
assert references[0].Identification == "1"
|
||||
assert getattr(references[0], "ItemReference" if is_ifc2x3 else "Identification") == "1"
|
||||
assert references[0].ReferencedSource == self.file.by_type("IfcClassification")[0]
|
||||
|
||||
def test_adding_a_reference_to_a_resource(self):
|
||||
references2 = list(ifcopenshell.util.classification.get_references(element2))
|
||||
assert len(references2) == 1
|
||||
assert getattr(references2[0], "ItemReference" if is_ifc2x3 else "Identification") == "1"
|
||||
assert references2[0].ReferencedSource == self.file.by_type("IfcClassification")[0]
|
||||
assert references[0] == references2[0]
|
||||
|
||||
rel = next(
|
||||
rel
|
||||
for rel in self.file.by_type("IfcRelAssociatesClassification")
|
||||
if rel.RelatingClassification == references[0]
|
||||
)
|
||||
assert len(rel.RelatedObjects) == 2
|
||||
|
||||
def test_adding_a_reference_to_a_resource_and_to_a_root(self):
|
||||
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
|
||||
element = self.file.createIfcMaterial()
|
||||
element2 = self.file.createIfcCostValue()
|
||||
element3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
result = ifcopenshell.api.run("classification.add_classification", self.file, classification="Name")
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
)
|
||||
|
||||
if self.file.schema == "IFC2X3":
|
||||
with pytest.raises(TypeError):
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
products=[element, element2, element3],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
)
|
||||
return
|
||||
else:
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
products=[element, element2, element3],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
)
|
||||
|
||||
references = list(ifcopenshell.util.classification.get_references(element))
|
||||
assert len(references) == 1
|
||||
assert references[0].Identification == "X"
|
||||
assert references[0].Name == "Foobar"
|
||||
assert references[0].ReferencedSource == self.file.by_type("IfcClassification")[0]
|
||||
|
||||
element2 = self.file.createIfcCostValue()
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element2,
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
)
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0].Identification == "X"
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0].Name == "Foobar"
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0] == references[0]
|
||||
references2 = list(ifcopenshell.util.classification.get_references(element2))
|
||||
assert references2[0].Identification == "X"
|
||||
assert references2[0].Name == "Foobar"
|
||||
assert references2[0] == references[0]
|
||||
|
||||
references3 = list(ifcopenshell.util.classification.get_references(element3))
|
||||
assert references3[0].Identification == "X"
|
||||
assert references3[0].Name == "Foobar"
|
||||
assert references3[0] == references[0]
|
||||
|
||||
assert len(self.file.by_type("IfcExternalReferenceRelationship")[0].RelatedResourceObjects) == 2
|
||||
rel = next(
|
||||
rel
|
||||
for rel in self.file.by_type("IfcRelAssociatesClassification")
|
||||
if rel.RelatingClassification == references[0]
|
||||
)
|
||||
assert len(rel.RelatedObjects) == 1
|
||||
|
||||
|
||||
class TestAddReferenceIFC2X3(test.bootstrap.IFC2X3, TestAddReference):
|
||||
pass
|
||||
|
||||
@@ -34,7 +34,7 @@ class TestRemoveClassification(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
@@ -51,7 +51,7 @@ class TestRemoveClassification(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
|
||||
@@ -29,7 +29,7 @@ class TestRemoveReference(test.bootstrap.IFC4):
|
||||
reference = ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
@@ -45,7 +45,7 @@ class TestRemoveReference(test.bootstrap.IFC4):
|
||||
reference = ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
@@ -62,7 +62,7 @@ class TestRemoveReference(test.bootstrap.IFC4):
|
||||
reference = ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
@@ -70,7 +70,7 @@ class TestRemoveReference(test.bootstrap.IFC4):
|
||||
reference2 = ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element2,
|
||||
products=[element2],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
import test.bootstrap
|
||||
import ifcopenshell.api
|
||||
import ifcopenshell.util.classification
|
||||
import ifcopenshell.util.element
|
||||
import ifcopenshell.util.system
|
||||
from datetime import datetime
|
||||
@@ -186,3 +187,35 @@ class TestTemporarySupportForDeprecatedAPIArguments(test.bootstrap.IFC4):
|
||||
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
|
||||
assert len(self.file.by_type("IfcWall")) == 1
|
||||
assert len(self.file.by_type("IfcMaterial")) == 1
|
||||
|
||||
@deprecation_check
|
||||
def test_adding_a_reference(self):
|
||||
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
|
||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
result = ifcopenshell.api.run("classification.add_classification", self.file, classification="Name")
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
)
|
||||
references = list(ifcopenshell.util.classification.get_references(element))
|
||||
assert len(references) == 1
|
||||
assert references[0].Identification == "X"
|
||||
assert references[0].Name == "Foobar"
|
||||
assert references[0].ReferencedSource == self.file.by_type("IfcClassification")[0]
|
||||
|
||||
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element2,
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
)
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0].Identification == "X"
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0].Name == "Foobar"
|
||||
assert list(ifcopenshell.util.classification.get_references(element2))[0] == references[0]
|
||||
|
||||
@@ -34,14 +34,14 @@ class TestGetReferences(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
reference=reference1,
|
||||
classification=classification,
|
||||
)
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
reference=reference2,
|
||||
classification=classification,
|
||||
)
|
||||
@@ -54,7 +54,7 @@ class TestGetReferences(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
@@ -74,14 +74,14 @@ class TestGetReferences(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
reference=reference1,
|
||||
classification=classification,
|
||||
)
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element_type,
|
||||
products=[element_type],
|
||||
reference=reference2,
|
||||
classification=classification,
|
||||
)
|
||||
@@ -103,14 +103,14 @@ class TestGetReferences(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
reference=reference1,
|
||||
classification=classification,
|
||||
)
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element_type,
|
||||
products=[element_type],
|
||||
reference=reference2,
|
||||
classification=classification,
|
||||
)
|
||||
|
||||
@@ -225,7 +225,7 @@ class TestFilterElements(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run(
|
||||
"classification.add_reference",
|
||||
self.file,
|
||||
product=element,
|
||||
products=[element],
|
||||
identification="X",
|
||||
name="Foobar",
|
||||
classification=result,
|
||||
|
||||
Reference in New Issue
Block a user