• No results found

Retrieving Data from HyperMesh Entities

In document HM_Customization_v13.pdf (Page 63-75)

Chapter 5

Retrieving Data from HyperMesh Entities

In the previous chapters, commands were extracted from the command.cmf file and then modified as necessary to create scripts to be used in HyperMesh. This chapter will focus on creating Tcl routines which retrieve data from HyperMesh entities. These scripts make use of HyperMesh commands, Tcl core scripting commands, and HyperMesh Tcl commands.

HyperMesh Entities and Their Data Names

Data names are generic references to the information that physically defines an entity in the HyperMesh environment. An example of this would be the x, y, and z coordinates that define a node’s location in three-dimensional space. This

information is part of the entity’s definition and is consistent for all solvers. It is possible though that these can change from one HyperMesh version to the next.

For this reason, please refer to the online help. Information on the data names can be found in the following location:

HyperMesh > Reference Guide > Scripts > Creating Scripts > Tcl > Using Data Names

Data names are accessed using the hm_getentityvalue command. This command is one of the most common commands used by a developer in the HyperMesh environment. It is used to access the data names and template information associated with an entity. The command will return a value that is either a string or a numeric value, depending on the flag value and the value stored in that field or data name. The syntax for this command is:

hm_getentityvalue entity_type name_or_id data_name_string flag ?search_type?

Chapter 5: Retrieving Data from HyperMesh Entities

The following is an example using hm_getentityvalue:

set force_x [hm_getentityvalue loads 12 "comp1" 0]

In this example, loads corresponds to the entity_type, 12 is the

name_or_id of the load, “comp1” is the data_name_string, and 0 is the flag. The flag can be set to either 0 (indicating a floating value should be returned) or to 1 (indicating that a string should be returned. Using these

options, the x component of a force with ID 12 is returned and set to the variable force_x. The table below shows several of the data names for the force load:

node when a load is applied to a node, this serves as a pointer to the node

comp1 x component of the vector comp2 y component of the vector comp3 z component of the vector magnitude magnitude of the load vector

collector collector that owns the load (load collector pointer)

Data names for force load

Another example for hm_getentityvalue is shown below. This example is a Tcl acript which prompts the user to select several nodes and then displays their x, y, and z coordinates:

hm_markclear nodes 1

*createmarkpanel nodes 1

set nodes [hm_getmark nodes 1]

if { ! [ Null nodes ] } { foreach node $nodes {

set xVal [hm_getentityvalue nodes $node "x" 0]

set yVal [hm_getentityvalue nodes $node "y" 0]

set zVal [hm_getentityvalue nodes $node "z" 0]

tk_messageBox -message "Node $node = $xVal $yVal $zVal"

} }

In the example above, nodes corresponds to the entity_type, $nodes is a variable which passes the node id, “x”, “y”, or “z” is the

data_name_string, and 0 is the flag. Below is a table which shows some of the data names available for nodes.

Chapter 5: Retrieving Data from HyperMesh Entities

id The ID of the node (integer).

inputsystem Pointer to the node input system (pointer).

outputsystem Pointer to the node output system (pointer).

x The x coordinate of the node in its local system (real).

y

The y coordinate of the node in its local system (real).

z

The z coordinate of the node in its local system (real).

Data names for nodes

F

Note in both scripts that to assign the return value from the command to a variable, the command is placed within square brackets.

Pointers and Flags

Notice that several of the data names in the two tables above are defined as pointers. A pointer is used to directly access another data name. For example, the collector and node data names for force loads are pointers. This means they

“point” to the data names available for either collectors or 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 following are a few examples on how a pointer is used. To retrieve the node id that load 12 is applied to the following set of commands is used:

set node_id [hm_getentityvalue loads 12 "node.id" 0]

To retrieve the y coordinate of the node that load 12 is applied to use the following set of commands:

set node_id [hm_getentityvalue loads 12 "node.y" 0]

To retrieve the load collector name that contains load 12, use the following set of commands:

set ld_col [hm_getentityvalue loads 12 "collector.name" 1]

All data names for that particular entity are available for reference when using pointers. For the collector name, notice the flag value is set to 1 as the expected return value is a string value as opposed to a numeric value. In the node

Chapter 5: Retrieving Data from HyperMesh Entities

examples above, the flag value is set to 0, indicating that a floating point number is to be returned.

Another example is with component collectors. There is no data name

associated with a component collector to get the material name, only the material ID. The following set of commands is used to get the material ID:

set matID [hm_getentityvalue comps 12 "materialid" 0]

A second set of commands would then be required to get the name of the material with that ID:

set matName [hm_getentityvalue mats $matID "name" 1]

Alternatively, the component collector has a material pointer data name. From this pointer, any valid material data name can be substituted by separating the pointer and the new data name with a period (.). From the example above the following line can replace the previous two lines:

set matName [hm_getentityvalue comps 12 "material.name" 1]

Process for Creating a HyperMesh Tcl Script to Retrieve Data on HyperMesh Entities

1) Define the task to be automated.

2) Write the commands to a user-created *.tcl text file

● Skip doing the task in HyperMesh as there are likely no commands to capture to the command.cmf file; the task is to retrieve

information, not to perform a HyperMesh action.

● Use HyperMesh Tcl commands to extract data on HyperMesh entities.

● Use core Tcl commands to retrieve, manipulate, display and store extracted data in variables.

3) Test the script from the Command Window.

4) Define the Utility menu button for the macro in userpage.mac file.

5) In the current HyperMesh session, reload the hm.mac file (this also reloads the userpage.mac file).

6) Run the script from the Utility menu.

Chapter 5: Retrieving Data from HyperMesh Entities

Example 5.1: Automate Computing the Vector Sum of Forces

The purpose of this example is to become familiar with creating HyperMesh Tcl scripts to retrieve data on HyperMesh entities (nodes, elements, loads, etc.).

The following HyperMesh commands are modified in this exercise.

• *clearmark()

• *createmarkpanel()

The following HyperMesh Tcl commands are used in this exercise.

• hm_getentityvalue

• hm_getmark

• hm_usermessage

• hm_errormessage

The following core Tcl commands are used in this exercise.

• brackets [ ]

• expr

• foreach

• if

• set

• return

• llength

Step 1: Define the task to be automated.

1. Request the user to select forces.

2. If the user does not select loads, display a message stating this.

3. Retrieve the X, Y, and Z component information from the user-selected forces.

4. Sum the X, Y, and Z components.

5. Display the resulting X, Y, and Z components.

Step 2: Do the task in HyperMesh to capture its commands to the command.cmf file.

Do nothing for this step because the task is not performed on model entities.

Rather, the task is to retrieve information from model entities. There are no HyperMesh commands to capture to the command.cmf file for this task.

Step 3: Create a text file named sum_force_comps.tcl for the

script to be created.

Chapter 5: Retrieving Data from HyperMesh Entities

Step 4: Allow the user to select the forces.

1. Create a buffer (mark) to store loads selected in the panel by the user.

*clearmark loads 1

*createmarkpanel loads 1 “Select forces for resultant”

Notice the HyperMesh entity type for force is loads.

2. Retrieve loads from the buffer (mark) and create a list variable.

set loads_list [hm_getmark loads 1]

Step 5: Before proceeding to calculate the sum, determine whether or not loads were selected. If none were selected, display a message stating this.

By checking the length of the variable $loads_list, it can be determined if any loads have been selected. If the length of the list of loads is equal to 0, this means that no loads have been selected. By using this check within an if statement, a message will be displayed only when no loads are selected and the script will be exited.

if { [llength $loads_list] == 0 } {

hm_errormessage "No loads selected"

return }

Step 6: Initialize variables that store the X, Y and Z components of the vector sum of the selected forces.

Initialization is done by setting the variables xsum, ysum, and zsum to 0.

set xsum 0 set ysum 0 set zsum 0

Step 7: Loop through the list of selected loads, retrieve their component values, and add them to the total for each

component.

By using the foreach statement, each load in the list can be extracted and set to the variable load_id. Then, the x component can be retrieved and set to the variable xcomp, the y component can be retrieved and set to the variable ycomp,

Chapter 5: Retrieving Data from HyperMesh Entities

and the z component can be retrieved and set to the variable zcomp. Finally each component is added to the appropriate sum variable using the expr command.

foreach load_id $loads_list {

set xcomp [hm_getentityvalue LOADS $load_id "comp1" 0]

set ycomp [hm_getentityvalue LOADS $load_id "comp2" 0]

set zcomp [hm_getentityvalue LOADS $load_id "comp3" 0]

set xsum [expr $xsum +$xcomp]

set ysum [expr $ysum +$ycomp]

set zsum [expr $zsum +$zcomp]

}

Step 8: Display the result (components of the resultant).

Using the hm_usermessage command, a message is shown in the message bar which reports the x, y, and z components of the resultant force.

hm_usermessage “Resultant force: $xsum, $ysum, $zsum”

Step 9: Review your script to see if it looks similar to the following.

*clearmark loads 1

*createmarkpanel loads 1 “Select forces for resultant”

set loads_list [hm_getmark loads 1]

if { [llength $loads_list] == 0 } {

hm_errormessage "No loads selected"

return }

set xsum 0 set ysum 0 set zsum 0

foreach load_id $loads_list {

set xcomp [hm_getentityvalue LOADS $load_id "comp1" 0]

set ycomp [hm_getentityvalue LOADS $load_id "comp2" 0]

set zcomp [hm_getentityvalue LOADS $load_id "comp3" 0]

set xsum [expr $xsum +$xcomp]

set ysum [expr $ysum +$ycomp]

set zsum [expr $zsum +$zcomp]

}

Chapter 5: Retrieving Data from HyperMesh Entities

hm_usermessage “Resultant force: $xsum, $ysum, $zsum”

Step 10: Save the sum_force_comps.tcl file.

Step 11: Test the script by running it from the Command Window.

1. Open HyperMesh from your HyperMesh working folder.

2. Retrieve the file c_channel-tcl_vector.hm.

3. Go to View >> Command Window to open the Command Window if it isn’t already open.

4. In the Command Window, execute the command source sum_force_comps.tcl

5. A HyperMesh panel with a loads selector appears.

6. Select the 3 loads in the model and then press proceed to continue with running the script. (HINT: It may be easiest to select “displayed” to select all 3 loads).

7. The force resultant values appear in the message bar.

8. Run the script again, but this time, do not select any loads. The message

“No loads selected” appears in the message bar.

Step 12: Create a button for the script on the macro menu’s User page.

1. In your HyperMesh working folder, open the file userpage.mac. If this file does not already exist, create a text file named userpage.mac.

2. On the next blank line from the file’s top, type the following

*createbutton() line:

*createbutton(5,”Sum force comps”,-1,0,10,GREEN,”Sum comps of forces”, ”EvalTcl”, ”sum_force_comps.tcl”) 3. Save the userpage.mac file to your HyperMesh working folder.

Step 13: Test the script by running it from its button on the macro menu.

Step 14: The exercise is complete. Close the HyperMesh

session.

Chapter 5: Retrieving Data from HyperMesh Entities

Practical Exercises

Exercise 5a Description:

Create a HyperMesh Tcl script to automate the following task:

1) Request the user to select a component to be translated.

2) Request the user to select a HyperMesh defined vector along which the component is to be translated.

3) Get the Xcomp Ycomp and Zcomp of the selected vector from the HyperMesh database.

4) Request the user to enter in a translation distance.

5) Use all of the above information to translate the given component.

Test the file using the HyperMesh file c_channel-tcl_vector.hm.

HyperMesh commands used

*createmarkpanel hm_getfloat

hm_getmark

hm_getentityvalue

*createvector

*translatemark

*clearmark

TCL/TK commands used set

Hints

Chapter 5: Retrieving Data from HyperMesh Entities

Exercise 5b Description

Starting with the results of Exercise 4b:

Create a node at the centroid of the element. Translate the node in the positive direction of the element normal by an amount equal to the shortest diagonal of a quad element and the shortest side of a tria element. Create either a tetra element or a pyramid element using the original element node list and the new node created by the script. Test the file using the HyperMesh file c_channel-tcl_vector.hm.

HyperMesh commands used

*createmark *createvector

*translatemark *createnode

*createelement *collectorcreate

*currentcollector hm_getmark

hm_nodelist hm_entityinfo

hm_getentityvalue TCL/TK commands used

for foreach

set if

elseif else

Hints

This assignment will require you to think more, but the process is still the same.

Pay careful attention to vector directions and look closely at the data you have generated. What are some ways to make this cleaner in the user environment?

What are some problems that you might encounter?

Chapter 5: Retrieving Data from HyperMesh Entities

Exercise 5c Description

Create a HyperMesh Tcl script to automate the following task:

1) Request the user to select elements on which to create system.

2) Calculate the centroidal coordinates of each element.

3) Create a node at each of these centroidal locations.

4) Create systems with these nodes as an orientation node.

Writing this script will make you more familiar with the general process for creating HyperMesh Tcl scripts. Test the file using the HyperMesh file c_channel-tcl_vector.hm.

HyperMesh commands used

*createmarkpanel *createnode

*clearmark *hm_nodelist

hm_getmark *systemcreate

hm_entityinfo

TCL/TK commands used

set for

foreach eval

lindex llength

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.

Chapter 5: Retrieving Data from HyperMesh Entities

In document HM_Customization_v13.pdf (Page 63-75)