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+
123"""Plotly backend interface for visualization."""
2- from ansys . tools . visualization_interface . backends . _base import BaseBackend
3- from ansys . tools . visualization_interface . types . mesh_object_plot import MeshObjectPlot
24+ from typing import Any , Union
25+
426import plotly .graph_objects as go
527from pyvista import PolyData
6- from typing import Union , Iterable , Any
28+
29+ from ansys .tools .visualization_interface .backends ._base import BaseBackend
30+ from ansys .tools .visualization_interface .types .mesh_object_plot import MeshObjectPlot
731
832
933class PlotlyBackend (BaseBackend ):
@@ -16,14 +40,14 @@ def _pv_to_mesh3d(self, pv_mesh: PolyData) -> go.Mesh3d:
1640 """Convert a PyVista PolyData mesh to Plotly Mesh3d format."""
1741 points = pv_mesh .points
1842 x , y , z = points [:, 0 ], points [:, 1 ], points [:, 2 ]
19-
43+
2044 # Convert mesh to triangular mesh if needed, since Plotly only supports triangular faces
2145 triangulated_mesh = pv_mesh .triangulate ()
22-
46+
2347 # Extract triangular faces
2448 faces = triangulated_mesh .faces .reshape ((- 1 , 4 )) # Now we know all faces are triangular (3 vertices + count)
2549 i , j , k = faces [:, 1 ], faces [:, 2 ], faces [:, 3 ]
26-
50+
2751 return go .Mesh3d (x = x , y = y , z = z , i = i , j = j , k = k )
2852 @property
2953 def layout (self ) -> Any :
@@ -39,8 +63,8 @@ def plot_iter(self, plotting_list):
3963 """Plot multiple objects using Plotly."""
4064 for item in plotting_list :
4165 self .plot (item )
42-
43-
66+
67+
4468 def plot (self , plottable_object : Union [PolyData , MeshObjectPlot , go .Mesh3d ], ** plotting_options ):
4569 """Plot a single object using Plotly."""
4670 if isinstance (plottable_object , PolyData ):
@@ -58,19 +82,19 @@ def plot(self, plottable_object: Union[PolyData, MeshObjectPlot, go.Mesh3d], **p
5882 except Exception :
5983 raise TypeError ("Unsupported plottable_object type for PlotlyInterface." )
6084
61- def show (self ,
85+ def show (self ,
6286 plottable_object = None ,
6387 screenshot : str = None ,
6488 name_filter = None ,
6589 ** kwargs ):
6690 """Render the Plotly scene."""
6791 if plottable_object is not None :
6892 self .plot (plottable_object )
69-
93+
7094 # Only show in browser if no screenshot is being taken
7195 if not screenshot :
7296 self ._fig .show (** kwargs )
73-
97+
7498 if screenshot :
7599 screenshot_str = str (screenshot )
76100 if screenshot_str .endswith ('.html' ):
0 commit comments