Search code examples
dm-script

Defining a series of functions in DigitalMicrograph scripting


I have a set of functions inside a class that I need to define. Each passes a different value into another function:

void function00(object self, taggroup tg) self.otherfunction(tg,0,0)
void function01(object self, taggroup tg) self.otherfunction(tg,0,1)
void function02(object self, taggroup tg) self.otherfunction(tg,0,2)
void function03(object self, taggroup tg) self.otherfunction(tg,0,3)
void function04(object self, taggroup tg) self.otherfunction(tg,0,4)

I have 100 of these functions and I'd prefer not to define each one separately. Considering the above example I'd like to do something like:

for(number i=0; i<5; i++){
    void function0+i(object self, taggroup tg) self.otherfunction(tg,0,i)
}

which doesn't work on it's own. Any suggestions?

For some more context I create a series of check boxes inside 2 for loops with the following:

BOXinsides.DLGAddElement(DLGCreateCheckBox(label,0,"function"+i+j).DLGIdentifier("#function"+i+j))

and I need to define all the functions in some sensible way.


Solution

  • DigitalMicrograph scripting does not allow this type of template code. However, you can solve your problem by linking all checkbox items to the same action-method. The signature of the action method passed in the TagGroup which is the checkbox item itself. You can use this to derive information from it, for example by looking at a checkbox property such as its title:

    class myUI : UIframe
    {
      void generalFunction( object self , tagGroup checkTg )
      {
        // checkTg is the taggroup of the checkbox which fired the method.
        // Use its Title to get back the running value!
    
        string label = checkTg.DLGGetTitle()                
        Result( "\n label of checkbox:" + label )
        number i = val( right( label, len( label ) - 1 ) )   
        Result( "\n running index:" + i )
      }
    
      TagGroup CreateCheckboxes( object self )
      {
        TagGroup checkboxGroup = DLGCreateGroup()
        for ( number i = 0 ; I < 5 ; i++ )
        {
         checkboxGroup.DLGAddElement( DLGCreateCheckBox( "C" + I , 0 , "generalFunction" ) )
        }
        return checkboxGroup 
      }
    
      TagGroup CreateDLGTags( object self )
      {
        TagGroup dlg, dlgitems
        dlg = DLGCreateDialog( "Test" , dlgitems )
        dlgitems.DLGAddElement( self.CreateCheckboxes() )
        return dlg
      }
    
      object Init( object self )
      {
        return self.super.init( self.CreateDLGTags() )
      }
     }
    
    // MAIN SCRIPT calling the dialog
    {
      Object dlg = Alloc(myUI).Init()
      dlg.pose()
    }
    

    You can also 'attach' information directly to the checkbox. Checkboxes are - as all dialog items - really just specific TagGroup objects to which you can add whatever you like. In the example below, I'm adding an additional tag with a random number:

    class myUI : UIframe
    {
      void generalFunction( object self , tagGroup checkTg )
      {
        // checkTg is the taggroup of the checkbox which fired the method.
        // Use its Title to get back the running value!
    
        string label = checkTg.DLGGetTitle()                
        Result( "\n label of checkbox:" + label )
        number rnd
        if ( checkTG.TagGroupGetTagAsNumber( "Random NR", rnd ) )
        {
          Result( "\n Random number:" + rnd )
        }
      }
    
      TagGroup CreateCheckboxes( object self )
      {
        TagGroup checkboxGroup = DLGCreateGroup()
        for ( number i = 0; I < 5 ; i++ )
        {
         TagGroup checkbox = DLGCreateCheckBox( "C" + I , 0 , "generalFunction" )
         checkbox.TagGroupSetTagAsNumber( "Random NR", Random() )
         checkboxGroup.DLGAddElement( checkbox )
        }
        return checkboxGroup 
      }
    
      TagGroup CreateDLGTags( object self )
      {
        TagGroup dlg, dlgitems
        dlg = DLGCreateDialog( "Test" , dlgitems )
        dlgitems.DLGAddElement( self.CreateCheckboxes() )
        return dlg
      }
    
      object Init( object self )
      {
        return self.super.init( self.CreateDLGTags() )
      }
     }
    
    // MAIN SCRIPT calling the dialog
    {
      Object dlg=Alloc(myUI).Init()
      dlg.pose()
    }
    

    enter image description here