mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 09:48:32 +00:00
typing
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ifcopenshell
|
||||
import ifcopenshell.api
|
||||
import ifcopenshell.util.cost
|
||||
import ifcopenshell.util.unit
|
||||
import ifcopenshell.util.element
|
||||
|
||||
@@ -16,18 +16,24 @@
|
||||
# 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 ifcopenshell.util.cost
|
||||
import ifcopenshell.util.element
|
||||
import ifcopenshell.util.date
|
||||
from typing import Union, Any
|
||||
|
||||
|
||||
def get_productivity(resource, should_inherit=True):
|
||||
PRODUCTIVITY_PSET_DATA = Union[dict[str, Any], None]
|
||||
|
||||
|
||||
def get_productivity(resource: ifcopenshell.entity_instance, should_inherit: bool = True) -> PRODUCTIVITY_PSET_DATA:
|
||||
productivity = ifcopenshell.util.element.get_psets(resource).get("EPset_Productivity", None)
|
||||
if should_inherit and not productivity:
|
||||
#Note: This is not part of the Schema - but it makes sense to inherit from parent
|
||||
# Note: This is not part of the Schema - but it makes sense to inherit from parent
|
||||
productivity = get_parent_productivity(resource)
|
||||
return productivity
|
||||
|
||||
def get_parent_productivity(resource):
|
||||
|
||||
def get_parent_productivity(resource: ifcopenshell.entity_instance) -> PRODUCTIVITY_PSET_DATA:
|
||||
if not resource.Nests:
|
||||
return
|
||||
else:
|
||||
@@ -36,34 +42,34 @@ def get_parent_productivity(resource):
|
||||
return productivity
|
||||
|
||||
|
||||
def get_unit_consumed(productivity):
|
||||
def get_unit_consumed(productivity: PRODUCTIVITY_PSET_DATA) -> Union[Any, None]:
|
||||
duration = productivity.get("BaseQuantityConsumed", None)
|
||||
if not duration:
|
||||
return
|
||||
return ifcopenshell.util.date.ifc2datetime(duration)
|
||||
|
||||
|
||||
def get_quantity_produced(productivity):
|
||||
def get_quantity_produced(productivity: PRODUCTIVITY_PSET_DATA) -> float:
|
||||
if not productivity:
|
||||
return 0
|
||||
return productivity.get("BaseQuantityProducedValue", 0)
|
||||
return 0.0
|
||||
return productivity.get("BaseQuantityProducedValue", 0.0)
|
||||
|
||||
|
||||
def get_quantity_produced_name(productivity):
|
||||
def get_quantity_produced_name(productivity: PRODUCTIVITY_PSET_DATA):
|
||||
if not productivity:
|
||||
return ""
|
||||
return productivity.get("BaseQuantityProducedName", "")
|
||||
|
||||
|
||||
def get_total_quantity_produced(resource, quantity_name_in_process):
|
||||
def get_product_quantity(product, quantity_name):
|
||||
def get_total_quantity_produced(resource: ifcopenshell.entity_instance, quantity_name_in_process: str) -> float:
|
||||
def get_product_quantity(product: ifcopenshell.entity_instance, quantity_name: str):
|
||||
psets = ifcopenshell.util.element.get_psets(product)
|
||||
for pset in psets.values():
|
||||
for name, value in pset.items():
|
||||
if name == quantity_name:
|
||||
return float(value)
|
||||
|
||||
total = 0
|
||||
total = 0.0
|
||||
products = get_parametric_resource_products(resource)
|
||||
if quantity_name_in_process == "Count":
|
||||
total = len(products)
|
||||
@@ -73,7 +79,7 @@ def get_total_quantity_produced(resource, quantity_name_in_process):
|
||||
return total
|
||||
|
||||
|
||||
def get_parametric_resource_products(resource):
|
||||
def get_parametric_resource_products(resource: ifcopenshell.entity_instance) -> list[ifcopenshell.entity_instance]:
|
||||
products = []
|
||||
for rel in resource.HasAssignments or []:
|
||||
if not rel.is_a("IfcRelAssignsToProcess"):
|
||||
@@ -84,14 +90,15 @@ def get_parametric_resource_products(resource):
|
||||
products.append(rel2.RelatingProduct)
|
||||
return products
|
||||
|
||||
def get_task_assignments(resource):
|
||||
|
||||
def get_task_assignments(resource: ifcopenshell.entity_instance) -> Union[ifcopenshell.entity_instance, None]:
|
||||
for rel in resource.HasAssignments or []:
|
||||
if not rel.is_a("IfcRelAssignsToProcess"):
|
||||
continue
|
||||
return rel.RelatingProcess
|
||||
|
||||
|
||||
def get_resource_required_work(resource):
|
||||
def get_resource_required_work(resource: ifcopenshell.entity_instance) -> Union[str, None]:
|
||||
productivity = get_productivity(resource)
|
||||
if productivity:
|
||||
quantity_produced = get_quantity_produced(productivity)
|
||||
@@ -114,33 +121,38 @@ def get_resource_required_work(resource):
|
||||
return iso_string
|
||||
|
||||
|
||||
def get_nested_resources(resource):
|
||||
def get_nested_resources(resource: ifcopenshell.entity_instance) -> list[ifcopenshell.entity_instance]:
|
||||
return [object for rel in resource.IsNestedBy or [] for object in rel.RelatedObjects]
|
||||
|
||||
|
||||
def get_cost(resource):
|
||||
def get_cost(resource: ifcopenshell.entity_instance) -> tuple[float, Union[str, None]]:
|
||||
base_costs = getattr(resource, "BaseCosts", [])
|
||||
costs = [
|
||||
ifcopenshell.util.cost.calculate_applied_value(resource, cost_value)
|
||||
for cost_value in base_costs
|
||||
] if base_costs else []
|
||||
costs = (
|
||||
[ifcopenshell.util.cost.calculate_applied_value(resource, cost_value) for cost_value in base_costs]
|
||||
if base_costs
|
||||
else []
|
||||
)
|
||||
cost = sum(costs)
|
||||
unit_basis = next(
|
||||
(cost_value.UnitBasis for cost_value in base_costs if cost_value.UnitBasis),
|
||||
None
|
||||
) if base_costs else None
|
||||
unit = unit_basis.UnitComponent.Name if unit_basis and unit_basis.UnitComponent.is_a("IfcConversionBasedUnit") else None
|
||||
unit_basis = (
|
||||
next((cost_value.UnitBasis for cost_value in base_costs if cost_value.UnitBasis), None) if base_costs else None
|
||||
)
|
||||
unit = (
|
||||
unit_basis.UnitComponent.Name
|
||||
if unit_basis and unit_basis.UnitComponent.is_a("IfcConversionBasedUnit")
|
||||
else None
|
||||
)
|
||||
return cost, unit
|
||||
|
||||
def get_quantity(resource):
|
||||
total = 0
|
||||
|
||||
def get_quantity(resource: ifcopenshell.entity_instance) -> float:
|
||||
if resource.BaseQuantity:
|
||||
return resource.BaseQuantity[3]
|
||||
if resource.Usage and resource.Usage.ScheduleWork:
|
||||
duration = ifcopenshell.util.date.ifc2datetime(resource.Usage.ScheduleWork)
|
||||
return duration.total_seconds() / 3600
|
||||
|
||||
def get_parent_cost(resource):
|
||||
|
||||
def get_parent_cost(resource: ifcopenshell.entity_instance) -> Union[None, tuple[float, Union[str, None]]]:
|
||||
if not resource.Nests:
|
||||
return
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user