HM-8070: Create Spline Surfaces on Tria Elements

In this tutorial, you will create a Tcl script that creates spline surfaces from the nodes of selected tria elements.

The Tcl commands if, foreach, and incr will be used to add logic to the script. The command hm_getentityvalue is used to extract information from HyperMesh entities, based on data names.

Data names are generic references to the information that physically define an entity in the HyperMesh environment. An example of this is the x-, y-, and z-coordinates that define a node location in three-dimensional space. The available data names for each can be found in the HyperMesh Reference Guide Data Names topic.

Data names are accessed using the hm_getentityvalue command. This command uses the data names available for an entity to return the particular value of interest. The command will return a value that is either a string or a numeric value, depending on the command syntax and the value stored in that particular data name field. The basic syntax of the command is:
hm_getentityvalue entity_type id data_name flag

where entity_type is the requested entity type (elements, loads, nodes, and so on), id is the entity ID, the data_name is the data field name of interest, and flag is either 0 or 1 depending on whether the command should return a numeric value (0) or a string (1).

To retrieve the x-component of a force with ID 12, the following command can be used:
set force_x [hm_getentityvalue loads 12 "comp1" 0]
Note: To assign the value from the command to a variable, the command is placed within square brackets.

Define the Process

In this step, you will define the process.

The script should automate the following process:
  1. Prompt you to select a number of tria elements to create spline surfaces.
  2. Make sure you have selected one or more elements.
  3. If a selected element is not a tria, skip that element.
  4. Extract the node IDs of each element.
  5. Create the spline surface from the nodes.
  6. Report on the number of spline surfaces created.

Determine Data Names

In this step you will determine the data names to use to extract the element type and node IDs.

Use the following table to determine data names. It lists several relevant data names for tria elements.
Data Name Description
config The number, "103"
node1 first node (node pointer)
node2 second node (node pointer)
node3 third node (node pointer)

Create a Text File

In this step you will create a text file and save it as HM8070.tcl.

  1. Open a new text file in any text editor.
  2. Save it as HM8070.tcl.

Select Elements and Add Loads to a List

In this step, you will select the desired elements and add those loads to a list.

The *createmarkpanel command is used to allow you to graphically select the elements from the HyperMesh interface and add them to the mark. The command below adds the elements to mark 1. Once the elements have been added to mark 1, the element IDs are assigned to a list called elems_list, using the Tcl command set.

Add the following code to the file, HM8070.tcl:
*createmarkpanel elems 1 "Select tria elements to create surfaces";
set elems_list [hm_getmark elems 1];

Begin an If Loop

In this step you will begin an if loop which checks to see if the variable elems_list has values. If it does, then proceed with the macro.

Before continuing with the macro, you should check to make sure that the variable elems_list has values in it. This is done by using an if loop. In the if loop below, you are checking that the variable elems_list is not empty.

Add the following code to the Tcl file to initialize the if loop:
if {$elems_list != ""} {

Initialize a Variable

In this step, you will initialize a variable which counts the number of times the foreach loop is entered.

The variable success_count is initialized and set to 0. This variable is used to count the number of times the foreach loop, defined in Step 7, is entered. You will use this variable at the end of the script.

Add the following code to the Tcl script:
set success_count 0;

Use a Foreach Loop

In this step you will use a foreach loop to iterate through each element in the let, elems_list, and then set a variable, config, which store the element configuration. This is extracted using the hm_getentityvalue command and the appropriate data name.

Using a foreach loop, each element in the list elems_list will be iterated through. Within the foreach loop, each load is referenced by elem_id and then the variable config is defined. This variable is set to the result of the hm_getentityvalue which uses the element data name config to report the configuration of the element. A tria element will have an element configuration of 103 while a quad element will have a configuration of 104.

Add the following code to the Tcl file:
foreach elem_id $elems_list {
        set config [hm_getentityvalue elems $elem_id "config" 0];

Begin an If Loop

In this step you will begin an if loop which checks to see if the variable, config, has a value of 103. If it does, then proceed with the macro.

Using an if loop, the variable config is checked to see if it does not have a value of 103. A value of 103 means that the element configuration is a tria element. If the value is not equal to 103, the continue statement is used to move outside of the foreach loop. If the value is the config variable is 103, then the macro is continued.

Add the following code to the Tcl script:
if {$config != 103} {
            continue;
        }

Set Variables

In this step you will set three variables that contain the node id of each of the nodes used to define the tria element.

Three variables are defined (node1, node2, and node3) which represent the 3 nodes that define the tria element. These three nodes will be used to create the spline surface. Using the hm_getentityvalue command and the element data names node1, node2, and node3 along with the pointer id, the node id is retrieved and assigned to the appropriate variable.

Add the following code to the Tcl script:
set node1 [hm_getentityvalue elems $elem_id "node1.id" 0];
        set node2 [hm_getentityvalue elems $elem_id "node2.id" 0];
        set node3 [hm_getentityvalue elems $elem_id "node3.id" 0];

Set the Mode to Create the Surface

In this step, you will set the appropriate mode to create the surface.

Using the *surfacemode command, the surface mode can be set according to the following:
1
mesh keep surface
2
mesh delete surface
3
mesh without surface
4
surface only

In this example, you only want to create a surface, so mode 4 is used.

Add the following code to the Tcl script:
*surfacemode 4;

Create a Node Mark

In this step, you will create a node mark that contains the three nodes defined in Step 9 and then use the *splinesurface command to create a spline surface using the nodes in the mark.

Using the *createmark, mark 1 for nodes is created and it contains the 3 nodes defined in the variables node1, node2, and node3.

Add the following code to the Tcl script:
*createmark nodes 1 $node1 $node2 $node3;
        *splinesurface nodes 1 0 1 1;

Increase the Variable

In this step you will increase the variable, success_count, which is used to count the number of times the foreach loop is entered. Then, close the foreach loop.

Using the incr command, the variable success_count is increased. Following this command, a } is used to close the foreach loop.

Add the following code to the Tcl script:
incr success_count;
    }

Clear the Node and Element Marks

In this step you will clear the node and element marks, and then use the hm_usermessage command to report the number of spline surfaces created.

Using the command *clearmark, mark 1 for the nodes and elements is cleared. Following these commands, the hm_usermessage command is used to report the number of spline surfaces created. The variable success_count is used to do this. Because this variable was increased each time the foreach loop was entered and the element configuration was 103, this variable kept a count of the number of spline surfaces that were created.

Add the following code to the Tcl script:
*clearmark nodes 1;
    *clearmark elems 1;
    hm_usermessage "$success_count splines created."

Add an Else Statement

In this step you will add an else statement that compliments the if statement used to see if the elems_list variable was empty. If it is empty, the else statement is executed.

The else statement compliments the if statement defined in Step 5 which checks to see if the elems_list variable is empty. If it is empty the else statement is executed. Inside the else statement, the hm_errormessage command is used to report to you that no elements were selected. Following the hm_errormessage command, the if statement is closed using a }.

Add the following code to the Tcl script:
} else {
    hm_errormessage "No elements selected";
}

Test the Script

In this step you will test the script.

  1. From the menu bar, select File > Open > Model and then load the file, spline-tcl.hm.
  2. From the menu bar, select View > Command Window to display the command window at the bottom of the screen.
  3. Click and drag to open the command window from the bottom edge of the screen.
  4. Use the source command to execute the script. For example: HM8070.tcl.

    It is often necessary to debug Tcl scripts using the command window. This allows you to run the Tcl script and easily review error messages, as well as print out debug information. Additional details can be found in the Creating Tcl Scripts and Running Tcl Scripts sections.

  5. Select a few of the tria elements and observe the spline surfaces that are created.
    There are several important things to notice:
    • Only first order tria elements are considered. It is possible to add if/elseif logic to support other element configurations.
    • The data names for the nodes associated with an element are pointers. A pointer is used to directly access another data name. This means they “point” to the data names available for nodes. In order to retrieve any data from a pointer, the data name requested for the particular pointer must also be supplied. The additional data names are separated by a period or dot (.).
    • The *entityhighlighting and hm_commandfilestate commands are used to speed up the execution of the script. The *entityhighlighting command disables highlighting entities when the *createmark command is used. The hm_commandfilestate command controls if commands are written out to the command file. It is always important to “reset” these commands after a script is complete or before exiting due to an error.