2022-01-19 12:18:33 +11:00
|
|
|
# IfcOpenShell - IFC toolkit and geometry engine
|
|
|
|
|
# Copyright (C) 2021 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/>.
|
|
|
|
|
|
2024-06-28 12:36:31 +10:00
|
|
|
import ifcopenshell.api.cost
|
|
|
|
|
import ifcopenshell.api.control
|
2024-05-22 11:05:42 +05:00
|
|
|
from typing import Any
|
2021-05-13 14:25:05 +10:00
|
|
|
|
|
|
|
|
|
2024-05-09 14:18:36 +05:00
|
|
|
def assign_cost_item_quantity(
|
|
|
|
|
file: ifcopenshell.file,
|
|
|
|
|
cost_item: ifcopenshell.entity_instance,
|
|
|
|
|
products: list[ifcopenshell.entity_instance],
|
2024-05-22 11:05:42 +05:00
|
|
|
prop_name: str = "",
|
2024-05-09 14:18:36 +05:00
|
|
|
) -> None:
|
2024-05-06 14:35:39 +10:00
|
|
|
"""Adds a cost item quantity that is parametrically connected to a product
|
|
|
|
|
|
|
|
|
|
A cost item may have its subtotal calculated by multiplying a unit value
|
|
|
|
|
by a quantity associated with the cost item. That quantity may be either
|
|
|
|
|
manually specified or parametrically connected to a quantity on a
|
|
|
|
|
product. This API function lets you create that parametric connection.
|
|
|
|
|
|
|
|
|
|
For example, you may wish to have a cost item linked to the "NetVolume"
|
|
|
|
|
quantity on all IfcSlabs. Each quantity has a name which you can
|
|
|
|
|
specify. If the quantity is updated in-place (which should occur for
|
|
|
|
|
Native IFC applications) then the quantity for the cost item will
|
|
|
|
|
automatically update as well. If the quantity is deleted and then
|
|
|
|
|
re-added, then the parametric relationship is also lost.
|
|
|
|
|
|
|
|
|
|
This API also automatically assigns a control relationship between the
|
|
|
|
|
cost item and the product, so it is not necessary to use
|
|
|
|
|
ifcopenshell.api.control.assign_control.
|
|
|
|
|
|
2024-05-22 13:40:22 +05:00
|
|
|
If cost item has just 1 quantity and it's IfcQuantityCount, API will
|
|
|
|
|
assume that quantity is used for counting controlled objects
|
2025-06-08 10:28:19 +02:00
|
|
|
and it will recalculate the quantity value at the end of the API call
|
2025-06-09 09:55:42 +05:00
|
|
|
as long as the RelatedObjects are not IfcConstructionResource which do not
|
2025-06-08 10:28:19 +02:00
|
|
|
count towards the cost item (they only provide value).
|
2024-05-22 13:40:22 +05:00
|
|
|
|
2024-05-06 14:35:39 +10:00
|
|
|
:param cost_item: The IfcCostItem to assign parametric quantities to
|
|
|
|
|
:param products: The IfcObjects to assign parametric quantities to
|
|
|
|
|
:param prop_name: The name of the quantity. If this is not specified,
|
|
|
|
|
then it is assumed that there is no calculated quantity, and the
|
|
|
|
|
number of objects are counted instead.
|
|
|
|
|
:return: None
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
2024-06-28 12:36:31 +10:00
|
|
|
schedule = ifcopenshell.api.cost.add_cost_schedule(model)
|
|
|
|
|
item = ifcopenshell.api.cost.add_cost_item(model, cost_schedule=schedule)
|
2024-05-06 14:35:39 +10:00
|
|
|
|
|
|
|
|
# Let's imagine a unit cost of 5.0 per unit volume
|
2024-06-28 12:36:31 +10:00
|
|
|
value = ifcopenshell.api.cost.add_cost_value(model, parent=item)
|
|
|
|
|
ifcopenshell.api.cost.edit_cost_value(model, cost_value=value,
|
2024-05-06 14:35:39 +10:00
|
|
|
attributes={"AppliedValue": 5.0})
|
|
|
|
|
|
2024-06-28 12:36:31 +10:00
|
|
|
slab = ifcopenshell.api.root.create_entity(model, ifc_class="IfcSlab")
|
2024-05-06 14:35:39 +10:00
|
|
|
# Usually the quantity would be automatically calculated via a
|
|
|
|
|
# graphical authoring application but let's assign a manual quantity
|
|
|
|
|
# for now.
|
2024-06-28 12:36:31 +10:00
|
|
|
qto = ifcopenshell.api.pset.add_qto(model, product=slab, name="Qto_SlabBaseQuantities")
|
|
|
|
|
ifcopenshell.api.pset.edit_qto(model, qto=qto, properties={"NetVolume": 42.0})
|
2024-05-06 14:35:39 +10:00
|
|
|
|
|
|
|
|
# Now let's parametrically link the slab's quantity to the cost
|
|
|
|
|
# item. If the slab is edited in the future and 42.0 changes, then
|
|
|
|
|
# the updated value will also automatically be applied to the cost
|
|
|
|
|
# item.
|
2024-06-28 12:36:31 +10:00
|
|
|
ifcopenshell.api.cost.assign_cost_item_quantity(model,
|
2024-05-06 14:35:39 +10:00
|
|
|
cost_item=item, products=[slab], prop_name="NetVolume")
|
|
|
|
|
"""
|
|
|
|
|
usecase = Usecase()
|
|
|
|
|
usecase.file = file
|
|
|
|
|
usecase.settings = {
|
|
|
|
|
"cost_item": cost_item,
|
|
|
|
|
"products": products or [],
|
|
|
|
|
"prop_name": prop_name,
|
|
|
|
|
}
|
|
|
|
|
return usecase.execute()
|
|
|
|
|
|
2021-05-13 14:25:05 +10:00
|
|
|
|
2024-05-06 14:35:39 +10:00
|
|
|
class Usecase:
|
2024-05-22 11:05:42 +05:00
|
|
|
file: ifcopenshell.file
|
|
|
|
|
settings: dict[str, Any]
|
|
|
|
|
|
2021-05-13 14:25:05 +10:00
|
|
|
def execute(self):
|
2021-08-26 15:48:04 +10:00
|
|
|
if self.settings["prop_name"]:
|
|
|
|
|
self.quantities = set(self.settings["cost_item"].CostQuantities or [])
|
2021-05-13 14:25:05 +10:00
|
|
|
for product in self.settings["products"]:
|
2024-10-21 11:25:23 +05:00
|
|
|
if product.is_a("IfcSpatialElement"):
|
2024-09-11 18:48:59 +01:00
|
|
|
continue
|
2024-05-06 14:35:39 +10:00
|
|
|
self.assign_cost_control(related_object=product, cost_item=self.settings["cost_item"])
|
2021-08-26 15:48:04 +10:00
|
|
|
if self.settings["prop_name"]:
|
2023-08-12 15:17:32 +01:00
|
|
|
if (
|
|
|
|
|
self.settings["cost_item"].CostQuantities
|
2024-05-06 14:35:39 +10:00
|
|
|
and self.settings["cost_item"].CostQuantities[0].Name.lower() != self.settings["prop_name"].lower()
|
2024-09-11 18:48:59 +01:00
|
|
|
):
|
2023-08-12 15:17:32 +01:00
|
|
|
continue
|
2021-08-26 15:48:04 +10:00
|
|
|
self.add_quantity_from_related_object(product)
|
|
|
|
|
if self.settings["prop_name"]:
|
|
|
|
|
self.settings["cost_item"].CostQuantities = list(self.quantities)
|
|
|
|
|
else:
|
|
|
|
|
self.update_cost_item_count()
|
2021-05-13 14:25:05 +10:00
|
|
|
|
2024-05-22 11:05:42 +05:00
|
|
|
def assign_cost_control(
|
|
|
|
|
self, related_object: ifcopenshell.entity_instance, cost_item: ifcopenshell.entity_instance
|
|
|
|
|
) -> ifcopenshell.entity_instance:
|
2024-06-28 12:36:31 +10:00
|
|
|
return ifcopenshell.api.control.assign_control(
|
2023-08-13 12:16:29 +01:00
|
|
|
self.file,
|
|
|
|
|
related_object=related_object,
|
|
|
|
|
relating_control=cost_item,
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-22 11:05:42 +05:00
|
|
|
def add_quantity_from_related_object(self, element: ifcopenshell.entity_instance) -> None:
|
2021-08-12 09:54:01 +10:00
|
|
|
for relationship in element.IsDefinedBy:
|
|
|
|
|
if relationship.is_a("IfcRelDefinesByProperties"):
|
|
|
|
|
self.add_quantity_from_qto(relationship.RelatingPropertyDefinition)
|
2021-08-11 21:22:23 +10:00
|
|
|
|
2024-05-22 11:05:42 +05:00
|
|
|
def add_quantity_from_qto(self, qto: ifcopenshell.entity_instance) -> None:
|
2021-08-11 21:22:23 +10:00
|
|
|
if not qto.is_a("IfcElementQuantity"):
|
|
|
|
|
return
|
|
|
|
|
for prop in qto.Quantities:
|
2024-05-06 14:35:39 +10:00
|
|
|
if prop.is_a("IfcPhysicalSimpleQuantity") and prop.Name.lower() == self.settings["prop_name"].lower():
|
2021-08-11 21:22:23 +10:00
|
|
|
self.quantities.add(prop)
|
2021-08-26 15:48:04 +10:00
|
|
|
|
|
|
|
|
def update_cost_item_count(self):
|
|
|
|
|
# This is a bold assumption
|
|
|
|
|
# https://forums.buildingsmart.org/t/how-does-a-cost-item-know-that-it-is-counting-a-controlled-product/3564
|
|
|
|
|
if not self.settings["cost_item"].CostQuantities:
|
2024-06-28 12:36:31 +10:00
|
|
|
ifcopenshell.api.cost.add_cost_item_quantity(
|
2021-08-26 15:48:04 +10:00
|
|
|
self.file,
|
|
|
|
|
cost_item=self.settings["cost_item"],
|
|
|
|
|
ifc_class="IfcQuantityCount",
|
|
|
|
|
)
|
|
|
|
|
if len(self.settings["cost_item"].CostQuantities) == 1:
|
|
|
|
|
quantity = self.settings["cost_item"].CostQuantities[0]
|
|
|
|
|
if quantity.is_a("IfcQuantityCount"):
|
|
|
|
|
count = 0
|
|
|
|
|
for rel in self.settings["cost_item"].Controls:
|
2025-06-03 01:12:41 +02:00
|
|
|
for obj in rel.RelatedObjects:
|
2025-06-09 09:55:29 +05:00
|
|
|
# Only increment if not a resource
|
2025-06-03 01:12:41 +02:00
|
|
|
if not obj.is_a("IfcConstructionResource"):
|
2025-06-09 09:55:29 +05:00
|
|
|
count += 1
|
2021-08-26 15:48:04 +10:00
|
|
|
quantity[3] = count
|