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,42 @@
import ifcopenshell
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"product": None,
"relating_structure": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
contained_in_structure = self.settings["product"].ContainedInStructure
contains_elements = self.settings["relating_structure"].ContainsElements
if contains_elements and contained_in_structure and contained_in_structure[0] == contains_elements[0]:
return
if contained_in_structure:
related_elements = list(contained_in_structure[0].RelatedElements)
related_elements.remove(self.settings["product"])
if related_elements:
contained_in_structure[0].RelatedElements = related_elements
else:
self.file.remove(contained_in_structure[0])
if contains_elements:
related_elements = list(contains_elements[0].RelatedElements)
related_elements.append(self.settings["product"])
contains_elements[0].RelatedElements = related_elements
else:
contains_elements = self.file.create_entity(
"IfcRelContainedInSpatialStructure",
**{
"GlobalId": ifcopenshell.guid.new(),
# TODO "OwnerHistory": None
"RelatedElements": [self.settings["product"]],
"RelatingStructure": self.settings["relating_structure"],
}
)
@@ -0,0 +1,50 @@
class Data:
products = {}
spatial_elements = {}
@classmethod
def purge(cls):
cls.products = {}
cls.spatial_elements = {}
@classmethod
def load(cls, file, product_id=None):
if product_id:
return cls.load_product(file, product_id)
cls.load_spatial_elements(file)
@classmethod
def load_product(cls, file, product_id):
if not file:
return
product = file.by_id(product_id)
if not hasattr(product, "ContainedInStructure"):
cls.products[product_id] = None
elif product.ContainedInStructure:
structure = product.ContainedInStructure[0].RelatingStructure
cls.products[product_id] = {"type": structure.is_a(), "Name": structure.Name, "id": int(structure.id())}
else:
cls.products[product_id] = {"type": None, "Name": None, "id": None}
@classmethod
def load_spatial_elements(cls, file):
if not file:
return
cls.spatial_elements = {}
ifc_class = "IfcSpatialElement"
if file.schema == "IFC2X3":
ifc_class = "IfcSpatialStructureElement"
for element in file.by_type(ifc_class):
decomposes = None
if element.Decomposes:
decomposes = element.Decomposes[0].RelatingObject.id()
is_decomposed_by = []
if element.IsDecomposedBy:
for rel in element.IsDecomposedBy:
is_decomposed_by.extend([e.id() for e in rel.RelatedObjects])
cls.spatial_elements[element.id()] = {
"Name": element.Name,
"LongName": element.LongName,
"Decomposes": decomposes,
"IsDecomposedBy": is_decomposed_by
}
@@ -0,0 +1,20 @@
import ifcopenshell
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"product": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
contained_in_structure = self.settings["product"].ContainedInStructure
if contained_in_structure:
related_elements = list(contained_in_structure[0].RelatedElements)
related_elements.remove(self.settings["product"])
if related_elements:
contained_in_structure[0].RelatedElements = related_elements
else:
self.file.remove(contained_in_structure)