Import tcl script variable into macro

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

How can I run a tcl script through a macro using *evaltclscript and then import a variable from that tcl script to use in the same macro, such as the userpage.mac file ?

 

Thank you.

 

Any help is appreciated.

Answers

  • tinh
    tinh Altair Community Member
    edited March 2017

    Hi,

    variables in tcl script seem to be not available in macro file

    I think you don't need to use macro file, because all of its functions can be (and will be) done by tcl

    example buttongroup, button in macro file => are tk buttons

    macro commands in macro file => are same as hypermesh tcl commands

    you can use tcl script to retrieve / add / remove buttons with your own procs to utility tab

    example

    set frmUtilTab [hm_winfo utilitywindow]

    set frmMacros [winfo children $frmUtilTab]

    set WMgr [winfo manager $frmMacros]

    set WMgrOptions [$WMgr info $frmMacros]

    set frmMyUtils [frame .frmMyUtils]

    pack $frmMyUtils -in $frmUtilTab -fill both -expand 1

    set btnDemo [::ttk::button $frmMyUtils.btnDemo -text Hello -command {tk_messageBox -message 'This is a demo'}]

    pack $btnDemo -anchor center

     

    #if you want to restore original utility functions as in macro file:

    pack forget $frmMyUtils

    eval $WMgr

    1. $WMgrOptions
  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited March 2017

    Hi,

    variables in tcl script seem to be not available in macro file

    I think you don't need to use macro file, because all of its functions can be (and will be) done by tcl

    example buttongroup, button in macro file => are tk buttons

    macro commands in macro file => are same as hypermesh tcl commands

    you can use tcl script to retrieve / add / remove buttons with your own procs to utility tab

    example

    set frmUtilTab [hm_winfo utilitywindow]

    set frmMacros [winfo children $frmUtilTab]

    set WMgr [winfo manager $frmMacros]

    set WMgrOptions [$WMgr info $frmMacros]

    set frmMyUtils [frame .frmMyUtils]

    pack $frmMyUtils -in $frmUtilTab -fill both -expand 1

    set btnDemo [::ttk::button $frmMyUtils.btnDemo -text Hello -command {tk_messageBox -message 'This is a demo'}]

    pack $btnDemo -anchor center

     

    #if you want to restore original utility functions as in macro file:

    pack forget $frmMyUtils

    eval $WMgr

    1. $WMgrOptions

    @tinh : I tried the above code, however I only want to edit the buttons shown when the User button is clicked on the utility tab, rather than the entire utility tab, how could I go about doing that ?

  • tinh
    tinh Altair Community Member
    edited March 2017

    Hi, what do you mean 'edit the buttons'?

    if you want to run tcl script by macro buttons, example

    *createbutton(5,'Demo script',-1,0,10,BUTTON,'Run your script',EvalTcl,'C:/users/demo/documents/yourscript.tcl')

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited March 2017

    Hi, what do you mean 'edit the buttons'?

    if you want to run tcl script by macro buttons, example

    *createbutton(5,'Demo script',-1,0,10,BUTTON,'Run your script',EvalTcl,'C:/users/demo/documents/yourscript.tcl')

    @tinh : I understand that. However what I meant to say was, I only wanted to show the Demo button when I click on the User button on the utility tab. The code that you specified shows the demo button directly when I click on the utility tab.

     

    The User button that is shown in the image below appears to the bottom right when the utility menu is clicked in Hypermesh. My aim is to create some buttons to be displayed when Utility -> User is clicked , and I need to do this using a tcl script rather than a macro.

     

    How should I go about it ?

    <?xml version="1.0" encoding="UTF-8"?>UtilityMacros.png

  • tinh
    tinh Altair Community Member
    edited March 2017

    Hi,

    I see, there is no direct command. but you can do like this:

    - open ...\hm\bin\win64\globalpage.mac to edit

    - looking for macro definition ' *beginmacro(macroSetActivePage) ', and add a command inside it:

     

    *beginmacro(macroSetActivePage)

            //Purpose: Sets the active page in the macro meu to passed argument.

           *setactivepage($1)

            //add below command to customize your own page by tcl:

           *evaltclstring('p_ShowMyUserPage $1',0)

    *endmacro()

     

    - define tcl procedure 'p_ShowMyUserPage' in a tcl file and source it first

    or define it in ...\hm\bin\win64\hmcustom.tcl (will be sourced whenever opening hm)

     proc p_ShowMyUserPage {Page} {       if {$Page!=5} {catch {place forget .frmUserPage}; return}       if {![winfo exists .frmUserPage]} {           frame .frmUserPage           pack [::ttk::button .frmUserPage.btnDemo -text Demo \                -command {tk_messageBox -message 'This is a demo'}]           #add your buttons here\           .....       }       set frmUserMac [winfo children [hm_winfo utilitywindow]]       set y [expr [winfo height $frmUserMac]-200]       place .frmUserPage -in $frmUserMac -x 0 -y $y }

     

    - now click Utility>User button, it will invoke p_ShowMyUserPage with Page=5 and show your buttons

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited March 2017

    @tinh : I did try the above method as follows,

     *beginmacro(macroSetActivePage)     //  Purpose:    Sets the active page in the macro menu to passed argument.     *setactivepage($1)     *evaltclstring('source \'C:/sample.tcl\';',0)     *evaltclstring('p_ShowMyUserPage $1',0) *endmacro()

    Where sample.tcl has the code,

     proc p_ShowMyUserPage {Page} {       if {$Page!=5} {catch {place forget .frmUserPage}; return}       if {![winfo exists .frmUserPage]} {           frame .frmUserPage           pack [::ttk::button .frmUserPage.btnDemo -text Demo \                -command {tk_messageBox -message 'This is a demo'}]           #add your buttons here\           .....       }       set frmUserMac [winfo children [hm_winfo utilitywindow]]       set y [expr [winfo height $frmUserMac]-200]       place .frmUserPage -in $frmUserMac -x 0 -y $y }

    However, I do not see any output when I click the Utility -> User button. The Hypermesh status bar shows an error message saying,

    'Data in file could not be interpreted. Possibly incorrect format.'

     

    Also, how would I get this to work only for the userpage rather than for all the pages that have macroSetActivePage associated with them ?

  • tinh
    tinh Altair Community Member
    edited March 2017

    No no, jcd

    please remove the line :

      *evaltclstring('source \'C:/sample.tcl\';',0)

    we don't need this line,

    and what you have to do is create a tcl file named 'hmcustom.tcl' and put it in folder .../hm/bin/win64 

    copy codes of proc p_ShowMyUserPage ... and paste into hmcustom.tcl

     

    so that proc will be defined everytime you open hypermesh

     

    comeback to macro file. after remove line *evaltclstring('source .... ', macroSetActivePage will work whenever you click on User button (and also Disp, Geom/Mesh,...) but when you click User button , an argument with value equal 5 will be passed to proc p_ShowMyUserPage

    this proc then simply know that you pressed User button and place a frame with your own buttons on the tab

    if you click Disp, Geom/Mesh,... buttons, the argument value is not equal to 5 and proc p_ShowMyUserPage will remove your buttons from Utility tab 

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited March 2017

    No no, jcd

    please remove the line :

       *evaltclstring('source \'C:/sample.tcl\';',0)

    we don't need this line,

    and what you have to do is create a tcl file named 'hmcustom.tcl' and put it in folder .../hm/bin/win64 

    copy codes of proc p_ShowMyUserPage ... and paste into hmcustom.tcl

     

    so that proc will be defined everytime you open hypermesh

     

    comeback to macro file. after remove line *evaltclstring('source .... ', macroSetActivePage will work whenever you click on User button (and also Disp, Geom/Mesh,...) but when you click User button , an argument with value equal 5 will be passed to proc p_ShowMyUserPage

    this proc then simply know that you pressed User button and place a frame with your own buttons on the tab

    if you click Disp, Geom/Mesh,... buttons, the argument value is not equal to 5 and proc p_ShowMyUserPage will remove your buttons from Utility tab 

    @tinh : I did try this and it worked perfectly, however, I still had one small issue.

     

    Whenever I open a fresh HyperMesh window, navigate to the Utility frame and then click on User, the button appears perfectly. And if I stay on the utility frame and click on other buttons such as Disp or Abaqus and then click back on the User button, then the button .btnDemo still appears.

     

    However, when I click away from the Utility menu and click on the Mask or Model menu and then come back to the Utility menu and click on the User button, the demo button doesnt appear then. It's a really weird occurence, and I'm not even sure how to begin debugging this, since I assume that the macroSetActivePage is called every time I click on the User button, but the demo button inside it still doesnt show up.

     

    Any help is appreciated.

  • tinh
    tinh Altair Community Member
    edited March 2017

    Hi @jcd

    it is due to widget stacking order. Please add raise command to the end of p_ShowMyUserPage, to push it up:

    proc p_ShowMyUserPage {Page} {

     #......

        place .frmUserPage -in $frmUserMac -x 0 -y $y

        raise .frmUserPage

    }

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited March 2017

    Hi @jcd

    it is due to widget stacking order. Please add raise command to the end of p_ShowMyUserPage, to push it up:

    proc p_ShowMyUserPage {Page} {

     #......

        place .frmUserPage -in $frmUserMac -x 0 -y $y

        raise .frmUserPage

    }

    Yes, this worked perfectly! Thank you.