You are on page 1of 16

Macro Basics

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&ample)
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&ample, 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&ample, 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

*elseif, ?, e@, B, 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

2or e&ample, #ou can add an if;test to totvolume.mac to test


for +alid +alues of the input arument)
*if,arg1,lt,1,tAen ! *f arg1 D 1
*msg,2arn ! *ssue a 2arning...
/lement ty0e number must be 1 or greater
/eof ! = an! e?it tAe macro
*en!if
esel,s,ty0e,,arg1 ! Select elements of s0ecifie! ty0e
etable,volume,volu ! Store volume in element table
ssum ! Sum element table items
...
*ssuin totvolume,<1 will now result in)
Octo
ber
30,
2001
Inven
tory
#001
571
10-13
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
D. Looing
Do;loops allow #ou to loop throuh a "loc( of commands
se+eral times$
4here is +irtuall# no limit to what #ou can include in an
ANSYS do;loop$ You can loop throuh an entire anal#sis
session - includin preprocessin, solution, and
postprocessin - if the situation warrants it$
*>) or *>)3H*L/ "eins a loop, */#>>) ends it$
You can control the loopin usin */I*,, which e&its the do;
loop, and *'$'L/, which s(ips to the end of the do;loop$
!"it and c#c$e can also "e done as a result of an if;test$
As an e&ample, we can e&tend the totvolume.mac macro to
loop throuh all element t#pes in the model and store the
+olume for each t#pe in an arra# parameter$
Octo
ber
30,
2001
Inven
tory
#001
571
10-14
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
...Looing
! << &acro ,),-)L.&/.&"' to calculate total element volume.
! << .sage: *ssue ,),-)L.&/ in ;)S,1 after a solution.
! << (esult:
! << a9 evolume4i9 = total volume for element ty0e i
! << b9 totvol = gran! total volume
!
*get,numty0es,ety0e,,num,count ! Eet number of element ty0es
*!im,evolume,array,numty0es ! )0en a numty0es ? 1 array
*!o,i,1,numty0es ! 5or i = 1 < numty0es...
esel,s,ty0e,,i ! Select elements of ty0e i
etable,volume,volu ! Store volume in element table
ssum ! Sum element table items
*get,totvol,ssum,,item,volume ! totvol = sum of volume
evolume4i9 = totvol ! Store totvol in evolume4i9
*en!!o ! /n! of !o<loo0
*vscfun,totvol,sum,evolume4i9 ! totvol = gran! total volume
esel,all ! "ctivate full set of elements
Octo
ber
30,
2001
Inven
tory
#001
571
10-15
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
!. Genera$ Guide$ines
Start with small, simple macros$
As #ou create the macro, remem"er that #ou can cut and
aste the commands into the ANSYS *nput window to test
and ma(e sure that the command se%uence is correct$
Ase comments to descri"e the intent or e&pected outcome of
commands$
Place #our 8personal9 macros in #our loin director#$
Place compan#;wide macros in a director# that e+er#one can
access, and include that director# in "#S$S%&"'()L*+
en+ironment +aria"le$
Octo
ber
30,
2001
Inven
tory
#001
571
10-16
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
%. &or'sho
4his wor(shop consists of the followin pro"lem)
3<$ Berif#in Pressures
Please refer to #our &or'sho Su$ement for instructions$

You might also like