2022-03-21 17:28:16 +11:00
|
|
|
# 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 pytest
|
|
|
|
|
import test.bootstrap
|
|
|
|
|
import ifcopenshell.api
|
|
|
|
|
import ifcopenshell.util.selector as subject
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSelector(test.bootstrap.IFC4):
|
|
|
|
|
def test_selecting_by_class(self):
|
|
|
|
|
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
|
|
|
|
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSlab")
|
|
|
|
|
assert subject.Selector.parse(self.file, ".IfcWall") == [element]
|
|
|
|
|
|
|
|
|
|
def test_selecting_by_globalid(self):
|
|
|
|
|
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
|
|
|
|
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSlab")
|
|
|
|
|
assert subject.Selector.parse(self.file, f"#{element.GlobalId}") == [element]
|
|
|
|
|
|
2022-04-27 22:19:31 +10:00
|
|
|
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]') == []
|
|
|
|
|
|
2022-03-21 17:28:16 +11:00
|
|
|
def test_selecting_by_attribute(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="Foobar"]') == [element]
|
|
|
|
|
assert subject.Selector.parse(self.file, '.IfcElement[Name="Foobaz"]') == []
|
|
|
|
|
|
2022-04-27 22:19:31 +10:00
|
|
|
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]') == []
|
|
|
|
|
|
2022-03-21 17:28:16 +11:00
|
|
|
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")
|
|
|
|
|
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Foo": "Bar"})
|
|
|
|
|
assert subject.Selector.parse(self.file, '.IfcElement[Foo_Bar.Foo="Bar"]') == [element]
|
2022-03-21 18:03:25 +11:00
|
|
|
|
|
|
|
|
def test_selecting_by_integer_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")
|
|
|
|
|
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Foo": 42})
|
2022-03-22 11:23:13 +11:00
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo=42]") == [element]
|
2022-03-21 18:03:25 +11:00
|
|
|
|
|
|
|
|
def test_selecting_by_float_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")
|
|
|
|
|
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Foo": 4.2})
|
2022-03-22 11:23:13 +11:00
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo=4.2]") == [element]
|
2022-03-21 18:03:25 +11:00
|
|
|
|
|
|
|
|
def test_selecting_by_boolean_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")
|
|
|
|
|
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Foo": True})
|
2022-03-22 11:23:13 +11:00
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo=TRUE]") == [element]
|
2022-03-21 18:03:25 +11:00
|
|
|
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Foo": False})
|
2022-03-22 11:23:13 +11:00
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo=FALSE]") == [element]
|
2022-03-21 18:03:25 +11:00
|
|
|
|
|
|
|
|
def test_selecting_by_null_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")
|
|
|
|
|
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Foo": "Bar"})
|
2022-03-22 11:23:13 +11:00
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo=NULL]") == []
|
2022-03-21 18:03:25 +11:00
|
|
|
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Foo": None})
|
2022-03-22 11:23:13 +11:00
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo=NULL]") == [element]
|
2022-03-22 10:40:05 +11:00
|
|
|
|
|
|
|
|
def test_comparing_by_not_equal(self):
|
|
|
|
|
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
|
|
|
|
element.Name = "Foobar"
|
|
|
|
|
assert subject.Selector.parse(self.file, '.IfcElement[Name!="Foobaz"]') == [element]
|
|
|
|
|
assert subject.Selector.parse(self.file, '.IfcElement[Name!="Foobar"]') == []
|
|
|
|
|
|
|
|
|
|
def test_comparing_by_ranges(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": 4.2})
|
2022-03-22 11:23:13 +11:00
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo>2]") == [element]
|
|
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo>20]") == []
|
|
|
|
|
assert subject.Selector.parse(self.file, ".IfcElement[Foo_Bar.Foo<2]") == []
|
|
|
|
|
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]
|
2022-03-22 10:49:48 +11:00
|
|
|
|
|
|
|
|
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"]') == []
|
2022-03-22 11:23:13 +11:00
|
|
|
|
|
|
|
|
def test_getting_occurrences_of_a_filtered_type(self):
|
|
|
|
|
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
|
|
|
|
element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
|
|
|
|
ifcopenshell.api.run("type.assign_type", self.file, related_object=element, relating_type=element_type)
|
|
|
|
|
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
2022-03-22 11:32:40 +11:00
|
|
|
element_type2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
|
|
|
|
ifcopenshell.api.run("type.assign_type", self.file, related_object=element2, relating_type=element_type2)
|
|
|
|
|
assert set(subject.Selector.parse(self.file, "* .IfcWallType")) == {element, element2}
|
2022-03-22 11:23:13 +11:00
|
|
|
|
|
|
|
|
def test_getting_decomposition_of_a_filtered_type(self):
|
|
|
|
|
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
|
|
|
|
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcMember")
|
|
|
|
|
building = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
|
|
|
|
|
ifcopenshell.api.run("spatial.assign_container", self.file, product=element, relating_structure=building)
|
|
|
|
|
ifcopenshell.api.run("aggregate.assign_object", self.file, product=subelement, relating_object=element)
|
2022-03-22 11:32:40 +11:00
|
|
|
assert set(subject.Selector.parse(self.file, "@ .IfcBuilding")) == {element, subelement}
|
|
|
|
|
|
|
|
|
|
def test_selecting_elements_from_a_prefiltered_list(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")
|
|
|
|
|
assert subject.Selector.parse(self.file, ".IfcWall", elements=[element])
|
|
|
|
|
assert not subject.Selector.parse(self.file, ".IfcWall", elements=[element2])
|