mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-28 15:53:00 +00:00
moved web ui logic to web tool/core
This commit is contained in:
@@ -1,6 +1,2 @@
|
|||||||
def show_scene_elements(spatial):
|
def show_scene_elements(spatial):
|
||||||
spatial.show_scene_objects()
|
spatial.show_scene_objects()
|
||||||
|
|
||||||
|
|
||||||
def open_web_ui(search):
|
|
||||||
search.open_web_ui()
|
|
||||||
|
|||||||
@@ -1015,3 +1015,8 @@ class Voider:
|
|||||||
def set_void_display(cls, opening_obj): pass
|
def set_void_display(cls, opening_obj): pass
|
||||||
def unvoid(cls, opening_obj): pass
|
def unvoid(cls, opening_obj): pass
|
||||||
def void(cls, opening_obj, building_obj): pass
|
def void(cls, opening_obj, building_obj): pass
|
||||||
|
|
||||||
|
|
||||||
|
@interface
|
||||||
|
class Web:
|
||||||
|
pass
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
def open_web_ui(web):
|
||||||
|
web.open_web_ui()
|
||||||
@@ -13,11 +13,6 @@ import threading
|
|||||||
import websockets
|
import websockets
|
||||||
import json
|
import json
|
||||||
|
|
||||||
CONNECTION_CHECK_DELAY = 10
|
|
||||||
connected_sockets = set()
|
|
||||||
websocket_server: websockets.WebSocketServer
|
|
||||||
ws_thread_loop = asyncio.new_event_loop()
|
|
||||||
|
|
||||||
|
|
||||||
class Search(blenderbim.core.tool.Search):
|
class Search(blenderbim.core.tool.Search):
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -124,63 +119,6 @@ class Search(blenderbim.core.tool.Search):
|
|||||||
return value
|
return value
|
||||||
return '"' + value.replace('"', '\\"') + '"'
|
return '"' + value.replace('"', '\\"') + '"'
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_webui_data(cls):
|
|
||||||
data = {}
|
|
||||||
data["ifc_file"] = bpy.context.scene.BIMProperties.ifc_file
|
|
||||||
return json.dumps(data)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def broadcast(cls, message):
|
|
||||||
for sock in connected_sockets:
|
|
||||||
await sock.send(message)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def ws_server_handler(cls, websocket):
|
|
||||||
connected_sockets.add(websocket)
|
|
||||||
intial_data = cls.get_webui_data()
|
|
||||||
await cls.broadcast(intial_data)
|
|
||||||
try:
|
|
||||||
async for message in websocket:
|
|
||||||
# TODO: handle incoming messages
|
|
||||||
continue
|
|
||||||
finally:
|
|
||||||
connected_sockets.remove(websocket)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def run_ws_server(cls):
|
|
||||||
websocket_server = await websockets.serve(cls.ws_server_handler, "localhost", 8765)
|
|
||||||
print("Web-UI WebSocket server started.")
|
|
||||||
|
|
||||||
# Check if a client is connected every 10 seconds
|
|
||||||
async def check_connections():
|
|
||||||
while True:
|
|
||||||
await asyncio.sleep(CONNECTION_CHECK_DELAY)
|
|
||||||
if not connected_sockets:
|
|
||||||
print("No Web-UI connected, shutting down the WebSocket server")
|
|
||||||
websocket_server.close()
|
|
||||||
break
|
|
||||||
|
|
||||||
await check_connections()
|
|
||||||
await websocket_server.wait_closed()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def open_web_ui(cls):
|
|
||||||
def set_thread_loop():
|
|
||||||
asyncio.set_event_loop(ws_thread_loop)
|
|
||||||
asyncio.run(cls.run_ws_server())
|
|
||||||
|
|
||||||
wserver_thread = threading.Thread(target=set_thread_loop)
|
|
||||||
wserver_thread.daemon = True
|
|
||||||
wserver_thread.start()
|
|
||||||
|
|
||||||
def open_browser():
|
|
||||||
webbrowser.open("file://" + os.path.join(bpy.context.scene.BIMProperties.data_dir, "webui", "index.html"))
|
|
||||||
|
|
||||||
# Delay for 2 seconds before openning browser
|
|
||||||
timer = threading.Timer(2.0, open_browser)
|
|
||||||
timer.start()
|
|
||||||
|
|
||||||
|
|
||||||
class ImportFilterQueryTransformer(lark.Transformer):
|
class ImportFilterQueryTransformer(lark.Transformer):
|
||||||
def __init__(self, filter_groups):
|
def __init__(self, filter_groups):
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import bpy
|
||||||
|
import json
|
||||||
|
import blenderbim.core.tool
|
||||||
|
from typing import Union, Literal
|
||||||
|
import os
|
||||||
|
import webbrowser
|
||||||
|
import asyncio
|
||||||
|
import threading
|
||||||
|
import websockets
|
||||||
|
import json
|
||||||
|
|
||||||
|
CONNECTION_CHECK_DELAY = 10
|
||||||
|
connected_sockets = set()
|
||||||
|
websocket_server: websockets.WebSocketServer
|
||||||
|
ws_thread_loop = asyncio.new_event_loop()
|
||||||
|
|
||||||
|
class Web(blenderbim.core.tool.Web):
|
||||||
|
@classmethod
|
||||||
|
def get_webui_data(cls):
|
||||||
|
data = {}
|
||||||
|
data["ifc_file"] = bpy.context.scene.BIMProperties.ifc_file
|
||||||
|
return json.dumps(data)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def broadcast(cls, message):
|
||||||
|
for sock in connected_sockets:
|
||||||
|
await sock.send(message)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def ws_server_handler(cls, websocket):
|
||||||
|
connected_sockets.add(websocket)
|
||||||
|
intial_data = cls.get_webui_data()
|
||||||
|
await cls.broadcast(intial_data)
|
||||||
|
try:
|
||||||
|
async for message in websocket:
|
||||||
|
# TODO: handle incoming messages
|
||||||
|
continue
|
||||||
|
finally:
|
||||||
|
connected_sockets.remove(websocket)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def run_ws_server(cls):
|
||||||
|
websocket_server = await websockets.serve(cls.ws_server_handler, "localhost", 8765)
|
||||||
|
print("Web-UI WebSocket server started.")
|
||||||
|
|
||||||
|
# Check if a client is connected every 10 seconds
|
||||||
|
async def check_connections():
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(CONNECTION_CHECK_DELAY)
|
||||||
|
if not connected_sockets:
|
||||||
|
print("No Web-UI connected, shutting down the WebSocket server")
|
||||||
|
websocket_server.close()
|
||||||
|
break
|
||||||
|
|
||||||
|
await check_connections()
|
||||||
|
await websocket_server.wait_closed()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def open_web_ui(cls):
|
||||||
|
def set_thread_loop():
|
||||||
|
asyncio.set_event_loop(ws_thread_loop)
|
||||||
|
asyncio.run(cls.run_ws_server())
|
||||||
|
|
||||||
|
wserver_thread = threading.Thread(target=set_thread_loop)
|
||||||
|
wserver_thread.daemon = True
|
||||||
|
wserver_thread.start()
|
||||||
|
|
||||||
|
def open_browser():
|
||||||
|
webbrowser.open("file://" + os.path.join(bpy.context.scene.BIMProperties.data_dir, "webui", "index.html"))
|
||||||
|
|
||||||
|
# Delay for 2 seconds before openning browser
|
||||||
|
timer = threading.Timer(2.0, open_browser)
|
||||||
|
timer.start()
|
||||||
Reference in New Issue
Block a user