TCL script automatic note creation

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

Hello all,

 

This is my first post on the forum.

 

I'm trying to do some scripting in order to automate some actions that we do quite frequently. I'm new to the TCL language and programming in general, so please forgive me if I'm making beginners mistakes. For this script I'm trying to add a note with a predefined text and layout to a selected element. I started by editing a few scripts that I found on the exchange. I've managed to attach the note to a hard-coded element, so the next step is to attach the note to a selected element and that's where I get stuck. I should be able to pass the selected elements on to the note handler, but I'm not able to do that. This is what I've come up with so far:

 

namespace eval ::hw::post::Company {

}

proc ::hw::post::Company::BuildInitialUI {} {

    variable m_strFileName;
    variable panelPost;

    if { [ post panel get frame 'addNoteStress' ] == '' } {
        set panelPost [ post panel add 'addNoteStress'];
    }

    if {[winfo exists $panelPost.f1] == 0} {

        set top [frame $panelPost.f1 -bd 0 ];

        grid rowconfigure $panelPost 0 -weight 1;
        grid columnconfigure $panelPost 0 -weight 1;

        grid rowconfigure $top 3 -weight 1;
        grid columnconfigure $top 2 -weight 1;

        grid $top -row 0 -column 0 -sticky news -padx 0 -padx 4;

        set frm_mainFrame [frame $top.frm_mainFrame];
        pack $frm_mainFrame -anchor nw -fill both -expand 1 -padx 5 -pady 5;

        # Pack the main frame
        ::hwt::AddPadding $frm_mainFrame -height 5;

        label $frm_mainFrame.lbl -text 'Selection:';
        pack $frm_mainFrame.lbl -side top -anchor n -padx 5 -pady 5;

        set ::hw::post::Company::elemCol [ post::Collector $frm_mainFrame.entCol entity 'vecEntCol' ::post::SelectionSetCollector \
              -notifyFunc ::hw::post::Company::NotifyFuncForElemCol \
              -withreset 1 \
              -text 'Elements' \
              -types 'Elements'];

        pack $frm_mainFrame.lbl $frm_mainFrame.entCol -side left -anchor w -padx 5;
        focus -force $frm_mainFrame.entCol.colContainer.collectorButton;

        set n_width [::hwt::DluWidth 55];
        set n_height [::hwt::DluHeight 14];

        set frm_actionFrame [frame $top.frm_actionFrame];
        pack $frm_actionFrame -anchor nw -fill both -expand 1 -padx 5 -pady 5;

        set btnCreate [::hwt::CanvasButton $frm_actionFrame.btnCreate $n_width $n_height \
            -text 'Create' \
            -takefocus 1 \
            -help 'Create a note' \
            -command '::hw::post::Company::CreateNote'
        ];
        set btnClose [::hwt::CanvasButton $frm_actionFrame.btnClose $n_width $n_height \
            -text 'Close' \
            -takefocus 1 \
            -help 'Close the toplevel' \
            -command '::hw::post::Company::CloseUI'
        ];

        pack $btnCreate $btnClose -side left -anchor w -padx 5;

    }

    post panel draw 'addNoteStress';

}

proc ::hw::post::Company::NotifyFuncForElemCol {type id args} {
   
    variable elemCol;
   
    ::hw::post::Company::OnMultipleEntityColSel $elemCol [namespace current]::lst_elemsFromElemCol $type $id $args
    ::hw::post::Company::CreateNote $elemCol [namespace current]::lst_elemsFromElemCol $type $id $args


}

proc ::hw::post::Company::GetEntitiesFromSelectionSetCol {str_compCol} {
    
  set t [::hw::GetT]
 hwi OpenStack;
 set compCol $str_compCol
 ::post::GetActiveModelHandle modHandle$t;
  set selSetId [$compCol cget -id];
  
  if { $selSetId != 'ElemEntCol'} {
   modHandle$t GetSelectionSetHandle selSetHandle$t $selSetId
  selSetHandle$t SetSelectMode all;   
   set lst_entityList [selSetHandle$t GetList];
    
    if {[catch {set lst_elemsList [selSetHandle$t GetList]} error]} {
   
   #tk_messageBox -message '$error - unable to create selection set handle to get selected entities.' -icon error -title 'IIHS';
   hwi CloseStack;   
   return 0;
   }
  
  selSetHandle$t Clear;
  selSetHandle$t ReleaseHandle;
  modHandle$t ReleaseHandle
  hwi CloseStack;
  return $lst_entityList;

 } else {
       
       #tk_messageBox -message 'Unable to create selection set handle to get selected entities.' -icon error -title 'IIHS';
  #hwi ReleaseAllHandles;
  modHandle$t ReleaseHandle;      
        
  hwi CloseStack;
  return -1;
 }
   
 #hwi ReleaseAllHandles;
 
    modHandle$t ReleaseHandle;
   
    hwi CloseStack; 
 return -1;
}

proc ::hw::post::Company::OnMultipleEntityColSel {col lst_entitiesVar type id args } {
   
    if { ![::post::IsModelLoaded] } { return }
   
    #puts '$col-- $lst_entitiesVar -- $type -- $id -- $args'
   
    if { $type == 'update' } {
       
        #Apply; 
        ::post::ShowSetStatisticsInExtendedSelectionPanel [$col cget -id];
       
        set lst_entity [::hw::post::Company::GetEntitiesFromSelectionSetCol $col];
        foreach ent $lst_entity {
            if {[lsearch -exact [set $lst_entitiesVar] $ent] == -1} {
                lappend [set lst_entitiesVar] $ent;
            }                   
        }
       
        #set $lst_entitiesVar [lsort -unique [join [set $lst_entitiesVar]]];       
       
        return;
    }
   
    if { $type == 'update_id' } {
       
        # update collector id
        $col configure -id $id;
       
        # enable id visibility
        hwi OpenStack;
        set t [::post::GetT];
        ::post::GetActiveModelHandle m$t;
        m$t GetSelectionSetHandle s$t $id;
        s$t SetIDVisibility true;
       
        hwi CloseStack;
        ::post::ShowSetStatisticsInExtendedSelectionPanel $id;
       
        set $lst_entitiesVar '';
       
        return;
    }

    if { $type == 'update_selection' } {
   
        ::post::_Draw;
        ::post::ShowSetStatisticsInExtendedSelectionPanel [$col cget -id];
       
#        set $lst_entitiesVar '';
        return;
    }
   
    if {$type == 'type_change' } {
        #CreateCheckBoxes
        return;
    }
   
    if { $type == 'quick_window_select' } {

        #Apply;

        # re-activate regular window picking if it was active
        ::post::ReactivateByWindowSelection $col;
        return;
    }
    if { $type == 'quick_window_menu' } {

        # pop up the quick window options
        ::post::postBuildQuickWindowMenu;

        # re-activate regular window picking if it was active
        ::post::ReactivateByWindowSelection $col;
    }

}

proc ::hw::post::Company::CreateNote {} {
  
  #::hw::post::Company:GetEntitiesFromSelectionSetCol::str_compCol
   set t [::hw::GetT]
  hwi OpenStack;
  #set compCol $::hw::post::Company:GetEntitiesFromSelectionSetCol::lst_entityList
  ::post::GetActiveModelHandle modHandle$t;
   #set selSetId [$compCol cget -id];
   
   if { $selSetId != 'ElemEntCol'} {
    modHandle$t GetSelectionSetHandle selSetHandle$t $selSetId
   selSetHandle$t SetSelectMode all;   
    set lst_entityList [selSetHandle$t GetList];
     
     if {[catch {set lst_elemsList [selSetHandle$t GetList]} error]} {
    
    #tk_messageBox -message '$error - unable to create selection set handle to get selected entities.' -icon error -title 'IIHS';
    hwi CloseStack;   
    return 0;
    }
   
   selSetHandle$t Clear;
   selSetHandle$t ReleaseHandle;
   modHandle$t ReleaseHandle
   hwi CloseStack;
   return $lst_entityList;
 
  } else {
        
        #tk_messageBox -message 'Unable to create selection set handle to get selected entities.' -icon error -title 'IIHS';
   #hwi ReleaseAllHandles;
   modHandle$t ReleaseHandle;      
         
   hwi CloseStack;
   return -1;
  }
    
  #hwi ReleaseAllHandles;
  
     modHandle$t ReleaseHandle;
    
     hwi CloseStack; 
 return -1;
  
 set elemIDsare $lst_entityList

 foreach el_id $elemIDsare {
  ::post::GetPostHandle pst$t
  set nid [pst$t AddNote]
  pst$t GetNoteHandle n$t $nid
  n$t SetAttachment 'element $el_id'
  n$t SetPositionToAttachment false
  n$t SetText 'Element ID: {entity.id} \n{entity.contour_val,%.2f} MPa'
  n$t SetTransparency false
  n$t SetBackgroundColor '255 255 255'
  n$t SetColor '0 0 0'
  n$t SetTextColor '0 0 0'
  n$t ReleaseHandle
  pst$t ReleaseHandle
  ::post::Draw
  }
 }

proc ::hw::post::Company::CloseUI {} {

    if { [winfo exists .postPanel.addNoteStress] } {

        # Destroy the panel
        catch {destroy .postPanel.addNoteStress} strError;
    }

}


::hw::post::Company::BuildInitialUI

 

I'd appreciate all help and feedback. Thank you!

 

Best regards,

Ruben

Answers

  • tinh
    tinh Altair Community Member
    edited March 2018

    proc CreateNote has no argument. please confirm it

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited March 2018

    Hello Tinh,

     

    Thanks for your quick reply. By argument I assume that you are referring to a value that should be given between the brackets of proc ::hw::post::Company::CreateNote {}, correct? What argument should I give to that function? I already tried a few things, but with no success so far.

  • tinh
    tinh Altair Community Member
    edited March 2018

    Yes, you invoked it with some args but when define it you do not provide args

    so it doesn't work

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited March 2018

    Hello Tinh,

     

    Any chance you could help me figuring out which arguments should be included in the function? I don't have any idea where to start.

  • tinh
    tinh Altair Community Member
    edited March 2018

    I saw this line

    if you wrote it, you do know its args

    hw::post::Company::CreateNote $elemCol [namespace current]::lst_elemsFromElemCol $type $id $args

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited March 2018

    I already tried adding those args to the function:

     

    proc ::hw::post::Company::CreateNote {col lst_entitiesVar type id args } {

    CODE

    }

     

    But then I get the following error:

     

    wrong # args: should be '::hw::post::Company::CreateNote col lst_entitiesVar type id ...'
    wrong # args: should be '::hw::post::Company::CreateNote col lst_entitiesVar type id ...'
        while executing
    '::hw::post::Company::CreateNote'
        ('eval' body line 1)
        invoked from within
    '# Compiled -- no source code available
    error 'called a copy of a compiled script''
        (procedure '::hwt::ReleaseCanvasButton' line 1)
        invoked from within
    '::hwt::ReleaseCanvasButton 0 .postPanel.addNoteStress.f1.frm_actionFrame.btnCreate'
        (command bound to event)

     

  • tinh
    tinh Altair Community Member
    edited March 2018

    there is a position at which you invoke the command without any args

    please verify it