PCOMP(G) FROM EXCEL

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

Hi, has anybody an idea of how to import data from an excel file to setup the PCOMP or PCOMP(G) property in HM? I can do it with visual basic 2008 (read the .csv exported from the excel, write PCOMP(G) lines to a .bdf or .dat file and import it into HM), but I was wondering if someone from the MACRO-experts world knows a quicker (and probably more intelligent...) way to this.

Thanks in advance!

Answers

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited May 2011

    you can use tcl file to parse the csv file and write out the cards into the file. I can try it out if you send me a test case (bdf/dat file and csv file)

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited May 2011

    you can use tcl in HM to parse the csv file and write out the cards into the file. I can try it out if you send me a test case (bdf/dat file and csv file)

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

  • mvass
    mvass Altair Community Member
    edited May 2011

    Thanks for the reply! No, problem, I'll send you a simple .csv file to test it. By the way, can't it be done directly with the excel (.xls) file? I mentioned the .csv because it is easier for the code I've written in VB2008 to read a text file instead of the excel itself. Not that .csv is a problem for me.

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited May 2011

    csv is always easier compared to excel, because excel is a proprietry format of microsoft while csv is simple comma seperated values

    i had couple of more observations

    1. In VB you can directly read from excel, because VB is also from microsoft and VB has direct API's to do this

    2, Excel can be read from TCL also, there is a package called as tcom which can be used to read all MS Office formats like word, excel, ppt etc , but its a bit complicated

    3. Regarding your question about a smarter way, if you already have the model loaded in hypermesh, then doing in hypermesh would be faster, but if you have to load the file in hypermesh just for this, the loading operation may take some time for big models, in that case, using a VB exe to just append those cards at the end of the file would be a faster way

  • mvass
    mvass Altair Community Member
    edited June 2011

    I agree. I am not a VB expert, nor a software engineer however after consulting a good colleague of mine, you will be surprised to hear that even with MS Excel files VB is not as 'flexible' as with good old text files like .csv. To answer to your call for an example, imagine a .csv file -exported from the respective excel- of the form:

    2,1,0.125,45,YES

    2,1,0.125,-45,YES

    2,1,0.125,-45,YES

    2,1,0.125,45,YES

    where the first number is the PCOMP card ID (same for all lines), the second is the material assigned to PCOMP card ID, the third is the ply thickness, the fourth is the ply orientation and the last one is the SOUT option (YES or NO)

    Using the above-mentioned information as an input for your MACRO the resulting output (RADIOSS bulk or NASTRAN) for import inside HM will be:

    PCOMP,2

    ,1,0.125,45.0,YES

    ,1,0.125,-45.0,YES

    ,1,0.125,-45.0,YES

    ,1,0.125,45.0,YES

    Similar rule applies for the PCOMP(G) (ply-based modeling) entry card.

    I think it is useful to use excel tables or the respective .csv as an input (instead of filling the tables in hyperlaminate inside HM) especially when dealing with large number of plies, or receiving ply data from the CATIA users.

    Thanks for your attention

    mvass

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2011

    let me know of your mail id

  • mvass
    mvass Altair Community Member
    edited June 2011

    Hi, you can send me a PM if you like.

    Thanks

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2011

    hi mvass,

    use the following code, wil do your work, hope this helps

    ##have kept comments for each line so that you can understand it and modify if required

    proc setupPCOMP {} {

    set matId 0

    set plyThk 0

    set plyAng 0

    set numPlies 0

    ##ask for the csv file

    set types {

    {{csv Files} {.csv} }

    }

    set fname [tk_getOpenFile -filetypes $types]

    #set fname 'c:/temp/pcomp.csv'

    ##read the file to get number of plies

    set fp 0;

    set ret [catch {set fp [open '$fname' r+]} msg];

    while {![eof $fp]} {

    gets $fp line;

    incr numPlies

    }

    ##get num of proerties

    set numprops [hm_entitymaxid props]

    incr numprops

    ##create a propertycollector

    *collectorcreateonly properties 'pcomp-$numprops' '' 5

    *createmark prop 1 -1

    set colId [hm_getmark prop 1]

    ##load PCOMP dictionary

    set dict '$::env(ALTAIR_HOME)/templates/feoutput/nastran/general'

    *createmark prop 1 -1

    *dictionaryload properties 1 '$dict' 'PCOMP'

    ##set it to created property collector

    *initializeattributes properties 'pcomp-$numprops'

    ##create mark

    *createmark properties $colId 'pcomp-$numprops'

    ##set num plies

    *attributeupdateint properties $colId 3027 1 0 0 $numPlies

    ##loop through the file again to set mat, thickness and ply angle

    set fp 0;

    set ret [catch {set fp [open '$fname' r+]} msg];

    set matId 0

    set matName ''

    set plyThk {}

    set plyAng {}

    set soutopt {}

    while {![eof $fp]} {

    gets $fp line;

    set tmplist [split $line ,];

    lappend matId [lindex $tmplist 1]

    ##set material, will work only if material already exists !! hence kept in catch

    catch {set matName [hm_getentityvalue mats $matId name 1 -byid]}

    catch {*materialupdate properties $colId $matName}

    lappend plyThk [lindex $tmplist 2]

    lappend plyAng [lindex $tmplist 3]

    lappend soutopt [lindex $tmplist 4]

    }

    ##set ply thickness

    eval *createdoublearray $numPlies $plyThk

    *attributeupdatedoublearray properties $colId 3024 1 2 0 1 $numPlies

    ##set ply angle

    eval *createdoublearray $numPlies $plyAng

    *attributeupdatedoublearray properties $colId 3025 1 2 0 1 $numPlies

    ##set SOUT

    eval *createstringarray $numPlies $soutopt

    *attributeupdatestringarray properties $colId 3026 1 2 0 1 $numPlies

    }

    setupPCOMP

  • mvass
    mvass Altair Community Member
    edited June 2011

    Thank you very much CAE.DEVELOPER! What a fantastic work! I'll try it and let you know about the results. I am sure that many people will appreciate your effort.

  • mvass
    mvass Altair Community Member
    edited June 2011

    Hi! I've created a new MAT8 (2D orthotropic) material and then ran your script. It asked for a csv file, I loaded a simple one (just like the one in my example with material ID = 1) and I got the following error message:

    can't read '::env(ALTAIR_HOME)': no such variable

    can't read '::env(ALTAIR_HOME)': no such variable

    while executing

    'set dict '$::env(ALTAIR_HOME)/templates/feoutput/nastran/general''

    (procedure 'setupPCOMP' line 33)

    invoked from within

    'setupPCOMP'

    (file 'G:/Documents and Settings/user/Desktop/pcomp_from_excel.tcl' line 88)

    invoked from within

    'source {G:/Documents and Settings/user/Desktop/pcomp_from_excel.tcl}'

    ('uplevel' body line 1)

    invoked from within

    'uplevel #0 'source {$file}''

    (procedure '::HM_Framework:image/emoticons/default_tongue.png' alt=':P' srcset='/emoticons/tongue@2x.png 2x' width='20' height='20'>_FileLoad' line 49)

    invoked from within

    '::HM_Framework:image/emoticons/default_tongue.png' alt=':P' srcset='/emoticons/tongue@2x.png 2x' width='20' height='20'>_FileLoad 24'

    (menu invoke)

    Thank you in advance for your assistance.

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2011

    actually there will be an environment variable ALTAIR_HOME define in HM installation, which points to ../altair100/hw10.0 directory. In my case it is like c:/prog/altair100/hw10.0.

    The script needs to know this location to load some libraries.

    If it is not there, can you add this environment variable yourself and then run the script?

  • mvass
    mvass Altair Community Member
    edited June 2011

    Hi, thanks for the reply. I kinda understood that the script cannot find the environment variable, however I cannot locate it in our installation. We have HWs installed in C:\Altair\hw10.0\

    So, can you please explain how to do this? I am sorry but I haven't programmed with tcl before.

    Thank you in advance

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2011

    hi mvass,

    try this

    ##have kept comments for each line so that you can understand it and modify if required

    proc setupPCOMP {} {

    set matId 0

    set plyThk 0

    set plyAng 0

    set numPlies 0

    ##ask for the csv file

    set types {

    {{csv Files} {.csv} }

    }

    set fname [tk_getOpenFile -filetypes $types]

    #set fname 'c:/temp/pcomp.csv'

    ##read the file to get number of plies

    set fp 0;

    set ret [catch {set fp [open '$fname' r+]} msg];

    while {![eof $fp]} {

    gets $fp line;

    incr numPlies

    }

    ##get num of proerties

    set numprops [hm_entitymaxid props]

    incr numprops

    ##create a propertycollector

    *collectorcreateonly properties 'pcomp-$numprops' '' 5

    *createmark prop 1 -1

    set colId [hm_getmark prop 1]

    set ::env(ALTAIR_HOME) 'C:/Altair/hw10.0'

    ##load PCOMP dictionary

    set dict '$::env(ALTAIR_HOME)/templates/feoutput/nastran/general'

    *createmark prop 1 -1

    *dictionaryload properties 1 '$dict' 'PCOMP'

    ##set it to created property collector

    *initializeattributes properties 'pcomp-$numprops'

    ##create mark

    *createmark properties $colId 'pcomp-$numprops'

    ##set num plies

    *attributeupdateint properties $colId 3027 1 0 0 $numPlies

    ##loop through the file again to set mat, thickness and ply angle

    set fp 0;

    set ret [catch {set fp [open '$fname' r+]} msg];

    set matId 0

    set matName ''

    set plyThk {}

    set plyAng {}

    set soutopt {}

    while {![eof $fp]} {

    gets $fp line;

    set tmplist [split $line ,];

    lappend matId [lindex $tmplist 1]

    ##set material, will work only if material already exists !! hence kept in catch

    catch {set matName [hm_getentityvalue mats $matId name 1 -byid]}

    catch {*materialupdate properties $colId $matName}

    lappend plyThk [lindex $tmplist 2]

    lappend plyAng [lindex $tmplist 3]

    lappend soutopt [lindex $tmplist 4]

    }

    ##set ply thickness

    eval *createdoublearray $numPlies $plyThk

    *attributeupdatedoublearray properties $colId 3024 1 2 0 1 $numPlies

    ##set ply angle

    eval *createdoublearray $numPlies $plyAng

    *attributeupdatedoublearray properties $colId 3025 1 2 0 1 $numPlies

    ##set SOUT

    eval *createstringarray $numPlies $soutopt

    *attributeupdatestringarray properties $colId 3026 1 2 0 1 $numPlies

    }

    setupPCOMP

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

    -- cae.developer@gmail.com

  • mvass
    mvass Altair Community Member
    edited June 2011

    I am sorry to trouble you again but now I get the following error message:

    0

    0

    while executing

    '*dictionaryload properties 1 '$dict' 'PCOMP''

    (procedure 'setupPCOMP' line 35)

    invoked from within

    'setupPCOMP'

    (file 'C:/Documents and Settings/user/Desktop/pcomp_excel_v2.tcl' line 88)

    invoked from within

    'source {C:/Documents and Settings/user/Desktop/pcomp_excel_v2.tcl}'

    ('uplevel' body line 1)

    invoked from within

    'uplevel #0 'source {$file}''

    (procedure '::HM_Framework:image/emoticons/default_tongue.png' alt=':P' srcset='/emoticons/tongue@2x.png 2x' width='20' height='20'>_FileLoad' line 49)

    invoked from within

    '::HM_Framework:image/emoticons/default_tongue.png' alt=':P' srcset='/emoticons/tongue@2x.png 2x' width='20' height='20'>_FileLoad 24'

    (menu invoke)

    I simply copied your code to notepad++ and saved it as pcomp_excel_v2.tcl on my desktop. Then I created a simple .csv file like:

    1,1,0.125,45,YES

    1,1,0.125,-45,YES

    1,1,0.125,-45,YES

    1,1,0.125,45,YES

    In Hypermesh I created a MAT8 material with material ID 1 and loaded the .tcl created before. After loading the .csv a pcomp property is created but with the error message seen above.

    Again, thank you in advance for your kind assistance.

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2011

    are you working in nastran profile?

    do you have the following file on your system?

    'C:/Altair/hw10.0/templates/feoutput/nastran/general'

  • mvass
    mvass Altair Community Member
    edited June 2011

    Confusion reins!

    Your code works like a charm! Thank you, THANK YOU! Sorry for troubling you but I've made a huge mistake: HWs is not installed in c:/Altair/hw10.0 on our system but on: e:/Altair... (](*,) ), so after changing this in your code -guess what...- it WORKED!

    You have my respects and thanks!

    (Now I'll try to copy and modify your code for the PCOMPG entry. I'll let you know)

    mvass

    PS: Yes, I am using the NASTRAN profile

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2011

    you are welcome mvass, good to know this helps you

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2011

    instead of using system variables or hard-coding the paths, use the following code to set the nastran template.

    	set templatedir [hm_info -appinfo SPECIFIEDPATH TEMPLATES_DIR];	set dict [file join $templatedir 'feoutput' 'nastran' 'general'];

    Hypermesh knows where it is installed, the code above will take care of that.

    have fun!

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2011

    thanx Cesar, that helps, however I thought the env var ALTAIR_HOME was always available