how to read a csv file and create nodes from coordinates

Jouher_20929
Jouher_20929 Altair Community Member
edited October 2020 in Community Q&A

I have written a CSV file. Now i have to read the file, create nodes at each coordinates, get created node ids.

 

set dir [tk_chooseDirectory \
        -initialdir ~ -title 'Choose a directory']
cd $dir
set fp [open '$dir/Connectors_comp.csv' w+]

*createmark connectors 1 all
set ce_list [ hm_getmark connectors 1];


puts $fp 'connector id,connector coordinates,linked comps id'

foreach CE_L $ce_list {

puts $fp '$CE_L,[hm_ce_getcords $CE_L],[hm_ce_getlinkentities $CE_L comps]'


}
close $fp

 

 

 

 

 

 


set sphereNd {}
eval *createmark nodes 2 displayed
set nids [hm_getmark nodes 2]
*createmark components 1 displayed
set acmtr [hm_getmark components 1]

*displayall
foreach Acmtr $acmtr {

*createmark nodes 1 'by comps' $Acmtr
set nd [hm_getmark nodes 1]

foreach Nd $nd {

set x1 [hm_getentityvalue nodes $Nd 'x' 0]
set y1 [hm_getentityvalue nodes $Nd 'y' 0]
set z1 [hm_getentityvalue nodes $Nd 'z' 0]
puts $x1
*createmark nodes 1 'by sphere' $x1 $y1 $z1 3 inside 0 0 0
set c [hm_getmark nodes 1]
lappend sphereNd $c

}

eval *createmark nodes 1 {*}$sphereNd
set s_nd [hm_getmark nodes 1]
puts $s_nd

eval *createmark nodes 2 {*}$nids
eval *markdifference nodes 1 nodes 2
set E3 [hm_getmark nodes 1]
puts $E3 

#create enity sets

*createmark nodes 1 {*}$E3
set st [hm_latestentityid sets 0]
puts &st
set c [hm_getsolverids sets '$st' -byid]

set d [lindex $c 0]
set e [lindex $d 0]
set f [expr $e+1]
set abc 'Set_$f'
 
*entitysetcreate $abc nodes 1
*createmark sets 2 $abc
set xz [hm_getmark sets 2]

#Check why dictionary loading and note drive name too.

*dictionaryload sets 2 'R:/Altair/14.0/templates/feoutput/ls-dyna971/dyna.key' 'Node'
*attributeupdateint sets $xz 4343 9 2 0 1
*attributeupdateint sets $xz 9001 9 2 0 0
*attributeupdateint sets $xz 90 9 2 0 0
*attributeupdatedouble sets $xz 1901 9 0 0 0
*attributeupdatedouble sets $xz 1902 9 0 0 0
*attributeupdatedouble sets $xz 1903 9 0 0 0
*attributeupdatedouble sets $xz 1904 9 0 0 0
*attributeupdatestring sets $xz 6254 9 2 0 'MECH'

#Create Xtra constarined node set card

set cn [hm_latestentityid constrainedextranodes 0]
set vn [expr $cn+1]
*createentity constrainedextranodes config=102 name='XtraNodes_$vn'

#*setvalue constrainedextranodes id=$vn name=Test_card

#Update Xtra constrained

*setvalue constrainedextranodes id=$vn compid={comps $Acmtr}
*setvalue constrainedextranodes id=$vn entityid={sets $xz}


}

Answers

  • tinh
    tinh Altair Community Member
    edited March 2018

    Hi

    Try this sample:

    set fpt [open '$dir/Connectors_comp.csv']

    set buffer [read -nonewline $fpt]

    close $fpt

    set NodeList {}

    foreach Line [split $buffer \n] {

         lassign [split $Line ,] CE_L CE_Coords CE_Comps

         *createnode {*}$CE_Coords

         lappend NodeList [hm_latestentityid nodes]

    }

  • Jouher_20929
    Jouher_20929 Altair Community Member
    edited March 2018

    Hi Tinh,

    I have written below code,

     

        set fileName [tk_getOpenFile]

    set fp [open $fileName r];

    while {![eof $fp]} {

            set x [gets $fp line]
            if {$x > 0} {
               set line [split $line ,]

      set a [lindex $line 1]
       set x1 [lindex $a 0]
     set y1 [lindex $a 1]
      set z1 [lindex $a 2]
    *createnode $x1 $y1 $z1 0 0 0

            }
           }
        }
        close $fp;

     

    But the problem is I have realiz acm shell gap using this. so I have to get node id and linked components at each step

  • tinh
    tinh Altair Community Member
    edited March 2018

    Hi

    Try this sample:

    set fpt [open '$dir/Connectors_comp.csv']

    set buffer [read -nonewline $fpt]

    close $fpt

    set NodeList {}

    foreach Line [split $buffer \n] {

         lassign [split $Line ,] CE_L CE_Coords CE_Comps

         *createnode {*}$CE_Coords

         lappend NodeList [hm_latestentityid nodes]

    }

     

    above red marks are your concerns. use them to realize connector each step

  • pohan
    pohan Altair Community Member
    edited March 2018

    Hi tinh

    I have a csv file also. There is one column and I use the value in each cell to assign to one variable  in the for loop in TCL. Could you please tell me the code for this case.

    Thank you

  • tinh
    tinh Altair Community Member
    edited March 2018

    tell me an example of your csv file

  • pohan
    pohan Altair Community Member
    edited March 2018

    Hi Tinh

    My csv file contains one column for example like that 1 2 3 4 5

    Each value in this column will be used in a for loop in tcl. So I need get the data from this csv file.

  • tinh
    tinh Altair Community Member
    edited March 2018

    just read file then íts content is a tcl list already

  • pohan
    pohan Altair Community Member
    edited March 2018

    Hi.

    Is the command to read file like that

    set fpt [open '$dir/Connectors_comp.csv']

    set buffer [read -nonewline $fpt]

    close $fpt

  • tinh
    tinh Altair Community Member
    edited March 2018

    yes it is

  • pohan
    pohan Altair Community Member
    edited September 2018

    HI.

    I used this sample and it work

     

    set dir [tk_chooseDirectory \
            -initialdir ~ -title 'Choose a directory']
    cd $dir
    set fpt [open '$dir/Listnodes.csv']

    set buffer [read -nonewline $fpt]

    close $fpt

    set NodeList {}

    foreach Line [split $buffer \n] {

         lassign [split $Line ,] CE_L CE_Coords CE_Comps

         *createnode 0 {*}$CE_L $CE_Coords

         lappend NodeList [hm_latestentityid nodes]

    }

    I put both tcl and csv file in the same foldoer 

    Could you please tell me how to make this folder to be default directory so I dont have to select this folder each time.

     

     

  • vipin_22401
    vipin_22401 Altair Community Member
    edited September 2018

    @pohan

    add this line so that by default it will take script directory.

    set dir [file dir [info script]]

  • pohan
    pohan Altair Community Member
    edited September 2018

    Hi

    I have an other csv file with only one column. I used this script to read the csv file

    ------------

    set dir [file dir [info script]]

    cd $dir

    set fpt [open '$dir/Listcontact.csv']

    set buffer [read -nonewline $fpt]

    close $fpt

    foreach Line [split $buffer \n] {

         lassign [split $Line ,] y
    }
    puts $y{2} 

    -----------------

    I used 'puts $y{2} to show the second value in the list but It does not works.

    Could you please show me how to do.

    Thank you

  • tinh
    tinh Altair Community Member
    edited September 2018

    Hi

     lassign [split $Line ,] y1 y2 y3 ...

    puts $y2

  • pohan
    pohan Altair Community Member
    edited October 2018

    Hi tinh

    I did like your advice but nothing happen.

    For convenience I attached the csv file.

    My idea is 45 (the number in the second row) will be displayed

    Unable to find an attachment - read this blog

  • pohan
    pohan Altair Community Member
    edited October 2018

    Hi

    I modified my script and it works. 

     

    foreach Line [split $buffer \n] {

             lassign [split $Line ,] y
        set i [expr 1 + $i]
        set B($i,0) $y
    }

     

    Thanks

  • tinh
    tinh Altair Community Member
    edited October 2018

     Hi

    Technically, using 'incr i' instead of

    set i [expr 1 + $i]

    It's effective with big loops