HM-8040: Create a Macro from a Tcl Script

In this tutorial you will create a Utility menu macro from a Tcl script.

You will:
  • Determine the commands to save the current HyperMesh model
  • Create a Utility menu macro to execute the commands

  • Create a new button on the User page of the Utility menu to run the macro

In order to execute command file commands or Tcl scripts from a button on any of the HyperMesh Utility menu pages, a Utility menu macro must first be defined. A Utility menu macro contains valid command file or templex commands that execute the appropriate operations, and is defined using the *beginmacro and *endmacro commands. Macros may accept data passed to them using the arguments $1, $2, and so on. Each argument specifies where the values should be substituted. These macros are defined within the .mac files, including the userpage.mac file.

The following skeleton code shows the format of a Utility menu macro:
*beginmacro(macroname)
            command statements go here
        *endmacro()

Utility menu macros consist of HyperMesh Tcl modify commands.

Delete the Command File

In this step you will delete the existing command file.

  1. Locate the existing command.tcl file in the working directory.
  2. Delete the file.

Perform Operations

In this step you will perform operations in HyperMesh.

Every command issued in HyperMesh appears in the order executed and is reflected in the command file.

  1. From the menu bar, select File > Save as > Model.
  2. Using the File Browser, locate a directory to save the temporary file with the name temp.hm.
  3. Click Save.

Extract Commands

In this step you will extract the commands from the command file.

  1. Open the command file using any text editor.
  2. Locate the *writefile command at or near the end of the command file. This is the command that writes the model file.
  3. Select and copy this line.

Create a Script and Convert Commands

In this step you will create a Tcl script named savefile.tcl and convert the commands to tcl format.

  1. Create a new file named savefile.tcl using any text editor.
  2. Paste the *writefile command copied from the command file inside the savefile.tcl file.
  3. Remove all () and replace them with spaces. Also remove " ". The command should look as follows:
    *writefile temp.hm 0
  4. Add the command *answer yes after the *writefile command.

    The command *answer yes automatically answers “yes” if prompted to overwrite the file in the event temp.hm already exists. Notice that there are no parentheses.

  5. Save the savefile.tcl script in the current working directory.

Create a Macro to Run a Script

In this step you will create a Utility menu macro that runs a Tcl script.

  1. Create a new Utility menu macro that calls the *evaltclscript command to run a Tcl script, using the macro wrapper commands *beginmacro and *endmacro. In the *beginmacro command, name the macro EvalTcl.
    *beginmacro("EvalTcl")
                    *evaltclscript($1,0)        
                *endmacro()

    The macro name EvalTcl will be used to connect the button with the macro via the macroName field in the *createbutton command.

  2. Save the userpage.mac file.

Add the Macro Button

In this step you will add the macro button to the User page.

  1. Create a new button in the userpage.mac file.
    *createbutton(5,"SaveFile TCL",15,0,10,GREEN,"Save file using TCL macro", 
    "EvalTcl","savefile.tcl")

    This creates a button on page 5 (User page), names it, places it in the 20th row, starts it at column 0, sets its width at 10 columns, applies to it the color green, provides a help string and references the macro EvalTcl defined in Step 5.

    Notice that the full path is not used to reference the savefile.tcl script. A full path can be specified if the file is not located in one of the predefined paths that HyperMesh searches to find scripts. Users can add additional search paths using the TCL_INCLUDE environment variable. Relative paths can also be used from these search paths.

  2. Save the userpage.mac file.

Reload the Command File

In this step you will reload the current .mac file to load the modified userpage.mac.

From the menu bar, select Preferences > Menu Config and click on retrieve next to macro file.

Make sure to load the proper .mac file from the hm\scripts\<profile name> directory based on the current user profile, or load the default hm.mac in hm\bin\<platform> if no user profile is loaded.

Test the Macro

In this step you will test the macro.

  1. Click the User button on the Utility menu.
    The new button labeled Save File TCL should be on the User page.
  2. Click this button to automatically save your file.
    The file is saved to the directory specified in the *writefile command. In this case no directory is specified so HyperMesh saves the file to the start-up or current working directory. It will always save with the name specified in the macro, in this case temp.hm.