1+ # Copyright (C) 2024 - 2025 ANSYS, Inc. and/or its affiliates.
2+ # SPDX-License-Identifier: MIT
3+ #
4+ #
5+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6+ # of this software and associated documentation files (the "Software"), to deal
7+ # in the Software without restriction, including without limitation the rights
8+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+ # copies of the Software, and to permit persons to whom the Software is
10+ # furnished to do so, subject to the following conditions:
11+ #
12+ # The above copyright notice and this permission notice shall be included in all
13+ # copies or substantial portions of the Software.
14+ #
15+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+ # SOFTWARE.
22+ """
23+ .. _ref_plain_usage_plotly:
24+
25+ =================================
26+ Plain usage of the plotly backend
27+ =================================
28+
29+ This example shows the plain usage of the Plotly backend in the Visualization Interface Tool to plot different objects,
30+ including PyVista meshes, custom objects, and Plotly-specific objects.
31+ """
32+
33+ from ansys .tools .visualization_interface .backends .plotly .plotly_interface import PlotlyBackend
34+ from ansys .tools .visualization_interface .types .mesh_object_plot import MeshObjectPlot
35+ from ansys .tools .visualization_interface import Plotter
36+ import pyvista as pv
37+ from plotly .graph_objects import Mesh3d
38+
39+
40+ # Create a plotter with the Plotly backend
41+ pl = Plotter (backend = PlotlyBackend ())
42+
43+ # Create a PyVista mesh
44+ mesh = pv .Sphere ()
45+
46+ # Plot the mesh
47+ pl .plot (mesh )
48+
49+
50+ # Create a PyVista MultiBlock
51+ multi_block = pv .MultiBlock ()
52+ multi_block .append (pv .Sphere (center = (- 1 , - 1 , 0 )))
53+ multi_block .append (pv .Cube (center = (- 1 , 1 , 0 )))
54+
55+ # Plot the MultiBlock
56+ pl .plot (multi_block )
57+
58+ #####################
59+ # Display the plotter
60+ #
61+ # code-block:: python
62+ #
63+ # pl.show()
64+
65+ # Now create a custom object
66+ class CustomObject :
67+ def __init__ (self ):
68+ self .name = "CustomObject"
69+ self .mesh = pv .Cube (center = (1 , 1 , 0 ))
70+
71+ def get_mesh (self ):
72+ return self .mesh
73+
74+ def name (self ):
75+ return self .name
76+
77+
78+ # Create a custom object
79+ custom_cube = CustomObject ()
80+ custom_cube .name = "CustomCube"
81+
82+ # Create a MeshObjectPlot instance
83+ mesh_object_cube = MeshObjectPlot (custom_cube , custom_cube .get_mesh ())
84+
85+ # Plot the custom mesh object
86+ pl .plot (mesh_object_cube )
87+
88+ ###########################
89+ # Display the plotter again
90+ # =========================
91+ # Since Plotly is a web-based visualization, we can show the plot again to include the new object.
92+ #
93+ # code-block:: python
94+ #
95+ # pl.show()
96+
97+ # Add a Plotly Mesh3d object directly
98+ custom_mesh3d = Mesh3d (
99+ x = [0 , 1 , 2 ],
100+ y = [0 , 1 , 0 ],
101+ z = [0 , 0 , 1 ],
102+ i = [0 ],
103+ j = [1 ],
104+ k = [2 ],
105+ color = 'lightblue' ,
106+ opacity = 0.50
107+ )
108+ pl .plot (custom_mesh3d )
109+
110+ # Show other plotly objects like Scatter3d
111+ from plotly .graph_objects import Scatter3d
112+
113+ scatter = Scatter3d (
114+ x = [0 , 1 , 2 ],
115+ y = [0 , 1 , 0 ],
116+ z = [0 , 0 , 1 ],
117+ mode = 'markers' ,
118+ marker = dict (size = 5 , color = 'red' )
119+ )
120+ pl .plot (scatter )
121+
122+
123+
124+ ###########################
125+ # Display the plotter again
126+ # =========================
127+ #
128+ # code-block:: python
129+ #
130+ # pl.show()
0 commit comments