diff --git a/src/ifcopenshell-python/ifcopenshell/api/pset/add_qto.py b/src/ifcopenshell-python/ifcopenshell/api/pset/add_qto.py index b1e723f456..d07aa079eb 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/pset/add_qto.py +++ b/src/ifcopenshell-python/ifcopenshell/api/pset/add_qto.py @@ -28,7 +28,7 @@ class Usecase: self.settings[key] = value def execute(self): - if self.settings["product"].is_a("IfcObject"): + if self.settings["product"].is_a("IfcObject") or self.settings["product"].is_a("IfcContext"): for rel in self.settings["product"].IsDefinedBy or []: if ( rel.is_a("IfcRelDefinesByProperties") @@ -36,14 +36,7 @@ class Usecase: ): return rel.RelatingPropertyDefinition - qto = self.file.create_entity( - "IfcElementQuantity", - **{ - "GlobalId": ifcopenshell.guid.new(), - "OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file), - "Name": self.settings["name"], - } - ) + qto = self.create_qto() self.file.create_entity( "IfcRelDefinesByProperties", **{ @@ -54,3 +47,20 @@ class Usecase: } ) return qto + elif self.settings["product"].is_a("IfcTypeObject"): + for definition in self.settings["product"].HasPropertySets or []: + if definition.Name == self.settings["name"]: + return definition + qto = self.create_qto() + has_property_sets = list(self.settings["product"].HasPropertySets or []) + has_property_sets.append(qto) + self.settings["product"].HasPropertySets = has_property_sets + return qto + + def create_qto(self): + return self.file.create_entity( + "IfcElementQuantity", + GlobalId=ifcopenshell.guid.new(), + OwnerHistory=ifcopenshell.api.run("owner.create_owner_history", self.file), + Name=self.settings["name"], + ) diff --git a/src/ifcopenshell-python/test/api/pset/test_add_qto.py b/src/ifcopenshell-python/test/api/pset/test_add_qto.py new file mode 100644 index 0000000000..d4cbd74a5c --- /dev/null +++ b/src/ifcopenshell-python/test/api/pset/test_add_qto.py @@ -0,0 +1,48 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2022 Dion Moult +# +# 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 . + +import test.bootstrap +import ifcopenshell.api + + +class TestAddQto(test.bootstrap.IFC4): + def test_adding_a_qto_to_an_object(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + qto = ifcopenshell.api.run("pset.add_qto", self.file, product=element, name="Qto_WallBaseQuantities") + assert qto.is_a("IfcElementQuantity") + assert "Qto_WallBaseQuantities" in ifcopenshell.util.element.get_psets(element) + + def test_adding_a_qto_to_a_type_object(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType") + qto = ifcopenshell.api.run("pset.add_qto", self.file, product=element, name="Custom_Qto") + assert qto.is_a("IfcElementQuantity") + assert "Custom_Qto" in ifcopenshell.util.element.get_psets(element) + + def test_adding_a_qto_to_a_context(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + qto = ifcopenshell.api.run("pset.add_qto", self.file, product=element, name="Custom_Qto") + assert qto.is_a("IfcElementQuantity") + assert "Custom_Qto" in ifcopenshell.util.element.get_psets(element) + + +class TestAddQtoIFC2X3(test.bootstrap.IFC2X3): + def test_adding_a_qto_to_a_project(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + qto = ifcopenshell.api.run("pset.add_qto", self.file, product=element, name="Custom_Qto") + assert qto.is_a("IfcElementQuantity") + assert "Custom_Qto" in ifcopenshell.util.element.get_psets(element)