Skip to main content

Prerequisites

This demo requires you to have the following on your local machine:
  • Ansys v2024R1 including: i) Ansys Mechanical ii) Ansys Dynamic Reporting (ADR)
  • Python3 including the following modules: i) ansys-mechanical-core ii) ansys-dynamicreporting-core
  • A Mechanical database file (.mechdb or .mechdat).

User inputs

First, let’s define the user inputs: the path to Mechanical database file and the path to a working directory.

Import Python modules

Next we will import our required Python modules

Define constants and create sub-folders

We will set our Ansys version to 24.1, which corresponds to 2024R1, and then get the Ansys installation directory, We will have 2 sub-folders in our working directory. ‘Exports’ will contain all the report files generated from Mechanical. ‘Nexus’ will the contain the Nexus database that is required by ADR, and needs to initially be empty so that ADR can automatically create the new Nexus database. To start from scratch, we will delete these folders if they already exist. If an existing Nexus folder cannot be deleted, it likely means that the server is still running.
Go to your working directory (defined in User Inputs section) and confirm that you have 2 empty folders: ‘Nexus’ and ‘Exports’.

Start ADR service

We will start a new Ansys Dynamic Reporting (ADR) instance based on a new Nexus database. This may take a minute.
The preceding code takes a minute to run. While it is running, the ‘Nexus’ folder in your working directory should be filling up with some files and folders. Note that you will not be able to delete some of these files and folders while the ADR server is running. The preceding code block should print a URL. Copy/paste the URL in to a web browser and confirm that the ADR server is running. The next step takes a minute to run. While it is running, you can explore the ADR server’s webpage. For example, click Reports > Generate Reports to see a list of available report templates. For now, the only available template is ‘No Template’.

Launch Mechanical and import Mechanical scripting variables

We will launch Mechanical using embedded PyMechanical, import the global variables from Mechanical scripting language to our instance of Python, then finally open the saved Mechanical project. This code make take a minute to run.

Define Mechanical helper functions

Let’s define some reusable functions for creating report items from Mechanical

Define ADR helper functions

Similarly, let’s define some reusable functions for pushing report items to ADR. Let’s also create a function for exporting an ADR report as a PDF.

Create report items and push them to ADR

3D and 2D images

For several objects in the Mechanical tree (geometry, mesh, and results objects), we will create both AVZ and PNG files and push them to ADR with appropriate tags. The tags will be used later to place report items in to report templates.
In your working directory’s “Exports” sub-folder, you should now see several .png and .avz files. You should also be able to view these files on the ADR server by running the following command:
The above command will open your default web browser with URL to create a report with no template and filter. With no template or filter all report items will displayed without any formatting.

Collapsible trees

Let’s also create some collapsible trees. The first will contain material data for each body in Mechanical. The second will contain some simulation details.
If you re-run the command to visual the no template report (or refresh the previous page), you should now see the collapsible trees at the bottom of the page.
You can also return the ADR homepage (use the below command to reprint the URL if needed) and click Query > Report Items > Run Query to see a list of report items.

Create the report templates

Next, we will create report templates to make our reports more legible. We are going to create 2 versions of the report template. The first version will use 3D image files (AVZ) to allow us to pan, zoom, and rotate while viewing the report in a web browser. The second version is very similar, but will use 2D image files (PNG) to create a report that is more suitable to export to .pdf These 2 code blocks were not written by hand. The templates were created in the ADR Template Editor and then exported as Python files. We first created the dynamic version, then made a copy and slightly modified it to create the static version. Report templates do not have to be created using Python. They can be created and modified using a GUI, as mentioned above, and stored in a Nexus database. They can be exported/imported directly from one Nexus database or ADR server to another. However, for this example we wanted to create everything “on the fly” using Python.

Dynamic template

Static template

View the reports

Let’s look at the dynamic report and then create a PDF.

Dynamic report

This code will open the dynamic report in your web browser.

PDF report

Now let’s create a PDF version of the report. The PDF will be created in your working directory’s ‘Exports’ folder
We can also view the reports from the ADR homepage. Click Reports > Generate Report. Then select a report template and click on the ‘Generate Report’ button.

Clean up

When we are done viewing reports, we can stop the Nexus server and close Mechanical:
If you fail to stop the ADR server before closing your Python interpreter, you can use Task Manager to end any nginx.exe processes. If you do not stop the ADR server, you will not be able to re-run this demo in the same working directory, because you will not be able to delete the old ‘Nexus’ sub-folder. If you fail to stop the ADR server before closing your Python interpreter, you can use Task Manager to end any nginx.exe processes. If you do not close Mechanical, you will not be able to re-run this demo on the same Mechanical model, because the Mechanical database file will remain locked.