04 Chapter 4 Using TCL To Control The HyperMesh Session 12 1 PDF
04 Chapter 4 Using TCL To Control The HyperMesh Session 12 1 PDF
Chapter 4
The Tcl scripting language can be utilized to provide support for more advanced
tasks. Using the previous example, Tcl could be used to request a name for the
load collector from the user and use that information to tell the HyperMesh
session to create a load collector with the user provided name.
Notice that while the parentheses and commas have been removed, each of the
commands still begin with an asterisk (*) and that the command name hasn’t
changed. In addition to the syntax change, Tcl core commands can also be used
(these are the commands presented in Chapter 1). The addition of the Tcl
commands provides added functionality to the macros.
where both the caption and message are optional arguments. The caption is
shown in the panel while the message is shown in the message bar. For
example:
This command can be used with the set Tcl command to assign the string
entered in the panel to a variable:
With the addition of the HyperMesh Tcl commands, there are a few commands
which replace the HyperMesh Modify commands. Specifically there are the
hm_createmark and hm_clearmark commands. These commands perform
the same functionality as the *createmark and *clearmark commands, but
they are the HyperMesh Tcl equivalent. The advantage of using these
commands is that it avoids situations where additional Tcl commands need to be
used to evaluate the HyperMesh command. Using hm_createmark and
hm_clearmark when writing Tcl scripts is the more efficient and recommended
method.
HyperWorks 12.0 Introduction to HM Customization 49
The following table contains a list of the commonly used HyperMesh Tcl
commands as well as a short description of that command. For a full explanation
of the commands, please refer to the Reference Guide in the HyperWorks help.
In the Command Window, users can evaluate any command that can be issued
in Tcl or in HyperMesh through Tcl. Let’s examine a few commands. General
Unix command such as ls, pwd, and cd can be used within the Command
Window. The ls command will return the directory contents, the pwd command
will return the current working directory, and the cd command allows users to
change directories.
The above command with the provided options will return the current template
directory. Any command that can be issued in Tcl or in HyperMesh through the
Tcl command layer can be entered at the Command Window prompt and it will
be evaluated. This capability provides the automation tool developer an easy
means of testing a concept before writing the full procedure.
The Command Window also provides the ability to run Tcl scripts by either using
the source command or using the File/Load Menu option. When using the
source command, the complete path and filename needs to be given (unless
the file is located in the directory the user is currently in within the Command
Window).
source C:/temp/my_macro.tcl
The other option is to use the File/Load Menu. This menu is available in the
Command Window by right clicking in the Command Window (shown below).
You can also save the session contents in the Command Window using the
File/Save Menu option.
Tcl scripts can also be run in HyperMesh by creating a button on the Utility
menu User page. As was shown in Chapter 2, when creating a button for a Tcl
script, the EvalTcl basic macro will need to be referenced in the
*createbutton() command. In the examples and exercises following, we will
be using this macro to add buttons to the User page for the Tcl scripts that will be
written.
Step 3: Run HyperMesh Tcl and core Tcl commands from the
Command Window
Create a mark which contains all the elements in the component upper using
the “by comp name” option in the *createmark command. In the Command
Window, type the following:
Step 6: Retrieve the element ids from the mark and store them
in a Tcl list variable.
In order to get the elements ids stored in the mark, the hm_getmark command is
used. This command returns the entity IDs of the passed entity type from the
specified mark In the Command Window, type the following:
Step 7: Count the number of elements in the list and display the
result
By using the llength Tcl command, the number of entities in the list elemIds
can be found.
HyperWorks 12.0 Introduction to HM Customization 53
In the Command Window the value of 390 is returned, which is the number of
elements in the component collector upper.
puts $numElems
Again the value returned is 390 which is the number of elements in the
component collector upper.
1. Right click in the Command Window and select File >> Load File.
2. Select the command_window_ex.tcl file.
“390” is returned.
1. Open HyperMesh.
2. From File > Open, select the file c_channel-tcl_vector.hm
1. Click the Model tab in the tab or go to the menu bar and select View >
Browsers > HyperMesh > Model.
2. Right-click in the white blank area and from the pop-up menu, select
Create > Load Collector..
3. For Name, type forces.
4. Select any color for Color.
5. Verify that there is a check mark next to Close dialog upon creation.
6. Click Create to create the load collector.
1. Open a text editor and create a new text file named create_force.tcl.
Save the file to your HyperMesh working directory (My Documents on
Windows).
2. From your HyperMesh working directory, open the command.cmf file.
3. Locate the following four commands at or near the end of the file.
4. Copy these commands and add them to the text file create_force.tcl
1. Before the *createmark command, add the following line which clears
the node ids currently in mark 1.
*clearmark nodes 1
1. Get a force magnitude from the user using a HyperMesh panel and store it
in a variable. Enter the following line after the *createmarkpanel
command.
2. Substitute the hard coded force magnitude with the variable mag_val.
Step 11: Test the script by running it from the command window
1. Go to the currently open HyperMesh session.
2. Open the file c_channel-tcl_vector.hm.
This refreshes the HyperMesh database. The load collector and forces
you created earlier no longer exist.
3. Go to the View menu and select Command Window.
4. In the Command Window, type the following and then press Enter:
source create_force.tcl
Step 13: Load the userpage.mac file into the current HyperMesh
session.
The userpage.mac file can be loaded into the current HyperMesh session in a
couple of ways. By changing the User Profile the userpage.mac file gets
reloaded. While this method works, if you don’t want to change the user profile,
another method is to simply reload the hm.mac file or to simply close and reopen
HyperMesh.
Step 14: Test the script from the Utility menu’s button.
1. Open the file c_channel-tcl_vector.hm.
2. In the Utility tab’s User page, click the Create Force button. A
HyperMesh panel with a nodes selector appears.
3. Select node(s) in any manner you wish.
4. Click proceed to continue the script’s task.
A HyperMesh panel with an input field for the force magnitude appears.
5. For Magnitude =, type any number.
6. Click proceed to continue the script’s task.
The force(s) are created.
Practical Exercises
Exercise 4a
Description
Using the file c_channel-tcl_vector.hm, create a HyperMesh Tcl script to
automate the following task
1) Create a component collector with a user specified name
2) Select elements and move those elements into the new component
collector.
3) Translate the elements in the new component collector a user
specified distance in the z direction.
Writing this script will make you more familiar with the general process for
creating HyperMesh Tcl scripts.
hm_getstring *collectorcreateonly
*createmarkpanel *movemark
*clearmark *createvector
hm_getfloat *translatemark
set
Hints
Exercise 4b
Description
Using the file c_channel-tcl_vector.hm, for each element in the HyperMesh
model, build a list where the contents of the list are as follows
*createmark hm_getmark
hm_nodelist
for foreach
list lappend
set incr
if elseif
else
Hints
The majority of this assignment will be discovering where to find information
about commands and how to apply the information you find. Be patient and use
the resources available to you, including the HyperWorks online help.