TCL script to load points and forces

Altair Forum User
Altair Forum User
Altair Employee
edited October 2020 in Community Q&A

Hi,

 

I am making a TCL script to import a CSV file in Altair HyperMesh. The CSV file contains 7 columns (x-coordinate, y-coordinate, z-coordinate, x-force, y-force, z-force and resultant force). The idea is to run the script, the nodes are created on the coordinates given in the first 3 columns, the node is selected, a force is created on the selected note. (This should run through all the rows of the csv file.)

The code I have until now is the following:

 

lappend auto_path 'C:/Program Files/Altair/2017/hw/tcl/tcllib-1.19/modules'

package require csv
package require struct::matrix

set data [::struct::matrix]
$data add rows 16
$data add columns 7
set csvfile [open 'coordinates-forces.csv']
csv::read2matrix $csvfile data  , auto 
close $csvfile

set rows [data rows]

for {set i 0} {$i < $rows} {incr i} {
*createnode [data get cell 0 i] [data get cell 1 i] [data get cell 2 i]
*createmark nodes 1 i
*loadcreateonentity_curve nodes i 1 1 {data get cell 4 i} {data get cell 5 i} {data get cell 6 i} 0 0 {data get cell 7 i} 0 0 0 0 0
}

 

The error I currently get is 'invalid command name 'data''. Any help would be appeciated.

 

Thanks in advance!

Philip

Tagged:

Answers

  • Q.Nguyen-Dai
    Q.Nguyen-Dai Altair Community Member
    edited November 2018

    Before talking about your code, my question: how do you do with your loaded nodes? How to link to your mesh? your structure?

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2018

    The loaded nodes will be linked to the mesh with RBE2 or RBE3 elements (depending on the type of load). These will, for now, be implemented manually afterwards.

  • tinh
    tinh Altair Community Member
    edited November 2018

    Use $data instead of data

    Use [ ] instead of { }

     

    I think a simpler code can do , don't need structmatrix package, it just messes up your codes

     

    set fpt [open 'coordinates-forces.csv']

    set buf [read -nonewline $fpt]

    close $fpt

    foreach line [split $buf \n] {

       lassign [split $line ,] x y z fx fy fz f

       CreateNode

        CreateForce

    }

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2018

    Okay, so trying the simpler code you provided above, I still get an error.

     

    foreach line [split $buf \n] {
    lassign [split $line] x y z fx fy fz f
    *createnode $x $y $z
    *createmark nodes 1 1
    *loadcreateonentity_curve nodes 1 1 1 $fx $fy $fz 0 0 $f 0 0 0 0 0
    }

     

    The reason I put [split $line] is because it is actually separated with a semicolon, so a comma wouldn't work.

    The error I get is the following: extra characters after close-quote.

    I would assume that there might be a problem with the '{' after the foreach command, but I'm not sure.

     

    Any advice?

     

  • tinh
    tinh Altair Community Member
    edited November 2018

    Take a screenshot about the error please

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2018

    You can find the error attached.

    <?xml version="1.0" encoding="UTF-8"?>Screenshot.PNG

  • tinh
    tinh Altair Community Member
    edited November 2018

    Did you copy code from webbrowser then paste to text editor?

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2018

    Yeah at first I did. Now, I typed it in myself and it actually gives me another error.

    Note that now, I created an extra variable $n to show on which node the force needs to be applied. I checked it in the command file and this should actually work. But, I don't really understand the error showing up now.

    <?xml version="1.0" encoding="UTF-8"?>Screenshot2.PNG

  • tinh
    tinh Altair Community Member
    edited November 2018

    Use *createmark nodes 1 -1

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2018

    What the script does now is creating a node at 0 0 0 and a force on that node with components 0 0 0. My test csv file doesn't contain this node, nor a zero force.

    I'm not sure what's wrong here?

  • tinh
    tinh Altair Community Member
    edited November 2018

    to debug, try running with a short csv file , maybe 5 lines

     

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2018

    I have debugged it! There were some minor errors like \n instead of '\n' and the semicolon separator {;} in the split command. For those of you wondering what the final code looks like, you can find it beneath.

    Note: I've added a line to create a loadcollector for each created force.

     

    Thanks for the help tinh!

    <?xml version="1.0" encoding="UTF-8"?>Screenshot4.PNG

  • tinh
    tinh Altair Community Member
    edited November 2018

    \n instead of '\n'

     

    I don't think so. Actually \n and '\n' are the same. Anyway, gòod job