2022-08-02 10:09:51 +10:00
|
|
|
# IfcOpenShell - IFC toolkit and geometry engine
|
|
|
|
|
# Copyright (C) 2022 Dion Moult <dion@thinkmoult.com>
|
|
|
|
|
#
|
|
|
|
|
# This file is part of IfcOpenShell.
|
|
|
|
|
#
|
|
|
|
|
# IfcOpenShell is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# IfcOpenShell is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-04-15 12:06:39 +05:00
|
|
|
import pytest
|
2022-08-02 10:09:51 +10:00
|
|
|
import test.bootstrap
|
|
|
|
|
import ifcopenshell.api
|
|
|
|
|
import ifcopenshell.util.classification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestAddReference(test.bootstrap.IFC4):
|
|
|
|
|
def test_adding_a_reference(self):
|
2024-04-15 12:06:39 +05:00
|
|
|
is_ifc2x3 = self.file.schema == "IFC2X3"
|
|
|
|
|
|
2022-08-02 10:09:51 +10:00
|
|
|
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
|
|
|
|
|
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
2024-04-15 12:06:39 +05:00
|
|
|
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
2022-08-02 10:09:51 +10:00
|
|
|
result = ifcopenshell.api.run("classification.add_classification", self.file, classification="Name")
|
|
|
|
|
ifcopenshell.api.run(
|
|
|
|
|
"classification.add_reference",
|
|
|
|
|
self.file,
|
2024-04-15 12:06:39 +05:00
|
|
|
products=[element, element2],
|
2022-08-02 10:09:51 +10:00
|
|
|
identification="X",
|
|
|
|
|
name="Foobar",
|
|
|
|
|
classification=result,
|
|
|
|
|
)
|
|
|
|
|
references = list(ifcopenshell.util.classification.get_references(element))
|
|
|
|
|
assert len(references) == 1
|
2024-04-15 12:06:39 +05:00
|
|
|
assert getattr(references[0], "ItemReference" if is_ifc2x3 else "Identification") == "X"
|
2022-08-02 10:09:51 +10:00
|
|
|
assert references[0].Name == "Foobar"
|
|
|
|
|
assert references[0].ReferencedSource == self.file.by_type("IfcClassification")[0]
|
|
|
|
|
|
2024-04-15 12:06:39 +05:00
|
|
|
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]
|
2022-08-03 17:33:23 +10:00
|
|
|
)
|
2024-04-15 12:06:39 +05:00
|
|
|
assert len(rel.RelatedObjects) == 2
|
2022-08-03 17:33:23 +10:00
|
|
|
|
2022-09-02 18:23:49 +10:00
|
|
|
def test_adding_a_library_based_reference(self):
|
2024-04-15 12:06:39 +05:00
|
|
|
is_ifc2x3 = self.file.schema == "IFC2X3"
|
|
|
|
|
|
2022-08-02 10:09:51 +10:00
|
|
|
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")
|
2024-04-15 12:06:39 +05:00
|
|
|
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
2022-08-02 10:09:51 +10:00
|
|
|
result = ifcopenshell.api.run("classification.add_classification", self.file, classification=classification)
|
|
|
|
|
ifcopenshell.api.run(
|
|
|
|
|
"classification.add_reference",
|
|
|
|
|
self.file,
|
2024-04-15 12:06:39 +05:00
|
|
|
products=[element, element2],
|
2022-08-02 10:09:51 +10:00
|
|
|
reference=reference,
|
|
|
|
|
classification=result,
|
|
|
|
|
)
|
|
|
|
|
references = list(ifcopenshell.util.classification.get_references(element))
|
|
|
|
|
assert len(references) == 1
|
2024-04-15 12:06:39 +05:00
|
|
|
assert getattr(references[0], "ItemReference" if is_ifc2x3 else "Identification") == "1"
|
2022-08-02 10:09:51 +10:00
|
|
|
assert references[0].ReferencedSource == self.file.by_type("IfcClassification")[0]
|
2022-09-02 18:23:49 +10:00
|
|
|
|
2024-04-15 12:06:39 +05:00
|
|
|
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):
|
2022-09-02 18:23:49 +10:00
|
|
|
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
|
|
|
|
|
element = self.file.createIfcMaterial()
|
2024-04-15 12:06:39 +05:00
|
|
|
element2 = self.file.createIfcCostValue()
|
|
|
|
|
element3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
2022-09-02 18:23:49 +10:00
|
|
|
result = ifcopenshell.api.run("classification.add_classification", self.file, classification="Name")
|
2024-04-15 12:06:39 +05:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-02 18:23:49 +10:00
|
|
|
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]
|
|
|
|
|
|
2024-04-15 12:06:39 +05:00
|
|
|
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]
|
2022-09-02 18:23:49 +10:00
|
|
|
|
|
|
|
|
assert len(self.file.by_type("IfcExternalReferenceRelationship")[0].RelatedResourceObjects) == 2
|
2024-04-15 12:06:39 +05:00
|
|
|
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
|