From acbdc0ed4ca535381c096ad372ffdcbeb6bce823 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 3 Sep 2024 15:17:17 +0500 Subject: [PATCH] typing --- .../api/geometry/assign_representation.py | 10 ++++++-- .../api/geometry/unassign_representation.py | 25 ++++++++++++------- .../ifcopenshell/util/type.py | 13 +++++++--- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/geometry/assign_representation.py b/src/ifcopenshell-python/ifcopenshell/api/geometry/assign_representation.py index 33c012fd3f..d2dee0a86e 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/geometry/assign_representation.py +++ b/src/ifcopenshell-python/ifcopenshell/api/geometry/assign_representation.py @@ -19,6 +19,7 @@ import ifcopenshell.api.owner import ifcopenshell.api.geometry import ifcopenshell.util.element +from typing import Any def assign_representation( @@ -31,7 +32,10 @@ def assign_representation( class Usecase: - def execute(self): + file: ifcopenshell.file + settings: dict[str, Any] + + def execute(self) -> None: if self.settings["product"].is_a("IfcProduct"): product_type = ifcopenshell.util.element.get_type(self.settings["product"]) if ( @@ -73,7 +77,9 @@ class Usecase: self.assign_product_representation(element, mapped_representation) ifcopenshell.api.owner.update_owner_history(self.file, **{"element": self.settings["product"]}) - def assign_product_representation(self, product, representation): + def assign_product_representation( + self, product: ifcopenshell.entity_instance, representation: ifcopenshell.entity_instance + ) -> None: definition = product.Representation if not definition: definition = self.file.createIfcProductDefinitionShape() diff --git a/src/ifcopenshell-python/ifcopenshell/api/geometry/unassign_representation.py b/src/ifcopenshell-python/ifcopenshell/api/geometry/unassign_representation.py index 6396914fb4..da5f0d99d1 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/geometry/unassign_representation.py +++ b/src/ifcopenshell-python/ifcopenshell/api/geometry/unassign_representation.py @@ -18,6 +18,7 @@ import ifcopenshell.api.geometry import ifcopenshell.util.element +from typing import Any def unassign_representation( @@ -30,13 +31,20 @@ def unassign_representation( class Usecase: - def execute(self): - if self.settings["product"].is_a("IfcProduct"): - self.unassign_product_representation(self.settings["product"], self.settings["representation"]) - elif self.settings["product"].is_a("IfcTypeProduct"): + file: ifcopenshell.file + settings: dict[str, Any] + + def execute(self) -> None: + product: ifcopenshell.entity_instance = self.settings["product"] + representation: ifcopenshell.entity_instance = self.settings["representation"] + if product.is_a("IfcProduct"): + self.unassign_product_representation(product, representation) + elif product.is_a("IfcTypeProduct"): self.unassign_type_representation() - def unassign_product_representation(self, product, representation): + def unassign_product_representation( + self, product: ifcopenshell.entity_instance, representation: ifcopenshell.entity_instance + ) -> None: representations = list(product.Representation.Representations or []) if representation not in representations: return @@ -46,8 +54,7 @@ class Usecase: else: product.Representation.Representations = representations - def unassign_type_representation(self): - + def unassign_type_representation(self) -> None: matching_representation_map = None representation_maps = self.settings["product"].RepresentationMaps or [] @@ -63,11 +70,11 @@ class Usecase: ] or None self.remove_representation_map_only(matching_representation_map) - def remove_representation_map_only(self, representation_map): + def remove_representation_map_only(self, representation_map: ifcopenshell.entity_instance) -> None: representation_map.MappedRepresentation = self.file.createIfcShapeRepresentation() ifcopenshell.util.element.remove_deep2(self.file, representation_map) - def unassign_products_using_mapped_representation(self, representation_map): + def unassign_products_using_mapped_representation(self, representation_map: ifcopenshell.entity_instance) -> None: mapped_representations = [] just_representations = [] for map_usage in representation_map.MapUsage or []: diff --git a/src/ifcopenshell-python/ifcopenshell/util/type.py b/src/ifcopenshell-python/ifcopenshell/util/type.py index bfb0f40bc0..fed14d69a0 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/type.py +++ b/src/ifcopenshell-python/ifcopenshell/util/type.py @@ -19,7 +19,6 @@ import os import json import ifcopenshell.util.schema -from typing import List cwd = os.path.dirname(os.path.realpath(__file__)) @@ -52,11 +51,19 @@ for schema in mapped_schemas: type_to_entity_map[schema][element_type] = [e for e in elements if guessed_element in e] -def get_applicable_types(ifc_class: str, schema="IFC4") -> List[str]: +def get_applicable_types(ifc_class: str, schema="IFC4") -> list[str]: + """Get applicable types IFC classes for the occurrence IFC class. + + E.g. "IfcWindow" -> ["IfcWindowType"]. + """ schema = ifcopenshell.util.schema.get_fallback_schema(schema.upper()) return entity_to_type_map[schema].get(ifc_class, []) -def get_applicable_entities(ifc_type_class: str, schema="IFC4") -> List[str]: +def get_applicable_entities(ifc_type_class: str, schema="IFC4") -> list[str]: + """Get applicable occurrence IFC classes for the type IFC class. + + E.g. "IfcWindowType" -> ["IfcWindow"]. + """ schema = ifcopenshell.util.schema.get_fallback_schema(schema.upper()) return type_to_entity_map[schema].get(ifc_type_class, [])