From 137a3062698ed4c9eed0a0ad4c476b74b6f12291 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 30 Apr 2024 12:17:23 +0500 Subject: [PATCH] typing --- src/blenderbim/blenderbim/bim/export_ifc.py | 14 +++++++++----- src/blenderbim/blenderbim/bim/import_ifc.py | 2 +- .../blenderbim/bim/module/drawing/operator.py | 1 + .../blenderbim/bim/module/drawing/svgwriter.py | 2 +- .../blenderbim/bim/module/geometry/data.py | 1 + .../blenderbim/bim/module/material/data.py | 1 + .../blenderbim/bim/module/model/product.py | 5 +++-- src/blenderbim/blenderbim/tool/geometry.py | 5 +++++ .../api/geometry/add_representation.py | 18 +++++++++++------- .../ifcopenshell/api/material/add_profile.py | 16 ++++++++++++---- .../api/owner/update_owner_history.py | 5 +++-- .../ifcpatch/recipes/RegenerateGlobalIds.py | 3 ++- 12 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/export_ifc.py b/src/blenderbim/blenderbim/bim/export_ifc.py index 5efaefa4b1..94d457918d 100644 --- a/src/blenderbim/blenderbim/bim/export_ifc.py +++ b/src/blenderbim/blenderbim/bim/export_ifc.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with BlenderBIM Add-on. If not, see . +from __future__ import annotations import os import bpy import json @@ -36,6 +37,7 @@ import blenderbim.core.style from blenderbim.bim.ifc import IfcStore from mathutils import Vector from typing import Union +from logging import Logger class IfcExporter: @@ -163,10 +165,10 @@ class IfcExporter: bpy.ops.bim.update_representation(obj=obj.name) tool.Geometry.record_object_position(obj) - def get_application_name(self): + def get_application_name(self) -> str: return "BlenderBIM" - def get_application_version(self): + def get_application_version(self) -> str: version = ".".join( [ str(x) @@ -184,11 +186,13 @@ class IfcExporter: class IfcExportSettings: def __init__(self): - self.logger = None - self.output_file = None + self.logger: Logger = None + self.output_file: str = None + self.json_version: str = None + self.json_compact: bool = None @staticmethod - def factory(context, output_file, logger): + def factory(context: bpy.types.Context, output_file: str, logger: Logger) -> IfcExportSettings: settings = IfcExportSettings() settings.output_file = output_file settings.logger = logger diff --git a/src/blenderbim/blenderbim/bim/import_ifc.py b/src/blenderbim/blenderbim/bim/import_ifc.py index 086827f1d5..c64b9058d4 100644 --- a/src/blenderbim/blenderbim/bim/import_ifc.py +++ b/src/blenderbim/blenderbim/bim/import_ifc.py @@ -2041,7 +2041,7 @@ class IfcImporter: class IfcImportSettings: def __init__(self): - self.logger = None + self.logger: logging.Logger = None self.input_file = None self.diff_file = None self.should_use_cpu_multiprocessing = True diff --git a/src/blenderbim/blenderbim/bim/module/drawing/operator.py b/src/blenderbim/blenderbim/bim/module/drawing/operator.py index 844100c12e..7da481f1b4 100644 --- a/src/blenderbim/blenderbim/bim/module/drawing/operator.py +++ b/src/blenderbim/blenderbim/bim/module/drawing/operator.py @@ -29,6 +29,7 @@ import subprocess import numpy as np import multiprocessing import ifcopenshell +import ifcopenshell.ifcopenshell_wrapper import ifcopenshell.geom import ifcopenshell.util.selector import ifcopenshell.util.representation diff --git a/src/blenderbim/blenderbim/bim/module/drawing/svgwriter.py b/src/blenderbim/blenderbim/bim/module/drawing/svgwriter.py index 39de461c70..4f5b40a52f 100644 --- a/src/blenderbim/blenderbim/bim/module/drawing/svgwriter.py +++ b/src/blenderbim/blenderbim/bim/module/drawing/svgwriter.py @@ -719,7 +719,7 @@ class SvgWriter: self.svg.text(sheet_id, insert=(text_position[0], text_position[1] + 2.5), class_="ELEVATION", **text_style) ) - def get_reference_and_sheet_id_from_annotation(self, element): + def get_reference_and_sheet_id_from_annotation(self, element: ifcopenshell.entity_instance) -> tuple[str, str]: reference_id = "-" sheet_id = "-" drawing = tool.Drawing.get_annotation_element(element) diff --git a/src/blenderbim/blenderbim/bim/module/geometry/data.py b/src/blenderbim/blenderbim/bim/module/geometry/data.py index 4925a482f4..bbf10b8392 100644 --- a/src/blenderbim/blenderbim/bim/module/geometry/data.py +++ b/src/blenderbim/blenderbim/bim/module/geometry/data.py @@ -17,6 +17,7 @@ # along with BlenderBIM Add-on. If not, see . import bpy +import ifcopenshell.util.element import blenderbim.tool as tool import ifcopenshell.util.placement from mathutils import Vector diff --git a/src/blenderbim/blenderbim/bim/module/material/data.py b/src/blenderbim/blenderbim/bim/module/material/data.py index cfdf3a8058..5e395438c8 100644 --- a/src/blenderbim/blenderbim/bim/module/material/data.py +++ b/src/blenderbim/blenderbim/bim/module/material/data.py @@ -19,6 +19,7 @@ import os import bpy import ifcopenshell +import ifcopenshell.util.element import ifcopenshell.util.doc import ifcopenshell.util.schema import blenderbim.tool as tool diff --git a/src/blenderbim/blenderbim/bim/module/model/product.py b/src/blenderbim/blenderbim/bim/module/model/product.py index 8c66e838d9..5da0e79283 100644 --- a/src/blenderbim/blenderbim/bim/module/model/product.py +++ b/src/blenderbim/blenderbim/bim/module/model/product.py @@ -39,6 +39,7 @@ from mathutils import Vector, Matrix from bpy_extras.object_utils import AddObjectHelper from . import prop import json +from typing import Any class EnableAddType(bpy.types.Operator, tool.Ifc.Operator): @@ -511,7 +512,7 @@ def regenerate_profile_usage(usecase_path, ifc_file, settings): ) -def ensure_material_assigned(usecase_path, ifc_file, settings): +def ensure_material_assigned(usecase_path: str, ifc_file: ifcopenshell.file, settings: dict[str, Any]) -> None: if usecase_path == "material.assign_material": if not settings.get("material", None): return @@ -550,7 +551,7 @@ def ensure_material_assigned(usecase_path, ifc_file, settings): obj.data.materials.append(IfcStore.get_element(material[0].id())) -def ensure_material_unassigned(usecase_path, ifc_file, settings): +def ensure_material_unassigned(usecase_path: str, ifc_file: ifcopenshell.file, settings: dict[str, Any]) -> None: elements = settings["products"] if elements[0].is_a("IfcElementType"): elements.extend(ifcopenshell.util.element.get_types(elements[0])) diff --git a/src/blenderbim/blenderbim/tool/geometry.py b/src/blenderbim/blenderbim/tool/geometry.py index 7acd2429ed..eed3d3412b 100644 --- a/src/blenderbim/blenderbim/tool/geometry.py +++ b/src/blenderbim/blenderbim/tool/geometry.py @@ -23,9 +23,14 @@ import hashlib import logging import numpy as np import ifcopenshell +import ifcopenshell.api +import ifcopenshell.util.element +import ifcopenshell.util.system import blenderbim.core.tool +import blenderbim.core.drawing import blenderbim.core.style import blenderbim.core.spatial +import blenderbim.core.system import blenderbim.core.geometry import blenderbim.tool as tool import blenderbim.bim.import_ifc diff --git a/src/ifcopenshell-python/ifcopenshell/api/geometry/add_representation.py b/src/ifcopenshell-python/ifcopenshell/api/geometry/add_representation.py index b435cf685b..2bd501cb49 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/geometry/add_representation.py +++ b/src/ifcopenshell-python/ifcopenshell/api/geometry/add_representation.py @@ -29,7 +29,7 @@ EPSILON = 1e-6 class Usecase: - def __init__(self, file, **settings): + def __init__(self, file: ifcopenshell.file, **settings): # TODO: This usecase currently depends on Blender's data model self.file = file self.settings = { @@ -58,7 +58,7 @@ class Usecase: for key, value in settings.items(): self.settings[key] = value - def execute(self): + def execute(self) -> ifcopenshell.entity_instance: self.is_manifold = None if ( isinstance(self.settings["geometry"], bpy.types.Mesh) @@ -379,7 +379,7 @@ class Usecase: Axis=self.file.createIfcDirection(polygon.normal), )) - def create_annotation_fill_areas(self, is_2d=False): + def create_annotation_fill_areas(self, is_2d=False) -> list[ifcopenshell.entity_instance]: items = [] if self.file.schema != "IFC2X3": points = self.create_cartesian_point_list_from_vertices(self.settings["geometry"].vertices, is_2d=is_2d) @@ -391,7 +391,9 @@ class Usecase: items.append(self.file.createIfcAnnotationFillArea(OuterBoundary=curve)) return items - def create_curve_from_polygon(self, points, polygon, is_2d=False): + def create_curve_from_polygon( + self, points: ifcopenshell.entity_instance, polygon: bpy.types.MeshPolygon, is_2d=False + ) -> ifcopenshell.entity_instance: indices = list(polygon.vertices) indices.append(indices[0]) edge_loop = [self.file.createIfcLineIndex((v1 + 1, v2 + 1)) for v1, v2 in zip(indices, indices[1:])] @@ -460,7 +462,7 @@ class Usecase: return False return True - def create_curves(self, should_exclude_faces=False, is_2d=False): + def create_curves(self, should_exclude_faces=False, is_2d=False, ignore_non_loose_edges=False): geom_data = self.settings["geometry"] if isinstance(geom_data, bpy.types.Mesh): @@ -530,7 +532,9 @@ class Usecase: bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001) tool.Blender.apply_bmesh(mesh, bm) - def create_curves_from_mesh_ifc2x3(self, should_exclude_faces=False, is_2d=False): + def create_curves_from_mesh_ifc2x3( + self, should_exclude_faces=False, is_2d=False + ) -> list[ifcopenshell.entity_instance]: geom_data = self.settings["geometry"].copy() self.remove_doubles_from_mesh(geom_data) curves = [] @@ -810,7 +814,7 @@ class Usecase: z = self.convert_si_to_unit(z) return self.file.createIfcCartesianPoint((x, y, z)) - def create_cartesian_point_list_from_vertices(self, vertices, is_2d=False): + def create_cartesian_point_list_from_vertices(self, vertices: list[bpy.types.MeshVertex], is_2d=False): if is_2d: return self.file.createIfcCartesianPointList2D([self.convert_si_to_unit(v.co.xy) for v in vertices]) return self.file.createIfcCartesianPointList3D([self.convert_si_to_unit(v.co) for v in vertices]) diff --git a/src/ifcopenshell-python/ifcopenshell/api/material/add_profile.py b/src/ifcopenshell-python/ifcopenshell/api/material/add_profile.py index 8b322b71f7..51b622b3f3 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/material/add_profile.py +++ b/src/ifcopenshell-python/ifcopenshell/api/material/add_profile.py @@ -15,10 +15,18 @@ # # You should have received a copy of the GNU Lesser General Public License # along with IfcOpenShell. If not, see . +import ifcopenshell +from typing import Optional class Usecase: - def __init__(self, file, profile_set=None, material=None, profile=None): + def __init__( + self, + file: ifcopenshell.file, + profile_set: ifcopenshell.entity_instance, + material: Optional[ifcopenshell.entity_instance] = None, + profile: Optional[ifcopenshell.entity_instance] = None, + ): """Add a new profile item to a profile set A profile item in a profile set represents an extruded 2D profile curve @@ -41,10 +49,10 @@ class Usecase: how to add a profile set. :type profile_set: ifcopenshell.entity_instance.entity_instance :param material: The IfcMaterial that the profile item is made out of. - :type material: ifcopenshell.entity_instance.entity_instance + :type material: ifcopenshell.entity_instance.entity_instance, optional :param profile: The IfcProfileDef that represents the 2D cross section of the the profile item. - :type profile: ifcopenshell.entity_instance.entity_instance + :type profile: ifcopenshell.entity_instance.entity_instance, optional :return: The newly created IfcMaterialProfile :rtype: ifcopenshell.entity_instance.entity_instance @@ -84,7 +92,7 @@ class Usecase: self.file = file self.settings = {"profile_set": profile_set, "material": material, "profile": profile} - def execute(self): + def execute(self) -> ifcopenshell.entity_instance: profiles = list(self.settings["profile_set"].MaterialProfiles or []) profile = self.file.create_entity("IfcMaterialProfile") if self.settings["material"]: diff --git a/src/ifcopenshell-python/ifcopenshell/api/owner/update_owner_history.py b/src/ifcopenshell-python/ifcopenshell/api/owner/update_owner_history.py index 01f39410d9..15c13d98f0 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/owner/update_owner_history.py +++ b/src/ifcopenshell-python/ifcopenshell/api/owner/update_owner_history.py @@ -21,10 +21,11 @@ import ifcopenshell import ifcopenshell.api import ifcopenshell.api.owner.settings import ifcopenshell.util.element +from typing import Union class Usecase: - def __init__(self, file, element=None): + def __init__(self, file: ifcopenshell.file, element: ifcopenshell.entity_instance): """Updates the owner that is assigned to an object This ensures that the owner is tracked to have modified the object last, @@ -60,7 +61,7 @@ class Usecase: self.file = file self.settings = {"element": element} - def execute(self): + def execute(self) -> Union[ifcopenshell.entity_instance, None]: if not hasattr(self.settings["element"], "OwnerHistory"): return user = ifcopenshell.api.owner.settings.get_user(self.file) diff --git a/src/ifcpatch/ifcpatch/recipes/RegenerateGlobalIds.py b/src/ifcpatch/ifcpatch/recipes/RegenerateGlobalIds.py index 58e35cb2c3..9556c38544 100644 --- a/src/ifcpatch/ifcpatch/recipes/RegenerateGlobalIds.py +++ b/src/ifcpatch/ifcpatch/recipes/RegenerateGlobalIds.py @@ -17,10 +17,11 @@ # along with IfcPatch. If not, see . import ifcopenshell +from logging import Logger class Patcher: - def __init__(self, src, file, logger, only_duplicates=False): + def __init__(self, src: str, file: ifcopenshell.file, logger: Logger, only_duplicates=False): """Regenerate GlobalIds in an IFC model All root elements in an IFC model must be identified by a unique Global