From 7fec46756510d49c0cac72b573c3744cd7eb474b Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Thu, 20 Jul 2023 19:42:34 +1000 Subject: [PATCH] Implement guid facet in facet selector --- src/ifcopenshell-python/ifcopenshell/util/selector.py | 11 +++++++++-- src/ifcopenshell-python/test/util/test_selector.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/util/selector.py b/src/ifcopenshell-python/ifcopenshell/util/selector.py index 1e3227872e..8e43ee5571 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/selector.py +++ b/src/ifcopenshell-python/ifcopenshell/util/selector.py @@ -55,8 +55,10 @@ def filter_elements(ifc_file, query, elements=None): filter_group: facet_list ("+" facet_list)* facet_list: facet ("," facet)* - facet: entity | attribute | type | material | property | classification | location + facet: instance | entity | attribute | type | material | property | classification | location + instance: not? globalid + globalid: /[0-3][a-zA-Z0-9_$]{21}/ entity: not? ifc_class attribute: attribute_name comparison value type: "type" comparison value @@ -127,7 +129,6 @@ class FacetTransformer(lark.Transformer): self.elements = elements or set() self.container_parents = {} self.container_trees = {} - print("INIT TRANSFORMER") def get_results(self): results = set() @@ -140,6 +141,12 @@ class FacetTransformer(lark.Transformer): self.results.append(self.elements) self.elements = set() + def instance(self, args): + if args[0].data == "globalid": + self.elements.add(self.file.by_guid(args[0].children[0].value)) + else: + self.elements.remove(self.file.by_guid(args[1].children[0].value)) + def entity(self, args): if args[0].data == "ifc_class": self.elements |= set(self.file.by_type(args[0].children[0].value)) diff --git a/src/ifcopenshell-python/test/util/test_selector.py b/src/ifcopenshell-python/test/util/test_selector.py index aaa47fdf1e..0ff7227b47 100644 --- a/src/ifcopenshell-python/test/util/test_selector.py +++ b/src/ifcopenshell-python/test/util/test_selector.py @@ -75,6 +75,16 @@ class TestGetElementValue(test.bootstrap.IFC4): class TestFilterElements(test.bootstrap.IFC4): + def test_selecting_by_globalid(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSlab") + guid1 = element.GlobalId + guid2 = element2.GlobalId + assert subject.filter_elements(self.file, f"{guid1}") == {element} + assert subject.filter_elements(self.file, f"{guid1}, {guid2}") == {element, element2} + assert subject.filter_elements(self.file, f"{guid1}, {guid2}, ! {guid2}") == {element} + assert subject.filter_elements(self.file, f"IfcElement, ! {guid2}") == {element} + def test_selecting_by_class(self): element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") element.Name = "Foo"