File: pv_selection.py

package info (click to toggle)
python-trame 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (146 lines) | stat: -rw-r--r-- 4,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Installation requirements:
    pip install trame trame-vuetify trame-vtk
"""

# import asyncio
from pathlib import Path

from paraview import simple

from trame.app import get_server
from trame.assets.local import LocalFileManager
from trame.ui.vuetify import SinglePageLayout
from trame.widgets import paraview, vuetify

# -----------------------------------------------------------------------------
# Trame setup
# -----------------------------------------------------------------------------

server = get_server(client_type="vue2")
state, ctrl = server.state, server.controller

ICON_SIZE = 16

ASSETS = LocalFileManager(__file__)
ASSETS.url("select_surface_point", "icons/pqSurfaceSelectionPoint.svg")
ASSETS.url("select_surface_cell", "icons/pqSurfaceSelectionCell.svg")
ASSETS.url("select_frustrum_points", "icons/pqFrustumSelectionPoint.svg")
ASSETS.url("select_frustrum_cells", "icons/pqFrustumSelectionCell.svg")
# ASSETS.url("select_poly_points", "icons/pqPolygonSelectSurfacePoint.svg")
# ASSETS.url("select_poly_cells", "icons/pqPolygonSelectSurfaceCell.svg")
ASSETS.url("select_block", "icons/pqSelectBlock.svg")
# ASSETS.url("select_hover_point", "icons/pqSurfaceHoveringPoint.svg")
# ASSETS.url("select_hover_cell", "icons/pqSurfaceHoveringCell.svg")

MODE_TO_METHOD = dict(
    select_surface_point=simple.SelectSurfacePoints,
    select_surface_cell=simple.SelectSurfaceCells,
    select_frustrum_points=simple.SelectPointsThrough,
    select_frustrum_cells=simple.SelectCellsThrough,
    select_block=simple.SelectSurfaceBlocks,
)

# -----------------------------------------------------------------------------
# ParaView code
# -----------------------------------------------------------------------------

DATA_FILE_PATH = str(Path(__file__).parent.parent.parent / "data/can.ex2")

dataset = simple.OpenDataFile(DATA_FILE_PATH)
representation = simple.Show(dataset)
view = simple.Render()


def clear_selection():
    simple.ClearSelection()
    ctrl.view_update()


@ctrl.set("on_selection_change")
def on_box_selection(event):
    fn = MODE_TO_METHOD[state.selection_mode]
    min_x, max_x, min_y, max_y = event.get("selection")
    fn(Rectangle=[int(min_x), int(min_y), int(max_x), int(max_y)], Modifier=None)
    ctrl.view_update()
    state.selection_mode = None


@state.change("selection_mode")
def on_mode_change(selection_mode, **kwargs):
    # Use box for selection
    state.box_selection = selection_mode in [
        "select_surface_point",
        "select_surface_cell",
        "select_frustrum_points",
        "select_frustrum_cells",
        "select_block",
    ]

    # Toggle from interactive to selection
    if selection_mode is None:
        view.InteractionMode = "3D"
    else:
        view.InteractionMode = "Selection"

    # Handle hover with live update
    state.send_mouse = selection_mode in [
        "select_block",
        "select_hover_point",
        "select_hover_cell",
    ]
    # if state.send_mouse:
    #     asynchronous.create_task(animate())

    # Disable active mode
    if selection_mode == "-":
        state.selection_mode = None


# async def animate():
#     while state.send_mouse:
#         await asyncio.sleep(0.1)
#         ctrl.view_update()

# -----------------------------------------------------------------------------
# GUI
# -----------------------------------------------------------------------------

with SinglePageLayout(server) as layout:
    layout.icon.click = ctrl.view_reset_camera
    layout.title.set_text("ParaView Selection")

    with layout.toolbar:
        vuetify.VSpacer()
        with vuetify.VBtnToggle(v_model=("selection_mode", None)):
            for name, value in ASSETS.assets.items():
                with vuetify.VBtn(value=name, small=True):
                    vuetify.VImg(
                        src=value,
                        contain=True,
                        height=ICON_SIZE,
                        width=ICON_SIZE,
                    )
            with vuetify.VBtn(small=True, click=clear_selection, value="-"):
                vuetify.VIcon("mdi-trash-can-outline", small=True)

    with layout.content:
        with vuetify.VContainer(fluid=True, classes="pa-0 fill-height"):
            html_view = paraview.VtkRemoteView(
                view,
                ref="view",
                interactive_ratio=1,
                enable_picking=("send_mouse", False),
                box_selection=("box_selection", False),
                box_selection_change=(ctrl.on_selection_change, "[$event]"),
            )
            ctrl.view_reset_camera = html_view.reset_camera
            ctrl.view_update = html_view.update


def main():
    server.start()


if __name__ == "__main__":
    main()