You can create macros either within Mechanical APDL itself or using your text editor of choice (such as emacs, vi, or wordpad). If your macro is fairly simple and short, creating it in Mechanical APDL can be very convenient. If you are creating a longer, more complex macro or editing an existing macro then you will need a text editor. Also, using a text editor allows you to use a similar macro or Mechanical APDL log file as the source for your macro.
For any long, complex macro you should always consider either using a similar macro as a starting point or running the task interactively in Mechanical APDL and using the resulting log file as the basis of your macro. Either method can greatly reduce the time and effort required to create a suitable macro.
The following creating macro topics are available:
Macros are a sequence of Mechanical APDL commands stored in a file. Macros should not have the same name as an existing Mechanical APDL command, or start with the first four characters of a Mechanical APDL command, because Mechanical APDL executes the internal command instead of the macro. The following naming restrictions apply to macro files:
The file name cannot exceed 32 characters.
The file name cannot begin with a numeral.
The file extension cannot contain more than eight characters (if you are executing the macro as if it were a Mechanical APDL command it should have the extension .mac.)
The file name or extension cannot contain spaces.
The file name or extension cannot contain any characters prohibited by your file system and for portability should not contain any characters prohibited by either Linux or Windows file systems.
To ensure that you are not using the name of a Mechanical APDL command, before creating a macro try running the file name that you wish to use as a Mechanical APDL command. If Mechanical APDL returns the message shown below, you will know that the command is not used in the current processor. You should check the macro file name in each processor in which you plan to use the macro. (You could also check if the macro file name matches any command listed in the online documentation; however, this method cannot locate the names of undocumented commands.)
Using the .mac extension allows Mechanical APDL to execute the macro as it would any internal command. You should avoid using the extension .MAC because it is used for Mechanical APDL internal macros.
By default, Mechanical APDL searches for a user macro file (.mac extension) in the following locations:
The /ansys_inc/v242/ansys/apdl directory.
The directory (or directories) designated by the ANSYS_MACROLIB environment variable (if defined) or the login (home) directory. This environment variable is documented in The Mechanical APDL Environment chapter of the Operations Guide.
The directory designated by the $HOME environment variable.
The working directory.
You can place macros for your personal use in your home directory. Macros that should be available across your site should be placed in the /ansys_inc/v242/ansys/apdl directory or some commonly accessible directory that everyone can reference through the ANSYS_MACROLIB environment variable.
For Windows users: The "current directory" is the default directory (usually a network resource) set by administrators and you should ask your network administrator for its location. You can use environment variables to create a local "home directory." The local home directory is checked after the default directory designated in your domain profile.
You can create a macro by four methods from within Mechanical APDL:
Issue the *CREATE command in the input window. Parameter values are not resolved and parameter names are written to the file.
Use the *CFOPEN, *CFWRITE, and *CFCLOS commands. Parameter names are resolved to their current values and those values are written to the macro file.
Issue the /TEE command in the input window. This command writes a list of commands to a file at the same time that the commands are being executed. As the commands are executed in the current Mechanical APDL session, parameter names are resolved to their current values. However, in the file that is created, parameter values are not resolved and parameter names are written instead.
Choose the
menu item. This method opens a dialog box that can be used as a simple, multiline editor for creating macros. Parameter values are not resolved and parameter names are written to the file.
The following sections detail each of these methods.
Issuing *CREATE redirects Mechanical APDL commands entered in the command input window to the file designated by the command. All commands are redirected until you issue the *END command. If an existing file has the same name as the macro file name you specify, the Mechanical APDL program overwrites the existing file.
For example, suppose that you want to create a macro called matprop.mac, which automatically defines a set of material properties. The set of commands entered into the input window for this macro might look like this:
*CREATE,matprop,mac,macros MP,EX,1,2.07E11 MP,NUXY,1,.27 MP,DENS,1,7835 MP,KXX,1,42 *END
The *CREATE command takes arguments of the file name, the file extension, and the directory path (in this case, the macros directory is specified).
When using *CREATE, all parameters used in commands are written to the file (the currently assigned values for the parameter are not substituted).
You cannot use *CREATE within a DO loop.
If you wish to create a macro file in which current values are substituted for parameters you can use *CFWRITE. Unlike *CREATE, the *CFWRITE command cannot specify a macro name; you must first specify the macro file with the *CFOPEN command. Only those Mechanical APDL commands that are explicitly prefaced with a *CFWRITE command are then written to the designated file; all other commands entered in the command input window are executed. As with the *CREATE command, *CFOPEN can specify a file name, a file extension, and a path. The following example writes a BLOCK command to the currently open macro file.
*cfwrite,block,,a,,b,,c
Note that parameters were used for arguments to the BLOCK command. The current value of those parameters (and not the parameter names) are written to the file. So, for this example, the line written to the macro file might be
*cfwrite,block,,4,,2.5,,2
To close the macro file, issue the *CFCLOS command.
Note: While it is possible to create a macro through this method, these commands are most useful as a method for writing Mechanical APDL commands to a file during macro execution.
Issuing /TEE,NEW or /TEE,APPEND redirects Mechanical APDL commands entered in the command input window to the file designated by the command at the same time that the commands are being executed. All commands are executed and redirected until you issue the /TEE,END command. If an existing file has the same name as the macro file name you specify with /TEE,NEW, the Mechanical APDL program overwrites the existing file. To avoid this, use /TEE,APPEND instead.
In addition to the Label
argument (which can have a
value of NEW, APPEND, or END), the /TEE command takes
arguments of the file name, the file extension, and the directory path.
As the commands are executed in the current Mechanical APDL session, all parameter names are resolved to their current values. However, in the file that is created, parameter names are written (the currently assigned values for the parameter are not substituted). If your current parameter values are important, you can save the parameters to a file using the PARSAV command.
For an example, see the description of the /TEE command in the Command Reference.
Choosing this menu item opens a Mechanical APDL dialog box that you can use as a simple editor for creating macros. You cannot open and edit an existing macro with this facility; if you use the name of an existing macro as the arguments for the *CREATE field, the existing file will be overwritten.
As with the *CREATE command, parameters are not evaluated but are written verbatim into the macro file. Note that you do not make the last line a *END command.
You can use your favorite text editor to create or edit macro files. Any ASCII editor will work. Moreover, Mechanical APDL macros can have their lines terminated by either Linux or Windows line ending conventions (carriage-return, line-feed pairs or simply line-feeds) so you can create a macro on one platform and use it on several platforms.
If you use this method to create macros, do not include the *CREATE and *END commands.
As a convenience, Mechanical APDL allows you to place a set of macros in a single file, called a macro library file. You can create these either through the *CREATE command or through a text editor. Given that macro libraries tend to be longer than single macros, using a text editor normally provides the best approach.
Macros libraries have no explicit file extension and follow the same file naming conventions as macro files. A macro library file has the following structure:
MACRONAME1 . . . /EOF MACRONAME2 . . . /EOF MACRONAME3 . . . ./EOF
For example, the following macro file contains two simple macros:
mybloc /prep7 /view,,-1,-2,-3 block,,4,,3,,2 finish /EOF mysphere /prep7 /view,,-1,-2,-3 sphere,1 finish /EOF
Note that each macro is prefaced with a macro name (sometimes referred to as a data block name) and ends with a /EOF command.
A macro library file can reside anywhere on your system, although for convenience you should place it within the macro search path. Unlike macro files, a macro library file can have any extension up to eight characters.