0% found this document useful (0 votes)
249 views21 pages

What's New at 2019 R1 Mechanical Enhancements

This document provides an overview of scripting capabilities in Ansys Mechanical using Python. It discusses how scripting allows automation of tasks, access to object data, and customization of the GUI. The document reviews the scripting console, APIs for interacting with objects, and examples of common scripting tasks like defining materials, meshes, solving and post-processing. It emphasizes that scripting requires knowledge of Python and practice with the Mechanical APIs.

Uploaded by

Fgirard scribt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
249 views21 pages

What's New at 2019 R1 Mechanical Enhancements

This document provides an overview of scripting capabilities in Ansys Mechanical using Python. It discusses how scripting allows automation of tasks, access to object data, and customization of the GUI. The document reviews the scripting console, APIs for interacting with objects, and examples of common scripting tasks like defining materials, meshes, solving and post-processing. It emphasizes that scripting requires knowledge of Python and practice with the Mechanical APIs.

Uploaded by

Fgirard scribt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Scripting in Mechanical

Ansys Version 2021 R1

May 2021
Scripting in Ansys Mechanical

• Scripting allows the use of programming for interacting directly with Mechanical.
‐ Access and interact with data of Mechanical objects.
‐ Automate routine tasks.
‐ Efficiently perform tasks that typically require significant time and effort.
‐ Create user-friendly User Buttons.
• First step toward developing more advanced customization.
‐ Find, test, develop, and debug commands for creating ACT extensions.
‐ Add capability to the Mechanical menus.
‐ Generate workflows for non-experts.

2
Scripting in Ansys Mechanical

• Console:
‐ Editor:
• For developing long scripts for your workflows.
• Can read in from files, promote to buttons, save, run.
‐ Shell:
• Enter commands to see what they do immediately.
• Aid in testing and development of scripts.

3
Scripting in Ansys Mechanical

• Mechanical Python APIs (Application Programming Interfaces)


exist that provide ways to find, add, delete, and interact with
Mechanical objects.
• ExtAPI.DataModel.Project is the root node for all objects in
Mechanical.
‐ Starting from here, the Automation API follows the same structure as the
Mechanical tree.
• For example, going down the tree to the Mesh object:
‐ Project > Model > Mesh
‐ Use: ExtAPI.DataModel.Project.Model.Mesh.
• All objects that can have only one instance in the tree follow this
rule.
‐ For all other objects, the Children or GetChildren functions are used.

4
Scripting in Ansys Mechanical

• Main access points:

5
Scripting in Ansys Mechanical

• There are other access points for various features:


‐ Ansys.Mechanical.Graphics.Tools
• To set legend features.
‐ ExtAPI.SelectionManager
• For entity selection.
‐ ExtAPI.DataModel.GeoData
• For finding geometry information.
‐ ExtAPI.DataModel.GetObjectByName
• To select an entity by its descriptive name.

6
Scripting in Ansys Mechanical

• Key features:
‐ Auto-completion of commands.
• Upon entering the “.”, list of available selections appears.
• Start typing or select desired item and <enter> to add to command.
• Ctrl + <up arrow> brings back the previous command.
‐ Text coloration.
‐ Snippets.
• Predefined commands that can be used for later insertion.

• Scripting requirements:
‐ Knowledge of Python.
‐ Lots of practice, trying things, and guesswork.
‐ Scripting examples (many in the Ansys Help).
‐ Scripting class in Ansys Learning Hub.
‐ Use of the new recording feature.

7
Scripting in Ansys Mechanical

• Python is an open-source programming language (http://www.python.org).


• Python can be easy to pick up whether a first-time programmer or experienced
with other languages.
• There are many on-line resources that contain tutorials, introductory classes,
and full courses.
• Ansys Workbench uses IronPython 2.6.
‐ Generally compatible with existing standard Python scripts.

8
Scripting in Ansys Mechanical

• Description of the console:


‐ Saving and reading in scripts.
‐ Using the recorder.
‐ User buttons.
• Console has tabs for multiple
scripts. New Start/stop
script Save
recording
script
Open Run User
script script buttons

9
Scripting in Ansys Mechanical

• Recording scripts:
‐ Index based, which is typically not the form one may want.
• Similar to FITEM, P51X picking options in MAPDL log file.
• Great way to find the APIs you need, then can edit to add more general capability.
• Not everything is available via recording.
‐ Example: Insert a Total Deformation Item generates the following:
• Internal Object ID 26 points to the Solution item in this model:

• Could later update with:


▪ solution_1 = ExtAPI.DataModel.Project.Model.Analyses[0].Solution
• And to find the Id:
▪ solution_1_id = solution_1.ObjectId

10
Scripting in Ansys Mechanical

• What if API does not exist?


‐ Working toward making sure anything within GUI has an API.
‐ Can try dir() to see all options:

‐ Can ask for help!

11
Scripting in Ansys Mechanical

• Several basic scripting demonstrations will be performed:


‐ Demonstration of using scripting console.
‐ Changing geometry settings, getting geometry information.
‐ Selections.
‐ Define material assignments.
‐ Define mesh settings, getting mesh information.
‐ Define boundary conditions.
‐ Solve and post-process.
‐ Automatic creation of debonding pairs.

12
Scripting in Ansys Mechanical

• Demonstration of using scripting console (shell).


‐ ExtAPI.DataModel.Project.Model.Analyses[0].Name
‐ a1 = ExtAPI.DataModel.Project.Model.Analyses[0]
‐ aname = a1.Name
‐ aname
‐ <Select a face>
‐ myforce=a1.AddForce()
‐ myforce.DefineBy=LoadDefineBy.Components
‐ myforce.ZComponent.Output.DiscreteValues=[Quantity(’20 [lbf]’)]

13
Scripting in Ansys Mechanical

• Changing geometry settings, getting geometry information (shell):


‐ geom = ExtAPI.DataModel.Project.Model.Geometry
‐ body = geom.Children[0].Children[1]
‐ body.Name
‐ body.StiffnessBehavior
‐ geodata = ExtAPI.DataModel.GeoData
‐ geobody = geodata.Assemblies[0].Parts[0].Bodies[1]
‐ geobody.Name
‐ geobody.Volume
‐ geobody.Faces.Count

14
Scripting in Ansys Mechanical

• Selections.
‐ Get IDs of entities currently selected in UI:
• ExtAPI.SelectionManager.CurrentSelection
‐ Create a new empty selection:
• tempSel = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
‐ Select list of Ids to select:
• tempSel.Ids = [34]
‐ Create a pressure and assign location from selection:
• Pressure = ExtAPI.DataModel.Project.Analysis[0].AddPressure()
• Pressure.Location = tempSel

15
Scripting in Ansys Mechanical

• Define material assignments (console).


‐ model = ExtAPI.DataModel.Project.Model
‐ matAssign = model.Materials.AddMaterialAssignment()
‐ body = ExtAPI.DataModel.GetObjectsByName(“Solid2”)[0].GetGeoBody()
‐ tempSel =
ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
‐ tempSel.Ids = [body.Id]
‐ matAssign.Location = tempSel
‐ matAssign.Material = “Gray Cast Iron” # material must be added to engineering data
‐ ExtAPI.DataModel.Tree.Refresh()

16
Scripting in Ansys Mechanical

• Define mesh settings (console).


‐ mesh = ExtAPI.DataModel.Project.Model.Mesh
‐ sizing = mesh.AddSizing()
‐ sizing.ElementSize = Quantity(‘1.0 [mm]’)
‐ body = ExtAPI.DataModel.GetObjectByName(“Solid2”)[0].GetGeoBody()
‐ tempSel =
ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
‐ tempSel.Ids = [body.Id]
‐ sizing.Location = tempSel
‐ mesh.GenerateMesh()

17
Scripting in Ansys Mechanical

• Get mesh data (shell):


‐ meshdata = ExtAPI.DataModel.Project.Model.Analyses[0].MeshData
‐ meshdata.ElementCount
‐ meshdata.NodeCount
‐ meshdata.Elements[0].Id
‐ meshdata.Elements[0].Centroid
‐ meshdata.Elements[0].CornerNodeIds

18
Scripting in Ansys Mechanical

• Define boundary conditions (console).


‐ analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
‐ fixedSupport = analysis.AddFixedSupport()
‐ fixedSupport.Location = ExtAPI.DataModel.GetObjectsByName(“FixedSupportFaces”)[0]
‐ pressure = analysis.AddPressure()
‐ pressure.Location = ExtAPI.DataModel.GetObjectsByName(“InsideFaces”)[0]
‐ pressure.Magnitude.Inputs[0].DiscreteValues = [Quantity(“0 [s]”), Quantity(“1 [s]”)]
‐ pressure.Magnitude.Output.DiscreteValues = [Quantity(“0 [MPa]”), Quantity(“15 [MPa]”)]

• FixedSupportFaces and InsideFaces are Named Selections that must already exist.

• Promote to User Button.

19
Scripting in Ansys Mechanical

• Solve and post-process (console).


‐ ExtAPI.DataModel.Project.Model.Solve()
‐ analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
‐ solution = analysis.Solution
‐ solution.AddTotalDeformation()
‐ solution.AddEquivalentStress()
‐ solution.EvaluateAllResults()
‐ filePath=r”D:\training\scripting\result”
‐ fileExtension=r”.txt”
‐ Results = solution.GetChildren(DataModelObjectCategory.Result,True)
‐ For result in results:
• fileName = str(result.Name)
• Result.ExportToTextFile(True,filePath+fileName+fileExtension)

20
Scripting in Ansys Mechanical

• Automatic creation of debonding pairs (console).


‐ con = ExtAPI.DataModel.Project.Model.Connections.Children[0].Children
‐ conct = con.Count
‐ for k in range(conct):
• czm=ExtAPI.DataModel.Project.Model.Fracture.AddContactDebonding()
• czm.Material=“Test CZM”
• conreg=ExtAPI.DataModel.Project.Model.Connections.Children[0].Children[k]
• czm.ContactRegion=conreg

21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy