From 0aad3d99b5b312209634faddea94c39e3a12eac7 Mon Sep 17 00:00:00 2001 From: Ziad-I <68874104+Ziad-I@users.noreply.github.com> Date: Sat, 15 Jun 2024 14:40:33 +0300 Subject: [PATCH] add data.py for web module and sending data via ws connection --- src/blenderbim/blenderbim/bim/handler.py | 2 + .../blenderbim/bim/module/web/data.py | 40 +++++++++++++++++++ .../blenderbim/bim/module/web/ui.py | 5 +++ src/blenderbim/blenderbim/tool/web.py | 17 +++----- 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/blenderbim/blenderbim/bim/module/web/data.py diff --git a/src/blenderbim/blenderbim/bim/handler.py b/src/blenderbim/blenderbim/bim/handler.py index 4d5f0c6a12..7fd6e0c183 100644 --- a/src/blenderbim/blenderbim/bim/handler.py +++ b/src/blenderbim/blenderbim/bim/handler.py @@ -191,6 +191,8 @@ def refresh_ui_data(): tool.Ifc.get().clear_cache() bpy.context.scene.DocProperties.should_draw_decorations = bpy.context.scene.DocProperties.should_draw_decorations + if bpy.context.scene.WebProperties.is_connected: + tool.Web.send_webui_data() @persistent diff --git a/src/blenderbim/blenderbim/bim/module/web/data.py b/src/blenderbim/blenderbim/bim/module/web/data.py new file mode 100644 index 0000000000..c13920e47a --- /dev/null +++ b/src/blenderbim/blenderbim/bim/module/web/data.py @@ -0,0 +1,40 @@ +# BlenderBIM Add-on - OpenBIM Blender Add-on +# Copyright (C) 2021 Dion Moult +# +# This file is part of BlenderBIM Add-on. +# +# BlenderBIM Add-on is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# BlenderBIM Add-on is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with BlenderBIM Add-on. If not, see . + +import bpy +import blenderbim.tool as tool + + +def refresh(): + WebData.is_loaded = False + + +class WebData: + data = {} + is_loaded = False + + @classmethod + def load(cls): + cls.data = { + "IFC_File": cls.ifc_file_name(), + } + cls.is_loaded = True + + @classmethod + def ifc_file_name(cls): + return bpy.context.scene.BIMProperties.ifc_file diff --git a/src/blenderbim/blenderbim/bim/module/web/ui.py b/src/blenderbim/blenderbim/bim/module/web/ui.py index 8693cfeaca..f1fda8a1bd 100644 --- a/src/blenderbim/blenderbim/bim/module/web/ui.py +++ b/src/blenderbim/blenderbim/bim/module/web/ui.py @@ -17,6 +17,7 @@ # along with BlenderBIM Add-on. If not, see . from bpy.types import Panel +from blenderbim.bim.module.web.data import WebData class BIM_PT_webui(Panel): @@ -29,6 +30,10 @@ class BIM_PT_webui(Panel): bl_parent_id = "BIM_PT_tab_collaboration" def draw(self, context): + + if not WebData.is_loaded: + WebData.load() + layout = self.layout scene = context.scene diff --git a/src/blenderbim/blenderbim/tool/web.py b/src/blenderbim/blenderbim/tool/web.py index 87b975664a..42d4d43b45 100644 --- a/src/blenderbim/blenderbim/tool/web.py +++ b/src/blenderbim/blenderbim/tool/web.py @@ -17,9 +17,8 @@ # along with BlenderBIM Add-on. If not, see . import bpy -import json +from blenderbim.bim.module.web.data import WebData import blenderbim.core.tool -from typing import Union, Literal import socket import os import subprocess @@ -27,8 +26,6 @@ import webbrowser import asyncio import socketio import threading -import json - sio = None ws_process = None @@ -79,6 +76,7 @@ class Web(blenderbim.core.tool.Web): ws_url = f"ws://localhost:{port}/blender" ws_thread.run_coro(cls.sio_connect(ws_url)) cls.set_is_connected(True) + cls.send_webui_data() @classmethod def disconnect_websocket_server(cls): @@ -110,14 +108,11 @@ class Web(blenderbim.core.tool.Web): @classmethod def send_webui_data(cls): global ws_thread - data = cls.get_webui_data() - ws_thread.run_coro(cls.sio_send(data)) - @classmethod - def get_webui_data(cls): - data = {} - data["ifc_file"] = bpy.context.scene.BIMProperties.ifc_file - return json.dumps(data) + if not WebData.is_loaded: + WebData.load() + + ws_thread.run_coro(cls.sio_send(WebData.data)) @classmethod def open_web_browser(cls, port):