Professional Documents
Culture Documents
2 10-Macro
2 10-Macro
Module 10
Octo
ber
30,
2001
Inven
tory
#001
571
10-2
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
10. Macro Basics
One of the most powerful features of APDL (ANSYS
Parametric Desin Lanuae! is the a"ilit# to create macros$
A macro is a se%uence of ANSYS commands stored in a file
and e&ecuted 'ust li(e a reular command$
Some useful macro capa"ilities)
*t can ha+e arguments as in a standard ANSYS command$
Branchin and loopin to control the se%uence of commands$
*nteracti+e features such as raphical pic(in, promptin, and
dialo "o&es$
Nested macros - one macro callin a second one, which in turn
calls a third one, etc$ - up to .0 le+els deep$
Octo
ber
30,
2001
Inven
tory
#001
571
10-3
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
...Macro Basics
*n this chapter, we will present the "asics of macro writin)
A$ /reatin a Macro
B$ Macro with Aruments
/$ Branchin
D$ Loopin
0$ 1eneral 1uidelines
2$ 3or(shop
2or more details, please refer to #our APDL Programmers
Guide or the Programming in ANSYS seminar notes$
Octo
ber
30,
2001
Inven
tory
#001
571
10-4
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
A. Creating a Macro
4o create a macro, simpl# start a te&t editor, insert the
desired se%uence of commands, and sa+e them to a file
called name.mac$
name can "e up to 5. characters, startin with a letter$
Spaces are not allowed in the name$
Also a+oid special characters$
Ma(e sure that name is not a +alid ANSYS command "#
t#pin in name at Bein le+el and in all processors (P60P7,
POS41, etc$!$ *f #ou et the messae not a recognized
command or macro then the name is 8safe$9
0&tension .mac allows #ou to e&ecute the macro as if it were a
command "# simpl# t#pin in name$
Octo
ber
30,
2001
Inven
tory
#001
571
10-5
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
...Creating a Macro
0&le)
A macro totvolume.mac to calculate the total +olume of all
elements)
esel,all ! Select all elements
etable,volume,volu ! Store volume in element table
ssum ! Sum element table items
*get,totvol,ssum,,item,volume ! totvol = sum of volume
*stat,totvol ! List totvol value
*ssue totvolume in POS41 (after a sol+e! to calculate the total
+olume$
Octo
ber
30,
2001
Inven
tory
#001
571
10-6
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
...Creating a Macro
Search Path)
ANSYS will e&ecute the first name.mac file it finds in the
followin search se%uence)
1$ /ansys6/!ocu
.$ director#(ies! in "#S$S%&"'()L*+ en+ironment +aria"le
5$ loin director# (home director# on 3indows s#stems!
:$ current (wor(in! director#
*f the search finds "oth upper;case and lower;case files of the
same name, the upper;case file is used$
Octo
ber
30,
2001
Inven
tory
#001
571
10-7
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
B. Macro with Arguments
B# usin special parameter names, #ou can create a macro
with up to .0 aruments)
NAME, arg1, arg2, arg3, , ar10, ar11, ar12, , ar20
4he aruments "eha+e 'ust li(e the fields on a standard
ANSYS command and can accept)
num"ers
alphanumeric characters (enclosed in sinle %uotes!
parameters (scalar or arra#!
parametric e&pressions
4he meanin of the aruments depends on how #ou want to
desin the macro$
Octo
ber
30,
2001
Inven
tory
#001
571
10-8
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
...Macro with Arguments
2or e&le, we could desin totvolume.mac to calculate the
total +olume for all elements of a specified t#pe)
,),-)L.&/, TYPE
4he macro would then loo( li(e this)
esel,s,ty0e,,arg1 ! Select elements of s0ecifie! ty0e
etable,volume,volu ! Store volume in element table
ssum ! Sum element table items
*get,totvol,ssum,,item,volume ! totvol = sum of volume
*v2rite,arg1,totvol ! 3rite out arg1 an! totvol
4,otal volume for ty0e , 56., elements = , 57.89
*ssuin totvolume,1 in POS41 after a solution will then result in)
Octo
ber
30,
2001
Inven
tory
#001
571
10-9
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
...Macro with Arguments
Notes)
4he special parameter names A611;A61< and A610;A6<< are
local parameters +alid onl# within a macro$
4he# hold no meanin once the macro has finished e&ecution
and control is returned to 8main9 ANSYS$
A+oid usin these names elsewhere in the model$
3hene+er #ou use aruments, "e sure to descri"e their meanin
"# includin comments in the macro$
2or e&le, the followin comments at the "einnin of
totvolume.mac would "e helpful$
! &acro ,),-)L.&/.&"' to calculate total volume of elements
! .sage: ,),-)L.&/, ,$;/ < vali! only in ;)S,1 after a solve
! ,$;/ = vali! element ty0e number
esel,s,ty0e,,arg1 ! Select elements of s0ecifie! ty0e
=
=
Octo
ber
30,
2001
Inven
tory
#001
571
10-10
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
C. Branching
B# usin an *2;4=0N;0LS0 construct, #ou can e&ecute a command
or "loc( of commands onl# if certain conditions are met$
Additional comparison operation are a+aila"le for the >*2 and
>0LS0*2 commands with AND, O6, or ?O6 options$
>*2,A,0@,B,AND,/,14,D,4=0N
Branchin "eins with **5 and ends with */#>*5$ */LS/*5 and
*/LS/ are also allowed in "etween)
*if, ?, e@, y, tAen
*else
*en!if
>*2 constructs can "e nested up to twent# le+els
Octo
ber
30,
2001
Inven
tory
#001
571
10-11
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
...Branching
4he condition can "e)
?, /C, y ! ? = y
?, #/, y ! ? y
?, L,, y ! ? D y
?, E,, y ! ? F y
?, L/, y ! ? y
?, E/, y ! ? y
?, "+L,, y ! G?G D GyG
?, "+E,, y ! G?G F GyG
& and # can "e num"ers,
parameters, or parametric
e&pressions$
4he action can "e)
4=0N to e&ecute the
su"se%uent "loc( of
commands
>0?*4 to e&it a do;loop
>/Y/L0 to s(ip to the end of a
do;loop
4he action ta(es place onl# if
the condition is true$
Otherwise, ANSYS will mo+e
on to */LS/*5 (if present!,
*/LS/ (if present!, and */#>*5$
*if, ?, e@, y, tAen
Octo
ber
30,
2001
Inven
tory
#001
571
10-12
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
I
N
T
R
O
D
U
C
T
I
O
N
T
O
A
N
S
Y
S
6
.
0
-
P
a
r
t
2 2
Training Manual
Macro Basics
...Branching