Talk to ODTK

Now that you have toolbar buttons and menu items, you may want one of those items to manipulate objects in your scenario, using either Connect or the Object Model. To do that, you need to create an instance of the ODTKRoot.

For more information about using Connect or the Object Model, see Using Core Libraries.

Add ODTK Object Model References

  1. Right-click References in the Solution Explorer and select Add Reference...
  2. On the COM tab, select AGI STK Objects 11 and click OK.
  3. Add the corresponding using directive to the MySampleUIPlugin class:
    [C#]
    using AGI.STKObjects;
    
    [Visual Basic .NET]
    Imports AGI.STKObjects
    

Declare and Instantiate the Root

  1. Add the following line under your other class variables:
    [C#]
    private ODTKRoot m_root;
    
    [Visual Basic .NET]
    Dim m_root As ODTKRoot
    
  2. To make the private Object Model 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 ODTKRoot ODTKRoot
    {
        get { return m_root; }
    }
    
    [Visual Basic .NET]
    Public Property STKRoot() As ODTKRoot
        Get
            Return m_root
        End Get
        Set(ByVal value As ODTKRoot)
            m_root = value
        End Set
    End Property
    
  3. Instantiate the root early in the execution of the application. Add code to the "OnStartup" method to access the application object, and pull the Object Model root class from it:
    [C#]
    public void OnStartup (IAgUiPluginSite PluginSite)
    {
        m_psite = PluginSite;
        IAgUiApplication AgUiApp = m_psite.Application;
        m_root = AgUiApp.Personality2 as ODTKRoot;
    }
    
    [Visual Basic .NET]
    Public Sub OnStartup(PluginSite As AGI.Ui.Plugins.IAgUiPluginSite) Implements AGI.Ui.Plugins.IAgUiPlugin.OnStartup
        m_psite = PluginSite
        m_root = DirectCast(m_pSite.Application.Personality2, ODTKRoot)
    End Sub
    
  4. 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)
        {
            if (m_root.CurrentScenario == null)
            {
                m_root.NewScenario("MyNewScenario");
            }
        }
        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
            If (m_root.CurrentScenario Is Nothing) Then
                m_root.NewScenario("MyNewScenario")
            End If
        ElseIf (String.Compare(CommandName, "MyCompany.MySampleUIPlugin.MySecondCommand", True) = 0) Then
            MessageBox.Show(CommandName)
        End If
    End Sub
    
  5. Build your project, then start up ODTK. Click the toolbar button, and confirm that it creates a new scenario.



<<Previous

Add a Toolbar Button or Menu Item

Next>>

Add a Custom User Control

ODTK 6.5