Вы находитесь на странице: 1из 16

Module 10

Macro Basics

10. Macro Basics


Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

One of the most powerful features of APDL (ANSYS Parametric Desi n Lan ua e! 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 re ular 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 Octo calls a third one, etc$ - up to .0 le+els deep$

ber 30, 2001 Inven

...Macro Basics

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

*n this chapter, we will present the "asics of macro writin )


A$ /reatin a Macro B$ Macro with Ar uments /$ 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

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$

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

Ma(e sure that name is not a +alid ANSYS command "# t#pin in name at Be in le+el and in all processors (P60P7, POS41, etc$!$ *f #ou et the messa e 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

Macro Basics

...Creating a Macro
0&ample)
A macro totvolume.mac to calculate the total +olume of all elements) esel,all etable,volume,volu ssum *get,totvol,ssum,,item,volume *stat,totvol

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

! Select all elements ! Store volume in element table ! Sum element table items ! totvol = sum of volume ! List totvol value

*ssue totvolume in POS41 (after a sol+e! to calculate the total +olume$

Octo ber 30, 2001 Inven

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

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

.$ director#(ies! in "#S$S%&"'()L*+ en+ironment +aria"le 5$ lo in 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

Macro Basics

B. Macro with Arguments

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

B# usin special parameter names, #ou can create a macro with up to .0 ar uments)
NAME, arg1, arg2, arg3, , ar10, ar11, ar12, , ar20

4he ar uments "eha+e 'ust li(e the fields on a standard ANSYS command and can accept)
num"ers alphanumeric characters (enclosed in sin le %uotes! parameters (scalar or arra#! parametric e&pressions

4he meanin of the ar uments depends on how #ou want toOcto desi n the macro$ ber

30, 2001 Inven

Macro Basics

...Macro with Arguments

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

2or e&ample, we could desi n 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

Octo *ssuin totvolume,1 in POS41 after a solution will then result in)

ber 30, 2001 Inven

Macro Basics

...Macro with Arguments


Notes)

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

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 ar uments, "e sure to descri"e their meanin "# includin comments in the macro$ 2or e&ample, the followin comments at the "e innin 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 Octo esel,s,ty0e,,arg1 ! Select elements of s0ecifie! ty0e ber = = 30,

2001 Inven

Macro Basics

C. Branching

Training Manual

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

INTRODUCTION TO ANSYS 6.0 - Part 2

Branchin "e ins 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

Macro Basics

...Branching
*if, ?, e@, y, tAen
4he condition can "e)
?, /C, y ?, #/, y ?, L,, y ?, E,, y ?, L/, y ?, E/, y ?, "+L,, y ?, "+E,, y !?=y !?y !?Dy !?Fy !?y !?y ! G?G D GyG ! G?G F GyG >0?*4

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

4he action can "e)


4=0N to e&ecute the su"se%uent "loc( of commands to e&it a do;loop

>/Y/L0 to s(ip to the end of a do;loop

& and # can "e num"ers, parameters, or parametric e&pressions$

4he action ta(es place onl# if the condition is true$ Otherwise, ANSYS will mo+e Octo on to */LS/*5 (if present!, ber */LS/ (if present!, and */#>*5$

30, 2001 Inven

Macro Basics

...Branching

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

2or e&ample, #ou can add an if;test to totvolume.mac to test for +alid +alues of the input ar ument)
*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

Macro Basics

D. Loo ing

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

Do;loops allow #ou to loop throu h 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 throu h an entire anal#sis session - includin preprocessin , solution, and postprocessin - if the situation warrants it$ *>) or *>)3H*L/ "e ins 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$

Octo As an e&ample, we can e&tend the totvolume.mac macro to ber loop throu h all element t#pes in the model and store the 30, +olume for each t#pe in an arra# parameter$ 2001 Inven

Macro Basics

...Loo ing
! << &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

Training Manual

Octo ber 30, 2001 Inven

INTRODUCTION TO ANSYS 6.0 - Part 2

Macro Basics

!. Genera$ Guide$ines
Start with small, simple macros$

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

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 lo in 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

Macro Basics

%. &or'sho
4his wor(shop consists of the followin pro"lem)
3<$ Berif#in Pressures

Training Manual

INTRODUCTION TO ANSYS 6.0 - Part 2

Please refer to #our &or'sho Su

$ement for instructions$

Octo ber 30, 2001 Inven

Вам также может понравиться