Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Integrate ansys visualization tool #846

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/846.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Feat: Integrate ansys visualization tool
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ doc = [
"sphinxemoji==0.3.1",
]
viz = [
"pyvista>=0.39.1",
"ansys-tools-visualization-interface>=0.2.6",
"usd-core==24.8",
]

Expand Down
31 changes: 19 additions & 12 deletions src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
from ansys.mechanical.core.embedding.warnings import connect_warnings, disconnect_warnings

try:
import pyvista # noqa: F401
import ansys.tools.visualization_interface # noqa: F401

HAS_PYVISTA = True
HAS_ANSYS_VIZ = True

Check warning on line 39 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L39

Added line #L39 was not covered by tests
"""Whether or not PyVista exists."""
except:

HAS_PYVISTA = False
HAS_ANSYS_VIZ = False


def _get_default_addin_configuration() -> AddinConfiguration:
Expand Down Expand Up @@ -229,13 +229,9 @@
rets = None
return self.script_engine.ExecuteCode(script, SCRIPT_SCOPE, light_mode, args, rets)

def plot(self) -> None:
"""Visualize the model in 3d.

Requires installation using the viz option. E.g.
pip install ansys-mechanical-core[viz]
"""
if not HAS_PYVISTA:
def plotter(self) -> None:
"""Reuturns ansys.tools.visualization_interface Plotter object."""
dipinknair marked this conversation as resolved.
Show resolved Hide resolved
if not HAS_ANSYS_VIZ:

Check warning on line 234 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L234

Added line #L234 was not covered by tests
warnings.warn(
"Installation of viz option required! Use pip install ansys-mechanical-core[viz]"
)
Expand All @@ -245,9 +241,20 @@
warnings.warn("Plotting is only supported with version 2024R2 and later!")
return

from ansys.mechanical.core.embedding.viz.pyvista_plotter import plot_model
# TODO Check if anything loaded inside app or else show warning and return
dipinknair marked this conversation as resolved.
Show resolved Hide resolved

from ansys.mechanical.core.embedding.viz.embedding_plotter import to_plotter

Check warning on line 246 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L246

Added line #L246 was not covered by tests

plot_model(self)
return to_plotter(self)

Check warning on line 248 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L248

Added line #L248 was not covered by tests

def plot(self) -> None:
"""Visualize the model in 3d.

Requires installation using the viz option. E.g.
pip install ansys-mechanical-core[viz]
"""
_plotter = self.plotter()
return _plotter.show()

Check warning on line 257 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L256-L257

Added lines #L256 - L257 were not covered by tests
dipinknair marked this conversation as resolved.
Show resolved Hide resolved

@property
def poster(self) -> Poster:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import Ansys # isort: skip

from ansys.tools.visualization_interface import Plotter
import numpy as np
import pyvista as pv

Expand Down Expand Up @@ -68,9 +69,9 @@ def _get_nodes_and_coords(node: "Ansys.Mechanical.Scenegraph.Node"):
return None, None


def to_pyvista_plotter(app: "ansys.mechanical.core.embedding.App"):
def to_plotter(app: "ansys.mechanical.core.embedding.App"):
"""Convert the app's geometry to a pyvista plotter instance."""
dipinknair marked this conversation as resolved.
Show resolved Hide resolved
plotter = pv.Plotter()
plotter = Plotter()
for body in app.DataModel.GetObjectsByType(
Ansys.Mechanical.DataModel.Enums.DataModelObjectCategory.Body
):
Expand All @@ -81,11 +82,5 @@ def to_pyvista_plotter(app: "ansys.mechanical.core.embedding.App"):
pv_transform = _transform_to_pyvista(scenegraph_node.Transform)
polydata = pv.PolyData(np_coordinates, np_indices).transform(pv_transform)
color = pv.Color(bgr_to_rgb_tuple(body.Color))
plotter.add_mesh(polydata, color=color, smooth_shading=True)
plotter.plot(polydata, color=color, smooth_shading=True)
dipinknair marked this conversation as resolved.
Show resolved Hide resolved
return plotter


def plot_model(app: "ansys.mechanical.core.embedding.App"):
"""Plot the model."""
plotter = to_pyvista_plotter(app)
plotter.show()
Loading