First attempt at ripping out agnostic code into ifcopenshell.api namespace. See #1399.

This commit is contained in:
Dion Moult
2021-03-25 10:48:31 +11:00
parent 911d703f57
commit d77bc6bdaa
190 changed files with 388 additions and 437 deletions
@@ -0,0 +1,39 @@
import ifcopenshell
import ifcopenshell.util.schema
import ifcopenshell.util.date
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"classification": None,
}
for key, value in settings.items():
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]],
"RelatingClassification": result
})
return # See bug #1272
try:
result = self.file.add(self.settings["classification"])
except:
migrator = ifcopenshell.util.schema.Migrator()
result = migrator.migrate(self.settings["classification"], self.file)
@@ -0,0 +1,63 @@
import ifcopenshell
import ifcopenshell.util.schema
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"product": None,
"reference": None,
"classification": None,
}
for key, value in settings.items():
self.settings[key] = value
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 == identification:
relating_classification = reference
break
else:
if reference.Identification == identification:
relating_classification = reference
break
if relating_classification:
association = self.get_association(relating_classification)
related_objects = set(association.RelatedObjects)
related_objects.add(self.settings["product"])
association.RelatedObjects = list(related_objects)
return
migrator = ifcopenshell.util.schema.Migrator()
# This removal patch is to support a lightweight classification
old_referenced_source = self.settings["reference"].ReferencedSource
self.settings["reference"].ReferencedSource = None
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,
}
)
def get_association(self, reference):
if self.file.schema == "IFC2X3":
for association in self.file.by_type("IfcRelAssociatesClassification"):
if association.RelatingClassification == reference:
return association
elif reference.ClassificationRefForObjects:
return reference.ClassificationRefForObjects[0]
@@ -0,0 +1,76 @@
import ifcopenshell
import ifcopenshell.util.date
class Data:
is_loaded = False
products = {}
classifications = {}
references = {}
library_file = None
library_classifications = {}
library_references = {}
@classmethod
def purge(cls):
cls.is_loaded = False
cls.products = {}
cls.classifications = {}
cls.references = {}
cls.library_file = None
cls.library_classifications = {}
cls.library_references = {}
@classmethod
def load(cls, file, product_id=None):
cls._file = file
if not cls._file:
return
if product_id:
return cls.load_product_classifications(product_id)
cls.load_classifications()
cls.load_references()
cls.is_loaded = True
@classmethod
def load_product_classifications(cls, product_id):
product = cls._file.by_id(product_id)
cls.products[product_id] = []
if not product.HasAssociations:
return
for association in product.HasAssociations:
if association.is_a("IfcRelAssociatesClassification"):
cls.products[product_id].append(association.RelatingClassification.id())
@classmethod
def load_classifications(cls):
cls.classifications = {}
for classification in cls._file.by_type("IfcClassification"):
data = classification.get_info()
if cls._file.schema == "IFC2X3" and data["EditionDate"]:
data["EditionDate"] = ifcopenshell.util.date(data.EditionDate).isoformat()
cls.classifications[classification.id()] = data
@classmethod
def load_references(cls):
cls.references = {}
for reference in cls._file.by_type("IfcClassificationReference"):
data = reference.get_info()
if reference.ReferencedSource:
#data["ReferencedSource"] = cls.get_referenced_source(reference.ReferencedSource)
data["ReferencedSource"] = reference.ReferencedSource.id()
cls.references[reference.id()] = data
@classmethod
def get_referenced_source(cls, reference):
if reference.is_a("IfcClassification"):
return reference
elif reference.is_a("IfcClassificationReference") and reference.ReferencedSource:
return cls.get_referenced_source(reference.ReferencedSource)
@classmethod
def load_library(cls, filepath):
cls.library_file = ifcopenshell.open(filepath)
cls.library_classifications = {}
for classification in cls.library_file.by_type("IfcClassification"):
cls.library_classifications[classification.id()] = classification.Name
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"classification": None,
"attributes": {}
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["classification"], name, value)
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"reference": None,
"attributes": {}
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["reference"], name, value)
@@ -0,0 +1,24 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"classification": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
references = self.get_references(self.settings["classification"])
for reference in references:
self.file.remove(reference)
self.file.remove(self.settings["classification"])
for rel in self.file.by_type("IfcRelAssociatesClassification"):
if not rel.RelatingClassification:
self.file.remove(rel)
def get_references(self, classification):
results = []
if not classification.HasReferences:
return results
for reference in classification.HasReferences:
results.append(reference)
results.extend(self.get_references(reference))
return results
@@ -0,0 +1,28 @@
import ifcopenshell.util.schema
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"reference": None, "product": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
total_related_objects = 0
for association in self.file.by_type("IfcRelAssociatesClassification"):
if association.RelatingClassification == self.settings["reference"] and association.RelatedObjects:
total_related_objects += len(association.RelatedObjects)
related_objects = list(association.RelatedObjects)
try:
related_objects.remove(self.settings["product"])
except:
continue
if len(related_objects):
association.RelatedObjects = related_objects
else:
self.file.remove(association)
# TODO: we only handle lightweight classifications here
if total_related_objects == 1:
self.file.remove(self.settings["reference"])