From e6d3396630051c484e7ac1c6efbd844857d37406 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Fri, 21 Feb 2025 14:45:17 +0500 Subject: [PATCH] typing --- src/bonsai/bonsai/bim/import_ifc.py | 2 +- .../bonsai/bim/module/drawing/annotation.py | 5 +- .../bonsai/bim/module/geometry/operator.py | 2 +- src/bonsai/bonsai/tool/drawing.py | 49 +++++++++++-------- .../api/style/add_surface_style.py | 18 +++---- .../ifcopenshell/util/element.py | 2 +- 6 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/bonsai/bonsai/bim/import_ifc.py b/src/bonsai/bonsai/bim/import_ifc.py index 21c8d16e3e..256bd8ff85 100644 --- a/src/bonsai/bonsai/bim/import_ifc.py +++ b/src/bonsai/bonsai/bim/import_ifc.py @@ -970,7 +970,7 @@ class IfcImporter: object_type = element.ObjectType return ( object_type in tool.Drawing.ANNOTATION_TYPES_DATA - and tool.Drawing.ANNOTATION_TYPES_DATA[object_type][3] == "curve" + and tool.Drawing.ANNOTATION_TYPES_DATA[object_type].data_type == "curve" ) def get_drawing_group(self, element): diff --git a/src/bonsai/bonsai/bim/module/drawing/annotation.py b/src/bonsai/bonsai/bim/module/drawing/annotation.py index 8bc9ee46d5..6acf5a03f7 100644 --- a/src/bonsai/bonsai/bim/module/drawing/annotation.py +++ b/src/bonsai/bonsai/bim/module/drawing/annotation.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with Bonsai. If not, see . +from __future__ import annotations import os import bpy import math @@ -125,7 +126,9 @@ class Annotator: return obj @staticmethod - def get_annotation_obj(drawing: ifcopenshell.entity_instance, object_type: str, data_type: str) -> bpy.types.Object: + def get_annotation_obj( + drawing: ifcopenshell.entity_instance, object_type: str, data_type: tool.Drawing.ANNOTATION_DATA_TYPE + ) -> bpy.types.Object: camera = tool.Ifc.get_object(drawing) co1, _, _, _ = Annotator.get_placeholder_coords(camera) matrix_world = tool.Drawing.get_camera_matrix(camera) diff --git a/src/bonsai/bonsai/bim/module/geometry/operator.py b/src/bonsai/bonsai/bim/module/geometry/operator.py index fbccff9266..0763bd4963 100644 --- a/src/bonsai/bonsai/bim/module/geometry/operator.py +++ b/src/bonsai/bonsai/bim/module/geometry/operator.py @@ -683,7 +683,7 @@ class CopyRepresentation(bpy.types.Operator, tool.Ifc.Operator): ) -def lock_error_message(name): +def lock_error_message(name: str) -> str: return f"'{name}' is locked. Unlock it via the Spatial panel in the Project Overview tab." diff --git a/src/bonsai/bonsai/tool/drawing.py b/src/bonsai/bonsai/tool/drawing.py index 088e5b7025..07e274b011 100644 --- a/src/bonsai/bonsai/tool/drawing.py +++ b/src/bonsai/bonsai/tool/drawing.py @@ -51,7 +51,7 @@ from shapely.ops import unary_union from lxml import etree from mathutils import Vector, Matrix from fractions import Fraction -from typing import Optional, Union, Iterable, Any, Literal, Sequence, TYPE_CHECKING +from typing import Optional, Union, Iterable, Any, Literal, Sequence, TYPE_CHECKING, NamedTuple from pathlib import Path if TYPE_CHECKING: @@ -65,25 +65,32 @@ class Drawing(bonsai.core.tool.Drawing): # ObjectType: annotation_name, description, icon, data_type # fmt: off - ANNOTATION_TYPES_DATA = { - "DIMENSION": ("Dimension", "Add dimensions annotation.\nMeasurement values can be hidden through ShowDescriptionOnly property\nof BBIM_Dimension property set", "FIXED_SIZE", "curve"), - "ANGLE": ("Angle", "", "DRIVER_ROTATIONAL_DIFFERENCE", "curve"), - "RADIUS": ("Radius", "", "FORWARD", "curve"), - "DIAMETER": ("Diameter", "Add diameter annotation.\nMeasurement values can be hidden through ShowDescriptionOnly property\nof BBIM_Dimension property set", "ARROW_LEFTRIGHT", "curve"), - "TEXT": ("Text", "", "SMALL_CAPS", "empty"), - "TEXT_LEADER": ("Leader", "", "TRACKING_BACKWARDS", "curve"), - "STAIR_ARROW": ("Stair Arrow", "Add stair arrow annotation.\nIf you have IfcStairFlight object selected, it will be used as a reference for the annotation", "SCREEN_BACK", "curve"), - "PLAN_LEVEL": ("Level (Plan)", "", "SORTBYEXT", "curve"), - "SECTION_LEVEL": ("Level (Section)", "", "TRIA_DOWN", "curve"), - "BREAKLINE": ("Breakline", "", "FCURVE", "mesh"), - "SYMBOL": ("Symbol", "", "KEYFRAME", "empty"), - "MULTI_SYMBOL": ("Multi-Symbol", "", "OUTLINER_DATA_POINTCLOUD", "mesh"), - "LINEWORK": ("Line", "", "SNAP_MIDPOINT", "mesh"), - "BATTING": ("Batting", "Add batting annotation.\nThickness could be changed through Thickness property of BBIM_Batting property set", "FORCE_FORCE", "mesh"), - "REVISION_CLOUD":("Revision Cloud", "Add revision cloud", "VOLUME_DATA", "mesh"), - "FILL_AREA": ("Fill Area", "", "NODE_TEXTURE", "mesh"), - "FALL": ("Fall", "", "SORT_ASC", "curve"), - "IMAGE": ("Image", "Add reference image attached to the drawing", "TEXTURE", "mesh"), + + class AnnotationObjectType(NamedTuple): + annotation_name: str + description: str + icon: str + data_type: Drawing.ANNOTATION_DATA_TYPE + + ANNOTATION_TYPES_DATA: dict[str, AnnotationObjectType] = { + "DIMENSION": AnnotationObjectType("Dimension", "Add dimensions annotation.\nMeasurement values can be hidden through ShowDescriptionOnly property\nof BBIM_Dimension property set", "FIXED_SIZE", "curve"), + "ANGLE": AnnotationObjectType("Angle", "", "DRIVER_ROTATIONAL_DIFFERENCE", "curve"), + "RADIUS": AnnotationObjectType("Radius", "", "FORWARD", "curve"), + "DIAMETER": AnnotationObjectType("Diameter", "Add diameter annotation.\nMeasurement values can be hidden through ShowDescriptionOnly property\nof BBIM_Dimension property set", "ARROW_LEFTRIGHT", "curve"), + "TEXT": AnnotationObjectType("Text", "", "SMALL_CAPS", "empty"), + "TEXT_LEADER": AnnotationObjectType("Leader", "", "TRACKING_BACKWARDS", "curve"), + "STAIR_ARROW": AnnotationObjectType("Stair Arrow", "Add stair arrow annotation.\nIf you have IfcStairFlight object selected, it will be used as a reference for the annotation", "SCREEN_BACK", "curve"), + "PLAN_LEVEL": AnnotationObjectType("Level (Plan)", "", "SORTBYEXT", "curve"), + "SECTION_LEVEL": AnnotationObjectType("Level (Section)", "", "TRIA_DOWN", "curve"), + "BREAKLINE": AnnotationObjectType("Breakline", "", "FCURVE", "mesh"), + "SYMBOL": AnnotationObjectType("Symbol", "", "KEYFRAME", "empty"), + "MULTI_SYMBOL": AnnotationObjectType("Multi-Symbol", "", "OUTLINER_DATA_POINTCLOUD", "mesh"), + "LINEWORK": AnnotationObjectType("Line", "", "SNAP_MIDPOINT", "mesh"), + "BATTING": AnnotationObjectType("Batting", "Add batting annotation.\nThickness could be changed through Thickness property of BBIM_Batting property set", "FORCE_FORCE", "mesh"), + "REVISION_CLOUD":AnnotationObjectType("Revision Cloud", "Add revision cloud", "VOLUME_DATA", "mesh"), + "FILL_AREA": AnnotationObjectType("Fill Area", "", "NODE_TEXTURE", "mesh"), + "FALL": AnnotationObjectType("Fall", "", "SORT_ASC", "curve"), + "IMAGE": AnnotationObjectType("Image", "Add reference image attached to the drawing", "TEXTURE", "mesh"), } # fmt: on @@ -112,7 +119,7 @@ class Drawing(bonsai.core.tool.Drawing): @classmethod def get_annotation_data_type(cls, object_type: str) -> ANNOTATION_DATA_TYPE: - return cls.ANNOTATION_TYPES_DATA[object_type][3] + return cls.ANNOTATION_TYPES_DATA[object_type].data_type @classmethod def create_annotation_object(cls, drawing: ifcopenshell.entity_instance, object_type: str) -> bpy.types.Object: diff --git a/src/ifcopenshell-python/ifcopenshell/api/style/add_surface_style.py b/src/ifcopenshell-python/ifcopenshell/api/style/add_surface_style.py index 64d38cfd2f..944240e618 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/style/add_surface_style.py +++ b/src/ifcopenshell-python/ifcopenshell/api/style/add_surface_style.py @@ -35,7 +35,7 @@ def add_surface_style( style: ifcopenshell.entity_instance, ifc_class: SURFACE_STYLE_TYPES = "IfcSurfaceStyleShading", attributes: Optional[dict[str, Any]] = None, -) -> None: +) -> ifcopenshell.entity_instance: """Adds a new presentation item to a surface style A surface style can have multiple different types of presentation items @@ -123,20 +123,20 @@ def add_surface_style( "SpecularHighlight": {"SpecularRoughness": 0.5}, # Roughness factor }) """ - settings = {"style": style, "ifc_class": ifc_class, "attributes": attributes or {}} + attributes = attributes or {} + style_item = file.create_entity(ifc_class) + ifcopenshell.api.style.edit_surface_style(file, style=style_item, attributes=attributes) + styles: list[ifcopenshell.entity_instance] + styles = list(style.Styles or []) - style_item = file.create_entity(settings["ifc_class"]) - ifcopenshell.api.style.edit_surface_style(file, style=style_item, attributes=settings["attributes"]) - styles = list(settings["style"].Styles or []) - - select_class = settings["ifc_class"] + select_class = ifc_class if select_class == "IfcSurfaceStyleRendering": select_class = "IfcSurfaceStyleShading" duplicate_items = [s for s in styles if s.is_a(select_class)] for duplicate_item in duplicate_items: ifcopenshell.api.style.remove_surface_style(file, style=duplicate_item) - styles = list(settings["style"].Styles or []) + styles = list(style.Styles or []) styles.append(style_item) - settings["style"].Styles = styles + style.Styles = styles return style_item diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index 524f86b8ac..f3865bdc7e 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -454,7 +454,7 @@ def get_elements_by_pset(pset: ifcopenshell.entity_instance) -> set[ifcopenshell return elements -def get_predefined_type(element: ifcopenshell.entity_instance) -> str: +def get_predefined_type(element: ifcopenshell.entity_instance) -> Union[str, None]: """Retrieves the PrefefinedType attribute of an element. If the predefined type is user defined, the custom type (such as object