Skip to content

Commit 24dd359

Browse files
committed
refactor examples
1 parent 227d017 commit 24dd359

25 files changed

+7671
-333
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ repos:
6363
tests/scripts/run_python_error.py |
6464
tests/scripts/run_python_success.py |
6565
tests/scripts/log_message.py |
66-
examples/embedding_n_remote/embedding_remote.py
66+
examples/.*$
6767
)$
6868
6969
- repo: https://github.com/pre-commit/pre-commit-hooks

doc/source/conf.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import warnings
1414

1515
from ansys_sphinx_theme import ansys_favicon, get_version_match
16+
from pyvista.plotting.utilities.sphinx_gallery import DynamicScraper
1617
from sphinx_gallery.sorting import FileNameSortKey
1718

1819
import ansys.mechanical.core as pymechanical
@@ -22,7 +23,7 @@
2223
pymechanical.BUILDING_GALLERY = True
2324

2425
# Whether or not to build the cheatsheet
25-
BUILD_CHEATSHEET = True if os.environ.get("BUILD_EXAMPLES", "true") == "true" else False
26+
BUILD_CHEATSHEET = False
2627

2728
# suppress annoying matplotlib bug
2829
warnings.filterwarnings(
@@ -60,6 +61,8 @@
6061
"sphinx_autodoc_typehints",
6162
"sphinx_copybutton",
6263
"sphinx_design",
64+
"pyvista.ext.plot_directive",
65+
"pyvista.ext.viewer_directive",
6366
]
6467

6568
if pymechanical.BUILDING_GALLERY:
@@ -175,8 +178,7 @@
175178
# path to your examples scripts
176179
"examples_dirs": ["../../examples/"],
177180
# path where to save gallery generated examples
178-
"gallery_dirs": ["examples/gallery_examples"],
179-
# Pattern to search for example files
181+
"gallery_dirs": ["examples/gallery_examples"], # Pattern to search for example files
180182
"filename_pattern": r"\.py",
181183
# Remove the "Download all examples" button from the top level gallery
182184
"download_all_examples": False,
@@ -186,7 +188,7 @@
186188
"backreferences_dir": None,
187189
# Modules for which function level galleries are created. In
188190
"doc_module": "ansys-mechanical-core",
189-
"image_scrapers": ("matplotlib"),
191+
"image_scrapers": (DynamicScraper(), "matplotlib"),
190192
"ignore_pattern": "flycheck*",
191193
"thumbnail_size": (350, 350),
192194
}
@@ -229,7 +231,6 @@
229231
"sidebar_pages": ["changelog", "index"],
230232
},
231233
"ansys_sphinx_theme_autoapi": {"project": project, "templates": "_templates/autoapi"},
232-
"navigation_depth": 10,
233234
}
234235

235236
if BUILD_CHEATSHEET:
@@ -245,7 +246,6 @@
245246

246247
html_sidebars = {
247248
"changelog": [],
248-
"examples/index": [],
249249
"contributing": [],
250250
}
251251

doc/source/examples/index.rst

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
.. _ref_examples:
22

3+
========
34
Examples
45
========
56

6-
Here are examples using Mechanical with the ``ansys-mechanical-core`` library through
7-
an embedded instance or remote session.
7+
Here is a series of examples using Mechanical with the ``ansys-mechanical-core`` library.
88

9-
.. grid:: 1 2 2 2
9+
.. === EXAMPLES Gallery ===
1010
11-
.. grid-item-card:: Embedding Examples
12-
:padding: 2 2 2 2
13-
:link: https://embedding.examples.mechanical.docs.pyansys.com/examples/index.html
11+
..
12+
We have to include this rather than include it in a tree.
1413
15-
Uses PyMechanical to embed an instance of Mechanical directly within Python as a Python object.
14+
.. include:: gallery_examples/index.rst
15+
:start-line: 2
1616

17-
.. grid-item-card:: Remote Session Examples
18-
:padding: 2 2 2 2
19-
:link: https://examples.mechanical.docs.pyansys.com/examples/index.html
17+
.. === REMOTE SESSIONS EXAMPLES ===
2018
21-
Uses PyMechanical as a client to a remote Mechanical instance.
19+
.. toctree::
20+
:hidden:
21+
:maxdepth: 3
2222

23-
.. grid:: 1
23+
Remote Sessions <https://examples.mechanical.docs.pyansys.com/index.html>
2424

25-
.. grid-item-card:: Embedding Instance & Remote Session Example
26-
:padding: 2 2 2 2
27-
:link: gallery_examples/embedding_n_remote/embedding_remote.html
2825

29-
Demonstrates the same model setup in both an embedded instance and remote session,
30-
as well as examples using a combination of embedded instances and remote sessions.
26+
.. include:: remote_session_examples/index.rst
27+
:start-line: 2
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
. _ref_remote_session_examples:
2+
3+
Remote session examples
4+
------------------------
5+
6+
Remote session examples demonstrate the basic simulation capabilities of Mechanical using remote sessions.
7+
8+
For more information, see `Remote sessions examples <pymechanical_remote_ex_all_>`_.

examples/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
00_utilities/out/
2+
01_basic/out/
3+
02_advanced/out/

examples/00_utilities/readme.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Utilities
2+
---------
3+
4+
This section showcases some of the useful functions that can be used
5+
in PyMechanical embedding workflow.

examples/00_utilities/tips_01.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Copyright (C) 2022 - 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_tips_01:
24+
25+
3D visualization
26+
----------------
27+
localtoc
28+
The following example demonstrates how to visualize imported geometry in 3D.
29+
"""
30+
31+
# %%
32+
# Import the necessary libraries
33+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34+
35+
from ansys.mechanical.core import App
36+
from ansys.mechanical.core.examples import delete_downloads, download_file
37+
38+
# %%
39+
# Initialize the embedded application
40+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41+
42+
app = App(globals=globals())
43+
print(app)
44+
45+
# %%
46+
# Download and import the geometry file
47+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48+
49+
# %%
50+
# Download the geometry file
51+
geometry_path = download_file("Valve.pmdb", "pymechanical", "embedding")
52+
53+
# %%
54+
# Define the model and import the geometry file
55+
model = app.Model
56+
57+
geometry_import = model.GeometryImportGroup.AddGeometryImport()
58+
geometry_import.Import(geometry_path)
59+
60+
# %%
61+
# Visualize the model in 3D
62+
# ~~~~~~~~~~~~~~~~~~~~~~~~~
63+
64+
app.plot()
65+
66+
# %%
67+
# .. note::
68+
# This visualization is currently available only for geometry and on version 24R2 or later
69+
70+
# %%
71+
# Clean up the files and app
72+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
74+
# Delete the downloaded files
75+
delete_downloads()
76+
77+
# Close the app
78+
app.close()

examples/00_utilities/tips_02.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Copyright (C) 2022 - 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_tips_02:
24+
25+
Export image
26+
------------
27+
28+
The following example demonstrates how to export an image of the imported geometry
29+
and display it using matplotlib.
30+
"""
31+
32+
# %%
33+
# Import the necessary libraries
34+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35+
36+
from pathlib import Path
37+
38+
from ansys.mechanical.core import App
39+
from ansys.mechanical.core.examples import delete_downloads, download_file
40+
41+
# %%
42+
# Initialize the embedded application
43+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44+
45+
# Create an instance of the App class
46+
app = App(globals=globals())
47+
48+
# Print the app to ensure it is working
49+
print(app)
50+
51+
# %%
52+
# Download and import the geometry file
53+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54+
55+
# %%
56+
# Download the geometry file
57+
geometry_path = download_file("Valve.pmdb", "pymechanical", "embedding")
58+
59+
# %%
60+
# Import the geometry file
61+
62+
geometry_import = Model.GeometryImportGroup.AddGeometryImport()
63+
geometry_import.Import(geometry_path)
64+
65+
# %%
66+
# Configure graphics for image export
67+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68+
69+
# Set the orientation of the camera
70+
ExtAPI.Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)
71+
72+
# Set the image export format
73+
image_export_format = GraphicsImageExportFormat.PNG
74+
75+
# Configure the export settings for the image
76+
settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
77+
settings_720p.Resolution = GraphicsResolutionType.EnhancedResolution
78+
settings_720p.Background = GraphicsBackgroundType.White
79+
settings_720p.Width = 1280
80+
settings_720p.Height = 720
81+
settings_720p.CurrentGraphicsDisplay = False
82+
83+
# Rotate the geometry on the Y-axis
84+
Graphics.Camera.Rotate(180, CameraAxisType.ScreenY)
85+
86+
# %%
87+
# Create a function to display the image
88+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
from matplotlib import image as mpimg
91+
from matplotlib import pyplot as plt
92+
93+
# Directory to save the image
94+
output_path = Path.cwd() / "out"
95+
96+
97+
def display_image(image_name) -> None:
98+
"""Display the image using matplotlib.
99+
100+
Parameters
101+
----------
102+
image_name : str
103+
The name of the image file to display.
104+
"""
105+
# Create the full path to the image
106+
image_path = output_path / image_name
107+
108+
# Plot the figure and display the image
109+
plt.figure(figsize=(16, 9))
110+
plt.imshow(mpimg.imread(str(image_path)))
111+
plt.xticks([])
112+
plt.yticks([])
113+
plt.axis("off")
114+
plt.show()
115+
116+
117+
# %%
118+
# Export and display the image
119+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120+
121+
# Fit the geometry in the viewing area
122+
Graphics.Camera.SetFit()
123+
124+
# Export the image
125+
geometry_image = output_path / "geometry.png"
126+
Graphics.ExportImage(str(geometry_image), image_export_format, settings_720p)
127+
128+
# Display the image
129+
display_image(geometry_image.name)
130+
131+
# %%
132+
# Clean up the downloaded files and app
133+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134+
135+
# Delete the downloaded files
136+
delete_downloads()
137+
138+
# Close the app
139+
app.close()

0 commit comments

Comments
 (0)