Jump to content
Jouher

how to read a csv file and create nodes from coordinates

Recommended Posts

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}


}

Share this post


Link to post
Share on other sites

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]

}

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
1 hour ago, tinh said:

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

 

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...