From 4ec8345716f10223d981a36e301cfc360a89bfc5 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 27 Jun 2024 14:07:48 +0500 Subject: [PATCH] typing --- .../blenderbim/bim/module/model/root.py | 3 +- src/blenderbim/blenderbim/core/owner.py | 90 +++++++++++-------- src/blenderbim/blenderbim/tool/owner.py | 65 +++++++------- .../ifcopenshell/api/owner/add_actor.py | 5 +- .../ifcopenshell/api/owner/add_address.py | 6 +- 5 files changed, 98 insertions(+), 71 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/model/root.py b/src/blenderbim/blenderbim/bim/module/model/root.py index 861033b750..25e58acd64 100644 --- a/src/blenderbim/blenderbim/bim/module/model/root.py +++ b/src/blenderbim/blenderbim/bim/module/model/root.py @@ -20,7 +20,7 @@ import bpy import ifcopenshell.file import blenderbim.bim.handler import blenderbim.tool as tool -from blenderbim.bim.ifc import IfcStore +from blenderbim.bim.ifc import IFC_CONNECTED_TYPE from typing import Any @@ -38,6 +38,7 @@ def sync_name(usecase_path: str, ifc_file: ifcopenshell.file, settings: dict[str obj = tool.Ifc.get_object(element) if not obj: return + obj: IFC_CONNECTED_TYPE if isinstance(obj, bpy.types.Object): new_name = "{}/{}".format(element.is_a(), settings["attributes"]["Name"] or "Unnamed") collection = obj.BIMObjectProperties.collection diff --git a/src/blenderbim/blenderbim/core/owner.py b/src/blenderbim/blenderbim/core/owner.py index dfeec08970..e6f8793685 100644 --- a/src/blenderbim/blenderbim/core/owner.py +++ b/src/blenderbim/blenderbim/core/owner.py @@ -16,160 +16,178 @@ # You should have received a copy of the GNU General Public License # along with BlenderBIM Add-on. If not, see . +from __future__ import annotations +from typing import TYPE_CHECKING, Optional, Union -def add_person(ifc): +if TYPE_CHECKING: + import bpy + import ifcopenshell + import blenderbim.tool as tool + from ifcopenshell.api.owner.add_address import ADDRESS_TYPE + from ifcopenshell.api.owner.add_actor import ACTOR_TYPE + + +def add_person(ifc: tool.Ifc) -> ifcopenshell.entity_instance: return ifc.run("owner.add_person") -def remove_person(ifc, person=None): +def remove_person(ifc: tool.Ifc, person: ifcopenshell.entity_instance) -> None: ifc.run("owner.remove_person", person=person) -def enable_editing_person(owner, person=None): +def enable_editing_person(owner: tool.Owner, person: ifcopenshell.entity_instance) -> None: owner.set_person(person) owner.import_person_attributes() -def disable_editing_person(owner): +def disable_editing_person(owner: tool.Owner) -> None: owner.clear_person() -def edit_person(ifc, owner): +def edit_person(ifc: tool.Ifc, owner: tool.Owner) -> None: ifc.run("owner.edit_person", person=owner.get_person(), attributes=owner.export_person_attributes()) disable_editing_person(owner) -def add_person_attribute(owner, name=None): +def add_person_attribute(owner: tool.Owner, name: str) -> None: owner.add_person_attribute(name) -def remove_person_attribute(owner, name=None, id=None): +def remove_person_attribute(owner: tool.Owner, name: str, id: int) -> None: owner.remove_person_attribute(name, id) -def add_role(ifc, parent=None): +def add_role(ifc: tool.Ifc, parent: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance: return ifc.run("owner.add_role", assigned_object=parent) -def remove_role(ifc, role=None): +def remove_role(ifc: tool.Ifc, role: ifcopenshell.entity_instance) -> None: ifc.run("owner.remove_role", role=role) -def enable_editing_role(owner, role=None): +def enable_editing_role(owner: tool.Owner, role: ifcopenshell.entity_instance) -> None: owner.set_role(role) owner.import_role_attributes() -def disable_editing_role(owner): +def disable_editing_role(owner: tool.Owner) -> None: owner.clear_role() -def edit_role(ifc, owner): +def edit_role(ifc: tool.Ifc, owner: tool.Owner) -> None: ifc.run("owner.edit_role", role=owner.get_role(), attributes=owner.export_role_attributes()) owner.clear_role() -def add_address(ifc, parent=None, ifc_class="IfcPostalAddress"): +def add_address( + ifc: tool.Ifc, parent: ifcopenshell.entity_instance, ifc_class: ADDRESS_TYPE = "IfcPostalAddress" +) -> ifcopenshell.entity_instance: return ifc.run("owner.add_address", assigned_object=parent, ifc_class=ifc_class) -def remove_address(ifc, address=None): +def remove_address(ifc: tool.Ifc, address: ifcopenshell.entity_instance) -> None: ifc.run("owner.remove_address", address=address) -def enable_editing_address(owner, address=None): +def enable_editing_address(owner: tool.Owner, address: ifcopenshell.entity_instance) -> None: owner.set_address(address) owner.import_address_attributes() -def disable_editing_address(owner): +def disable_editing_address(owner: tool.Owner) -> None: owner.clear_address() -def edit_address(ifc, owner): +def edit_address(ifc: tool.Ifc, owner: tool.Owner) -> None: address = owner.get_address() ifc.run("owner.edit_address", address=address, attributes=owner.export_address_attributes()) owner.clear_address() -def add_address_attribute(owner, name=None): +def add_address_attribute(owner: tool.Owner, name: str) -> None: owner.add_address_attribute(name) -def remove_address_attribute(owner, name=None, id=None): +def remove_address_attribute(owner: tool.Owner, name: str, id: int) -> None: owner.remove_address_attribute(name, id) -def add_organisation(ifc): +def add_organisation(ifc: tool.Ifc) -> ifcopenshell.entity_instance: return ifc.run("owner.add_organisation") -def remove_organisation(ifc, organisation=None): +def remove_organisation(ifc: tool.Ifc, organisation: ifcopenshell.entity_instance) -> None: ifc.run("owner.remove_organisation", organisation=organisation) -def enable_editing_organisation(owner, organisation=None): +def enable_editing_organisation(owner: tool.Owner, organisation: ifcopenshell.entity_instance) -> None: owner.set_organisation(organisation) owner.import_organisation_attributes() -def disable_editing_organisation(owner): +def disable_editing_organisation(owner: tool.Owner) -> None: owner.clear_organisation() -def edit_organisation(ifc, owner): +def edit_organisation(ifc: tool.Ifc, owner: tool.Owner) -> None: organisation = owner.get_organisation() ifc.run("owner.edit_organisation", organisation=organisation, attributes=owner.export_organisation_attributes()) owner.clear_organisation() -def add_person_and_organisation(ifc, person=None, organisation=None): +def add_person_and_organisation( + ifc: tool.Ifc, person: ifcopenshell.entity_instance, organisation: ifcopenshell.entity_instance +) -> ifcopenshell.entity_instance: return ifc.run("owner.add_person_and_organisation", person=person, organisation=organisation) -def remove_person_and_organisation(ifc, owner, person_and_organisation): +def remove_person_and_organisation( + ifc: tool.Ifc, owner: tool.Owner, person_and_organisation: ifcopenshell.entity_instance +) -> None: if owner.get_user() == person_and_organisation: owner.clear_user() ifc.run("owner.remove_person_and_organisation", person_and_organisation=person_and_organisation) -def set_user(owner, user=None): +def set_user(owner: tool.Owner, user: ifcopenshell.entity_instance) -> None: owner.set_user(user) -def get_user(owner): +def get_user(owner: tool.Owner) -> Union[ifcopenshell.entity_instance, None]: return owner.get_user() -def clear_user(owner): +def clear_user(owner: tool.Owner) -> None: owner.clear_user() -def add_actor(ifc, ifc_class=None, actor=None): +def add_actor( + ifc: tool.Ifc, ifc_class: ACTOR_TYPE, actor: ifcopenshell.entity_instance +) -> ifcopenshell.entity_instance: return ifc.run("owner.add_actor", ifc_class=ifc_class, actor=actor) -def remove_actor(ifc, actor=None): +def remove_actor(ifc: tool.Ifc, actor: ifcopenshell.entity_instance) -> None: ifc.run("owner.remove_actor", actor=actor) -def enable_editing_actor(owner, actor=None): +def enable_editing_actor(owner: tool.Owner, actor: ifcopenshell.entity_instance) -> None: owner.set_actor(actor) owner.import_actor_attributes(actor) -def disable_editing_actor(owner): +def disable_editing_actor(owner: tool.Owner) -> None: owner.clear_actor() -def edit_actor(ifc, owner): +def edit_actor(ifc: tool.Ifc, owner: tool.Owner) -> None: ifc.run("owner.edit_actor", actor=owner.get_actor(), attributes=owner.export_actor_attributes()) disable_editing_actor(owner) -def assign_actor(ifc, actor=None, element=None): +def assign_actor(ifc: tool.Ifc, actor: ifcopenshell.entity_instance, element: ifcopenshell.entity_instance) -> None: ifc.run("owner.assign_actor", relating_actor=actor, related_object=element) -def unassign_actor(ifc, actor=None, element=None): +def unassign_actor(ifc: tool.Ifc, actor: ifcopenshell.entity_instance, element: ifcopenshell.entity_instance) -> None: ifc.run("owner.unassign_actor", relating_actor=actor, related_object=element) diff --git a/src/blenderbim/blenderbim/tool/owner.py b/src/blenderbim/blenderbim/tool/owner.py index b805112689..80ae7db456 100644 --- a/src/blenderbim/blenderbim/tool/owner.py +++ b/src/blenderbim/blenderbim/tool/owner.py @@ -19,13 +19,14 @@ import bpy import blenderbim.core.tool import blenderbim.tool as tool +import blenderbim.bim.helper import ifcopenshell -from typing import Union +from typing import Union, Any class Owner(blenderbim.core.tool.Owner): @classmethod - def set_user(cls, user): + def set_user(cls, user: ifcopenshell.entity_instance) -> None: bpy.context.scene.BIMOwnerProperties.active_user_id = user.id() @classmethod @@ -38,15 +39,15 @@ class Owner(blenderbim.core.tool.Owner): return users[0] @classmethod - def clear_user(cls): + def clear_user(cls) -> None: bpy.context.scene.BIMOwnerProperties.active_user_id = 0 @classmethod - def set_address(cls, address): + def set_address(cls, address: ifcopenshell.entity_instance) -> None: bpy.context.scene.BIMOwnerProperties.active_address_id = address.id() @classmethod - def import_address_attributes(cls): + def import_address_attributes(cls) -> None: props = bpy.context.scene.BIMOwnerProperties props.address_attributes.clear() props.address_lines.clear() @@ -77,15 +78,15 @@ class Owner(blenderbim.core.tool.Owner): blenderbim.bim.helper.import_attributes(address.is_a(), props.address_attributes, address.get_info(), callback) @classmethod - def clear_address(cls): + def clear_address(cls) -> None: bpy.context.scene.BIMOwnerProperties.active_address_id = 0 @classmethod - def get_address(cls): + def get_address(cls) -> ifcopenshell.entity_instance: return tool.Ifc().get().by_id(bpy.context.scene.BIMOwnerProperties.active_address_id) @classmethod - def export_address_attributes(cls): + def export_address_attributes(cls) -> dict[str, Any]: props = bpy.context.scene.BIMOwnerProperties attributes = blenderbim.bim.helper.export_attributes(props.address_attributes) if cls.get_address().is_a("IfcPostalAddress"): @@ -98,7 +99,7 @@ class Owner(blenderbim.core.tool.Owner): return attributes @classmethod - def add_address_attribute(cls, name): + def add_address_attribute(cls, name: str) -> None: props = bpy.context.scene.BIMOwnerProperties if name == "AddressLines": props.address_lines.add() @@ -112,7 +113,7 @@ class Owner(blenderbim.core.tool.Owner): props.messaging_ids.add() @classmethod - def remove_address_attribute(cls, name, id): + def remove_address_attribute(cls, name: str, id: int) -> None: props = bpy.context.scene.BIMOwnerProperties if name == "AddressLines": props.address_lines.remove(id) @@ -126,11 +127,11 @@ class Owner(blenderbim.core.tool.Owner): props.messaging_ids.remove(id) @classmethod - def set_organisation(cls, organisation): + def set_organisation(cls, organisation: ifcopenshell.entity_instance) -> None: bpy.context.scene.BIMOwnerProperties.active_organisation_id = organisation.id() @classmethod - def import_organisation_attributes(cls): + def import_organisation_attributes(cls) -> None: organisation = tool.Ifc.get().by_id(bpy.context.scene.BIMOwnerProperties.active_organisation_id) props = bpy.context.scene.BIMOwnerProperties props.organisation_attributes.clear() @@ -140,25 +141,25 @@ class Owner(blenderbim.core.tool.Owner): ) @classmethod - def clear_organisation(cls): + def clear_organisation(cls) -> None: bpy.context.scene.BIMOwnerProperties.active_organisation_id = 0 @classmethod - def export_organisation_attributes(cls): + def export_organisation_attributes(cls) -> dict[str, Any]: props = bpy.context.scene.BIMOwnerProperties attributes = blenderbim.bim.helper.export_attributes(props.organisation_attributes) return attributes @classmethod - def get_organisation(cls): + def get_organisation(cls) -> ifcopenshell.entity_instance: return tool.Ifc().get().by_id(bpy.context.scene.BIMOwnerProperties.active_organisation_id) @classmethod - def set_person(cls, person): + def set_person(cls, person: ifcopenshell.entity_instance) -> None: bpy.context.scene.BIMOwnerProperties.active_person_id = person.id() @classmethod - def import_person_attributes(cls): + def import_person_attributes(cls) -> None: person = tool.Ifc.get().by_id(bpy.context.scene.BIMOwnerProperties.active_person_id) props = bpy.context.scene.BIMOwnerProperties props.person_attributes.clear() @@ -180,11 +181,11 @@ class Owner(blenderbim.core.tool.Owner): blenderbim.bim.helper.import_attributes("IfcPerson", props.person_attributes, person.get_info(), callback) @classmethod - def clear_person(cls): + def clear_person(cls) -> None: bpy.context.scene.BIMOwnerProperties.active_person_id = 0 @classmethod - def export_person_attributes(cls): + def export_person_attributes(cls) -> dict[str, Any]: props = bpy.context.scene.BIMOwnerProperties attributes = blenderbim.bim.helper.export_attributes(props.person_attributes) attributes["MiddleNames"] = [v.name for v in props.middle_names] if props.middle_names else None @@ -193,11 +194,11 @@ class Owner(blenderbim.core.tool.Owner): return attributes @classmethod - def get_person(cls): + def get_person(cls) -> ifcopenshell.entity_instance: return tool.Ifc().get().by_id(bpy.context.scene.BIMOwnerProperties.active_person_id) @classmethod - def add_person_attribute(cls, name): + def add_person_attribute(cls, name: str) -> None: if name == "MiddleNames": bpy.context.scene.BIMOwnerProperties.middle_names.add() elif name == "PrefixTitles": @@ -206,7 +207,7 @@ class Owner(blenderbim.core.tool.Owner): bpy.context.scene.BIMOwnerProperties.suffix_titles.add() @classmethod - def remove_person_attribute(cls, name, id): + def remove_person_attribute(cls, name: str, id: int) -> None: if name == "MiddleNames": bpy.context.scene.BIMOwnerProperties.middle_names.remove(id) elif name == "PrefixTitles": @@ -215,48 +216,48 @@ class Owner(blenderbim.core.tool.Owner): bpy.context.scene.BIMOwnerProperties.suffix_titles.remove(id) @classmethod - def set_role(cls, role): + def set_role(cls, role: ifcopenshell.entity_instance) -> None: bpy.context.scene.BIMOwnerProperties.active_role_id = role.id() @classmethod - def import_role_attributes(cls): + def import_role_attributes(cls) -> None: role = cls.get_role() props = bpy.context.scene.BIMOwnerProperties props.role_attributes.clear() blenderbim.bim.helper.import_attributes("IfcActorRole", props.role_attributes, role.get_info()) @classmethod - def clear_role(cls): + def clear_role(cls) -> None: bpy.context.scene.BIMOwnerProperties.active_role_id = 0 @classmethod - def get_role(cls): + def get_role(cls) -> ifcopenshell.entity_instance: return tool.Ifc().get().by_id(bpy.context.scene.BIMOwnerProperties.active_role_id) @classmethod - def export_role_attributes(cls): + def export_role_attributes(cls) -> dict[str, Any]: return blenderbim.bim.helper.export_attributes(bpy.context.scene.BIMOwnerProperties.role_attributes) @classmethod - def set_actor(cls, actor): + def set_actor(cls, actor: ifcopenshell.entity_instance) -> None: bpy.context.scene.BIMOwnerProperties.active_actor_id = actor.id() @classmethod - def import_actor_attributes(cls, actor): + def import_actor_attributes(cls, actor: ifcopenshell.entity_instance) -> None: props = bpy.context.scene.BIMOwnerProperties props.actor_attributes.clear() blenderbim.bim.helper.import_attributes2(actor, props.actor_attributes) @classmethod - def clear_actor(cls): + def clear_actor(cls) -> None: bpy.context.scene.BIMOwnerProperties.active_actor_id = 0 @classmethod - def export_actor_attributes(cls): + def export_actor_attributes(cls) -> dict[str, Any]: props = bpy.context.scene.BIMOwnerProperties attributes = blenderbim.bim.helper.export_attributes(props.actor_attributes) return attributes @classmethod - def get_actor(cls): + def get_actor(cls) -> ifcopenshell.entity_instance: return tool.Ifc().get().by_id(bpy.context.scene.BIMOwnerProperties.active_actor_id) diff --git a/src/ifcopenshell-python/ifcopenshell/api/owner/add_actor.py b/src/ifcopenshell-python/ifcopenshell/api/owner/add_actor.py index d4c82315c9..ab2794573c 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/owner/add_actor.py +++ b/src/ifcopenshell-python/ifcopenshell/api/owner/add_actor.py @@ -22,10 +22,13 @@ import ifcopenshell.api from typing import Literal +ACTOR_TYPE = Literal["IfcActor", "IfcOccupant"] + + def add_actor( file: ifcopenshell.file, actor: ifcopenshell.entity_instance, - ifc_class: Literal["IfcActor", "IfcOccupant"] = "IfcActor", + ifc_class: ACTOR_TYPE = "IfcActor", ) -> ifcopenshell.entity_instance: """Adds a new actor diff --git a/src/ifcopenshell-python/ifcopenshell/api/owner/add_address.py b/src/ifcopenshell-python/ifcopenshell/api/owner/add_address.py index cc8d8bfb33..32eaf1a7ab 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/owner/add_address.py +++ b/src/ifcopenshell-python/ifcopenshell/api/owner/add_address.py @@ -16,10 +16,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with IfcOpenShell. If not, see . import ifcopenshell +from typing import Literal + + +ADDRESS_TYPE = Literal["IfcPostalAddress", "IfcTelecomAddress"] def add_address( - file: ifcopenshell.file, assigned_object: ifcopenshell.entity_instance, ifc_class: str = "IfcPostalAddress" + file: ifcopenshell.file, assigned_object: ifcopenshell.entity_instance, ifc_class: ADDRESS_TYPE = "IfcPostalAddress" ) -> ifcopenshell.entity_instance: """Add a new telecom or postal address to an organisation or person