Interacting with ODTK Objects

Assuming that you have toolbar buttons and menu items, you may want one of those items to manipulate objects in your scenario using the ODTK scripting interface.

For more information on working with objects, see Interacting with ODTK Elements.

Declare and instantiate the root

  1. Add the following line under your other class variables:
    [C#]
    private dynamic m_root;
    
    [Visual Basic .NET]
    Dim m_root 
  2. To make the private root available outside the MySampleUIPlugin class, add the following property to your UI Plugin. This isn't needed yet, but will be in a later section.
    [C#]
    public dynamic ODTKRoot
    {
        get { return m_root; }
    }
    [Visual Basic .NET]
    Public Property ODKRoot() 
        Get
            Return m_root
        End Get
        Set(ByVal value)
            m_root = value
        End Set
    End Property
    
    Instantiate the root early in the execution of the application. Add code to the "OnStartup" method to access the application object, and pull the ODTK root from it:
    [C#]
    public void OnStartup (IAgUiPluginSite PluginSite)
    {
        m_psite = PluginSite;
        IAgUiApplication AgUiApp = m_psite.Application;
        m_root = AgUiApp.Personality ;
    }
    [Visual Basic .NET]
    Public Sub OnStartup(PluginSite As AGI.Ui.Plugins.IAgUiPluginSite) Implements AGI.Ui.Plugins.IAgUiPlugin.OnStartup
        m_psite = PluginSite
        m_root = m_pSite.Application.Personality
    End Sub
    
  3. Use the root to interact with ODTK. For this example, change the Exec function so that the toolbar button, instead of displaying a message box, creates a new scenario:
    [C#]
    public void Exec(string CommandName, IAgProgressTrackCancelM TrackCancel, IAgUiPluginCommandParameters Parameters)
    {
        if (string.Compare(CommandName, "MyCompany.MySampleUIPlugin.MyFirstCommand", true) == 0)
        {
           dynamic scen = m_root.CreateObj(m_root, "Scenario", "ExampleScenario");
        }
        else if (string.Compare(CommandName, "MyCompany.MySampleUIPlugin.MySecondCommand", true) == 0)
        {
            MessageBox.Show(CommandName);
        }
    }
    [Visual Basic .NET]
    Public Sub Exec(CommandName As String, TrackCancel As IAgProgressTrackCancel, Parameters As IAgUiPluginCommandParameters) Implements IAgUiPluginCommandTarget.Exec
        If (String.Compare(CommandName, "MyCompany.MySampleUIPlugin.MyFirstCommand", True) = 0) Then
            Dim scen = m_root.CreateObj(m_root, "Scenario", "ExampleScenario")
        ElseIf (String.Compare(CommandName, "MyCompany.MySampleUIPlugin.MySecondCommand", True) = 0) Then
            MessageBox.Show(CommandName)
        End If
    End Sub
  4. Build your project and then start up ODTK. Click the toolbar button and confirm that it creates a new scenario.