From 3bbdc3f277c130eae3dc7473972d69c3967549f7 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Fri, 22 Jan 2021 16:55:38 +1100 Subject: [PATCH] WIP fix IFC2X3 errors in classification refactor --- .../classification/add_classification.py | 13 ++++++++++ .../module/classification/add_reference.py | 26 +++++++++++++------ .../bim/module/classification/data.py | 8 ++---- .../classification/remove_classification.py | 3 --- .../ifcopenshell/util/date.py | 2 ++ 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/ifcblenderexport/blenderbim/bim/module/classification/add_classification.py b/src/ifcblenderexport/blenderbim/bim/module/classification/add_classification.py index 7c3c473557..7c16292e9f 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/classification/add_classification.py +++ b/src/ifcblenderexport/blenderbim/bim/module/classification/add_classification.py @@ -1,5 +1,6 @@ import ifcopenshell import ifcopenshell.util.schema +import ifcopenshell.util.date class Usecase: def __init__(self, file, settings=None): @@ -11,8 +12,20 @@ class Usecase: self.settings[key] = value def execute(self): + edition_date = None + if self.settings["classification"].EditionDate: + edition_date = ifcopenshell.util.date.ifc2datetime(self.settings["classification"].EditionDate) + self.settings["classification"].EditionDate = None + migrator = ifcopenshell.util.schema.Migrator() result = migrator.migrate(self.settings["classification"], self.file) + + # TODO: should auto date migration be part of the migrator? + if self.file.schema == "IFC2X3" and edition_date: + result.EditionDate = ifcopenshell.util.date.datetime2ifc(edition_date, "IfcCalendarDate") + else: + result.EditionDate = ifcopenshell.util.date.datetime2ifc(edition_date, "IfcDate") + self.file.create_entity("IfcRelAssociatesClassification", **{ "GlobalId": ifcopenshell.guid.new(), "RelatedObjects": [self.file.by_type("IfcProject")[0]], diff --git a/src/ifcblenderexport/blenderbim/bim/module/classification/add_reference.py b/src/ifcblenderexport/blenderbim/bim/module/classification/add_reference.py index d6dff741ef..5c880510eb 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/classification/add_reference.py +++ b/src/ifcblenderexport/blenderbim/bim/module/classification/add_reference.py @@ -1,6 +1,7 @@ import ifcopenshell import ifcopenshell.util.schema + class Usecase: def __init__(self, file, settings=None): self.file = file @@ -14,13 +15,19 @@ class Usecase: def execute(self): relating_classification = None + + if hasattr(self.settings["reference"], "ItemReference"): + identification = self.settings["reference"].ItemReference # IFC2X3 + else: + identification = self.settings["reference"].Identification + for reference in self.file.by_type("IfcClassificationReference"): if self.file.schema == "IFC2X3": - if reference.ItemReference == self.settings["reference"].ItemReference: + if reference.ItemReference == identification: relating_classification = reference break else: - if reference.Identification == self.settings["reference"].Identification: + if reference.Identification == identification: relating_classification = reference break @@ -38,16 +45,19 @@ class Usecase: relating_classification = migrator.migrate(self.settings["reference"], self.file) relating_classification.ReferencedSource = self.settings["classification"] self.settings["reference"].ReferencedSource = old_referenced_source - self.file.create_entity("IfcRelAssociatesClassification", **{ - "GlobalId": ifcopenshell.guid.new(), - "RelatedObjects": [self.settings["product"]], - "RelatingClassification": relating_classification - }) + self.file.create_entity( + "IfcRelAssociatesClassification", + **{ + "GlobalId": ifcopenshell.guid.new(), + "RelatedObjects": [self.settings["product"]], + "RelatingClassification": relating_classification, + } + ) def get_association(self, reference): if self.file.schema == "IFC2X3": for association in self.file.by_type("IfcRelAssociatesClassification"): - if relating_classification == reference: + if association.RelatingClassification == reference: return association elif reference.ClassificationRefForObjects: return reference.ClassificationRefForObjects[0] diff --git a/src/ifcblenderexport/blenderbim/bim/module/classification/data.py b/src/ifcblenderexport/blenderbim/bim/module/classification/data.py index b51711342f..204fd23be3 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/classification/data.py +++ b/src/ifcblenderexport/blenderbim/bim/module/classification/data.py @@ -1,6 +1,6 @@ import ifcopenshell +import ifcopenshell.util.date from blenderbim.bim.ifc import IfcStore -from datetime import datetime class Data: @@ -39,11 +39,7 @@ class Data: for classification in cls._file.by_type("IfcClassification"): data = classification.get_info() if cls._file.schema == "IFC2X3" and data["EditionDate"]: - data["EditionDate"] = datetime( - classification.EditionDate.YearComponent, - classification.EditionDate.MonthComponent, - classification.EditionDate.DayComponent, - ).isoformat() + data["EditionDate"] = ifcopenshell.util.date(data.EditionDate).isoformat() cls.classifications[classification.id()] = data @classmethod diff --git a/src/ifcblenderexport/blenderbim/bim/module/classification/remove_classification.py b/src/ifcblenderexport/blenderbim/bim/module/classification/remove_classification.py index dccd8c2e62..a6708c871c 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/classification/remove_classification.py +++ b/src/ifcblenderexport/blenderbim/bim/module/classification/remove_classification.py @@ -1,6 +1,3 @@ -import ifcopenshell.util.schema - - class Usecase: def __init__(self, file, settings=None): self.file = file diff --git a/src/ifcopenshell-python/ifcopenshell/util/date.py b/src/ifcopenshell-python/ifcopenshell/util/date.py index e1bca4089b..1da652f74e 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/date.py +++ b/src/ifcopenshell-python/ifcopenshell/util/date.py @@ -35,6 +35,8 @@ def ifc2datetime(element): def datetime2ifc(dt, ifc_type): + if isinstance(dt, str): + dt = datetime.fromisoformat(dt) if ifc_type == "IfcTimeStamp": return int(dt.timestamp()) elif ifc_type == "IfcDateTime":