HyperMesh Customization
Introduction to HyperMesh Customization
Altair Engineering Support Contact Information Web site www.altairhyperworks.com
Location Telephone e-mail
Australia 64.9.413.7981 [email protected] Brazil 55.11.3884.0414 [email protected] Canada 416.447.6463 [email protected] China 86.400.619.6186 [email protected] France 33.1.4133.0992 [email protected] Germany 49.7031.6208.22 [email protected] India 91.80. 6629.4500 1.800.425.0234 (toll free) [email protected] Italy 39.800.905.595 [email protected] Japan 81.3.5396.2881 [email protected] Korea 82.70.4050.9200 [email protected] Mexico 55.56.58.68.08 [email protected] New Zealand 64.9.413.7981 [email protected]
North America 248.614.2425 [email protected] Scandinavia 46.46.460.2828 [email protected] United Kingdom 01926.468.600 [email protected]
In addition, the following countries have resellers for Altair Engineering: Colombia, Czech Republic, Ecuador, Israel, Russia, Netherlands, Turkey, Poland, Singapore, Vietnam, Indonesia
Official offices with resellers: Canada, China, France, Germany, India, Malaysia, Italy, Japan, Korea, Spain, Taiwan, United Kingdom, USA
Copyright© Altair Engineering Inc. All Rights Reserved for:
HyperMesh® 1990-2014; HyperCrash® 2001-2014; OptiStruct® 1996-2014; RADIOSS®1986-2014; HyperView®1999-2014; HyperView Player® 2001-2014; HyperStudy® 1999-2014; HyperGraph®1995-2014; MotionView® 1993-2014; MotionSolve® 2002-2014; HyperForm® 1998-2002-2014; HyperXtrude® 1999-2002-2014; Process Manager™ 2003-2002-2014; Templex™ 1990-2002-2014; TextView™ 1996-2014; MediaView™ 1999-2014; TableView™ 2013-2014; BatchMesher™ 2003-2014; HyperMath® 2007-2014;
Manufacturing Solutions™ 2005-2014; HyperWeld® 2009-2014; HyperMold® 2009-2014; solidThinking® 1993-2014; solidThinking Inspire® 2009-2014; solidThinking Evolve®™ 1993-2014; Durability Director™ 2009-2014; Suspension Director™ 2009-2014; AcuSolve® 1997-2014; AcuConsole® 2006-2014; SimLab®™2004-2014 and Virtual Wind Tunnel™ 2012-2014.
In addition to HyperWorks® trademarks noted above, Display Manager™, Simulation Manager™, Compute Manager™, PBS™, PBSWorks™, PBS GridWorks®, PBS Professional®, PBS Analytics™, PBS Desktop™, PBS Portal™, PBS Application Services™, e-BioChem™, e-Compute™ and e-Render™ are trademarks of ALTAIR ENGINEERING INC.
Altair trademarks are protected under U.S. and international laws and treaties. Copyright© 1994-2014. Additionally, Altair software is protected under patent #6,859,792 and other patents pending. All other marks are the property of their respective owners. ALTAIR ENGINEERING INC. Proprietary and Confidential. Contains Trade Secret Information. Not for use or disclosure outside of ALTAIR and its licensed clients. Information contained inHyperWorks® shall not be decompiled, disassembled, or “unlocked”, reverse translated, reverse engineered, or publicly displayed or publicly performed in any manner. Usage of the software is only as explicitly permitted in the end user software license agreement.
Table of Contents
Introduction to HyperMesh
Customization
Chapter 1: Introduction to TCL ... 5 About TCL ... 5 About Tk ... 6 Basic Tcl Syntax ... 6 TCL Command Overview ... 6 Tk Basic Commands ... 21Chapter 2: HyperMesh Process Automation Introduction ... 27
What is a HyperMesh Macro ... 27
The HyperMesh Utility Menu ... 28
Practical Exercises ... 33
Chapter 3: HyperMesh Automation with HyperMesh Macros ... 35
HyperMesh Commands ... 35
HyperMesh Macro Structure ... 39
Modifying Commands to Create Generalized Macros ... 44
Practical Exercises ... 46
Chapter 4: Using Tcl to Control the HyperMesh Session ... 47
HyperMesh Commands vs. Tcl Modify Commands ... 47
Tcl GUI Commands and Tcl Query Commands ... 48
Using the Command Window ... 50
Process to Create a Tcl HyperMesh Macro ... 55
Practical Exercises ... 60
Chapter 5: Retrieving Data from HyperMesh Entities ... 63
HyperMesh Entities and Their Data Names ... 63
Pointers and Flags ... 65
Process for Creating a HyperMesh Tcl Script to Retrieve Data on HyperMesh Entities ... 67
Practical Exercises ... 71
Chapter 6: Interfacing with HyperMesh Solver Templates ... 75
Solver Attributes and Card Images ... 76
Querying Solver Attributes ... 77
Assigning Data to Solver Attributes ... 81
Creating a Reusable and Modular Procedure ... 82
Updating Solver Attributes ... 88
Practical Exercises ... 91
Chapter 1: Introduction to Tcl/Tk
Chapter 1
Introduction to Tcl/Tk
About TCL
Tcl (Tool Command Language) is used by developers worldwide and has become a critical component in thousands of corporations. It has a simple and programmable syntax and can be either used as a standalone application or embedded in application programs. Best of all, Tcl is open source so it's completely free.
The main difference between Tcl and languages such as C is that Tcl is an interpreted rather than a compiled language. Tcl programs are simply scripts consisting of Tcl commands that are processed by a Tcl interpreter at run time. One advantage that this offers is that Tcl programs can themselves generate Tcl scripts that can be evaluated at a later time. This can be useful, for example, when creating a graphical user interface with a command button that needs to perform different actions at different times. One of Tcl's most useful features is its extensibility. If an application requires some functionality not offered by standard Tcl, new Tcl commands can be implemented using the C language, and integrated fairly easily. Since Tcl is so easy to extend, many people have written extension packages for some common tasks, and made these freely
available on the Internet.
Businesses and engineering teams today are often faced with the problem of making diverse collections of resources work together. We call these programming tasks integration applications. For enterprises, the integration platform is becoming as strategically important as the operating system and database platforms.
Tcl is the integration platform of choice because of its speed of use, breadth of functionality, and enterprise-ready features such as thread-safety, internationalization and cross platform deployment. The latest version of Tcl provides all the features an enterprise needs for all integration and scripting needs.
Chapter 1: Introduction to Tcl/Tk
About TK
Tk is a graphical user interface toolkit that makes it possible to create powerful GUIs quickly. It proved so popular that it now ships with all distributions of Tcl. Tk shares many concepts with other windowing toolkits, but you do not need to know much about graphical user interfaces to get started with Tk.
Tk adds about 35 Tcl commands that let you create and manipulate widgets in a graphical user interface. A widget is a window in a graphical user interface that has a particular appearance and behavior. The terms widget and window are often used interchangeably. Widget types include buttons, scrollbars, menus, and text windows. Tk also has a general-purpose drawing widget called a canvas that lets you create lighter-weight items such as lines, boxes, and bitmaps.
Tcl and Tk are highly portable, running on essentially all flavors of Unix (Linux, Solaris, IRIX, AIX, *BSD*, the list goes on and on), Windows, Macintosh, and more.
Some basic Tk commands will be discussed later. For additional information, please refer to the manual.
Basic Tcl Syntax
• First line of a command line is a command name.
• The words in the command line are separated by one or more spaces. • Words can be grouped with double quotes or curly braces.
• Commands are terminated with new line or semi-colon.
• A word starting with a dollar sign ($) must be a variable name. The string will be replaced by the value of the variable.
• Words enclosed within square brackets must be a legal Tcl command. The strings would be replaced by the results of evaluating the command.
TCL Command Overview
In the following command overview it is recommend that the students try the examples themselves.
1. Text Output
Printing out a string using Tcl is done using the puts command. A single unit of text after the puts command will be output.
Example 1.1
puts HyperWorks
Chapter 1: Introduction to Tcl/Tk
If the string is more than one word, the string must be enclosed in either double quotes (“ “) or braces ({ }). Many commands, including the puts command, accept multiple arguments. If the string is not enclosed in quotes or braces, the individual words are considered arguments. A set of words enclosed in quotes or braces will treated as a single unit. The two options of quotes or braces behave differently and the differences will be discussed in the next section.
Example 1.2
puts “This is an example with quotes” puts {This is an example with braces}
Output:
This is an example with quotes This is an example with braces
As mentioned above, a command in Tcl is terminated with a newline or semicolon. Tcl comments are designated with a # at the beginning of the line or after a semicolon.
Example 1.3
# An example using a semicolon
puts “This is line 1”; puts {This is line 2};#Note after the ;
Output: This is line 1 This is line 2
2. Variables and Variable Substitution
Unlike C, Tcl does not require that variables be declared before they are used. Tcl variables are simply created when they are first assigned values. Values are assigned using the set command. Although they do not have to be deleted, Tcl variables can be deleted using the unset command.
The value stored in a variable can be accessed by prefacing the name of the variable with a dollar sign ("$"). This is known as variable substitution, and is illustrated in the examples below. A variable can be used to store a number, a date, a string, or even another Tcl script.
Example 2.1:
set software "HyperWorks"
puts "The software we are using is $software"
Output:
Chapter 1: Introduction to Tcl/Tk
Example 2.1 illustrates the use of variable substitution. The value "HyperWorks" is assigned to the variable "software", whose value is then substituted for $software. Note that variable substitution can occur within a string.
As Example 2.2 (below) shows, the backslash (\) disables substitution for the single character following the backslash. Any character following the backslash will stand without substitution. This is also true when the backslash precedes a quote, brace, or bracket.
Example 2.2 set Z Albany
set Z_LABEL “This Capitol of New York is: “ puts “$Z_LABEL $Z” ; # Prints the value of Z
puts “$Z_LABEL \$Z”; #Prints a literal $Z instead of the value of Z
Output Albany
The Capitol of New York is:
The Capitol of New York is: Albany The Capitol of New York is: $Z
When defining a string, if there are multiple words in the string we have learned that we can either use double quotes or braces. The difference between these two options is how they treat variables that are in their strings.
Example 2.3
puts "$Z_LABEL $Z" puts {$Z_LABEL $Z}
Output
The Capitol of New York is: Albany $Z_LABEL $Z
Example 2.3 shows how using the double brace actually disables the substitution of variables within the braces. Note that braces have this effect only when they are used for grouping (i.e. at the beginning and end of a sequence of words). If a string is already grouped, either with quotes or braces, and braces occur in the middle of the grouped string (i.e. "foo{bar"), then the braces are treated as regular characters with no special meaning. If the string is grouped with quotes, substitutions will occur within the quoted string, even between the braces.
Example 2.4: set month 2 set day 3 set year 09
Chapter 1: Introduction to Tcl/Tk
puts $date
Output: 2:3:09
Here variable substitution is used in several places: The values of the variables "month", "day", and "year" are substituted in the set command that assigns the value of the "date" variable, and the value of the "date" variable is then substituted in the line that displays the output.
Example 2.5:
set foo "puts hi" eval $foo
Output: hi
In this example, the variable "foo" holds another (small) Tcl script that simply prints the word "hi". The value of the variable "foo" is substituted into an eval command. The
eval command takes one or more arguments which together comprise a Tcl script and
passes it to the Tcl interpreter.
3. Expressions
Tcl allows several types of expressions, including mathematical expressions and
relational expressions. Tcl expressions are usually evaluated using the expr command. A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between the operands and operators and parentheses; it is ignored by the expression's processor.
Operands may be specified in any of the following ways: • As a numeric value, either integer or floating-point.
• As a Tcl variable, using standard $ notation. The variable's value will be used as the operand.
• As a string enclosed in double-quotes. The expression parser will perform backslash, variable, and command substitutions on the information between the quotes, and use the resulting value as the operand
• As a string enclosed in braces. The characters between the open brace and matching close brace will be used as the operand without any substitutions. • As a Tcl command enclosed in brackets. The command will be executed and its
result will be used as the operand.
The valid operators are listed below, grouped in decreasing order of precedence. For a detailed listing of what these operators please view the Tcl manual.
• - + ~ ! Unary minus, unary plus, bit-wise NOT, logical NOT. • * / % Multiply, divide, remainder.
• + - Add and subtract. • << >> Left and right shift.
Chapter 1: Introduction to Tcl/Tk
• < > <= >= Boolean less, greater, less than or equal, and greater than or equal.
• == != Boolean equal and not equal.
• & Bit-wise AND.
• ^ Bit-wise exclusive OR.
• | Bit-wise OR.
• && Logical AND.
• || Logical OR. Example 3.1: expr 0 == 1 Output: 0 Example 3.2: expr 1 == 1 Output: 1
Examples 3.1 and 3.2 illustrate the use of relational expressions with the expr
command. The first expression evaluates to 0 (false) since 0 does not equal 1, whereas the second expression evaluates to 1 (true), since, obviously, 1 does equal 1. The relational operator "==" is used to do the comparison.
Example 3.3: expr 4 + 5
Output: 9
Example 3.3 shows how to use the expr statement to evaluate an arithmetic expression. Here the result is simply the sum of 4 and 5.
Tcl supports the following mathematical functions in expressions:
abs cosh log sqrt acos double log10 srand asin exp pow tan atan floor rand tanh atan2 fmod round wide ceil hypot sin
cos int sinh
In addition to these predefined functions, applications may define additional functions using Tcl_CreateMathFunc().
Chapter 1: Introduction to Tcl/Tk
Example 3.4: expr sin(2)
Output: 0.909297
This example shows that the expr statement can be used to evaluate the result of a mathematical function, in this case, the sine of an angle.
4. Command Substitution
Just as variable substitution is used to substitute the value of a variable into a Tcl script, command substitution can be used to replace a Tcl command with the result that it returns. Consider the following example:
Example 4.1:
puts "I am [expr 10*2] years old, and my I.Q. is [expr 100 - 25]"
Output:
I am 20 years old, and my I.Q. is 75
As this example shows, square brackets are used to achieve command substitution. The text between the square brackets is evaluated as a Tcl script, and its result is then substituted in its place. In this case, command substitution is used to place the results of two mathematical expressions into a string. Command substitution is often used in conjunction with variable substitution, as shown in Example 4.2:
Example 4.2:
set my_height 6.0
puts "If I was 2 inches taller, I would be [expr $my_height + (2.0 / 12.0)] feet tall"
Output:
If I was 2 inches taller, I would be 6.16667 feet tall
In this example, the value of the variable "my_height" is substituted inside the square brackets before the command is evaluated. This is a good illustration of Tcl's one-pass recursive parsing mechanism. When evaluating a statement, the Tcl interpreter, makes one pass over it, and in doing so makes all the necessary substitutions. Once this is done, the interpreter then evaluates the resulting expression. If, during its pass over the expression, the interpreter encounters square brackets (indicating that command substitution is to be performed), it recursively parses the script inside the square brackets in the same manner.
5. Comparisons and Loops
In all but the simplest scripts, some mechanism is needed to control the flow of
Chapter 1: Introduction to Tcl/Tk
well as looping constructs (while, for, and foreach statements), both of which can alter the flow of execution in response to some condition. The following examples serve to illustrate these constructs.
Example 5.1: set k 35
if {$k == 35} {
puts "Handling is good." } elseif {$k == 20} {
puts "Ride is good." } else {
puts "I am not sure of the quality of ride or handling." }
Output:
Handling is good.
Example 5.1 uses the if statement. It sets the value of the variable "k" to 35, and then uses an if statement to choose which statement to print. The general syntax of the if statement is as follows:
if test1 body1 ?elseif test2 body2 elseif ...? ?else bodyn?
If the test1 expression evaluates to a true value, then body1 is executed. If not, then if there are any elseif clauses present, their test expressions are evalutated and, if true, their bodies are executed. If any one of the tests is made successfully, after its
corresponding body is executed, the if statement terminates, and does not make any further comparisons. If there is an else clause present, its body is executed if no other test succeeds.
Another decision making construct is the switch statement. It is a simplification of the
if statement that is useful when one needs to take one of several actions depending on
the value of a variable whose possible values are known. This is illustrated in Example 5.2, which uses a switch statement to print a sentence, depending on the value of the variable "num_legs".
Example 4.2: set num_legs 4 switch $num_legs {
2 {puts "It could be a human."} 4 {puts "It could be a cow."} 6 {puts "It could be an ant."} 8 {puts "It could be a spider."}
default {puts "It could be anything."} }
Output:
Chapter 1: Introduction to Tcl/Tk
The switch statement has two general forms (both of which are described in detail in the manual page), but the form used here is as follows:
switch ?options? string {pattern body ?pattern body ...?}
The string argument is compared to each of the patterns and if a comparison succeeds, the corresponding body is executed, after which the switch statement returns. The pattern "default", if present, is always matched, and thus its body always executed if none of the earlier comparisons succeed.
It is often useful to execute parts of a program repeatedly, until some condition is met. In order to facilitate this, Tcl offers three looping constructs: the while, for, and foreach statements, each of which is shown in the examples below.
Example 5.3:
for {set i 0} {$i < 5} {incr i 1} {
puts "In the for loop, and i == $i" }
Output:
In the for loop, and i == 0 In the for loop, and i == 1 In the for loop, and i == 2 In the for loop, and i == 3 In the for loop, and i == 4
The general syntax for the for loop is as follows:
for init test reinit body
The init argument is a Tcl script that initializes a looping variable. In the for loop used in Example 5.3, the looping variable was called "i", and the init argument simply set it to 0. The test argument is a Tcl script which will be evaluated to decide whether or not to enter the body of the for loop. Each time this script evaluates to a true value, the body of the loop is executed. The first time this script evaluates to false, the loop terminates. The reinit argument specifies a script that will be called after each time the body is executed. In Example 5.3, the reinit script increments the value of the looping variable, "i" using the incr command. Thus, the for loop in this example executes its body 5 times, before its test script evaluates to false, causing the loop to terminate.
Example 5.4: set i 0
while {$i < 5} {
puts "In the while loop, and i == $i" incr i 1
Chapter 1: Introduction to Tcl/Tk
Output:
In the while loop, and i == 0 In the while loop, and i == 1 In the while loop, and i == 2 In the while loop, and i == 3 In the while loop, and i == 4
Example 5.4 illustrates the use of a while loop, the general syntax of which follows the form:
while test body
The basic concept behind the while loop is that while the script specified by the test argument evaluates to a true value, the script specified by the body argument is executed. The while loop in Example 5.4 accomplishes the same effect as the for loop in Example 5.3. A looping variable, "i", is again initialized to 0 and incremented each time the loop is executed. The loop terminates when the value of "i" reaches 5. Note, that in the case of the while loop, the initialization and re-initialization of the looping variable are not part of the while statement itself. Therefore, the initialization of the variable is done before the while loop, and the reinitialization is incorporated into its body. If these statements were left out, the code would probably still run, but with
unexpected results.
Example 5.5:
foreach vowel {a e i o u} { puts "$vowel is a vowel" } Output: a is a vowel e is a vowel i is a vowel o is a vowel u is a vowel
The foreach loop, illustrated in Example 5.5, operates in a slightly different manner to the other types of Tcl loops described in this section. Whereas for loops and while loops execute while a particular condition is true, the foreach loop executes once for each element of a fixed list. The general syntax for the foreach loop is:
foreach varName list body
The variable specified by varName takes on each of the values in the list in turn, and the body script is executed each time. In Example 5.5, the variable "vowel" takes on each of the values in the list "{a e i o u}" (Tcl list structure will be discussed in more detail
Chapter 1: Introduction to Tcl/Tk
in the next section), and for each value, the body of the loop is executed, resulting in one printed statement each time.
6. Lists
Lists in Tcl provide a simple means by which to group collections of items, and deal with the collection as a single entity. When needed, the single items in the group can be accessed individually. Lists are represented in Tcl as strings with a specified format. As such, they can be used in any place where strings are normally allowed. The elements of a list are also strings, and therefore any form of data that can be represented by a string can be included in a list (allowing lists to be nested within one another).
Lists can be created in the following ways, in addition to being represented by a string.
by setting a variable to be a list of values
set lst {{item 1} {item 2} {item 3}}
with the split command
set lst [split "item 1.item 2.item 3" "."]
with the list command
set lst [list "item 1" "item 2" "item 3"]
An individual list member can be accessed with the lindex command. The following examples will illustrate many important list commands:
Example 6.1:
set simple_list “John Joe Mary Susan” puts [lindex $simple_list 0]
puts [lindex $simple_list 2]
Output: John Mary
Example 6.1 creates a simple list of four elements, each of which consists of one word. The lindex command is then used to extract two of the elements in the list: the 0th
element and the 2nd element. Note that list indexing is zero-based. It is also important to see that the lindex command, along with most other list commands, takes an actual list as its first argument, not the name of a variable containing a list. Thus the value of the variable "simple_list" is substitued into the lindex command.
The items in a list can be iterated through using the foreach command:
foreach varName list body
As mentioned previously, the foreach command will execute the body code one time for each list item in list. On each pass, varName will contain the value of the next
Chapter 1: Introduction to Tcl/Tk
The length of a list can be determined using the llength command. Also, a value can be inserted into a list using the linsert command. Another option is the lappend command which appends values to a list. Examples 6.2, 6.3, and 6.4 show how these commands can be used.
Example 6.2:
set names {Canada India USA UK}
puts “List length is: [llength $names]”
Output:
List length is: 4
Example 6.3
set names {Canada India USA UK}
set newnames [linsert $names 2 China] puts “New list is: $newnames”
Output:
New list is: Canada India China USA UK
Example 6.4
set names {Canada India USA UK} set newnames [lappend names China] puts “New list is: $newnames”
Output:
New list is: Canada India USA UK China
Notice how with lappend the name of a variable containing the list (names) is used rather than the list itself ($names).
7. Arrays
Unlike arrays in many other languages, Tcl arrays are indexed by keywords. The
keywords can be easy to remember strings or integers. The values of the array elements can also be strings or numbers. A Tcl array is created when you assign the first array element:
Example 7.1
set myArray(foo) "bar" puts $myArray(foo)
Output: bar
The array command is used in Tcl to manipulate array data. In Example 7.2 we use the
Chapter 1: Introduction to Tcl/Tk
Example 7.2
array set fruitColors {tomato red banana yellow} puts $fruitColors(tomato)
Output: red
For more information on arrays, please refer to your Tcl manual.
8. Strings
Most often, the task of extracting and manipulating information via a scripting language is described using the term “string manipulation”. Strings are a set of alphanumeric characters stored and manipulated together. These sets are concatenated, truncated, or partitioned by the script with a specific end in mind.
All data items in Tcl, including numeric values, are treated as strings. They are treated as other data types only as needed. This makes string manipulation and the associated commands very important and frequently utilized.
There are a large number of string manipulation commands, as well as pattern and regular expression matching. Example 8.1 contains some commonly used string
manipulation commands. For more in-depth explanations and a complete listing, please refer to the Tcl manual.
Example 8.1
set str "This is Canada" puts "The string is: $str"
puts "The length of the string is: [string length $str]" puts "The character at index 3 is: [string index $str 3]"
puts "The characters from index 3 through end are: [string range $str 3 end]"
puts "The index of the first occurrence of letter \"i\" is: [string first i $str]“
Output:
This is Canada
The string is: This is Canada The length of the string is: 14 The character at index 3 is: s
The characters from index 3 through end are: s is Canada The index of the first occurrence of letter “I” is 2.
9. Writing and Reading Files
Since Tcl/Tk scripts run on a variety of platforms, and there are so many use cases for accessing files, Tcl provides many commands for file manipulation. In this section we will focus on how to open a file and then read and write data from that file. For more in-depth explanations and a complete listing, please refer to the Tcl manual.
Chapter 1: Introduction to Tcl/Tk
The open command is used to open a file. The syntax for this command is:
open filename access
The access argument is used to indicate whether the file is read only (r), write only (w), or read/write (w+), as well as other options. Example 9.1 illustrates how to open a write only file and assign it to a variable. The open command returns a channel identifier which is used by Tcl in the commands we will discuss later.
Example 9.1
set filename [open temp.txt “w”]
Output: file4915610
In order to write data to the open file, the puts command is used. The syntax for the
puts command is
puts ?channelId? string
This command writes the characters given by string to the channel given by
channelId. ChannelId must be a channel identifier such as returned from the open
command. Also, the channel must have been opened for output (or writing). The puts command also normally outputs a newline character after the string. Example 9.2 shows how to open a write only file, add data to it, and then close the file.
Example 9.2
set filename [open "temp.txt" "w"] puts $filename "We live in Canada." puts $filename "Just Testing"
close $filename
Output: file4910020
The last option we will go over is how to read data from a file and assign it to variables. This is done with the gets command. The syntax is
gets channelId
This command reads the next line from channelId and returns everything in the line up to (but not including) the end-of-line character(s). Example 9.3 takes the file we wrote in Example 9.2 and assigns each line to a variable.
Example 9.3
set filname [open "temp.txt" "r"] set line1 [gets $filename]
set line2 [gets $filename] close $filename
Chapter 1: Introduction to Tcl/Tk
puts "Line 1: $line1" puts "Line 2: $line2" Output:
Line 1: We live in Canada. Line 2: Just Testing
10. Procedures
Procedures in Tcl serve a similar purpose to functions in C. They can take arguments, and can return values. The basic syntax for defining a procedure is:
proc name argList body
Once a procedure is created, it is considered to be a command, just like any other built-in Tcl command. As such, it may be called usbuilt-ing its name, followed by a value for each of its arguments. The return value from a procedure is equivalent to the result of a built-in Tcl command. Thus, command substitution can be used to substitute the return value of a procedure into another expression.
By default, the return value from a procedure is the result of the last command in its body. However, to return another value, the return command may be used. If an argument is given to the return command, then the value of this argument becomes the result of the procedure. The return command may be used anywhere in the body of the procedure, causing the procedure to exit immediately.
Example 10.1:
proc sum_proc {a b} {
return [expr $a + $b] }
proc magnitude {num} { if {$num > 0} {
return $num }
set num [expr $num * (-1)] return $num
}
set num1 12 set num2 14
set sum [sum_proc $num1 $num2] puts "The sum is $sum"
puts "The magnitude of 3 is [magnitude 3]" puts "The magnitude of -2 is [magnitude -2]"
Output:
The sum is 26
The magnitude of 3 is 3 The magnitude of -2 is 2
Chapter 1: Introduction to Tcl/Tk
This example first creates two procedures, "sum_proc" and "magnitude". "sum_proc" takes two arguments, and simply returns the value of their sum. "magnitude" returns the absolute value of a number. After the procedure definitions, three global variables are created. The last of these, "sum" is assigned the return value of the procedure "sum_proc", called with the values of the variables "num1" and "num2" as arguments. The "magnitude" procedure is then called twice, first with "3" as an argument, then with "-2".
The "sum_proc" procedure uses the expr command to calculate the sum of its
arguments. The result of the expr command is substituted into the return statement, making it the return value for the procedure.
The "magnitude" procedure makes use of an if statement to take different actions, depending on the sign of its argument. If the number is postive, its value is returned, and the procedure exits immediately, skipping all the rest of its code. Otherwise, the number is multiplied by -1 to obtain its magnitude, and this value is returned. The same effect could be achieved by moving the statement that multiplies the value by -1 into an else clause, but the purpose of this example was to illustrate the use of the return
statement at several locations within a procedure.
11. Namespaces
Namespaces provide a means of organizing procedures and variables in Tcl. A
namespace basically creates a unique local scope. Within each unique scope, variables and procedures created in that scope are isolated from both the global scope and from other scopes. This allows the developer a means to segregate procedures and
variables in an organized manner. Namespaces can be nested, and procedures and variables can be imported to, exported from, and referenced in other namespaces or the global scope.
A global procedure acts just like any function within Tcl. A global procedure can be called from a namespace without any need to import or otherwise localize the procedure. A procedure within a namespace is local to the namespace unless it is explicitly called or the procedure is exported from the namespace. The global scope is also designed such that any variables defined outside of a procedure or a namespace are automatically in the global scope. To access a global variable in a local scope, such as a procedure or namespace, you will need to use the global command. As a script grows and as the number of scripts running simultaneously increases, the ability of the developer to avoid conflict between the names of procedures and variables defined in the global scope decreases.
The namespace eval command lets you create new namespaces. For example,
namespace eval Counter { namespace export bump variable num 0
Chapter 1: Introduction to Tcl/Tk
variable num incr num }
}
creates a new namespace containing the variable num and the procedure bump. The commands and variables in this namespace are separate from other commands and variables in the same program. If there is a command named bump in the global namespace, for example, it will be different from the command bump in the Counter namespace.
For more information on namespaces, please refer to your Tcl manual.
Tk Basic Commands
In this section we will go over 4 basic Tk widgets that are used with Tcl. These are the
tk_getOpenFile, tk_getSaveFile, tk_chooseDirectory, and
tk_messageBox procedures. These procedures allow you to set the full file name and
directory paths to be used in your Tcl scripts.
1. tk_getOpenFile
The tk_getOpenFile pops up a dialog box for the user to select a file to open. The
tk_getOpenFile command is usually associated with the Open command in the File
menu. Its purpose is for the user to select an existing file only. If the user enters a non-existent file, the dialog box gives the user an error prompt and requires the user to give an alternative selection. This dialog box does not open a file, it simply returns the filename so that it can be used in your script.
Example 1.1 tk_getOpenFile
This command creates the GUI in Figure 1 where you can select an existing file from a dialog box:
Chapter 1: Introduction to Tcl/Tk
Figure 1: GUI created from tk_getOpenFile command.
Example 1..2
set filename [tk_getOpenFile] puts $filename
Output:
C:/Documents and Settings/training/My Documents/autosave.mvw
This sets the value of the variable filename to be the full file name of the file selected in the dialog box (as shown in the output above).
There are additional options that can be used with tk_getOpenFile. The format for the tk_getOpenFile with options is:
tk_getOpenFile ?option value ...?
The option presented below is the -filetypes option. There are additional options available, please refer to the manual for these other options.
-filetypes filePatternList
If a filetypes listbox exists in the file dialog on the particular platform, this option gives the filetypes in this listbox. When the user chooses a file type in the listbox, only the files of that type are listed. If this option is unspecified, or if it is set to the empty list, or if the filetypes listbox is not supported by the particular platform, then all files are listed regardless of their types. Example 1.3 below shows how to set the filetypes list and use it with the –filetypes option.
Chapter 1: Introduction to Tcl/Tk set types { {{Text Files} {.txt} } {{TCL Scripts} {.tcl} } {{All Files} * } }
set filename [tk_getOpenFile -filetypes $types]
Figure 2: GUI from tk_getOpenFile command with -filetypes option.
In Figure 2 above, the available file types are set to Text Files, TCL Scripts, and All Files. As a result of using the –filetypes option, only these file types are listed.
2. tk_getSaveFile
The procedure tk_getSaveFile pops up a dialog box for the user to select a file to save. This command is usually associated with the Saveas command in the File menu. If the user enters a file that already exists, the dialog box prompts the user for
confirmation whether the existing file should be overwritten or not. The dialog itself does not write out the file; it only returns the file name for your script to use.
This procedure behaves the same way as the tk_getOpenFile procedure does. It even has the same options available. Below the –title option is shown.
Exercise 2.1
set filename [tk_getSaveFile –title “Select a File”] puts $filename
Output:
Chapter 1: Introduction to Tcl/Tk
Figure 3: GUI from tk_getSavwFile command with -title option.
Notice that the title of the dialog box in Figure 3 is “Select a File”.
3. tk_chooseDirectory
The procedure tk_chooseDirectory pops up a dialog box for the user to select a directory. Example 3.1 shows how to set a directory name to a variable.
Example 3.1
set dirname [tk_chooseDirectory] puts $dirname
Output: C:/temp
Chapter 1: Introduction to Tcl/Tk
Figure 4: GUI from tk_chooseDirectory command.
4. tk_messageBox
The procedure tk_messageBox procedure creates and displays a message window with an application-specified message, an icon, and a set of buttons. The buttons, icon, and message are specified with the various options that are available. Please refer to your manual for a complete listing of these options.
Example 3.1 illustrates the tk_messageBox command with the message option. This option allows you to specify the message to be displayed in the message box.
Example 4.1
tk_messageBox -message "This is the message to be displayed"
Output:
Chapter 2: HyperMesh Process Automation Introduction
Chapter 2
HyperMesh Process
Automation Introduction
In this chapter we will discuss what HyperMesh automation is and the different levels of automation available within HyperMesh. We will also examine the HyperMesh Utility menu, the files that are associated with it, and how to run macros and scripts from it. Finally an example will be given and after which an exercise will be done.
What is a HyperMesh Macro
A HyperMesh macro functions like a script or command file and is used to automate a HyperMesh process or execute a series of steps. The HyperMesh macro language is an extension of the HyperMesh command layer. The types of macros you can write vary from basic to advanced. Basic types are simple macros of HyperMesh commands that run a sequential HyperMesh command file. More advanced macros involve using the Tcl/Tk scripting language to add additional logic and user interaction such has entity selection. Even more advanced automation can be done using the Altair Process Manager. The Process Manager is a programmable personal workflow manager that guides users through a standard work process. The Process Manager will be addressed in a different class. Here the focus will be on creating simple HyperMesh macros and HyperMesh macros with Tcl/Tk.
The HyperMesh Utility Menu commands provide the means to create an interface in the form of buttons and button groups. For each you specify the following characteristics:
• The macro page it is displayed on • Its label
Chapter 2: HyperMesh Process Automation Introduction
• Its help message
• The macro it calls, and any optional arguments
F
The macro command language is an interpreted language. This means each command executes in the order in which it appears in the macro file.!
While macros offer a great deal of flexibility, remember once a macro executes, there is no way to cancel the execution or reject the results. In addition, a macro may not be recursive (call itself).The HyperMesh Utility Menu
The Utility menu allows you to customize the standard interface to include function buttons, radio options, and text that have HyperMesh-supplied and user-defined macros associated with them. The Utility menu is located on a tab of the
Tab area pane(s), and can be shown or hidden from within the View > Browsers
> HyperMesh pull-down menu.
The Utility menu includes several pages of its own, each dedicated to different tasks. Thus it presents groups of functionalities accessible from a set of pages contained by the Utility tab, although only one set of functions is displayed at a time. Each page is associated with a button at the bottom of the Utility menu; clicking one of these buttons accesses the page associated with it. A macro file (hm.mac) controls the display and available operations of the Utility Menu. Attributes that you can change include:
• The Utility menu page on which the operations appear. • Text to be displayed on each control.
• Location and size of the menu.
• The help string to be displayed in the bubble as the mouse is hovered over the button.
• The macro to call when each control is used.
When HyperMesh starts, it looks for a macro file named hm.mac in the current directory, HOME directory (UNIX only), or the application’s base directory. If it finds this file, HyperMesh runs it automatically to define the attributes and contents of the Utility Menu.
The default hm.mac file sources the following additional macro files:
Chapter 2: HyperMesh Process Automation Introduction
geommeshpage.mac Populates the Geom/Mesh page of the Utility Menu
globalpage.mac Creates the button group that allows you to switch pages
qamodelpage.mac Populates the QA/Model page of the Utility Menu
userpage.mac Populates the User page of the Utility Menu
A userpage.mac file may exist in the installation directory for HyperMesh or in the directory from which HyperMesh launches. When HyperMesh starts, it first looks for the userpage.mac file in the directory from which it launches and then in the installation directory. UNIX users also have the option of putting the
userpage.mac file in their home directory. This file defines the attributes and
contents of the User page of the Utility menu. By default, the Utility menu displays when HyperMesh starts, but display of the menu can be turned off by going to the View > Browsers > HyperMesh pull-down menu.
HyperMesh Utility Menu Commands
HyperMesh macro commands reside in the default hm.mac file. When you open this file, you will see that it defines the Utility menu contents in order by page. To change a macro on a given page, find that page in the hm.mac file and the
macro name that you wish to modify.
Attributes you can change on a macro page include: • Buttons to display on that macro page
• The location and size of the buttons appearing on a macro page • The label for each button
• The help string displayed in the bubble as the mouse is hovered over the button.
• The macro called by each button, with optional arguments to pass
HyperMesh macros consist of valid command file or templex commands, and are enclosed by the *beginmacro(macroname) 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. The *callmacro() command allows you to call a macro from within another one, which allows you to create groups of standard reusable macros.
The following skeleton shows the format of a macro:
*beginmacro(macroname)
macro command statements go here
Chapter 2: HyperMesh Process Automation Introduction
In the next Chapter we will go over HyperMesh macros in more detail. Here we will focus on how to add a button to the Utility menu.
To activate the macro from HyperMesh, you must create a button on the Utility menu to invoke the macro. Use the *createbutton() command to define the button and its characteristics. The syntax for this command is:
*createbutton(page, name, row, column, width, COLOR, helpString, macroName [ , arg1 …])
Where:
page: Indicates the page number on which the button is to appear (values 1 through n; initially there are 5).
name: The text to display on the button. Enclose the text with quotes (“ “).
row: The row in which to place the button (values –1, 1 - n). The number of rows visible depends upon the graphics resolution setting of your hardware. A 0 indicates the next available position, and –1
indicates to skip a row.
column: The column where the button starts (values 0 - 10 ).
width: The width of the button ( max 10).
COLOR: The color of the button. The available button colors are: RED, BLUE, GREEN, CYAN, BUTTON, GREY, YELLOW and MAGENTA. The color name must appear in capital letters.
helpString: The string to be displayed in the bubble as the mouse is hovered
over the button. Enclose text of string in quotes (“ “).
macroName: The name of the macro to call when the button is selected.
Enclose text of string in quotes (“ “).
arg1…: A list of one or more optional arguments passed to the macro. You may have as many arguments as your computer’s memory will allow
Figure: Row and Column Designations
Row 1
Chapter 2: HyperMesh Process Automation Introduction
Below are two examples on how to create a button. The first example illustrates the button calls needed for a user-created HyperMesh macro:
*createbutton(5, “Create force”, -1, 0, 10, GREEN, “Create force on selected nodes”, “createForce_macro”)
F
The end of a command is a hard return <cr> (press the ENTER key). In your text editor, type each command until you get to the end of it, then press ENTER.The second example shows the button calls for a user created Tcl script using the “EvalTcl” command:
*createbutton(5, “Create force”, -1, 0, 10, GREEN, “Create force on selected nodes”, “EvalTcl”, “create_force.tcl”)
For each example, 5 refers to the Userpage, “Create force” is the text on the button, -1 refers to the row position, 0 is the column number, 10 is the column width, GREEN is the color of the button, and “Create force on selected nodes” is the help string. For the HyperMesh macro example, “createForce_macro” is the name of the macro to be evaluated. In the Tcl script example, “EvalTcl” needs to be entered before “create_force.tcl” which is the name of the Tcl file.
Notice that the full path for create_force.tcl isn’t given. A full path can be specified if the file isn’t located in one of the predefined paths that HyperMesh searches to find scripts. These paths include installation paths as well as the working directory. Users can add additional search paths using the
TCL_INCLUDE environment variable. Relative paths can also be used from these search paths.
F
The only commands used in this example are the menu macro commands, which called an existing tcl procedure. We have yet to use the macro command to create our own procedures, tasks or actions.Online Help
The Online Help for HyperMesh can be accessed by either typing “h” on your keyboard or by going to Help in the menu bar and selecting HyperWorks
Desktop. Within the HyperWorks 12.0 Welcome Page there is a HyperWorks Reference Guides Section. Selecting HyperMesh opens the HyperMesh
Chapter 2: HyperMesh Process Automation Introduction
the navigation area you will find another book named Scripts. This contains all the commands and functions used in HyperMesh.
Chapter 2: HyperMesh Process Automation Introduction
Practical Exercises
Exercise 2a
Description
Add a button to the User Page on the Utility Menu that executes the macro modeltour.tcl. This macro is located in the installation under hm\scripts, so a path is not needed. The name of the button should be “Model Tour”. The help string should be “Explore HyperMesh Session”. The color and location are up to you.
HyperMesh commands used *createbutton() TCL/TK commands used
none
Hints
On Windows, the working directory is located in the My Documents folder. Create a new text file called userpage.mac in this location and add the appropriate commands to make the button evaluate the tcl file.
Chapter 3: HyperMesh Automation with HyperMesh Macros
Chapter 3
HyperMesh Automation with
HyperMesh Macros
In this chapter we will go over HyperMesh commands and become familiar with the general process for creating HyperMesh command macros. These macros make use of only HyperMesh commands. Such commands are written to the command.cmf file when an action is performed in HyperMesh.
HyperMesh Commands
As an introduction to HyperMesh commands, an explanation of command files, entity types, marks, and temporary planes and vectors are described below. In addition, a list of common command file commands is presented.
Command Files
During each HyperMesh session, HyperMesh generates a command file named
command.cmf. If this file doesn't already exist, then HyperMesh creates it at the
start of a session. If the file does already exist, HyperMesh adds the commands for the new session to the end of the original file.
HyperMesh command files are standard ASCII files that HyperMesh can read or write. Command files allow you to program a series of procedures or, in the case of a power failure or other unexpected system loss, retrieve an earlier work
session. You can use command files in applications that rely on repetitive or iterative steps, or to create demonstrations.
Command files follow this syntax:
• A command begins with an asterisk (*). • A command ends with the left parenthesis.
Chapter 3: HyperMesh Automation with HyperMesh Macros
• The parameters for a command are separated by commas, ending with the right parenthesis.
The following command illustrates the command syntax. The shrink command is:
*shrink(.2)
The command name that HyperMesh reads is located between the asterisk and the left parenthesis; in this case, it is the word shrink. The command has one parameter, .2, which is the factor that HyperMesh shrinks the elements (in this example, the elements shrink by 20%, leaving open gaps between them). Commands executed in an active HyperMesh session are not immediately written to the command files. They are stored in memory and are written to the command file during idle time (when HyperMesh is not busy performing user operations).
Entity Types
HyperMesh entities are often used as string parameters in commands that act directly on entities in some fashion, such as creating filtered sets that contain only a specific entity type. Examples of entity types are elements, line, groups, sets, and loads. For a complete listing, please refer to the online help.
Marks
Marks specify groups of entities that are transferred to the command processor. Marks can be created for most entities, and commands are then issued to
perform operations on the entities on the mark. HyperMesh allows for two standard marks and a user mark.
Standard marks are referenced by the mark IDs 1 and 2. The following example demonstrates how to delete elements using standard marks; assume that you want to delete the elements 50, 51, and 52 from a database. To perform this operation, you must create a mark with these elements and then instruct HyperMesh to delete the mark:
*createmark(elements, 1) 50 51 52 *deletemark(elements, 1)
This *createmark command populates mark 1 with elements 50, 51, and 52. The *deletemark command deletes the elements contained on mark 1.
Chapter 3: HyperMesh Automation with HyperMesh Macros
Notice after the *createmark command there are a series of numbers which correspond to the element ids which were added to the mark. These ids will not be consistent from model to model. In order to make a macro useful for multiple models, we can change the selection method.
Several options exist to make the *createmark command general enough to work with any model. For example, to select all the currently displayed elements in the model use the command *createmark(elements,1)"by
displayed".
A mark can also be appended using the *appendmark() command. Imagine from the previous example, we want to delete elements 50, 51, and 52 as well as all the elements adjacent to those elements. To do this, the commands would be as follows:
*createmark(elements, 1) 50 51 52
*appendmark(elements, 1) “by adjacent” *deletemark(elements, 1)
Another option is to replace the *createmark command with
*createmarkpanel. When executed, this command presents the user with a
selection panel in the entity specified in the command can be selected.
Planes and Vectors
At times, it is necessary to define a vector or plane for a command. HyperMesh allocates two vectors and two planes with the IDs 1 and 2. The vectors and planes can be defined at any time during the processing of a command file or Tcl script.
The following example translates node 10 along the x axis by 5 units:
*createvector(1, 1.0, 0.0, 0.0) *createmark(nodes, 1) 10
*translatemark(nodes, 1, 1, 5.0)
In this case, vector 1 is loaded with the components (1.0,0.0,0.0) that define the global x-axis. Node mark 1 is populated with node 10. These are then passed to the *translatemark command to perform the action.
Additionally, a normal and base point are required to define a plane. The next example reflects all displayed elements about a plane that has a normal along the x axis and a base point of (5.0,0.0,0.0):
*createplane(1, 1.0, 0.0, 0.0, 5.0, 0.0, 0.0) *createmark(elements, 1) “displayed”
Chapter 3: HyperMesh Automation with HyperMesh Macros
For this example, plane 1 is defined by the normal of (1.0, 0.0, 0.0) and the base point of (5.0, 0.0, 0.0). The element mark is then populated with all the displayed elements. These elements and the plane definition are passed to the
*reflectmark() command to perform the reflection.
Common Command File Commands
Below is a table with many common command file commands as well as a description of the command.
Command Name Description
*appendmark() Add additional entities to a mark
*beginmacro(name) Start the definition of a Utility Menu macro
*callmacro() Call a macro from within another macro
*createbutton() Add a button to the Utility Menu
*createbuttongroup() Add a button group to the Utility Menu
*createlistpanel() Allow you to interactively select a list of entities
*createmarklast()
Place the entities from the last operation into a mark
*createmarkpanel() Allow you to interactively select entities
*createtext() Add text to the Utility Menu
*endmacro()
End the definition of a macro started with the beginmacro command
*enterpanel() Enter the passed HyperMesh panel
*includemacrofile() Include the contents of the passed macro file
*insertbutton() Insert a button into the Utility Menu
*inserttext() Insert text into the Utility Menu
*setactivegroup() Set the default selection for a button group
*setactivepage() Set the current displayed Utility Menu page
*setbuttongroupactivecolor() Set the color for the Utility Menu button group’s current selection indicator For a complete listing of command file commands, please refer to the online
help. In the Scripts book under the Reference Guide, there is a topic for
Commands and Functions. In this book you will find a complete listing of all the commands available in HyperMesh.
Chapter 3: HyperMesh Automation with HyperMesh Macros
HyperMesh Macro Structure
As mentioned in the previous chapter, the HyperMesh macro consists of valid command file or templex commands. These commands are enclosed by the
*beginmacro() and *endmacro() commands. The following skeleton shows the
format of a macro.
*beginmacro(macroName) [commands]
*endmacro()
Macros may accept data passed to them using the arguments $1, $2, etc. Each argument specifies where the values should be substituted. Below is an example to turn on/off all geometry.
*beginmacro(ToggleGeom_macro)
*displaycollectorwithfilter(comps,$1,””,0,1) *plot()
*endmacro()
By specifying “none” for $1 in the *createbutton command, the argument is passed to the macro so that all geometry is turned off. The *createbutton command is shown below:
*createbutton(5, “Turn off all geometry”, -1, 0, 10, GREEN, “Turn off the display of all geometry”, “ToggleGeom_macro”, “none”)
Process to Create HyperMesh Macros
The following is a process to assist you in creating HyperMesh macros. While this procedure does not have to be followed, it is strongly recommended that you follow this procedure as it has been found to be an effective way to create
HyperMesh macros.
1. Define the task to be automated.
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. Add the commands to the userpage.mac file.
6. Modify the commands as necessary and add macro wrapper commands *beginmacro and *endmacro.
7. Add the macro button using the *createbutton command that will call the new macro defined in Step 6.
Chapter 3: HyperMesh Automation with HyperMesh Macros
8. Reload the current .mac file into HyperMesh to load the modified userpage.mac.
9. Test the macro.
Example 3.1: Automate Creating a Load Collector
The purpose of this example is to become familiar with the general process for creating a HyperMesh macro. This example will use the process described above for creating a HyperMesh macro.
Step 1: Define the task to be automated
Create a load collector named forces. A card image does not need to be specified for the collector.
Step 2: Delete the existing command.cmf file
Every command executed in HyperMesh is recorded in the command.cmf file. To view only the tasks commands, it is suggested that you delete the
command.cmf file before doing the task. After deleting this file, the file is
automatically recreated when more commands are executed in HyperMesh. The command.cmf file is located in the My Documents folder on Windows.
Step 3: Do the task in HyperMesh
1. Open HyperMesh.
2. Click the Model tab in the Tab area if the Model Browser is open. Or go to the menu bar and select View > Browsers > HyperMesh menu and select Model.
3. Right-click in the white blank area and from the pop-up menu, select Create > Load Collector.
4. For Name, type forces. 5. Select any color for Color.
6. Verify that there is a check mark next to Close dialog upon creation. 7. Click Create to create the load collector.
8. Keep this HyperMesh session open for steps later in this exercise.
Step 4: Extract commands from the command file and add them
to the userpage.mac file
Chapter 3: HyperMesh Automation with HyperMesh Macros
1. From your HyperMesh working directory, open the command.cmf file. 2. Locate the command *collectorcreateonly() at or near the end of
the file. You should see a line similar to the following:
*collectorcreateonly(loadcols, “forces”, “”,7)
This command creates a load collector named “forces” and because the above command as a color id of 7, it has a color of light blue.
3. Copy the *collectorcreateonly() line.
4. From your HyperMesh working directory, open the userpage.mac file. If this file does not already exist, create an empty text file named
userpage.mac
5. In the userpage.mac file, on the next blank line from the top, paste the copied line.
6. Save the userpage.mac file.
Step 5: Modify the commands in the userpage.mac file as
necessary
Do not modify the *collectorcreateonly() command as it does not need to be modified for this example.
Step 6: Add macro wrapper commands
Add the *beginmacro() and *endmacro() commands to create the macro as shown below:
*beginmacro(macroCreate_LoadCol)
*collectorcreateonly(loadcols, “forces”, “”, 7) *endmacro()
Note: “macroCreate_LoadCol” is the macro’s name. It links the macro to its button, which you will create in the next step, via the macro name field in the
*createbutton() line.
Step 7: Create a button and label for the macro on the Utility
menu’s User page
Create an activation button in the Utility Menu to execute the macro. Place the button on page 5 (User) of the Utility Menu as that page is dedicated to user-defined macros.
Chapter 3: HyperMesh Automation with HyperMesh Macros
1. In the userpage.mac file, add the following line at the beginning of the file to create a green button called Create Load Col. Note that the
*createbutton command should all be on a single line. It is shown on
two lines below simply due to space limitations.
*createbutton(5, “Create Load Col”, -1, 0, 10, GREEN, “Create a load collector”, “macroCreate_LoadCol”)
2. Create a label for the button using the *createtext() command as shown below. Add this below the *createbutton command.
*createtext(5, “Collectors:”, -1, 0)
Step 8: Save the userpage.mac file
The userpage.mac file should look like the following:
*createbutton(5, “Create Load Col”, -1, 0, 10, GREEN, “Create a load collector”, “macroCreate_LoadCol”) *createtext(5, “Collectors:”, -1, 0)
*beginmacro(macroCreate_LoadCol)
*collectorcreateonly(loadcols,"forces","",5) *endmacro()
Save the userpage.mac file.
Step 9: 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 different 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 either reload the hm.mac file or simply close and
reopen HyperMesh.
1. Close HyperMesh and reopen HyperMesh. OR load a different user profile.
Step 10: Test the macro
1. Click on the Utility tab in the tab area and select the User page. Notice the Create Load Col button and the Collectors: label on the User page.