From 159355d63a5fb216def14d868e767f263f4e316b Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 31 Jul 2024 12:50:57 +0500 Subject: [PATCH] typing --- .../bim/module/attribute/operator.py | 3 ++- .../blenderbim/bim/module/model/covering.py | 1 + .../blenderbim/bim/module/qto/helper.py | 18 +++++++++++------- .../blenderbim/bim/module/spatial/prop.py | 1 + src/ifc5d/ifc5d/qto.py | 6 ++++-- .../recipes/ConvertPropertiesToQuantities.py | 17 ++++++++++------- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/attribute/operator.py b/src/blenderbim/blenderbim/bim/module/attribute/operator.py index bc6a983d9a..d7b1331beb 100644 --- a/src/blenderbim/blenderbim/bim/module/attribute/operator.py +++ b/src/blenderbim/blenderbim/bim/module/attribute/operator.py @@ -21,6 +21,7 @@ import json import ifcopenshell import ifcopenshell.api import ifcopenshell.guid +import ifcopenshell.util.element import blenderbim.bim.helper import blenderbim.bim.handler import blenderbim.tool as tool @@ -36,7 +37,7 @@ class EnableEditingAttributes(bpy.types.Operator): def execute(self, context): self.file = IfcStore.get_file() - obj = bpy.data.objects.get(self.obj) + obj = bpy.data.objects[self.obj] props = obj.BIMAttributeProperties props.attributes.clear() diff --git a/src/blenderbim/blenderbim/bim/module/model/covering.py b/src/blenderbim/blenderbim/bim/module/model/covering.py index efd1c47467..bca60ed16c 100644 --- a/src/blenderbim/blenderbim/bim/module/model/covering.py +++ b/src/blenderbim/blenderbim/bim/module/model/covering.py @@ -19,6 +19,7 @@ import bpy import ifcopenshell +import ifcopenshell.util.element import blenderbim.tool as tool import blenderbim.core.covering as core diff --git a/src/blenderbim/blenderbim/bim/module/qto/helper.py b/src/blenderbim/blenderbim/bim/module/qto/helper.py index 2c5aaec1c6..036055f8ac 100644 --- a/src/blenderbim/blenderbim/bim/module/qto/helper.py +++ b/src/blenderbim/blenderbim/bim/module/qto/helper.py @@ -18,25 +18,28 @@ import bpy import bmesh +from typing import Callable -def calculate_height(obj): +def calculate_height(obj: bpy.types.Object) -> float: return obj.dimensions[2] -def calculate_edges_lengths(objs, context): +def calculate_edges_lengths(objs: list[bpy.types.Object], context: bpy.types.Context): return calculate_mesh_quantity(objs, context, lambda bm: sum((e.calc_length() for e in bm.edges if e.select))) -def calculate_faces_areas(objs, context): +def calculate_faces_areas(objs: list[bpy.types.Object], context: bpy.types.Context) -> float: return calculate_mesh_quantity(objs, context, lambda bm: sum((f.calc_area() for f in bm.faces if f.select))) -def calculate_volumes(objs, context): +def calculate_volumes(objs: list[bpy.types.Object], context: bpy.types.Context) -> float: return calculate_mesh_quantity(objs, context, lambda bm: bm.calc_volume()) -def calculate_mesh_quantity(objs: bpy.types.Object, context, operation): +def calculate_mesh_quantity( + objs: list[bpy.types.Object], context: bpy.types.Context, operation: Callable[[bmesh.types.BMesh], float] +) -> float: """Get the sum of the target quantity on all passed mesh objects :param objs: iterable of mesh object @@ -58,7 +61,7 @@ def calculate_mesh_quantity(objs: bpy.types.Object, context, operation): return result -def calculate_formwork_area(objs, context): +def calculate_formwork_area(objs: list[bpy.types.Object], context: bpy.types.Context) -> float: """ Formwork is defined as the surface area required to cover all exposed surfaces of one or more objects, excluding top surfaces (i.e. that have a @@ -85,6 +88,7 @@ def calculate_formwork_area(objs, context): copied_obj.name = "Formwork" copied_obj.BIMObjectProperties.ifc_definition_id = 0 modifier = copied_obj.modifiers.new("Formwork", "REMESH") + assert isinstance(modifier, bpy.types.RemeshModifier) modifier.mode = "SHARP" # This hardcoded value may be optimised through a better understanding of the octree division. # These values are based off some trial and error heuristics I've learned through experience. @@ -108,7 +112,7 @@ def calculate_formwork_area(objs, context): return result -def calculate_side_formwork_area(objs, context): +def calculate_side_formwork_area(objs: list[bpy.types.Object], context: bpy.types.Context) -> float: """ Side formwork is defined as the surface area required to cover all exposed surfaces of one or more objects, excluding top and bottom surfaces (i.e. diff --git a/src/blenderbim/blenderbim/bim/module/spatial/prop.py b/src/blenderbim/blenderbim/bim/module/spatial/prop.py index 9a5608136e..8cc29ba15f 100644 --- a/src/blenderbim/blenderbim/bim/module/spatial/prop.py +++ b/src/blenderbim/blenderbim/bim/module/spatial/prop.py @@ -32,6 +32,7 @@ from bpy.props import ( ) import blenderbim.tool as tool import ifcopenshell +import ifcopenshell.util.element def get_subelement_class(self, context): diff --git a/src/ifc5d/ifc5d/qto.py b/src/ifc5d/ifc5d/qto.py index 202f0158ff..25c547a76f 100644 --- a/src/ifc5d/ifc5d/qto.py +++ b/src/ifc5d/ifc5d/qto.py @@ -21,10 +21,12 @@ import json import ifcopenshell import ifcopenshell.api import ifcopenshell.api.pset +import ifcopenshell.util.element import ifcopenshell.util.unit import ifcopenshell.util.selector import multiprocessing from collections import namedtuple +from typing import Any Function = namedtuple("Function", ["measure", "name", "description"]) @@ -47,7 +49,7 @@ def quantify(ifc_file: ifcopenshell.file, elements: set[ifcopenshell.entity_inst return results -def edit_qtos(ifc_file, results) -> None: +def edit_qtos(ifc_file: ifcopenshell.file, results: dict[ifcopenshell.entity_instance, Any]) -> None: for element, qtos in results.items(): for name, quantities in qtos.items(): qto = ifcopenshell.util.element.get_pset(element, name, should_inherit=False) @@ -59,7 +61,7 @@ def edit_qtos(ifc_file, results) -> None: class SI2ProjectUnitConverter: - def __init__(self, ifc_file): + def __init__(self, ifc_file: ifcopenshell.file): self.project_units = { "IfcAreaMeasure": ifcopenshell.util.unit.get_project_unit(ifc_file, "AREAUNIT"), "IfcLengthMeasure": ifcopenshell.util.unit.get_project_unit(ifc_file, "LENGTHUNIT"), diff --git a/src/ifcpatch/ifcpatch/recipes/ConvertPropertiesToQuantities.py b/src/ifcpatch/ifcpatch/recipes/ConvertPropertiesToQuantities.py index 4483048e0c..71ec724cd3 100644 --- a/src/ifcpatch/ifcpatch/recipes/ConvertPropertiesToQuantities.py +++ b/src/ifcpatch/ifcpatch/recipes/ConvertPropertiesToQuantities.py @@ -17,12 +17,15 @@ # along with IfcPatch. If not, see . import ifcopenshell +import ifcopenshell.api import ifcopenshell.util.pset import ifcopenshell.util.element +from logging import Logger +from typing import Union class Patcher: - def __init__(self, src, file, logger, property_name=None, quantity_name=None): + def __init__(self, src: str, file: ifcopenshell.file, logger: Logger, property_name: str, quantity_name: str): """Converts a property to a standardised quantity IFC can store arbitrary key value metadata associated with a elements @@ -40,14 +43,12 @@ class Patcher: :param property_name: The name of the property to convert into a quantity. The name of the property set is not considered. - :type property_name: str :param quantity_name: The name of the quantity that this property should be stored in. This should be a standard name that is one of the quantity names of a buildingSMART quantity template. For example, it may be "NetSideArea" for walls, which exists in Qto_WallBaseQuantities. The quantity set name will be based on the standard buildingSMART quantity template. - :type quantity_name: str Example: @@ -63,7 +64,7 @@ class Patcher: self.source_property_name = property_name self.destination_quantity_name = quantity_name - def patch(self): + def patch(self) -> None: self.qto_template_cache = {} self.psetqto = ifcopenshell.util.pset.get_template("IFC4") @@ -76,7 +77,9 @@ class Patcher: [r.RelatingPropertyDefinition for r in product.IsDefinedBy if r.is_a("IfcRelDefinesByProperties")], ) - def process_product(self, product, definitions): + def process_product( + self, product: ifcopenshell.entity_instance, definitions: list[ifcopenshell.entity_instance] + ) -> None: value = None has_quantity = False qtos = {} @@ -98,12 +101,12 @@ class Patcher: "pset.edit_qto", self.file, qto=qto, Properties={self.destination_quantity_name: value} ) - def get_qto_name(self, ifc_class): + def get_qto_name(self, ifc_class: str) -> Union[str, None]: for template in self.get_qto_templates(ifc_class): if self.destination_quantity_name in [t.Name for t in template.HasPropertyTemplates]: return template.Name - def get_qto_templates(self, ifc_class): + def get_qto_templates(self, ifc_class: str) -> list[ifcopenshell.entity_instance]: if ifc_class not in self.qto_template_cache: self.qto_template_cache[ifc_class] = self.psetqto.get_applicable(ifc_class, qto_only=True) return self.qto_template_cache[ifc_class]