Fix #5108. Selector now supports both GlobalId and Name as filter values.

This commit is contained in:
Dion Moult
2025-10-26 22:52:31 +11:00
parent eea585a3cd
commit 0d3be9d101
2 changed files with 29 additions and 9 deletions
@@ -856,8 +856,10 @@ class FacetTransformer(lark.Transformer):
comparison, value = args
def filter_function(element: ifcopenshell.entity_instance) -> bool:
element_value = getattr(ifcopenshell.util.element.get_type(element), "Name", None)
return self.compare(element_value, comparison, value)
element_type = ifcopenshell.util.element.get_type(element)
return self.compare(getattr(element_type, "Name", None), comparison, value) or self.compare(
getattr(element_type, "GlobalId", None), comparison, value
)
self.add_default_elements()
self.elements = set(filter(filter_function, self.elements))
@@ -940,7 +942,7 @@ class FacetTransformer(lark.Transformer):
containers = self.get_container_tree(container)
result = False if containers else None
for container in containers:
if self.compare(container.Name, "=", value):
if self.compare(container.Name, "=", value) or self.compare(container.GlobalId, "=", value):
result = True
if result is not None:
return result if comparison == "=" else not result
@@ -958,6 +960,8 @@ class FacetTransformer(lark.Transformer):
if rel.is_a("IfcRelAssignsToGroup") and rel.RelatingGroup:
if self.compare(rel.RelatingGroup.Name, "=", value):
result = True
elif self.compare(rel.RelatingGroup.GlobalId, "=", value):
result = True
return result if comparison == "=" else not result
self.add_default_elements()
@@ -969,32 +973,44 @@ class FacetTransformer(lark.Transformer):
parents = set()
for rel in self.file.by_type("IfcRelAggregates"):
parent = rel.RelatingObject
if parent and self.compare(parent.Name, comparison, value):
if parent and (
self.compare(parent.Name, comparison, value) or self.compare(parent.GlobalId, comparison, value)
):
parents.add(parent)
for rel in self.file.by_type("IfcRelContainedInSpatialStructure"):
parent = rel.RelatingStructure
if parent and self.compare(parent.Name, comparison, value):
if parent and (
self.compare(parent.Name, comparison, value) or self.compare(parent.GlobalId, comparison, value)
):
parents.add(parent)
for rel in self.file.by_type("IfcRelNests"):
parent = rel.RelatingObject
if parent and self.compare(parent.Name, comparison, value):
if parent and (
self.compare(parent.Name, comparison, value) or self.compare(parent.GlobalId, comparison, value)
):
parents.add(parent)
for rel in self.file.by_type("IfcRelVoidsElement"):
parent = rel.RelatingBuildingElement
if parent and self.compare(parent.Name, comparison, value):
if parent and (
self.compare(parent.Name, comparison, value) or self.compare(parent.GlobalId, comparison, value)
):
parents.add(parent)
for rel in self.file.by_type("IfcRelVoidsElement"):
parent = rel.RelatingBuildingElement
if parent and self.compare(parent.Name, comparison, value):
if parent and (
self.compare(parent.Name, comparison, value) or self.compare(parent.GlobalId, comparison, value)
):
parents.add(parent)
for rel in self.file.by_type("IfcRelFillsElement"):
parent = rel.RelatingOpeningElement
if parent and self.compare(parent.Name, comparison, value):
if parent and (
self.compare(parent.Name, comparison, value) or self.compare(parent.GlobalId, comparison, value)
):
parents.add(parent)
children: set[ifcopenshell.entity_instance] = set()
@@ -196,6 +196,7 @@ class TestFilterElements(test.bootstrap.IFC4):
assert subject.filter_elements(self.file, "IfcWall, type=Foo") == {element}
assert subject.filter_elements(self.file, 'IfcWall, type="Foo"') == {element}
assert subject.filter_elements(self.file, "IfcWall, type=/Fo.*/") == {element}
assert subject.filter_elements(self.file, f"IfcWall, type={element_type.GlobalId}") == {element}
def test_selecting_by_material(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
@@ -273,6 +274,7 @@ class TestFilterElements(test.bootstrap.IFC4):
assert subject.filter_elements(self.file, "IfcWall, location=G") == {element, element2}
assert subject.filter_elements(self.file, "IfcWall, location=Building") == {element, element2}
assert subject.filter_elements(self.file, "IfcWall, location!=Space") == {element2}
assert subject.filter_elements(self.file, f"IfcWall, location={space.GlobalId}") == {element}
def test_selecting_by_group(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
@@ -281,6 +283,7 @@ class TestFilterElements(test.bootstrap.IFC4):
ifcopenshell.api.group.assign_group(self.file, products=[element], group=group)
assert subject.filter_elements(self.file, "IfcWall, group=Foo") == {element}
assert subject.filter_elements(self.file, "IfcWall, group!=Foo") == {element2}
assert subject.filter_elements(self.file, f"IfcWall, group={group.GlobalId}") == {element}
def test_selecting_by_parent(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall", name="Element1")
@@ -300,6 +303,7 @@ class TestFilterElements(test.bootstrap.IFC4):
assert subject.filter_elements(self.file, "IfcWall, parent=Space") == {element}
assert subject.filter_elements(self.file, "IfcWall, parent=G") == {element, element2, element3}
assert subject.filter_elements(self.file, "IfcWall, parent=Element2") == {element3}
assert subject.filter_elements(self.file, "IfcWall, parent=Space") == {element}
def test_selecting_multiple_filter_groups(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")