Automation in Ansys Mechanical using Python

September 24, 2020 Dan Walsh

Before Ansys Mechanical for Workbench was developed, engineers relied on a scripting language called Ansys Parametric Design Language (APDL) to set up and perform analyses. APDL is a powerful tool, and it is still used behind the scenes whenever a user creates and solves a model in Ansys Mechanical. While the learning curve is steeper than using the modern Ansys Mechanical user-interface, APDL does have some distinct advantages: besides giving the user a fine level of control over the analysis, as a scripting language it also allows for automation of repetitive tasks. Users of Ansys Mechanical still have the option of incorporating APDL code into their model by inserting command snippets into the analysis tree. However, this approach has some drawbacks. Any objects created using a code snippet are created when the model is solved and are not well integrated into the Ansys Mechanical environment. Also, APDL was developed many years ago, and is somewhat cumbersome to use.

With the introduction of scripting into Ansys Mechanical, users can use the modern Python programming language to automate a vast array of tasks. Python scripting is well integrated into the software, and any objects created or analysis settings changed can immediately be seen in the Mechanical GUI. Lines of code can also be quickly saved to a user defined button in the ribbon for tasks that will be performed frequently.

Below, we’ll briefly introduce Python automation in Mechanical and perform a simple automation example to illustrate how powerful this approach can be.

Start by clicking the “Automation” tab in the Mechanical Ribbon. Clicking on the “Scripting” button will open the Mechanical Scripting Editor, where python commands can be entered. Two windows will appear. The top window allows for the user to write a script – multiple lines of code that will be executed sequentially. The bottom window is the Shell, which is useful for issuing single lines of code.

Picture1Picture2

For this example, we will generate point masses given user supplied arrays of masses, coordinates, and mass moments of inertia.  First, a few comments about the python programming language:

  • In Python, indentation is used for more than just aiding in readability—it is used to indicate a block of code. For instance, an indented block of code following a for control flow statement indicates all the code that should be executed in that for loop.
  • Python is a dynamically typed programming language, meaning that you do not need to declare variables and give them a specific data type.

To create the script, we first define three variables and assign string values representing the units used for length, mass, and mass moment of inertia.

Picture3Next, we create list variables to represent the values of the masses and mass moments of inertia for each point mass. The first value in each list corresponds to the first point mass, the second value to the second, and so on. In this example we will create three point-masses.

Picture5Now create list variables for the x, y, and z cartesian coordinates.

Picture6The command to create a point mass is:

Picture7“ExtAPI.DataModel” is used to access the Mechanical datamodel.  Once you type “ExtAPI.DataModel” followed by a period, a list will display showing options. From there, you can drill down through the Mechanical object tree (See below image). Because a point mass in Mechanical is added by right-clicking on “Geometry” in the tree, we need to access “Geometry” via the API in order to add a point mass. The “AddPointMass()” method will create a new point mass. Note the ending parenthesis “()” which are required in python to indicate a method.

Picture8Now that we know the command to create a new point mass, we will use a for loop to iterate through the values of the lists defined above and create the point masses. Note the “zip” function allows us to loop through multiple lists simultaneously. Also note that the values are assigned using the “Quantity” type, which allows the value to change based on the unit system selected.

The full code is shown below. Given a list of masses, mass moments of inertia, and cartesian coordinates, it can generate a massive number of point masses in a matter of seconds.

For a thorough introduction to automation in Ansys Mechanical, see “Ansys Mechanical Scripting” and “Introduction to Ansys ACT in Mechanical” located in the Structures Customization Learning Room of the Ansys Learning Hub.

Code

Previous Article
Explicit Dynamics and Ansys: Part II
Explicit Dynamics and Ansys: Part II

Which Explicit Tool is Right for Me? In my Explicit Dynamics and Ansys: Part I blog post, I discussed the f...

Next Flipbook
Ansys Mechanical Products
Ansys Mechanical Products

Get help from FEA & structural experts

Learn More