Resources

Auto-Generate Named Selections in Ansys Mechanical using Python Scripting

Written by Mark Lytell | Oct 29, 2024 7:50:28 PM

Introduction

Named Selections in Ansys Mechanical are a convenient way to group geometry or FEM entities for future use in model definition, i.e., mesh controls, material assignment, contact definition, etc., or post-processing.  In a previous article, we discussed manual creation of Named Selections using geometry selection or via a Worksheet.  Here, we will explain how to create Named Selections for all geometry bodies using Python scripting from within Ansys Mechanical.

Use Case

The example model that we will here has model geometry composed of one single-body part and one multi-body part composed of four solid bodies.  Our goal is to create a Named Selection for each of the five bodies as well as for the entire multi-body part so that we may use them in the scoping of model entities. 

Additionally, we would like that our newly created Named Selections meet the criteria that are required for them to be sent to the APDL solver, namely

  • maximum length = 32 characters,
  • containing only letters, numbers, underscores or periods.

Finally, we would like to place the auto-generated Named Selections in a Tree Grouping folder that can referenced in other scripts, for example, to automatically create Equivalent Stress results for post-processing.

Anatomy of the Script

The anatomy of the automation script consists of generating and manipulating the following entities:

  1. Create a Model object from the PyMechanical data model, ExtAPI.DataModel.Project.Model.
  2. Create a geometry selection manager.
  3. Get a list of all Parts.
  4. For each Part, get a list of the body IDs and names within it.
  5. For each Part, create a Named Selection for each of its bodies and the for the Part itself (if a multi-body Part).
  6. Rename the Named Selections so that they comply with APDL solver requirements.
  7. Collect all Named Selections and place in a Tree Grouping folder called "Auto-generated."

Results

The end result of running the script is shown in the figure below.

Here, we have a combination of the auto-generated Named Selections by script as well as some manually created ones that don't lend themselves to creation by automation.

Conclusion

In conclusion, the method to auto generate Named Selections by script has many applications most notably when the number of Parts in the Geometry branch is large enough to make manual creation of Named Selections tedious.  Additionally, placing the auto-generated Named Selections in a Tree Grouping folder allows for direct access in subsequent scripts.  Finally, it is best to automate repetitive tasks when possible so that one's workflow can be spent on value added activities.  Using a script such as the one presented here makes repetitive tasks less time consuming.

Going Further

  • Contact Ozen Engineering for your Mechanical scripting needs.  There are expert consultants ready to support such requests.
Downloadable Items

Script to create Named Selections - open in the Mechanical Scripting window under the Automation tab in Ansys Mechanical.

References

Blog:  Using Named Selections in Ansys Mechanical