Skip to content

Commit

Permalink
Feat: Integrate ansys visualization tool (#846)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <[email protected]>
Co-authored-by: Alex Fernandez <[email protected]>
  • Loading branch information
3 people authored Aug 2, 2024
1 parent 9016a97 commit 02a6566
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
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
18 changes: 16 additions & 2 deletions 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 Expand Up @@ -255,4 +255,18 @@ passenv = *
extras = doc
commands =
sphinx-build -d "{toxworkdir}/doc_doctree" doc/source "{toxinidir}/doc/_build/html" --color -vW -bhtml
"""
"""
[[tool.towncrier.type]]
directory = "documentation"
name = "Documentation"
showcontent = true

[[tool.towncrier.type]]
directory = "maintenance"
name = "Maintenance"
showcontent = true

[[tool.towncrier.type]]
directory = "test"
name = "Test"
showcontent = true
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
"""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 @@ def execute_script(self, script: str) -> typing.Any:
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:
"""Returns ``ansys.tools.visualization_interface.Plotter`` object."""
if not HAS_ANSYS_VIZ:
warnings.warn(
"Installation of viz option required! Use pip install ansys-mechanical-core[viz]"
)
Expand All @@ -245,9 +241,20 @@ def plot(self) -> None:
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

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

plot_model(self)
return to_plotter(self)

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()

@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"):
"""Convert the app's geometry to a pyvista plotter instance."""
plotter = pv.Plotter()
def to_plotter(app: "ansys.mechanical.core.embedding.App"):
"""Convert the app's geometry to an ``ansys.tools.visualization_interface.Plotter`` instance."""
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)
return plotter


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

0 comments on commit 02a6566

Please sign in to comment.