HyperMesh and BatchMesher

HM-8040: Create a Utility Menu Macro from a Tcl Script

HM-8040: Create a Utility Menu Macro from a Tcl Script

Previous topic Next topic No expanding text in this topic  

HM-8040: Create a Utility Menu Macro from a Tcl Script

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

In this tutorial 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

Tools


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, etc.  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.

 

Exercise


In this exercise you will create  a Tcl script from the command file commands, create a Utility Menu macro that runs the Tcl script and add a button on the User page that will launch the macro:

1.Define the task.
2.Delete the existing command.cmf file.  This file is located in either the start-in directory or the current working directory.
3.Perform the operations in HyperMesh that the script should run.
4.Extract the commands from the command.cmf file.
5.Create a Tcl script by converting the commands to Tcl format and modifying as necessary.
6.Create a new Utility Menu macro that runs a Tcl script.
7.Add macro button using *createbutton that calls the macro created in Step 6 with the appropriate Tcl script filename.
8.Reload the current .mac file into HyperMesh to load the modified userpage.mac.
9.Test the macro.

Step 1:  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. The actions necessary to complete this task are:

From the menu bar, select File > Save as > Model.
Use the file browser to locate a directory and enter the name for the filename.
Click Save.

Step 2:  Delete the existing command.cmf file.

The current command.cmf 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.cmf 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.

Step 3:  Perform the operations in HyperMesh.

Execute the full process within HyperMesh.  Every command issued in HyperMesh appears in the order executed and is reflected in the command.cmf file.

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

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

3.Click Save.

Step 4:  Extract the commands from the command.cmf file.

1.Open the command.cmf file using any text editor.
2.Locate the *writefile command at or near the end of the command.cmf file.

This is the command that writes the model file.

3.Select and copy this line.

Step 5:  Create a Tcl script names savefile.tcl, convert the commands to Tcl format and modify as necessary.

1.Create a new file named savefile.tcl using any text editor.
2.Paste the *writefile command copied from the command.cmf file inside the savefile.tcl file.
3.Remove all () and , and replace them with spaces. Also remove the “ “. The command should look like:

*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.

Step 6:  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.

Step 7:  Add the macro button.

Create a button on the User page to execute the macro.

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 6.

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.

Step 8:  Reload the current .mac file into HyperMesh to load the modified userpage.mac.

To reload the current macro menu .mac file while HyperMesh is open, 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 in hm\bin\<platform> if no user profile is loaded.

Step 9:  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.

 

 

See Also:

HyperMesh Tutorials