2022-01-19 12:18:33 +11:00
|
|
|
# IfcOpenShell - IFC toolkit and geometry engine
|
2022-08-02 10:09:51 +10:00
|
|
|
# Copyright (C) 2021, 2022 Dion Moult <dion@thinkmoult.com>
|
2022-01-19 12:18:33 +11:00
|
|
|
#
|
|
|
|
|
# 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
|
|
|
import ifcopenshell.util.date
|
2021-01-21 21:58:27 +11:00
|
|
|
|
2021-07-02 16:30:30 +10: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 = {
|
|
|
|
|
"classification": None,
|
|
|
|
|
}
|
|
|
|
|
for key, value in settings.items():
|
|
|
|
|
self.settings[key] = value
|
|
|
|
|
|
|
|
|
|
def execute(self):
|
2022-08-02 10:09:51 +10:00
|
|
|
if isinstance(self.settings["classification"], str):
|
|
|
|
|
classification = self.file.createIfcClassification(Name=self.settings["classification"])
|
|
|
|
|
self.relate_to_project(classification)
|
|
|
|
|
return classification
|
|
|
|
|
return self.add_from_library()
|
|
|
|
|
|
|
|
|
|
def add_from_library(self):
|
2021-01-22 16:55:38 +11:00
|
|
|
edition_date = None
|
|
|
|
|
if self.settings["classification"].EditionDate:
|
|
|
|
|
edition_date = ifcopenshell.util.date.ifc2datetime(self.settings["classification"].EditionDate)
|
|
|
|
|
self.settings["classification"].EditionDate = None
|
|
|
|
|
|
2021-01-21 21:58:27 +11:00
|
|
|
migrator = ifcopenshell.util.schema.Migrator()
|
|
|
|
|
result = migrator.migrate(self.settings["classification"], self.file)
|
2021-01-22 16:55:38 +11:00
|
|
|
|
|
|
|
|
# TODO: should auto date migration be part of the migrator?
|
|
|
|
|
if self.file.schema == "IFC2X3" and edition_date:
|
2021-07-02 16:30:30 +10:00
|
|
|
result.EditionDate = self.file.create_entity(
|
|
|
|
|
"IfcCalendarDate", **ifcopenshell.util.date.datetime2ifc(edition_date, "IfcCalendarDate")
|
|
|
|
|
)
|
2021-01-22 16:55:38 +11:00
|
|
|
else:
|
|
|
|
|
result.EditionDate = ifcopenshell.util.date.datetime2ifc(edition_date, "IfcDate")
|
|
|
|
|
|
2022-08-02 10:09:51 +10:00
|
|
|
self.relate_to_project(result)
|
|
|
|
|
|
2021-07-02 16:30:30 +10:00
|
|
|
return result # See bug #1272
|
2022-08-02 10:09:51 +10:00
|
|
|
|
2021-01-21 21:58:27 +11:00
|
|
|
try:
|
|
|
|
|
result = self.file.add(self.settings["classification"])
|
|
|
|
|
except:
|
|
|
|
|
migrator = ifcopenshell.util.schema.Migrator()
|
|
|
|
|
result = migrator.migrate(self.settings["classification"], self.file)
|
2022-08-02 10:09:51 +10:00
|
|
|
|
|
|
|
|
def relate_to_project(self, classification):
|
|
|
|
|
self.file.create_entity(
|
|
|
|
|
"IfcRelAssociatesClassification",
|
|
|
|
|
GlobalId=ifcopenshell.guid.new(),
|
|
|
|
|
RelatedObjects=[self.file.by_type("IfcProject")[0]],
|
|
|
|
|
RelatingClassification=classification,
|
|
|
|
|
)
|