attribute.edit_attributes - small optimizations

remove settings dictionary and remove some ifc calls
This commit is contained in:
Andrej730
2024-09-06 14:17:41 +05:00
parent 4c68ec6f82
commit 39fd9ce462
@@ -18,7 +18,8 @@
import ifcopenshell.api.owner import ifcopenshell.api.owner
import ifcopenshell.util.element import ifcopenshell.util.element
from typing import Any from typing import Any, Union
from types import EllipsisType
def edit_attributes(file: ifcopenshell.file, product: ifcopenshell.entity_instance, attributes: dict[str, Any]) -> None: def edit_attributes(file: ifcopenshell.file, product: ifcopenshell.entity_instance, attributes: dict[str, Any]) -> None:
@@ -52,25 +53,35 @@ def edit_attributes(file: ifcopenshell.file, product: ifcopenshell.entity_instan
ifcopenshell.api.attribute.edit_attributes(model, ifcopenshell.api.attribute.edit_attributes(model,
product=element, attributes={"Name": "Waldo"}) product=element, attributes={"Name": "Waldo"})
""" """
settings = {"product": product, "attributes": attributes or {}}
for name, value in settings["attributes"].items(): def getattr_safe(element: ifcopenshell.entity_instance, attr: str) -> Union[Any, EllipsisType]:
setattr(settings["product"], name, value) """Return attribute value or Ellipsis if attribute doesn't exist.
if hasattr(settings["product"], "PredefinedType"):
if hasattr(settings["product"], "ElementType"): Useful as an alternative to hasattr - hasattr under the hood just does
if settings["product"].ElementType is None and settings["product"].PredefinedType == "USERDEFINED": getattr but doesn't return a value. That helps reduce times IFC is accessed.
settings["product"].PredefinedType = "NOTDEFINED" """
elif settings["product"].ElementType and settings["product"].PredefinedType != "USERDEFINED": return getattr(element, attr, ...)
settings["product"].PredefinedType = "USERDEFINED"
elif hasattr(settings["product"], "ObjectType"): for name, value in attributes.items():
relating_type = ifcopenshell.util.element.get_type(settings["product"]) setattr(product, name, value)
if (predefined_type := getattr_safe(product, "PredefinedType")) is not ...:
if (element_type := getattr_safe(product, "ElementType")) is not ...:
if element_type is None and predefined_type == "USERDEFINED":
product.PredefinedType = "NOTDEFINED"
elif element_type and predefined_type != "USERDEFINED":
product.PredefinedType = "USERDEFINED"
elif (object_type := getattr_safe(product, "ObjectType")) is not ...:
relating_type = ifcopenshell.util.element.get_type(product)
# Allow for None due to https://github.com/buildingSMART/IFC4.3.x-development/issues/818 # Allow for None due to https://github.com/buildingSMART/IFC4.3.x-development/issues/818
if relating_type and relating_type.PredefinedType not in ("NOTDEFINED", None): if relating_type and relating_type.PredefinedType not in ("NOTDEFINED", None):
settings["product"].ObjectType = None product.ObjectType = None
settings["product"].PredefinedType = None product.PredefinedType = None
elif settings["product"].ObjectType is None and settings["product"].PredefinedType == "USERDEFINED": elif object_type is None and predefined_type == "USERDEFINED":
settings["product"].PredefinedType = "NOTDEFINED" product.PredefinedType = "NOTDEFINED"
elif settings["product"].ObjectType and settings["product"].PredefinedType != "USERDEFINED": elif object_type and predefined_type != "USERDEFINED":
settings["product"].PredefinedType = "USERDEFINED" product.PredefinedType = "USERDEFINED"
if hasattr(settings["product"], "OwnerHistory"):
ifcopenshell.api.owner.update_owner_history(file, **{"element": settings["product"]}) if hasattr(product, "OwnerHistory"):
ifcopenshell.api.owner.update_owner_history(file, **{"element": product})