Talk to STK

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 STK Object Model. To do that, you need to create an instance of the AgStkObjectRoot.

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

Add STK Object Model References

  1. Right-click References in the Solution Explorer and select Add Reference...
  2. On the COM tab, select AGI STK Objects 10 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 AgStkObjectRoot m_root;
    
    [Visual Basic .NET]
    Dim m_root As AgStkObjectRoot
    
  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 AgStkObjectRoot STKRoot
    {
        get { return m_root; }
    }
    
    [Visual Basic .NET]
    Public Property STKRoot() As AgStkObjectRoot
        Get
            Return m_root
        End Get
        Set(ByVal value As AgStkObjectRoot)
            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 STK 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 AgStkObjectRoot;
    }
    
    [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, AgStkObjectRoot)
    End Sub
    
  4. Use the root to interact with STK. 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 STK. Click the toolbar button, and confirm that it creates a new scenario.

See also: Isolate the Root




<<Previous

Add a Toolbar Button or Menu Item

Next>>

Add a Custom User Control

STK 11.2.1 Programming Interface