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,37 @@
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):
result = self.file.create_entity(self.settings["product"].is_a())
self.schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(self.file.schema)
self.copy_attributes(self.settings["product"], result)
for inverse in self.file.get_inverse(self.settings["product"]):
for i, value in enumerate(inverse):
if value == self.settings["product"]:
new_inverse = self.file.create_entity(inverse.is_a())
self.copy_attributes(inverse, new_inverse)
new_inverse[i] = result
elif isinstance(value, (tuple, list)) and self.settings["product"] in value:
new_value = list(value)
new_value.append(result)
inverse[i] = new_value
if result.is_a("IfcProduct"):
result.Representation = None
elif result.is_a("IfcTypeProduct"):
result.RepresentationMaps = None
return result
def copy_attributes(self, from_element, to_element):
declaration = self.schema.declaration_by_name(from_element.is_a())
for attribute in declaration.all_attributes():
if attribute.name() == "GlobalId":
setattr(to_element, attribute.name(), ifcopenshell.guid.new())
else:
setattr(to_element, attribute.name(), getattr(from_element, attribute.name()))
@@ -0,0 +1,31 @@
import ifcopenshell
from ifcopenshell.api.owner.api import create_owner_history
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"ifc_class": "IfcBuildingElementProxy",
"predefined_type": None,
"name": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
element = self.file.create_entity(self.settings["ifc_class"], **{
"GlobalId": ifcopenshell.guid.new(),
"OwnerHistory": create_owner_history()
})
element.Name = self.settings["name"] or None
if self.settings["predefined_type"]:
if hasattr(element, "PredefinedType"):
try:
element.PredefinedType = self.settings["predefined_type"]
except:
element.PredefinedType = "USERDEFINED"
element.ObjectType = self.settings["predefined_type"]
elif hasattr(element, "ObjectType"):
element.ObjectType = self.settings["predefined_type"]
return element
@@ -0,0 +1,17 @@
class Data:
products = {}
@classmethod
def purge(cls):
cls.products = {}
@classmethod
def load(cls, file, product_id):
if not file:
return
product = file.by_id(product_id)
cls.products[product_id] = {
"type": product.is_a(),
"PredefinedType": product.PredefinedType if hasattr(product, "PredefinedType") else None,
"ObjectType": product.ObjectType if hasattr(product, "ObjectType") else None
}
@@ -0,0 +1,26 @@
import ifcopenshell
import ifcopenshell.util.schema
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"product": None,
"ifc_class": "IfcBuildingElementProxy",
"predefined_type": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
element = ifcopenshell.util.schema.reassign_class(
self.file, self.settings["product"], self.settings["ifc_class"]
)
if self.settings["predefined_type"] and hasattr(element, "PredefinedType"):
try:
element.PredefinedType = self.settings["predefined_type"]
except:
element.PredefinedType = "USERDEFINED"
element.ObjectType = self.settings["predefined_type"]
return element
@@ -0,0 +1,27 @@
import ifcopenshell.api.geometry.unassign_representation as unassign_representation
import ifcopenshell.api.geometry.remove_representation as remove_representation
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):
representations = []
if self.settings["product"].is_a("IfcProduct"):
if self.settings["product"].Representation:
representations = self.settings["product"].Representation.Representations or []
else:
representations = []
elif self.settings["product"].is_a("IfcTypeProduct"):
representations = [rm.MappedRepresentation for rm in self.settings["product"].RepresentationMaps or []]
for representation in representations:
unassign_representation.Usecase(
self.file, {"product": self.settings["product"], "representation": representation}
).execute()
remove_representation.Usecase(self.file, {"representation": representation}).execute()
# TODO: remove object placement and other relationships
self.file.remove(self.settings["product"])