pset.assign_pset, pset.unassign_pset

This commit is contained in:
Andrej730
2024-09-04 17:12:57 +05:00
parent 691815fd41
commit 0ab70827e5
9 changed files with 350 additions and 77 deletions
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api.owner
import ifcopenshell.api.pset
import ifcopenshell.guid
import ifcopenshell.util.element
@@ -34,8 +35,28 @@ def unshare_pset(
:param products: Elements (or element types) to link the pset to.
:param pset: Shared property set.
:return: List of copied property sets.
Example:
.. code:: python
element1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
element2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.pset.assign_pset(self.file, [element1, element2], pset)
# Pset is now shared by 2 elements.
assert ifcopenshell.util.element.get_elements_using_pset(pset) == {element1, element2}
new_psets = ifcopenshell.api.pset.unshare_pset(self.file, [element2], pset)
# element2 was unassigned from the original pset.
assert ifcopenshell.util.element.get_elements_using_pset(pset) == {element1}
new_pset = new_psets[0]
# New pset was created and was assigned to element2.
assert new_pset != pset
assert ifcopenshell.util.element.get_elements_using_pset(new_pset) == {element2}
"""
is_ifc2x3 = file.schema == "IFC2X3"
products_occurrences: set[ifcopenshell.entity_instance] = set()
products_types: set[ifcopenshell.entity_instance] = set()
for product in products:
@@ -44,51 +65,12 @@ def unshare_pset(
else:
products_occurrences.add(product)
# Check occurrences using pset.
rels = pset.PropertyDefinitionOf if is_ifc2x3 else pset.DefinesOccurrence
for rel in rels:
objs = set(rel.RelatedObjects)
if not any(p in objs for p in products_occurrences):
continue
objs.difference_update(products_occurrences)
if objs:
rel.RelatedObjects = list(objs)
else:
history = rel.OwnerHistory
file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(file, history)
# Check types using pset.
for product in products_types:
if not (psets := product.HasPropertySets):
continue
psets: list[ifcopenshell.entity_instance] = list(psets)
psets.remove(pset)
product.HasPropertySets = psets
def assign_pset(product: ifcopenshell.entity_instance, pset: ifcopenshell.entity_instance) -> None:
if product.is_a("IfcTypeProduct"):
psets = list(product.HasPropertySets or [])
if not psets:
psets = []
product.HasPropertySets = psets + [pset]
return
file.create_entity(
"IfcRelDefinesByProperties",
**{
"GlobalId": ifcopenshell.guid.new(),
"OwnerHistory": ifcopenshell.api.owner.create_owner_history(file),
"RelatedObjects": [product],
"RelatingPropertyDefinition": pset_copy,
},
)
ifcopenshell.api.pset.unassign_pset(file, products, pset)
pset_copies: list[ifcopenshell.entity_instance] = []
for product in products:
pset_copy = ifcopenshell.util.element.copy_deep(file, pset)
pset_copies.append(pset_copy)
assign_pset(product, pset_copy)
ifcopenshell.api.pset.assign_pset(file, [product], pset_copy)
return pset_copies