|
本帖最后由 wdhd 于 2016-3-10 09:28 编辑
4.2. Creating a Macro
You can create macros either within ANSYS 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 ANSYS 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 ANSYS 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 ANSYS 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.
4.2.1. Macro File Naming Conventions
Macros are a sequence of ANSYS commands stored in a file. Macros should not have the same name as an existing ANSYS command; ANSYS will execute 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 an ANSYS 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 UNIX or Windows file systems.
To ensure that you are not using the name of an ANSYS command, before creating a macro try running the file name that you wish to use as an ANSYS command. If ANSYS 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 ANSYS to execute the macro as it would any internal command. You should avoid using the extension .MAC because it is used for ANSYS internal macros.
4.2.2. Macro Search Path
By default, ANSYS searches for a user macro file (.mac extension) in the following locations:
1. The ANSYSnn/docu directory.
2. 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 ANSYS installation and configuration guide for your platform.
3. The directory designated by the $HOME environment variable.
4. 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 ANSYSnn/docu directory or some commonly accessible directory that everyone can reference through the ANSYS_MACROLIB environment variable.
· For Windows Me users: You must designate the "home directory" and drive using environment variables, see the ANSYS Installation and Configuration Guide for Windows.
· For Windows XP, 2000, and NT 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. See the ANSYS Installation and Configuration Guide for Windows for more information.
4.2.3. Creating a Macro Within ANSYS
You can create a macro by four methods from within ANSYS:
· 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 ANSYS 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 Utility Menu>Macro>Create Macro menu item. This method opens a dialog box that can be used as a simple, multi-line 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.
4.2.3.1. Using *CREATE
Issuing *CREATE redirects ANSYS 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 ANSYS 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.
4.2.3.2. Using *CFWRITE
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 ANSYS 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.
4.2.3.3. Using /TEE
Issuing /TEE,NEW or /TEE,APPEND redirects ANSYS 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 ANSYS 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 ANSYS 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 ANSYS Commands Reference.
4.2.3.4. Using Utility Menu>Macro>Create Macro
Choosing this menu item opens an ANSYS 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.
|
|