11from logging import Logger
2+ from typing import Callable , Protocol
23
34from trame .app .core import Server
45from trame .decorators import TrameApp , trigger
56from vtk import vtkObject
67
7- from ansys .visor .viewer .app .trame .scene_trigger_app import SceneMutationApi , SceneTriggerApp
8+ from ansys .visor .viewer .app .trame .scene_trigger_app import (
9+ SceneMutationApi ,
10+ SceneTriggerApp ,
11+ parse_payload ,
12+ )
813from ansys .visor .viewer .config import settings
914from ansys .visor .viewer .core .visor_enums import RenderingMode
1015from ansys .visor .viewer .core .visor_logging import VisorDefaultLogger
16+ from ansys .visor .viewer .models .runtime .picking .geometry_pick import GeometryPick
17+ from ansys .visor .viewer .models .runtime .requests .geometry_pick_payloads import PickGeometryInViewPayload
18+ from ansys .visor .viewer .models .runtime .requests .pixel_scale_payload import SetPixelScalePayload
19+ from ansys .visor .viewer .vtk .picking .part_cell_hit import PickView
1120
1221logger = VisorDefaultLogger (__name__ )
1322
1423# Disable VTK warning display
1524vtkObject .GlobalWarningDisplayOff ()
1625
1726
27+ class LocalSceneMutationApi (SceneMutationApi , Protocol ):
28+ """The scene surface LocalApp's triggers call: the shared one plus the client-only pixel scale."""
29+
30+ def set_pixel_scale (self , pixel_scale : float ) -> None : ...
31+
32+
1833@TrameApp ()
1934class LocalApp (SceneTriggerApp ):
2035 """
2136 LocalApp serves the scene triggers shared by every rendering mode, plus
22- the local-only triggers for sub-geometry picking and client performance
23- reporting, for client-side rendering via VTK-WASM in the browser.
37+ the local-only triggers for sub-geometry picking through the client's
38+ view and client performance reporting, for client-side rendering via
39+ VTK-WASM in the browser.
2440
2541 Attributes:
2642 server (Server): the Trame server instance
2743 Methods:
28- pick_geometry : picks the geometry for rendering
44+ pick_geometry_in_view : picks the geometry feature under a pixel of the client's window
2945 perf_report_wasm: reports the performance of the wasm update cycle
3046 perf_report_server_update: reports the performance of the server update cycle
47+ set_pixel_scale: sets the device pixel ratio the geometry highlight is drawn at
3148 set_only_cookie: sets a cookie on the server (note: Trame server only allows a single cookie header)
3249 """
3350 def __init__ (
@@ -36,11 +53,11 @@ def __init__(
3653 get_scene_details_json : callable ,
3754 standalone : bool = True ,
3855 trame_logger : Logger | None = None ,
39- pick_geometry = None ,
40- scene_mutation_api : SceneMutationApi | None = None ,
56+ scene_mutation_api : LocalSceneMutationApi | None = None ,
57+ * ,
58+ pick_geometry_in_view : Callable [[PickView , int , int , str ], GeometryPick ],
4159 ):
42- # Callable for sub-geometry picking (optional), local-only.
43- self ._pick_geometry = pick_geometry
60+ self ._pick_geometry_in_view = pick_geometry_in_view
4461 super ().__init__ (
4562 server = server ,
4663 get_scene_details_json = get_scene_details_json ,
@@ -54,12 +71,28 @@ def rendering_mode(self) -> RenderingMode:
5471 """This app serves client-side rendering via VTK-WASM in the browser."""
5572 return RenderingMode .CLIENT
5673
57- @trigger ("pick_geometry" )
58- def pick_geometry (self , actor_wasm_id , cell_id , mode : str , world_x : float , world_y : float , world_z : float ):
59- """Frontend → Backend: perform sub-geometry picking given a WASM actor/cell ID and world-space point."""
60- if self ._pick_geometry is None :
61- return {"found" : False }
62- return self ._pick_geometry (actor_wasm_id , cell_id , mode , world_x , world_y , world_z )
74+ @trigger ("pick_geometry_in_view" )
75+ @parse_payload (PickGeometryInViewPayload )
76+ def pick_geometry_in_view (self , payload ) -> dict :
77+ """Frontend -> Backend: the geometry feature under a pixel of the client's window, dumped by alias.
78+
79+ Picked through the client's camera and window size; highlights nothing.
80+ """
81+ view = PickView (camera_state = payload .camera , width = payload .width , height = payload .height )
82+ return self ._pick_geometry_in_view (view , payload .x , payload .y , payload .mode ).model_dump (by_alias = True )
83+
84+ @trigger ("set_pixel_scale" )
85+ @parse_payload (SetPixelScalePayload )
86+ def set_pixel_scale (self , payload ) -> None :
87+ """Frontend -> Backend: the client reports its device pixel ratio for the geometry highlight.
88+
89+ Server mode learns the same ratio from the remote-canvas size report,
90+ so only client mode registers this.
91+ """
92+ api = self ._mutation_api ("set_pixel_scale" , payload )
93+ if api is None :
94+ return
95+ api .set_pixel_scale (payload .scale )
6396
6497 @trigger ("perf_report_wasm" )
6598 def perf_report_wasm (self , payload : dict ):
0 commit comments