From 4e014d1a10b795d0436eda4ab612685dc054536c Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Thu, 13 May 2021 15:11:18 +1000 Subject: [PATCH] New utility functions for copying and deep copying IFC elements --- .../ifcopenshell/util/element.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index b232721482..df8c72cd92 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -1,3 +1,6 @@ +import ifcopenshell + + def get_psets(element): psets = {} try: @@ -134,6 +137,29 @@ def remove_deep(ifc_file, element): ifc_file.unbatch() +def copy(ifc_file, element): + new = ifc_file.create_entity(element.is_a()) + for i, attribute in enumerate(element): + if attribute is None: + continue + new[i] = attribute + return new + + +def copy_deep(ifc_file, element): + new = ifc_file.create_entity(element.is_a()) + for i, attribute in enumerate(element): + if attribute is None: + continue + if isinstance(attribute, ifcopenshell.entity_instance): + attribute = copy_deep(ifc_file, attribute) + elif isinstance(attribute, tuple) and attribute and isinstance(attribute[0], ifcopenshell.entity_instance): + for j, item in enumerate(attribute): + attribute[j] = copy_deep(ifc_file, item) + new[i] = attribute + return new + + def get_representation(element, context, subcontext=None, target_view=None): if element.is_a("IfcProduct") and element.Representation: for r in element.Representation.Representations: