HM-8020: Create a Utility Menu Macro

In this tutorial, you will create a Utility menu macro from a command file.

You will:
  • Determine the commands to save the current HyperMesh model
  • Create a Utility menu macro to execute the command
  • 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.

Define the Task

In this step you will define the task.

The first step in creating a macro is to define the process you want to automate and recognize the individual tasks to reach the desired conclusion. Here, you want to create a one-button macro to automate saving the current HyperMesh model to a file named temp.hm.

  1. From the menu bar, select File > Save as > Model
  2. Use the File Browser to locate a directory and type the filename.
  3. Click Save.

Delete the Existing Command File

In this step you will delete the existing command file.

The command file is located in the current working directory. When first opening HyperMesh, the file is created in the directory HyperMesh is launched from. As soon as you begin working in HyperMesh all executed commands are written to the command file. If the file already exists, the commands are appended to the file. Deleting the file allows HyperMesh to create a new file and allows the user to easily find the relevant commands.

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

Perform Operations

In this step you will execute the full process within 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.

    This is just a temporary file and will be overwritten each time the macro is executed.

  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 file. This is the command that writes the model file.
  3. Select and copy the line.

Add Commands

In this step you will add the commands to the file, userpage.mac.

  1. Open the userpage.mac file using any text editor.
  2. Paste the *writefile command copied from the command file inside the userpage.mac file.

Modify Commands

In this step you will modify commands as necessary and add Utility menu macro wrapper commands.

  1. Remove the path in the *writefile command so that it looks like the following:
    *writefile("temp.hm",0)
  2. Enclose the commands from Step 5 between the wrapper commands, *beginmacro and *endmacro. In the *beginmacro command, name the macro macroSave.
    *beginmacro(macroSave)
                    *writefile("temp.hm",0)
                 *endmacro()
    The macro name macroSave will be used to connect the button with the macro via the macroName field in the *createButton command.
  3. 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.
  4. Save the userpage.mac file.

Add the Macro Button

In this step you will create a button on the User page to execute the macro.

  1. Create a new button in the userpage.mac file:
    *createbutton(5,"Save File",20,0,10,GREEN,"Save file","macroSave")
    This creates a button on page 5 (User page), names it, places it in the 20th row, starts it at column 0, sets the width at 10 columns, applies to it the color green, provides a help string and references the macro macroSave defined in Step 6.
  2. Save the userpage.mac file.

Reload the File

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

To reload the current macro menu .mac file, select Preferences > Menu Config from the menu bar 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 file in the hm\bin\<platform> directory 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 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).