From eaa3301b8fef20956278c2ecc0a2c6628b9772e4 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Tue, 22 Mar 2022 10:49:48 +1100 Subject: [PATCH] New support for filtering elements by checking values against an enumeration --- .../ifcopenshell/util/selector.py | 5 ++++- .../test/util/test_selector.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ifcopenshell-python/ifcopenshell/util/selector.py b/src/ifcopenshell-python/ifcopenshell/util/selector.py index cb524270f3..955e6a4b06 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/selector.py +++ b/src/ifcopenshell-python/ifcopenshell/util/selector.py @@ -46,7 +46,8 @@ class Selector: and: "&" or: "|" not: "!" - comparison: (not)* (contains | morethanequalto | lessthanequalto | equal | morethan | lessthan) + comparison: (not)* (oneof | contains | morethanequalto | lessthanequalto | equal | morethan | lessthan) + oneof: "%=" contains: "*=" morethanequalto: ">=" lessthanequalto: "<=" @@ -245,6 +246,8 @@ class Selector: return element_value >= value elif comparison == "lessthanequalto": return element_value <= value + elif comparison == "oneof": + return element_value in value.split(",") return False @classmethod diff --git a/src/ifcopenshell-python/test/util/test_selector.py b/src/ifcopenshell-python/test/util/test_selector.py index 8f655a686f..f7e9b071d4 100644 --- a/src/ifcopenshell-python/test/util/test_selector.py +++ b/src/ifcopenshell-python/test/util/test_selector.py @@ -90,3 +90,19 @@ class TestSelector(test.bootstrap.IFC4): assert subject.Selector.parse(self.file, '.IfcElement[Foo_Bar.Foo<20]') == [element] assert subject.Selector.parse(self.file, '.IfcElement[Foo_Bar.Foo>=4.2]') == [element] assert subject.Selector.parse(self.file, '.IfcElement[Foo_Bar.Foo<=4.2]') == [element] + + def test_comparing_if_value_contains_a_wildcard_string(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + element.Name = "Foobar" + assert subject.Selector.parse(self.file, '.IfcElement[Name*="Foo"]') == [element] + assert subject.Selector.parse(self.file, '.IfcElement[Name*="oba"]') == [element] + assert subject.Selector.parse(self.file, '.IfcElement[Name*="abc"]') == [] + + def test_comparing_if_value_is_in_a_list(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + element.Name = "Foobar" + assert subject.Selector.parse(self.file, '.IfcElement[Name%="Foobar,Foobaz"]') == [element] + element.Name = "Foobaz" + assert subject.Selector.parse(self.file, '.IfcElement[Name%="Foobar,Foobaz"]') == [element] + element.Name = "Foobat" + assert subject.Selector.parse(self.file, '.IfcElement[Name%="Foobar,Foobaz"]') == []