mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-18 11:20:21 +00:00
Tools for handling shared psets #5291
In IFC it's possible for a property set to be assigned to multiple elements and which may lead to confusing behaviour when you edit a pset on one element and other element seems to get edited too. Which makes it worse is that that it is possible that some software is might be doing this unintentionally when exporting IFC (as some sort of optimization as storing 1 is more optimal than n copies of it). So now there are some tools in Bonsai and in IfcOpenShell to handle the shared psest: 1) Indication that property is shared - https://imgur.com/a/9dd3jST (similar to how Blender indicates ID data-block users). You can click on it to "unshare" the pset - a new copy for the pset will be created and it's going to be linked only to the active object. 2) api pset.unshare_pset method that does the same. And util.element.get_elements_using_pset method that encapsulates schema differences and different approaches for occurrences/types. 3) ifcpatch recipe 'UnsharePsets' that's making all property sets in the IFC file to have just 1 element that's using them. You can limit the affected elements by providing query. ifcpatch recipe is also available in Bonsai - https://i.imgur.com/aOCx7HI.png
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
# IfcPatch - IFC patching utiliy
|
||||
# Copyright (C) 2020, 2021, 2022 Dion Moult <dion@thinkmoult.com>
|
||||
#
|
||||
# This file is part of IfcPatch.
|
||||
#
|
||||
# IfcPatch is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# IfcPatch is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with IfcPatch. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ifcopenshell
|
||||
import ifcopenshell.api.pset
|
||||
import ifcopenshell.guid
|
||||
import ifcopenshell.util.element
|
||||
import ifcopenshell.util.selector
|
||||
from typing import Union
|
||||
from logging import Logger
|
||||
|
||||
|
||||
class Patcher:
|
||||
def __init__(self, src: str, file: ifcopenshell.file, logger: Logger, query: str = ""):
|
||||
"""Create independent copies for shared psets in IFC file.
|
||||
|
||||
In IFC it's possible that same property set is shared by multiple elements,
|
||||
so editing it's properties will automatically change their values for all those elements.
|
||||
|
||||
Sometimes it's intended but sometimes it's not and it's just the way some other
|
||||
software exports IFC (e.g. there is a known case when Tekla exports shared psets for all the occurrences).
|
||||
While it is more optimized way to store data, it may lead to unexpected results when editing properties.
|
||||
|
||||
This recipe creates independent copies of all shared psets (may be limited by the query)
|
||||
and assigns them to the elements, so they can be edited without affecting any other elements.
|
||||
|
||||
:param query: A query to select the subset of IFC elements, optional.
|
||||
If not provided, patch will be applied to all shared property sets in the model.
|
||||
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
|
||||
# Unshare all psets in the IFC file.
|
||||
ifcpatch.execute({"input": "input.ifc", "file": model, "recipe": "UnsharePsets"})
|
||||
|
||||
# Unshare psets on all IfcWalls.
|
||||
ifcpatch.execute({"input": "input.ifc", "file": model, "recipe": "UnsharePsets", "arguments": ["IfcWall"]})
|
||||
"""
|
||||
self.src = src
|
||||
self.file = file
|
||||
self.logger = logger
|
||||
self.query = query
|
||||
|
||||
def patch(self):
|
||||
filtered_elements: set[ifcopenshell.entity_instance] = set()
|
||||
if self.query:
|
||||
filtered_elements = ifcopenshell.util.selector.filter_elements(self.file, self.query)
|
||||
|
||||
all_psets = self.file.by_type("IfcPropertySetDefinition")
|
||||
psets: dict[ifcopenshell.entity_instance, set[ifcopenshell.entity_instance]] = {}
|
||||
for pset in all_psets:
|
||||
elements = ifcopenshell.util.element.get_elements_using_pset(pset)
|
||||
# Skip non shared psets.
|
||||
if len(elements) < 2:
|
||||
continue
|
||||
# Skip non selected elements.
|
||||
if self.query:
|
||||
if not any(e in filtered_elements for e in elements):
|
||||
continue
|
||||
elements = elements.intersection(filtered_elements)
|
||||
psets[pset] = elements
|
||||
|
||||
new_psets = []
|
||||
for pset, elements in psets.items():
|
||||
# Let the first element to keep the original property set.
|
||||
elements = list(elements)[1:]
|
||||
new_psets.extend(ifcopenshell.api.pset.unshare_pset(self.file, elements, pset))
|
||||
|
||||
print(f"{len(new_psets)} new psets were created.")
|
||||
@@ -0,0 +1,94 @@
|
||||
# IfcOpenShell - IFC toolkit and geometry engine
|
||||
# Copyright (C) 2022 Dion Moult <dion@thinkmoult.com>
|
||||
#
|
||||
# This file is part of IfcOpenShell.
|
||||
#
|
||||
# IfcOpenShell is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# IfcOpenShell is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ifcpatch
|
||||
import ifcopenshell
|
||||
import ifcopenshell.api.pset
|
||||
import ifcopenshell.geom
|
||||
import ifcopenshell.util.element
|
||||
import test.bootstrap
|
||||
|
||||
|
||||
class TestUnsharePsets(test.bootstrap.IFC4):
|
||||
def test_unshare_all_psets(self):
|
||||
elements = [self.file.create_entity("IfcWall") for _ in range(3)]
|
||||
|
||||
ifcopenshell.api.pset.add_pset(self.file, elements[0], "Foo")
|
||||
rel = self.file.by_type("IfcRelDefinesByProperties")[0]
|
||||
rel.RelatedObjects = elements
|
||||
|
||||
ifcpatch.execute({"file": self.file, "recipe": "UnsharePsets"})
|
||||
assert len(psets := self.file.by_type("IfcPropertySet")) == 3
|
||||
assert len(self.file.by_type("IfcRelDefinesByProperties")) == 3
|
||||
|
||||
used_elements = set()
|
||||
for pset in psets:
|
||||
pset_elements = ifcopenshell.util.element.get_elements_using_pset(pset)
|
||||
assert len(pset_elements) == 1
|
||||
used_elements.update(pset_elements)
|
||||
|
||||
assert used_elements == set(elements)
|
||||
|
||||
def test_unshare_all_psets_include_types(self):
|
||||
elements = [self.file.create_entity("IfcWallType") for _ in range(3)]
|
||||
|
||||
pset = ifcopenshell.api.pset.add_pset(self.file, elements[0], "Foo")
|
||||
elements[1].HasPropertySets = (pset,)
|
||||
elements[2].HasPropertySets = (pset,)
|
||||
|
||||
ifcpatch.execute({"file": self.file, "recipe": "UnsharePsets"})
|
||||
assert len(psets := self.file.by_type("IfcPropertySet")) == 3
|
||||
assert len(self.file.by_type("IfcRelDefinesByProperties")) == 0
|
||||
|
||||
used_psets: set[ifcopenshell.entity_instance] = set()
|
||||
for element in elements:
|
||||
element_psets = element.HasPropertySets
|
||||
assert len(element_psets) == 1
|
||||
used_psets.add(element_psets[0])
|
||||
|
||||
assert used_psets == set(psets)
|
||||
|
||||
def test_unshare_psets_for_elements_from_query(self):
|
||||
shared_pset_elements = [self.file.create_entity("IfcSlab") for _ in range(3)]
|
||||
shared_pset = ifcopenshell.api.pset.add_pset(self.file, shared_pset_elements[0], "Foo")
|
||||
rel = self.file.by_type("IfcRelDefinesByProperties")[0]
|
||||
rel.RelatedObjects = shared_pset_elements
|
||||
|
||||
elements = [self.file.create_entity("IfcWall") for _ in range(3)]
|
||||
ifcopenshell.api.pset.add_pset(self.file, elements[0], "Foo")
|
||||
rel = self.file.by_type("IfcRelDefinesByProperties")[1]
|
||||
rel.RelatedObjects = elements
|
||||
|
||||
ifcpatch.execute({"file": self.file, "recipe": "UnsharePsets", "arguments": ["IfcWall"]})
|
||||
assert len(psets := self.file.by_type("IfcPropertySet")) == 4
|
||||
assert len(self.file.by_type("IfcRelDefinesByProperties")) == 4
|
||||
|
||||
psets.remove(shared_pset)
|
||||
used_elements = set()
|
||||
for pset in psets:
|
||||
pset_elements = ifcopenshell.util.element.get_elements_using_pset(pset)
|
||||
assert len(pset_elements) == 1
|
||||
used_elements.update(pset_elements)
|
||||
|
||||
assert used_elements == set(elements)
|
||||
# Leave shared pset untouched as it's not part of the provided query.
|
||||
assert ifcopenshell.util.element.get_elements_using_pset(shared_pset) == set(shared_pset_elements)
|
||||
|
||||
|
||||
class TestUnsharePsetsIFC2X3(test.bootstrap.IFC2X3, TestUnsharePsets):
|
||||
pass
|
||||
Reference in New Issue
Block a user