2022-01-19 12:18:33 +11:00
|
|
|
# IfcOpenShell - IFC toolkit and geometry engine
|
|
|
|
|
# Copyright (C) 2021 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/>.
|
|
|
|
|
|
2021-01-21 21:58:27 +11:00
|
|
|
import ifcopenshell
|
|
|
|
|
import ifcopenshell.util.schema
|
|
|
|
|
|
2021-01-22 16:55:38 +11:00
|
|
|
|
2021-01-21 21:58:27 +11:00
|
|
|
class Usecase:
|
2021-03-27 19:14:32 +11:00
|
|
|
def __init__(self, file, **settings):
|
2021-01-21 21:58:27 +11:00
|
|
|
self.file = file
|
|
|
|
|
self.settings = {
|
|
|
|
|
"product": None,
|
|
|
|
|
"reference": None,
|
2022-08-02 10:09:51 +10:00
|
|
|
"identification": None,
|
|
|
|
|
"name": None,
|
2021-01-21 21:58:27 +11:00
|
|
|
"classification": None,
|
2022-05-11 15:19:31 +10:00
|
|
|
"is_lightweight": True,
|
2021-01-21 21:58:27 +11:00
|
|
|
}
|
|
|
|
|
for key, value in settings.items():
|
|
|
|
|
self.settings[key] = value
|
|
|
|
|
|
|
|
|
|
def execute(self):
|
2022-09-02 18:23:49 +10:00
|
|
|
self.is_rooted = self.settings["product"].is_a("IfcRoot")
|
2022-08-02 10:09:51 +10:00
|
|
|
if self.settings["reference"]:
|
|
|
|
|
return self.add_from_library()
|
|
|
|
|
return self.add_from_identification()
|
2021-01-22 16:55:38 +11:00
|
|
|
|
2022-08-02 10:09:51 +10:00
|
|
|
def add_from_identification(self):
|
|
|
|
|
reference = self.get_existing_reference(self.settings["identification"])
|
2022-09-02 18:23:49 +10:00
|
|
|
if not reference:
|
2022-08-02 10:09:51 +10:00
|
|
|
reference = self.file.createIfcClassificationReference(
|
2022-08-02 19:30:37 +10:00
|
|
|
Name=self.settings["name"], ReferencedSource=self.settings["classification"]
|
2022-08-02 10:09:51 +10:00
|
|
|
)
|
2022-08-02 19:30:37 +10:00
|
|
|
if self.file.schema == "IFC2X3":
|
|
|
|
|
reference.ItemReference = self.settings["identification"]
|
|
|
|
|
else:
|
|
|
|
|
reference.Identification = self.settings["identification"]
|
2022-09-02 18:23:49 +10:00
|
|
|
|
|
|
|
|
relationship = self.get_existing_relationship(reference)
|
|
|
|
|
if relationship:
|
|
|
|
|
self.add_to_existing_relationship(relationship)
|
|
|
|
|
else:
|
2022-08-02 10:09:51 +10:00
|
|
|
self.add_new_relationship(reference)
|
|
|
|
|
return reference
|
|
|
|
|
|
|
|
|
|
def add_from_library(self):
|
2021-01-22 16:55:38 +11:00
|
|
|
if hasattr(self.settings["reference"], "ItemReference"):
|
|
|
|
|
identification = self.settings["reference"].ItemReference # IFC2X3
|
|
|
|
|
else:
|
|
|
|
|
identification = self.settings["reference"].Identification
|
|
|
|
|
|
2022-08-02 10:09:51 +10:00
|
|
|
reference = self.get_existing_reference(identification)
|
2022-09-02 18:23:49 +10:00
|
|
|
if not reference:
|
|
|
|
|
migrator = ifcopenshell.util.schema.Migrator()
|
2021-01-21 21:58:27 +11:00
|
|
|
|
2022-09-02 18:23:49 +10:00
|
|
|
if self.settings["is_lightweight"]:
|
|
|
|
|
old_referenced_source = self.settings["reference"].ReferencedSource
|
|
|
|
|
self.settings["reference"].ReferencedSource = None
|
|
|
|
|
else:
|
|
|
|
|
existing_classification = [
|
|
|
|
|
c for c in self.file.by_type("IfcClassification") if c.Name == self.settings["classification"].Name
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
reference = migrator.migrate(self.settings["reference"], self.file)
|
|
|
|
|
|
|
|
|
|
if self.settings["is_lightweight"]:
|
|
|
|
|
reference.ReferencedSource = self.settings["classification"]
|
|
|
|
|
self.settings["reference"].ReferencedSource = old_referenced_source
|
|
|
|
|
elif existing_classification:
|
|
|
|
|
to_delete = set()
|
|
|
|
|
for traversed_reference in self.file.traverse(reference):
|
|
|
|
|
if traversed_reference.ReferencedSource.is_a("IfcClassification"):
|
|
|
|
|
to_delete.add(traversed_reference.ReferencedSource)
|
|
|
|
|
traversed_reference.ReferencedSource = existing_classification[0]
|
|
|
|
|
break
|
|
|
|
|
for element in to_delete:
|
|
|
|
|
self.file.remove(element)
|
|
|
|
|
|
|
|
|
|
relationship = self.get_existing_relationship(reference)
|
|
|
|
|
if relationship:
|
2022-08-03 17:33:23 +10:00
|
|
|
self.add_to_existing_relationship(reference)
|
2022-06-21 19:28:53 +10:00
|
|
|
else:
|
2022-09-02 18:23:49 +10:00
|
|
|
self.add_new_relationship(reference)
|
|
|
|
|
|
|
|
|
|
return reference
|
2022-08-02 10:09:51 +10:00
|
|
|
|
|
|
|
|
def get_existing_reference(self, identification):
|
|
|
|
|
for reference in self.file.by_type("IfcClassificationReference"):
|
|
|
|
|
if self.file.schema == "IFC2X3":
|
|
|
|
|
if reference.ItemReference == identification:
|
|
|
|
|
return reference
|
|
|
|
|
else:
|
|
|
|
|
if reference.Identification == identification:
|
|
|
|
|
return reference
|
2022-06-21 19:28:53 +10:00
|
|
|
|
2022-08-02 10:09:51 +10:00
|
|
|
def add_new_relationship(self, reference):
|
2022-09-02 18:23:49 +10:00
|
|
|
if self.is_rooted:
|
|
|
|
|
self.file.create_entity(
|
|
|
|
|
"IfcRelAssociatesClassification",
|
|
|
|
|
GlobalId=ifcopenshell.guid.new(),
|
|
|
|
|
RelatedObjects=[self.settings["product"]],
|
|
|
|
|
RelatingClassification=reference,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
self.file.create_entity(
|
|
|
|
|
"IfcExternalReferenceRelationship",
|
|
|
|
|
RelatingReference=reference,
|
|
|
|
|
RelatedResourceObjects=[self.settings["product"]],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def add_to_existing_relationship(self, rel):
|
|
|
|
|
if self.is_rooted:
|
|
|
|
|
related_objects = set(rel.RelatedObjects)
|
|
|
|
|
related_objects.add(self.settings["product"])
|
|
|
|
|
rel.RelatedObjects = list(related_objects)
|
|
|
|
|
else:
|
|
|
|
|
related_objects = set(rel.RelatedResourceObjects)
|
|
|
|
|
related_objects.add(self.settings["product"])
|
|
|
|
|
rel.RelatedResourceObjects = list(related_objects)
|
|
|
|
|
|
|
|
|
|
def get_existing_relationship(self, reference):
|
|
|
|
|
if self.is_rooted:
|
|
|
|
|
if self.file.schema == "IFC2X3":
|
|
|
|
|
for rel in self.file.by_type("IfcRelAssociatesClassification"):
|
|
|
|
|
if rel.RelatingClassification == reference:
|
|
|
|
|
return rel
|
|
|
|
|
elif reference.ClassificationRefForObjects:
|
|
|
|
|
return reference.ClassificationRefForObjects[0]
|
|
|
|
|
elif self.file.schema != "IFC2X3":
|
|
|
|
|
if reference.ExternalReferenceForResources:
|
|
|
|
|
return reference.ExternalReferenceForResources[0]
|