TCL Hypermesh syntax again: loop to store elements

User: "Stéphane Combet"
Altair Community Member
Updated by Stéphane Combet

Hello,

Here again on my script, thanks to @Michael Herve and @Marc Arnold I go ahead little by little.

Now I try to store in a set some elements depending of their coordinates.

To simplify here, I have just a row of nodes, 2 rows of elements, and I would like to store elements only if their centerX is bigger than the X of the nodes.

I can't understant why my list $toKeepElems seems to remain empty with the following loop. I print the variables to see what happens and it seems it should be ok but at the end I have nothing with "puts $toKeepElems".

#Selecting nodes (user) and then elements connected to those nodes (with "by node id")
*createmarkpanel nodes 1 'Select Nodes'
set selectedNodes [hm_getmark nodes 1]
*createmark elems 1 "by node id" {*}$selectedNodes
set selectedElems [hm_getmark elems 1]
 
#Storing X coordinate of one of the nodes
set nodeA_id [lindex $selectedNodes 0]
set nodeA_xyz [join [hm_nodevalue $nodeA_id]]
set nodeA_x [lindex $nodeA_xyz 0]
 
#Creating an empty list to store elements
set toKeepElems {}
 
#Looping to store elements
foreach elemC $selectedElems {
set cx [hm_getvalue element id=$elemC dataname=centerx]
puts $cx
puts $nodeA_x
if {$cx > $nodeA_x} {
puts "We are in\n"
lappend $toKeepElems $elemC
}
}
 
#Displaying results
puts $selectedElems
puts $toKeepElems
 
Do you see where I am wrong?
Thanks!
 
image

Find more posts tagged with

Sort by:
1 - 1 of 11
    User: "Fred_Juras"
    Altair Employee
    Accepted Answer
    Updated by Fred_Juras

    Hi,

    The issue is from the lappend command line. Actually, lappend expects a varname as first argument (without the $ for substitution). More details here: https://www.tcl.tk/man/tcl/TclCmd/lappend.html

    So, just remove the $ in front of the toKeepElems variable (as below)  and your script should work as expected:

    lappend toKeepElems $elemC

    Regards,
    Fred.