Debian Codegraph @

examples/02-plot/surface_point_picking.py

raw
1 """
2 .. _surface_point_picking_example:
3
4 Picking a Point on the Surface of a Mesh
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 This example demonstrates how to pick meshes using
7 :func:`enable_surface_point_picking() <pyvista.Plotter.enable_surface_point_picking>`.
8
9 This allows you to pick points on the surface of a mesh.
10
11 """
12
13 import pyvista as pv
14
15 # sphinx_gallery_start_ignore
16 # picking does not work on interactive examples
17 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
18 # sphinx_gallery_end_ignore
19
20 # %%
21 # Create a mesh and enable picking using the default settings.
22
23 cube = pv.Cube()
24
25 pl = pv.Plotter()
26 pl.add_mesh(cube, show_edges=True)
27 pl.enable_surface_point_picking()
28 pl.show()
29
30
31 # %%
32 # Enable a callback that creates a cube at the right-clicked point and add a
33 # label at the point as well it.
34
35
36 def callback(point):
37 """Create a cube and a label at the click point."""
38 mesh = pv.Cube(center=point, x_length=0.05, y_length=0.05, z_length=0.05)
39 pl.add_mesh(mesh, style='wireframe', color='r')
40 pl.add_point_labels(point, [f'{point[0]:.2f}, {point[1]:.2f}, {point[2]:.2f}'])
41
42
43 pl = pv.Plotter()
44 pl.add_mesh(cube, show_edges=True)
45 pl.enable_surface_point_picking(callback=callback, show_point=False)
46 pl.show()
47 # %%
48 # .. tags:: plot