Skip to content

Commit

Permalink
Feat: Add tips section (#217)
Browse files Browse the repository at this point in the history
Co-authored-by: Kerry McAdams <[email protected]>
  • Loading branch information
dipinknair and klmcadams authored Aug 15, 2024
1 parent 90e48c3 commit 2ad0442
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 10 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ cython_debug/
PyMAPDL_Dev

# Results generated during doc build and example run locally
examples/technology_showcase/out
examples/basic/out
examples/02_technology_showcase/out
examples/01_basic/out
examples/00_tips/out
/out
doc/source/sg_execution_times.rst

Expand Down
5 changes: 5 additions & 0 deletions examples/00_tips/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Tips
====

This section showcases some of the useful functions that can be used
in PyMechanical embedding workflow.
53 changes: 53 additions & 0 deletions examples/00_tips/tips_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
""" .. _ref_tips_01:
3D visualization
----------------
Visualize 3D imported geometry
"""

# %%
# Import necessary libraries
# ~~~~~~~~~~~~~~~~~~~~~~~~~~


import ansys.mechanical.core as mech
from ansys.mechanical.core.examples import delete_downloads, download_file

# %%
# Embed mechanical and set global variables

app = mech.App(version=242)
app.update_globals(globals())
print(app)


# %%
# Download and import geometry
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Download geometry

geometry_path = download_file("Valve.pmdb", "pymechanical", "embedding")

# %%
# Import geometry

geometry_import = Model.GeometryImportGroup.AddGeometryImport()
geometry_import.Import(geometry_path)

# %%
# Visualize in 3D
# ~~~~~~~~~~~~~~~

app.plot()

# %%
# .. note::
# This visualization is currently available only for geometry and on version 24R2 or later

# %%
# Cleanup
# ~~~~~~~

delete_downloads()
app.new()
100 changes: 100 additions & 0 deletions examples/00_tips/tips_02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
""" .. _ref_tips_02:
Export image
------------
Export image and display
"""

# %%
# Import necessary libraries
# ~~~~~~~~~~~~~~~~~~~~~~~~~~

import os

import ansys.mechanical.core as mech
from ansys.mechanical.core.examples import delete_downloads, download_file

# %%
# Embed Mechanical and set global variables

app = mech.App(version=242)
app.update_globals(globals())
print(app)


# %%
# Download and import geometry
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Download geometry

geometry_path = download_file("Valve.pmdb", "pymechanical", "embedding")

# %%
# Import geometry

geometry_import = Model.GeometryImportGroup.AddGeometryImport()
geometry_import.Import(geometry_path)

# %%
# Configure graphics for image export
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Orientation
Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)

# Export format
image_export_format = GraphicsImageExportFormat.PNG

# Resolution and background
settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
settings_720p.Resolution = GraphicsResolutionType.EnhancedResolution
settings_720p.Background = GraphicsBackgroundType.White
settings_720p.Width = 1280
settings_720p.Height = 720
settings_720p.CurrentGraphicsDisplay = False

# Rotate the geometry if needed
ExtAPI.Graphics.Camera.Rotate(180, CameraAxisType.ScreenY)


# %%
# Custom function for displaying the image
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

from matplotlib import image as mpimg
from matplotlib import pyplot as plt

# Temporary directory to save the image
cwd = os.path.join(os.getcwd(), "out")


def display_image(image_name):
plt.figure(figsize=(16, 9))
plt.imshow(mpimg.imread(os.path.join(cwd, image_name)))
plt.xticks([])
plt.yticks([])
plt.axis("off")
plt.show()


# %%
# Export and display the image
# ~~~~~~~~~~~~~~~~~~~~~~~~

# Fits the geometry in the viewing area
Graphics.Camera.SetFit()

Graphics.ExportImage(
os.path.join(cwd, "geometry.png"), image_export_format, settings_720p
)

# Display the image using matplotlib
display_image("geometry.png")

# %%
# Cleanup
# ~~~~~~~

delete_downloads()
app.new()
54 changes: 54 additions & 0 deletions examples/00_tips/tips_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
""" .. _ref_tips_03:
Project tree
--------------------
Display the heirarchial Mechanical project structure.
"""

# %%
# Import necessary libraries
# ~~~~~~~~~~~~~~~~~~~~~~~~~~


import ansys.mechanical.core as mech
from ansys.mechanical.core.examples import delete_downloads, download_file

# %%
# Embed Mechanical and set global variables

app = mech.App(version=242)
app.update_globals(globals())
print(app)


# %%
# Download the mechdb file
# ~~~~~~~~~~~~~~~~~~~~~~~~

mechdb_path = download_file("graphics_test.mechdb", "pymechanical", "test_files")

# %%
# Load the mechdb file inside Mechanical

app.open(mechdb_path)

# %%
# Display the project tree
# ~~~~~~~~~~~~~~~~~~~~~~~~

app.print_tree()

# %%
# Display the tree only under the first analysis
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

app.print_tree(Model.Analyses[0])


# %%
# Cleanup
# ~~~~~~~

delete_downloads()
app.new()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 0 additions & 8 deletions examples/basic/valve.py → examples/01_basic/valve.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,6 @@ def display_image(image_name):
)
display_image("boundary_conditions.png")

# %%
# Solve
# ~~~~~
# Solve process settings

config = ExtAPI.Application.SolveConfigurations["My Computer"]
config.SolveProcessSettings.MaxNumberOfCores = 1
config.SolveProcessSettings.DistributeSolution = False

# %%
# Add results
Expand Down
File renamed without changes.

0 comments on commit 2ad0442

Please sign in to comment.