ACT continues to evolve rapidly, expanding in both features and the ANSYS products that it supports. Consequently, learning to use it can sometimes seem overwhelming. However, knowing these key concepts makes using the ACT Console to script in Mechanical easier:
In ExtAPI.DataModel, DataModel
refers to the Mechanical tree. Virtually any operation that involves information
in the tree begins with ExtAPI.DataModel. As demonstrated
by bookmarks supplied in the Helpers folder, you can find
objects in the tree by searching for object names and types or by accessing a
list of all selected objects.
Entering mesh =
ExtAPI.DataModel.MeshDataByName("Global") in the command line is
an easy way to access the mesh. Global is the main and only mesh. You can save
this command as a snippet to use it anytime that you want to access the
mesh.
It is important to understand a key but subtle distinction between two ways of dealing with the mesh:
ExtAPI.DataModel.Project.Model.Mesh
accesses the mesh tree object and thus its methods and
controls
ExtAPI.DataModel.MeshDataByName("Global")
accesses FE information (node and element locations, element
connectivity, and so on).
It is important to understand a key but subtle distinction between two ways of dealing with the geometry:
ExtAPI.DataModel.GeoData accesses the
underlying geometry attached to Mechanical.
ExtAPI.DataModel.Project.Model.Geometry
accesses the Geometry object in the Mechanical tree.
When accessing GeoData, you must use the tree
hierarchy. For example,
ExtAPI.DataModel.GeoData.Assemblies[0].Parts[0].Bodies[0].Volume
accesses the volume for the body of the first part in the first assembly. While
attribute names such as Assemblies[0],
Parts[0], and Bodies[0] are
plural, only the specified assembly, part, and body are accessed.
All bodies have parts as their parents, even if they are not multibody parts.
This is important to understand in both GeoData and when
traversing the Geometry object of the
DataModel. Although a part might be hidden from the
Mechanical interface, the part is always there.
You can use the ACT Console to both get and set
properties on an object. For example, assume that you want to get the variable
definition types for the X, Y, and Z components of a displacement that has
previously been defined as d. You would execute the following
commands in the ACT Console, one at a time:
d.XComponent.Output.DefinitionType
d.YComponent.Output.DefinitionType
d.ZComponent.Output.DefinitionType
Now assume that you want to change the variable definition type for the Y component from Free to Discrete and then verify the change and output value in the ACT Console. You would execute the following commands:
d.YComponent.Output.DefinitionType =VariableDefinitionType.Discrete
d.YComponent.Output.DefinitionType
d.YComponent.Output
For more information on accessing the properties of an object, including those for traversing the geometry, mesh, simulation, and results, see the following topics in the APIs for ANSYS Mechanical section of the ANSYS ACT Developer’s Guide:
It is far better to create objects and access them rather than recalling the
API over and over with the use of ExtAPI in your
commands. For example, using the following three commands is superior to calling
ExtAPI in the second and third commands:
mesh = ExtAPI.DataModel.MeshDataByName("Global")mesh.ElementCount
mesh.NodeCount
Looping is a fundamental concept in any scripting language. Within a Geometry object, you can use loops to add, modify, or export property data. The following script loops over all contacts and change their formulation to MPC:
contact_region_list = ExtAPI.DataModel.Project.Model.GetChildren(DataModelObjectCategory.ContactRegion, True)
For example, for contact_region in
contact_region_list, you execute this command:
contact_region.ContactFormulation = Ansys.ACT.Automation.Mechanical.Enums.ContactFormulation.MPC
Lastly, if you cannot find what you want, check the
InternalObject attribute. While the ACT API has
“wrapped” many useful aspects of Mechanical, it has not wrapped
everything, for various reasons. Many API objects have an
InternalObject attribute that you can use to find
additional capabilities that are not formally exposed in ACT.
For more information about how to complete many Mechanical tasks using scripts, see Script Examples