Creating Scripts

The functionality of the HyperMesh Tcl Modify commands are available through Tcl. Additionally, commands that allow you to extract information from the HyperWorks Desktop database, such as entity ID numbers on a mark, a list of assemblies, component names, elements per component, nodes per element, node values, and so forth. These are known as HyperMesh Tcl Query commands. They are used to query information from the database or template system.

In general, the process of determining the commands to use to automate a process or create a script involves the following steps:
  1. Define the task
  2. Delete the existing command.tcl file. This file is located in either the start-in directory or the current working directory.
  3. Perform the operations in HyperWorks Desktop that the script should run.
  4. Modify as necessary and introduce Tcl decision logic.
Here is an example of the process using the c-channel0.hm, found in the <altair_home>/tutorials/hm directory.
  1. Open the Forces panel.
  2. Select one node on the channel model.
    • For the direction of the force choose the z-axis option.
    • For the magnitude enter 23
    • Toggle from magnitude % option to uniform size option for load size and set it to size of 15.
    • Click create.
    • Open the command.tcl file. At the bottom of the file should be the lines:
      *createmark nodes 1 3237
      *loadcreateonentity nodes 1 1 1 0 0 23 0 0 23
Simply running the above commands will work without a problem, but note that the commands are hard coded to the single node picked when generating the command file. This is useful for a generic utility. However, to make this script accept user input as to which node(s) to use, make the following changes:
*createmarkpanel nodes 1 "Select nodes for load creation";
*loadcreateonentity nodes 1 1 1 0 0 23 0 0 23;
If you wish to specify the magnitude, make a modification to prompt for input:
set magVal [hm_getfloat "Magnitude=" "Enter force magnitude"];
*createmarkpanel nodes 1 "Select nodes for load creation";
*loadcreateonentity nodes 1 1 1 0 0 $magVal 0 0 $magVal;
Save the file as create_force.tcl. This file can be run from the command window using the source command:
source create_force.tcl
The Tcl scripting language can be utilized to provide support for more advanced tasks. Using the previous example, Tcl can be used to request a file name from the user that contains data lines with the node ID and force magnitude value for creating loads. This data can then be read using Tcl and the appropriate HyperWorks Desktop Tcl commands executed to create the loads:
set fileName [hm_getfilename "Select the file for input:"];
set fileID [open $fileName];
foreach dataLine [split [read $fileID] \n] {
   set nodeID [lindex $dataLine 0];
   set magVal [lindex $dataLine 1];
   *loadcreateonentity nodes 1 1 1 0 0 $magVal 0 0 $magVal;
}

If a command or Tcl script runs successfully when executed from the command window, a blue 1, or output from the last command will show on the next line. An error will cause the output to be red with a link to the stack trace. Error messages may also appear in popup windows with links to the stack trace as well. These error messages are important in order to debug scripts developed using the Tcl layer. They provide information on the specific command that caused the error, as well as the procedure name and line number of the command in order to quickly find and fix the issue.