#2082 You can now filter by metadata existence in the selector syntax

This commit is contained in:
Dion Moult
2022-04-27 22:19:31 +10:00
parent 11c3a6e547
commit 9ea6499b5a
3 changed files with 19 additions and 3 deletions
@@ -192,7 +192,9 @@ class Selector:
element_value = cls.get_element_value(element, key)
if element_value is None and value is not None:
continue
if not comparison or cls.filter_element(element, element_value, comparison, value):
if comparison and cls.filter_element(element, element_value, comparison, value):
results.append(element)
elif not comparison and element_value:
results.append(element)
return results
@@ -33,6 +33,13 @@ class TestSelector(test.bootstrap.IFC4):
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSlab")
assert subject.Selector.parse(self.file, f"#{element.GlobalId}") == [element]
def test_selecting_by_attribute_existence(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
element.Name = "Foobar"
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSlab")
assert subject.Selector.parse(self.file, '.IfcElement[Name]') == [element]
assert subject.Selector.parse(self.file, '.IfcElement[Description]') == []
def test_selecting_by_attribute(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
element.Name = "Foobar"
@@ -40,6 +47,13 @@ class TestSelector(test.bootstrap.IFC4):
assert subject.Selector.parse(self.file, '.IfcElement[Name="Foobar"]') == [element]
assert subject.Selector.parse(self.file, '.IfcElement[Name="Foobaz"]') == []
def test_selecting_by_property_existence(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
pset = ifcopenshell.api.run("pset.add_pset", self.file, product=element, name="Foo_Bar")
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Foo": "Bar"})
assert subject.Selector.parse(self.file, '.IfcElement[Foo_Bar.Foo]') == [element]
assert subject.Selector.parse(self.file, '.IfcElement[Foo_Bar.Fox]') == []
def test_selecting_by_string_property(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
pset = ifcopenshell.api.run("pset.add_pset", self.file, product=element, name="Foo_Bar")