TCL Hypermesh syntax again: loop to store elements

Stéphane Combet
Stéphane Combet Altair Community Member
edited December 2023 in Community Q&A

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

Welcome!

It looks like you're new here. Sign in or register to get started.

Best Answer

  • Fred_Juras
    Fred_Juras
    Altair Employee
    edited December 2023 Answer ✓

    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.

Answers

  • Fred_Juras
    Fred_Juras
    Altair Employee
    edited December 2023 Answer ✓

    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.

  • Michael Herve
    Michael Herve
    Altair Employee
    edited December 2023

    Hello @Stéphane Combet ,

     

    I see a typical tcl error in your command;. Please confirm if it is a typo error:

     

    lappend $toKeepElems $elemC

     

    lappend apply to the list entity itself, not the content in the list. So you should use instead

    lappend toKeepElems $elemC

     

    That said, from a performance point of view, if your intent is, for a first selection of elements, to isolate the elements whose x is greater than a value, you could use the following commands. That said, as xvalue in youer command above comes from one of the element node, not sure it can applied exactly this way.

     

    *createmark elems 1 "by node id" {*}$selectedNodes

    *createmark elems 2 "greater than or equal to value" centerx $xvalue

    *markintersection elems 1 elems 2

    set  toKeepElems [hm_getmark elems 1]

     

    Best Regards,

    Michael

  • Stéphane Combet
    Stéphane Combet Altair Community Member
    edited December 2023

    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.

    Thank you @Frederic Juras that was the point!

  • Stéphane Combet
    Stéphane Combet Altair Community Member
    edited December 2023

    Hello @Stéphane Combet ,

     

    I see a typical tcl error in your command;. Please confirm if it is a typo error:

     

    lappend $toKeepElems $elemC

     

    lappend apply to the list entity itself, not the content in the list. So you should use instead

    lappend toKeepElems $elemC

     

    That said, from a performance point of view, if your intent is, for a first selection of elements, to isolate the elements whose x is greater than a value, you could use the following commands. That said, as xvalue in youer command above comes from one of the element node, not sure it can applied exactly this way.

     

    *createmark elems 1 "by node id" {*}$selectedNodes

    *createmark elems 2 "greater than or equal to value" centerx $xvalue

    *markintersection elems 1 elems 2

    set  toKeepElems [hm_getmark elems 1]

     

    Best Regards,

    Michael

    Thank you Michael Herve this was the point!

    I will take in consideration your performance suggestion, as my script is more complex in terms of orientation/coordinates I will see if I can use the "greater than or equal to value" filter.

Welcome!

It looks like you're new here. Sign in or register to get started.

Welcome!

It looks like you're new here. Sign in or register to get started.