# HM-8080: Calculate the Radius of an Arc

In this tutorial you will create a Tcl script that determines the radius of an arc.

The Tcl commands if and expr will be used to add logic and mathematical functions 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 entity 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 that 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 line.
2. Make sure you have selected only one line.
3. Determine the center of the arc by creating temporary nodes.
4. Calculate the distance between one end of the arc and the center node using node coordinate data names.

## Determine Data Names

In this step you will determine the data names to use to extract the node coordinates.

Use the following table to determine data names for nodes.
Data Name Description
globalx x coordinate in the global system
globaly y coordinate in the global system
globalz z coordinate in the global system

## Create a Text File

In this step you will create a new text file and name it, HM8080.tcl.

1. Create a new text file using any text editor.
2. Save the file as, HM8080.tcl.

## Select a Line that Defines an Arc

In this step, you will select the desired line that defines a circle or an arc and then add that line to a variable.

The *createmarkpanel command is used to allow you to graphically select the line which defines a circle or an arc from the HyperMesh interface and add it to the mark. The command below adds the line to mark 1. Once the line has been added to mark 1, the line id is assigned to a variable called line_list, using the Tcl command set.

Add the following code to the file HM8080.tcl.
*createmarkpanel lines 1 "Select line to find radius";
set line_list [hm_getmark lines 1];

## Begin an If Loop

In this step, you will begin an if loop that checks if the variable, line_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 line_list has values in it. This is done by using an if loop. In the if loop below, you are checking to see if the variable line_list is empty. If the variable is empty, an error message is given to you. Also, using the elseif statement in the if loop, you can check to see if more than one line is selected. If more than one line is selected, an error message is reported. If neither of those conditions are met, the macro proceeds under the else statement.

Add the following code to the Tcl file to define the if loop:
if {$line_list == ""} { hm_errormessage "No lines selected"; } elseif {[llength$line_list] != 1} {
hm_errormessage "Only one line may be selected";
} else {

## Create Nodes on the Selected Line

In this step, you will create three nodes on the selected line and then create a node at the circle center of the three nodes. Then, you will add those nodes to a variable.

Use the *nodecreateonlines command to create 3 nodes on the line which is in mark 1. This is done with the first command below. Then, use the *createcenternode to create a node at the center of a circle formed by the three nodes that were just created in the *nodecreateonlines command.

These three nodes are referenced by using -1, -2, and -3 which reference the last node created, the second to last node created, and the third to last node created. Then, the nodes are added to the nodes mark 1 using the *createmark command. Again, the nodes are referenced using -1, -2, -3 and -4 to add the last 4 nodes created to the mark. Finally, the nodes in mark 1 are added to the variable node_list.

Add the following code to the Tcl script:
*nodecreateonlines lines 1 3 0 0;
*createcenternode -1 -2 -3;
*createmark nodes 1 -1 -2 -3 -4;
set node_list [hm_getmark nodes 1];

## Use the lindex Command to Obtain First Node ID

In this step you will use the lindex command to get the node id of the first node in the list, node_list. Then, you will get the x, y, and z coordinates for the node.

Set a variable called id which contains the node id for the first node in the list node_list. The id for the first node is retrieved using the lindex command which takes the variable node_list and using the index 0, retrieves the first node id in the list. Then, using the variable id and the hm_getentityvalue command with the node data names x, y, and z, the x, y, and z coordinates for the node are set to the variables x1, y1, and z1.

Add the following code to the Tcl script:
set id [lindex $node_list 0]; set x1 [hm_getentityvalue nodes$id "x" 0];
set y1 [hm_getentityvalue nodes $id "y" 0]; set z1 [hm_getentityvalue nodes$id "z" 0];

## Use the lindex Command to Obtain Second Node ID

In this step you will use the lindex command to get the node id of the last node in the list, node_list. Then, you will get the x, y, and z coordinates for the node.

Set a variable called id which contains the node id for the last node in the list node_list. The id for the first node is retrieved using the lindex command which takes the variable node_list and using the index 3, retrieves the first node id in the list. Then, using the variable id and the hm_getentityvalue command with the node data names x, y, and z, the x, y, and z coordinates for the node are set to the variables x2, y2, and z2.

Add the following code to the Tcl script:
set id [lindex $node_list 3 ]; set x2 [hm_getentityvalue nodes$id "x" 0];
set y2 [hm_getentityvalue nodes $id "y" 0]; set z2 [hm_getentityvalue nodes$id "z" 0];

## Define Variables

In this step you will define three variables which are the x, y, and z distance between the two nodes defined in the previous two steps.

Three variables are defined which are simply the x, y, and z distance between the two nodes defined in Steps 7 and 8. The component difference between each node is calculated using the coordinates defined in Steps 7 and 8 and the Tcl command expr.

Add the following code to the Tcl script:
set dx [expr $x1 -$x2];
set dy [expr $y1 -$y2];
set dz [expr $z1 -$z2];

## Define a Variable to Calculate Radius

In this step you will define a variable called radius which uses the variables dx, dy, and dz to calculate the radius of the line which is a circle or an arc.

Using the three variables which were defined in the previous step (dx, dy, and dz) the magnitude of the distance is calculated. This distance corresponds to the radius of the arc/circle which is defined by the line selected. To calculate the radius, the expr command is used.

Add the following code to the Tcl script:
set radius [expr sqrt(($dx*$dx) + ($dy*$dy) + ($dz*$dz))];

## Clear the Nodes

In this step you will clear the nodes in the temporary node mark.

To clear all the nodes in the temporary node mark, use the *nodecleartempmark command.

Add the following code to the Tcl script:
*nodecleartempmark;

## Report the Radius

In this step you will report the radius of the selected line.

Using the hm_usermessage command, the value of the variable radius is reported to you. Also, close the if loop which was started back in Step 5.

Add the following code to the Tcl script:
hm_usermessage "Radius = \$radius";
}

## Clear Lines and Nodes Mark

In this step you will clear the lines and nodes mark.

Using the hm_markclear command, the nodes mark and the lines mark are cleared.

Add the following code to the Tcl script:
hm_markclear lines 1;
hm_markclear nodes 1;

## Test the Script

In this step you will test the script.

1. From the File menu, open the file, radius-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, source HM8080.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 different lines to review the calculated radius.

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.