Skip to content

Commit 2ad0442

Browse files
Feat: Add tips section (#217)
Co-authored-by: Kerry McAdams <[email protected]>
1 parent 90e48c3 commit 2ad0442

17 files changed

+215
-10
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ cython_debug/
162162
PyMAPDL_Dev
163163

164164
# Results generated during doc build and example run locally
165-
examples/technology_showcase/out
166-
examples/basic/out
165+
examples/02_technology_showcase/out
166+
examples/01_basic/out
167+
examples/00_tips/out
167168
/out
168169
doc/source/sg_execution_times.rst
169170

examples/00_tips/readme.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Tips
2+
====
3+
4+
This section showcases some of the useful functions that can be used
5+
in PyMechanical embedding workflow.

examples/00_tips/tips_01.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
""" .. _ref_tips_01:
2+
3+
3D visualization
4+
----------------
5+
6+
Visualize 3D imported geometry
7+
"""
8+
9+
# %%
10+
# Import necessary libraries
11+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
13+
14+
import ansys.mechanical.core as mech
15+
from ansys.mechanical.core.examples import delete_downloads, download_file
16+
17+
# %%
18+
# Embed mechanical and set global variables
19+
20+
app = mech.App(version=242)
21+
app.update_globals(globals())
22+
print(app)
23+
24+
25+
# %%
26+
# Download and import geometry
27+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
# Download geometry
29+
30+
geometry_path = download_file("Valve.pmdb", "pymechanical", "embedding")
31+
32+
# %%
33+
# Import geometry
34+
35+
geometry_import = Model.GeometryImportGroup.AddGeometryImport()
36+
geometry_import.Import(geometry_path)
37+
38+
# %%
39+
# Visualize in 3D
40+
# ~~~~~~~~~~~~~~~
41+
42+
app.plot()
43+
44+
# %%
45+
# .. note::
46+
# This visualization is currently available only for geometry and on version 24R2 or later
47+
48+
# %%
49+
# Cleanup
50+
# ~~~~~~~
51+
52+
delete_downloads()
53+
app.new()

examples/00_tips/tips_02.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
""" .. _ref_tips_02:
2+
3+
Export image
4+
------------
5+
6+
Export image and display
7+
"""
8+
9+
# %%
10+
# Import necessary libraries
11+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
13+
import os
14+
15+
import ansys.mechanical.core as mech
16+
from ansys.mechanical.core.examples import delete_downloads, download_file
17+
18+
# %%
19+
# Embed Mechanical and set global variables
20+
21+
app = mech.App(version=242)
22+
app.update_globals(globals())
23+
print(app)
24+
25+
26+
# %%
27+
# Download and import geometry
28+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29+
# Download geometry
30+
31+
geometry_path = download_file("Valve.pmdb", "pymechanical", "embedding")
32+
33+
# %%
34+
# Import geometry
35+
36+
geometry_import = Model.GeometryImportGroup.AddGeometryImport()
37+
geometry_import.Import(geometry_path)
38+
39+
# %%
40+
# Configure graphics for image export
41+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+
43+
# Orientation
44+
Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)
45+
46+
# Export format
47+
image_export_format = GraphicsImageExportFormat.PNG
48+
49+
# Resolution and background
50+
settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
51+
settings_720p.Resolution = GraphicsResolutionType.EnhancedResolution
52+
settings_720p.Background = GraphicsBackgroundType.White
53+
settings_720p.Width = 1280
54+
settings_720p.Height = 720
55+
settings_720p.CurrentGraphicsDisplay = False
56+
57+
# Rotate the geometry if needed
58+
ExtAPI.Graphics.Camera.Rotate(180, CameraAxisType.ScreenY)
59+
60+
61+
# %%
62+
# Custom function for displaying the image
63+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64+
65+
from matplotlib import image as mpimg
66+
from matplotlib import pyplot as plt
67+
68+
# Temporary directory to save the image
69+
cwd = os.path.join(os.getcwd(), "out")
70+
71+
72+
def display_image(image_name):
73+
plt.figure(figsize=(16, 9))
74+
plt.imshow(mpimg.imread(os.path.join(cwd, image_name)))
75+
plt.xticks([])
76+
plt.yticks([])
77+
plt.axis("off")
78+
plt.show()
79+
80+
81+
# %%
82+
# Export and display the image
83+
# ~~~~~~~~~~~~~~~~~~~~~~~~
84+
85+
# Fits the geometry in the viewing area
86+
Graphics.Camera.SetFit()
87+
88+
Graphics.ExportImage(
89+
os.path.join(cwd, "geometry.png"), image_export_format, settings_720p
90+
)
91+
92+
# Display the image using matplotlib
93+
display_image("geometry.png")
94+
95+
# %%
96+
# Cleanup
97+
# ~~~~~~~
98+
99+
delete_downloads()
100+
app.new()

examples/00_tips/tips_03.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
""" .. _ref_tips_03:
2+
3+
Project tree
4+
--------------------
5+
6+
Display the heirarchial Mechanical project structure.
7+
"""
8+
9+
# %%
10+
# Import necessary libraries
11+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
13+
14+
import ansys.mechanical.core as mech
15+
from ansys.mechanical.core.examples import delete_downloads, download_file
16+
17+
# %%
18+
# Embed Mechanical and set global variables
19+
20+
app = mech.App(version=242)
21+
app.update_globals(globals())
22+
print(app)
23+
24+
25+
# %%
26+
# Download the mechdb file
27+
# ~~~~~~~~~~~~~~~~~~~~~~~~
28+
29+
mechdb_path = download_file("graphics_test.mechdb", "pymechanical", "test_files")
30+
31+
# %%
32+
# Load the mechdb file inside Mechanical
33+
34+
app.open(mechdb_path)
35+
36+
# %%
37+
# Display the project tree
38+
# ~~~~~~~~~~~~~~~~~~~~~~~~
39+
40+
app.print_tree()
41+
42+
# %%
43+
# Display the tree only under the first analysis
44+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45+
46+
app.print_tree(Model.Analyses[0])
47+
48+
49+
# %%
50+
# Cleanup
51+
# ~~~~~~~
52+
53+
delete_downloads()
54+
app.new()
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/basic/valve.py examples/01_basic/valve.py

-8
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,6 @@ def display_image(image_name):
127127
)
128128
display_image("boundary_conditions.png")
129129

130-
# %%
131-
# Solve
132-
# ~~~~~
133-
# Solve process settings
134-
135-
config = ExtAPI.Application.SolveConfigurations["My Computer"]
136-
config.SolveProcessSettings.MaxNumberOfCores = 1
137-
config.SolveProcessSettings.DistributeSolution = False
138130

139131
# %%
140132
# Add results

0 commit comments

Comments
 (0)