Professional Documents
Culture Documents
Receive the latest updates to our popular Tips & Tricks articles and videos by
subscribing to our Monthly Newsletter (https://go.simutechgroup.com/simutech-
newsletter-subscribe)
SOFTWARE and channel
(/ANSYS-SOFTWARE) SUPPORT (/SUPPORT) TRAINING (/TRAINING)
(https://www.youtube.com/c/SimuTechGroup).
For even more advanced ANSYS instruction, check out our consulting (/consulting-services)
and training (/training) services. Or simply contact us (/contact-us) to discuss any of your
ANSYS needs.TESTING (/TESTING-SERVICES)
(https://www.simutechgroup.com)
COMMAND
Posted in Tips & Tricks - Finite Element Analysis (FEA) articles (/tips-and-tricks/fea-articles)
ABOUT
CLIENT=STG1800&CCID=19000101_000001&TYPE=MP&LANG=EN_US)
https://www.simutechgroup.com/tips-and-tricks/fea-articles/178-fea-tips-tricks-ansys-macro-programming-use-command Page 1 of 6
ANSYS FEA Tips & Tricks: Macro Programming with the *USE Command 11/05/18 15'42
This article illustrates APDL programming with “subroutines” through the *USE command and a
macro library file. One large macro file can be used to execute many related “subroutines” and
optionally to define and support many toolbar buttons that execute complex procedures.
There is a way to write APDL programs that contain their subroutines in a single file, taking
advantage of the *USE command. A “package” of macros for one purpose does not have to
consist of a series of .mac files stored in one or multiple locations—all can be packaged in a
single .mac file. This can aid simplicity in version control and distribution. The technique keeps all
related macros inside one source file. An example illustrates.
This example starts in a “main” routine by declaring itself as a macro library with the *ULIB
command. The *ULIB command calls its own file using its own file name. It calls “subroutine”
macros inside itself with the *USE command. These subsidiary macros are positioned after the
“main” macro ends with the /EOF command. The principal trick in getting this technique to work
is to immediately follow each /EOF command that ends a “block” with the Label for the next *USE
macro block. Comments on each section follow the Label for the block that is a subsidiary macro
for the *USE command.
Except for “ARGn” parameters inside a macro, all other ANSYS scalar parameters, arrays,
coordinate systems, components and component assemblies have global scope. This
complicates programming. The following example would be stored in a macro file called
“mytest1.mac”, which declares itself with a *ULIB command.
https://www.simutechgroup.com/tips-and-tricks/fea-articles/178-fea-tips-tricks-ansys-macro-programming-use-command Page 2 of 6
ANSYS FEA Tips & Tricks: Macro Programming with the *USE Command 11/05/18 15'42
! Comments go here.
! This "main" routine calls "subroutine" macros further below.
!
! IMPORTANT: Place nothing between /EOF and the next data block label. <==
!
! This first block is the main routine, which can do various things.
! It is ended by an /EOF command, which is followed by data blocks
! for various "subroutine" macros.
!
*ULIB,mytest1,mac ! Declare this macro file as a macro library
*USE,part1 ! Call the first "subroutine", if desired
*USE,part2 ! Call the second "subroutine", if desired
! Exiting if no Toolbar actions are wanted...
*ULIB ! Un-declare macro library
*msg,UI ! Send completion message to user
FINISHED
!
! The following /EOF command ends the first block.
/EOF
PART1 ! Part 1 of the macro library--the first label.
!
! Description of part 1 of the macro library goes here. Commands follow.
!
*msg,UI
Part 1 of the macro
/wait,1
! End of "part1"
/EOF
PART2 ! Part 2 of the macro library,--the second label.
!
! Description of part2 of the macro library.
!
*msg,UI
Part 2 of the macro
/wait,1
! End of “part 2”
/EOF
https://www.simutechgroup.com/tips-and-tricks/fea-articles/178-fea-tips-tricks-ansys-macro-programming-use-command Page 3 of 6
ANSYS FEA Tips & Tricks: Macro Programming with the *USE Command 11/05/18 15'42
Below is the note that appears at the end of the run of the above macro:
If a macro library as above is to create and support toolbar buttons, either the “main” routine
above, one of its “subroutines”, or an independent macro can be used to refer to the macro library
with the *ULIB command, and define a set of toolbar buttons with the *ABBR commands. The
toolbar abbreviations would execute *USE commands that run the desired macro block in the
macro library file. The macro block, starting with a Label as above, could run a complex sequence
of commands.
The following example declares itself as a macro library with *ULIB, and creates two toolbar
buttons. It does not use a second *ULIB command to un-declare itself, so the toolbar buttons will
continue to function as long as “mytest2.mac” is the active macro library. Note that a toolbar
button could be defined to run a block that un-declares the macro library, and replaces the toolbar
buttons before ending. An ABBSAV and ABBRES command pair refreshes the toolbar to make the
new buttons visible. The following commands would be in a macro file called “mytest2.mac”:
https://www.simutechgroup.com/tips-and-tricks/fea-articles/178-fea-tips-tricks-ansys-macro-programming-use-command Page 4 of 6
ANSYS FEA Tips & Tricks: Macro Programming with the *USE Command 11/05/18 15'42
Clicking either of the two toolbar buttons, Part_1 or Part_2, will execute the block of commands
as entered into the *ABBR commands in the “main” first part of the above macro. If the *ULIB
command is executed without arguments, the new toolbar buttons cease finding the assigned
blocks.
The following figure shows the result of clicking the Part_1 button in the Toolbar:
Conclusion
A macro library file identified by the *ULIB command can access blocks inside itself with *USE
commands, passing argument lists are required. This gives the user an APDL programming
approach that permits a “main” routine, followed by many “subroutines”, all contained in one
https://www.simutechgroup.com/tips-and-tricks/fea-articles/178-fea-tips-tricks-ansys-macro-programming-use-command Page 5 of 6
ANSYS FEA Tips & Tricks: Macro Programming with the *USE Command 11/05/18 15'42
source file. The approach lets users employ one source file, rather than a large set of macro files
that must be kept together, when complex APDL programming is wanted.
A supplementary use of a macro library is to support a set of Toolbar buttons that execute blocks
inside the macro library by executing *USE commands when clicked. This permits Toolbar buttons
to execute complex APDL command sequences contained inside the blocks.
These approaches aid the use of complex APDL programs and Toolbar button actions, giving
more power to ANSYS users.
Critical to operation of the APDL macro library is to follow an /EOF command immediately with
the Label for the next block of commands.
https://www.simutechgroup.com/tips-and-tricks/fea-articles/178-fea-tips-tricks-ansys-macro-programming-use-command Page 6 of 6