0% found this document useful (0 votes)
20 views10 pages

Mech Scrip 2024R1 en LE01

This document provides an introduction to using the Ansys Mechanical Console for scripting, specifically focusing on accessing the Ansys Mechanical API and automating tasks. It covers basic scripting functions, Python programming basics, and how to modify analysis names within the Mechanical tree. Additionally, it includes practical exercises to reinforce learning and understanding of the console's capabilities.
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)
20 views10 pages

Mech Scrip 2024R1 en LE01

This document provides an introduction to using the Ansys Mechanical Console for scripting, specifically focusing on accessing the Ansys Mechanical API and automating tasks. It covers basic scripting functions, Python programming basics, and how to modify analysis names within the Mechanical tree. Additionally, it includes practical exercises to reinforce learning and understanding of the console's capabilities.
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/ 10

Ansys Mechanical Scripting

Lesson 01: Introduction –


Learn How to Use the Console
Please note:
• These training materials were developed and tested in Ansys Release 2024 R1. Although they are
expected to behave similarly in later releases, this has not been tested and is not guaranteed.
Release 2024 R1
• The screen images included with these training materials may vary from the visual appearance of a
local software session.

©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.


The Mechanical Console - Objectives
The Mechanical Console allows you to:
• Access the Ansys Mechanical API
• Create buttons in Mechanical user interface
• Automate certain tasks in Mechanical
• Use it as a starting point to write ACT apps

In this lesson you will learn:


• How to find and use the console
• How to program basic functions in the console
• How to find API items in Mechanical
2 ©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
The Mechanical Console
To start the console:
• Open Mechanical
• Under Automation select Scripting

The console will appear on the right of the screen New Open Save Run Debugger
To know what each button does simply hover on it and the Recorder
definition will appear

• The commands written in the script Editor (top) will be


executed when pressing run (or ctrl+F5) Clear Insert Snippet
• Any script written in the script Editor can be saved and
reopened in a new session
• The commands written in the Shell (bottom) will be
executed when pressing enter
• Anything written in the Shell can be cleared
• A good way to get started is to use ‘Insert Snippet’; which
has a list of basic API functions
3 ©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
Your First Script
• Open the console
• In the script Editor write print (‘Hello’)
• Hit ‘Run’ or Ctrl+F5
• Look at the Shell window

• Clear the Shell window and write print (‘Hello’) in this window
• Hit Enter

As you have just seen either the Shell or the Editor can be used to write scripts

4 ©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.


A Word About Python
• The Mechanical console uses python as a programing language
• More training about python can be found online or in the ACT training materials

You are going to try some basic python functionalities, by using the script editor:

• In the script editor write ‘a=3*3’ on the first line and ‘print (a)’ on the second line
• Run the script

The simplest loop in python is the for loop. The for loop needs to be indented
• In the script editor write:
for i in range(10):
print (I)
And run

Pay attention to the space in front of print. Anything written with an indent under for will be inside the loop.
To stop the loop, just remove the indent

To try it: under print (i), write print (‘Hello’) with an indent and without. Run each version.
Look at the shell window each time

In the same way you can create more indents for an ‘if’ statement for example

Note here range(10) goes from 0 to 9

5 ©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.


Mechanical's API Entry Points
Ansys Mechanical API has 5 entry points. These entry points can be used to read and edit anything in the
model. They are:
• ExtAPI: The general entry point
• Tree: Control everything in the Mechanical tree
• Model: Allow you to browse the model data, mesh, geometry...
• DataModel: read all that was created in a model
• Graphics: control the graphic window, take screen shot, draw...

• There is often more than one way to access something in the API using different entry points. For
example: Model.Analyses[0] = ExtAPI.DataModel.AnalysisList[0]= DataModel.AnalysisList[0]

6 ©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.


Modify the Analysis Name
Using the autocompletion, you are going to change the name of the first analysis in the
Mechanical tree

First, get the analysis.


You need to create an analysis system (e.g. Static Structural) then you can do this in many different
ways:

• Using Insert Snippet► Helpers ► Analysis


• Using the autocompletion you just used: analysis = Model.Analyses[0]
• ………

The current name is analysis.Name


You can change it: analysis.Name='My new analysis name'
7 ©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
Going Further
• Create several analyses in Mechanical: (Home► Analysis and pick any analysis type)
• Using a loop rename each analysis: Analysis 1, Analysis 2,…..
• Tips:
- The analysis name needs to be a string; to convert a number to a string use str(1)
- Model.Analyses will produce a list
- To access an element in a list use [], for example,
• list=[1,2,3]
• list[0] will return 1

- ‘for analysis in ModelAnalyses:’ will loop through all the analyses

The answer is provided on the next slide


8 ©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
Going Further - Answer
This task can be accomplished in different ways for example:
i=1
for analysis in Model.Analyses:
analysis.Name='Analysis '+str(i)
i=i+1

-------------------------------------------------------------------------------------------------------------------------------
for i in range(len(Model.Analyses)):
analysis=Model.Analyses[i]
analysis.Name='Analysis '+str(i+1)

-------------------------------------------------------------------------------------------------------------------------------
for i in range(len(DataModel.AnalysisList)):
analysis=Model.Analyses[i]
analysis.Name='Analysis '+str(i+1)

9 ©2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.

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