TrackObject.vb
Dynamic display — tracking dynamic object
TrackObject.vb
' Copyright 2006 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Xml
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Timers
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Controls
Imports System.Collections.Generic
Imports Microsoft.Win32
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.DataSourcesFile
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.SystemUI

  ''' <summary>
  ''' A helper method used to delegate calls to the main thread
  ''' </summary>
  Public NotInheritable Class InvokeHelper : Inherits Control
  'delegate used to pass the invoked method to the main thread
  Public Delegate Sub MessageHandler(ByVal navigationData As NavigationData)

  'class members
  Private m_activeView As IActiveView
  Private m_point As IPoint = Nothing

  ''' <summary>
  ''' class constructor
  ''' </summary>
  ''' <param name="activeView"></param>
  Public Sub New(ByVal activeView As IActiveView)
    'make sure tha the control got created and that it has a valid handle
    Me.CreateHandle()
    Me.CreateControl()

    'get the active view
    m_activeView = activeView
  End Sub

  ''' <summary>
  ''' delegate the required method onto the main thread
  ''' </summary>
  ''' <param name="navigationData"></param>
  Public Sub InvokeMethod(ByVal navigationData As NavigationData)
    Try
    ' Invoke the HandleMessage through its delegate
    If (Not Me.IsDisposed) AndAlso Me.IsHandleCreated Then
      Invoke(New MessageHandler(AddressOf CenterMap), New Object() { navigationData })
    End If
    Catch ex As Exception
    System.Diagnostics.Trace.WriteLine(ex.Message)
    End Try
  End Sub

  ''' <summary>
  ''' the method that gets executed by the delegate
  ''' </summary>
  ''' <param name="navigationData"></param>
  Public Sub CenterMap(ByVal navigationData As NavigationData)
    Try
    'get the current map visible extent
    Dim envelope As IEnvelope = m_activeView.ScreenDisplay.DisplayTransformation.VisibleBounds
    If Nothing Is m_point Then
      m_point = New PointClass()
    End If
    'set the new map center coordinate
    m_point.PutCoords(navigationData.X, navigationData.Y)
    'center the map around the new coordinate
    envelope.CenterAt(m_point)
    m_activeView.ScreenDisplay.DisplayTransformation.VisibleBounds = envelope
    'rotate the map to the new rotation angle
    m_activeView.ScreenDisplay.DisplayTransformation.Rotation = navigationData.Azimuth
    Catch ex As Exception
    System.Diagnostics.Trace.WriteLine(ex.Message)
    End Try
  End Sub

  ''' <summary>
  ''' control's initialization
  ''' </summary>
  Private Sub InitializeComponent()
  End Sub
  End Class

  ''' <summary>
  ''' A user defined data structure used to pass information to the invoke method
  ''' </summary>
  Public Structure NavigationData
  Public X As Double
  Public Y As Double
  Public Azimuth As Double

  ''' <summary>
  ''' struct constructor
  ''' </summary>
  ''' <param name="x">map x coordinate</param>
  ''' <param name="y">map x coordinate</param>
  ''' <param name="azimuth">the new map azimuth</param>
    Public Sub New(ByVal newX As Double, ByVal newY As Double, ByVal newAzimuth As Double)
      X = newX
      Y = newY
      Azimuth = newAzimuth
    End Sub
  End Structure

  ''' <summary>
  ''' This command triggers the tracking functionality using Dynamic Display
  ''' </summary>
  <Guid("803D4188-AB2F-49f9-9340-42C809887063"), ComVisible(True), ClassInterface(ClassInterfaceType.None), ProgId("DynamicObjectTracking.TrackObject")> _
  Public NotInheritable Class TrackObject : Inherits BaseCommand
  #Region "COM Registration Function(s)"
  <ComRegisterFunction(), ComVisible(False)> _
  Private Shared Sub RegisterFunction(ByVal registerType As Type)
    ' Required for ArcGIS Component Category Registrar support
    ArcGISCategoryRegistration(registerType)

    '
    ' TODO: Add any COM registration code here
    '
  End Sub

  <ComUnregisterFunction(), ComVisible(False)> _
  Private Shared Sub UnregisterFunction(ByVal registerType As Type)
    ' Required for ArcGIS Component Category Registrar support
    ArcGISCategoryUnregistration(registerType)

    '
    ' TODO: Add any COM unregistration code here
    '
  End Sub

  #Region "ArcGIS Component Category Registrar generated code"
  ''' <summary>
  ''' Required method for ArcGIS Component Category registration -
  ''' Do not modify the contents of this method with the code editor.
  ''' </summary>
  Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
    Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
    ControlsCommands.Register(regKey)
    MxCommands.Register(regKey)
  End Sub
  ''' <summary>
  ''' Required method for ArcGIS Component Category unregistration -
  ''' Do not modify the contents of this method with the code editor.
  ''' </summary>
  Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
    Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
    ControlsCommands.Unregister(regKey)
    MxCommands.Unregister(regKey)
  End Sub

  #End Region
  #End Region

  'class members
  Private m_hookHelper As IHookHelper = Nothing
    Private m_navigationDataFileName As String = String.Empty
    Private m_point As IPoint = Nothing
    Private m_points As List(Of WKSPoint) = Nothing
    Private m_timer As System.Timers.Timer = Nothing
    Private m_invokeHelper As InvokeHelper = Nothing
    Private m_objectLocation As ObjectLocationClass = Nothing
    Private Shared m_pointIndex As Integer = 0
    Private m_bIsRunning As Boolean = False
    Private m_bOnce As Boolean = True

    ''' <summary>
    ''' class constructor
    ''' </summary>
    Public Sub New()
        MyBase.m_category = ".NET Samples"
        MyBase.m_caption = "Track Dynamic Object"
        MyBase.m_message = "Tracking a dynamic object"
        MyBase.m_toolTip = "Tracking a dynamic object"
        MyBase.m_name = "DotNetSamples.TrackDynamicObject"

        Try
            MyBase.m_bitmap = New Bitmap(Me.GetType(), "TrackObject.bmp")
        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
        End Try
    End Sub

#Region "Overriden Class Methods"

    ''' <summary>
    ''' Occurs when this command is created
    ''' </summary>
    ''' <param name="hook">Instance of the application</param>
    Public Overrides Sub OnCreate(ByVal hook As Object)
        If hook Is Nothing Then
            Return
        End If

        If m_hookHelper Is Nothing Then
            m_hookHelper = New HookHelperClass()
        End If

        m_hookHelper.Hook = hook

        'get the ArcGIS path from the registry
        Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\ESRI\ArcGIS")
        Dim path As String = Convert.ToString(key.GetValue("InstallDir"))

        'set the path to the featureclass used by the GPS simulator
        m_navigationDataFileName = System.IO.Path.Combine(path, "DeveloperKit\SamplesNET\data\USAMajorHighways\NavigationData.xml")

        m_point = New PointClass()
        m_points = New List(Of WKSPoint)()

        'instantiate the timer
        m_timer = New System.Timers.Timer(60)
        m_timer.Enabled = False
        'set the timer's elapsed event handler
        AddHandler m_timer.Elapsed, AddressOf OnTimerElapsed
    End Sub

    ''' <summary>
    ''' Occurs when this command is clicked
    ''' </summary>
    Public Overrides Sub OnClick()
        'create the Invoke helper class
        If Nothing Is m_invokeHelper Then
            m_invokeHelper = New InvokeHelper(m_hookHelper.ActiveView)
        End If

        'make sure to switch into dynamic mode
        Dim dynamicMap As IDynamicMap = CType(m_hookHelper.FocusMap, IDynamicMap)
        If (Not dynamicMap.DynamicMapEnabled) Then
            dynamicMap.DynamicMapEnabled = True
        End If

        'do some initializations...
        If m_bOnce Then
            'generate the navigation data
            GenerateNavigationData()

            'initialize the dynamic layer which will be used to draw the tracked object
            m_objectLocation = New ObjectLocationClass()

            'add the dynamic layer to the map
            m_hookHelper.FocusMap.AddLayer(m_objectLocation)
            m_bOnce = False
        End If

        'Start the tracking timer
        If (Not m_bIsRunning) Then
            m_timer.Enabled = True
        Else
            m_timer.Enabled = False
        End If

        'set the running flag
        m_bIsRunning = Not m_bIsRunning
    End Sub

    ''' <summary>
    ''' set the state of the button of the command
    ''' </summary>
    Public Overrides ReadOnly Property Checked() As Boolean
        Get
            Return m_bIsRunning
        End Get
    End Property

#End Region

  ''' <summary>
  ''' Timer elapsed event handler
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  Private Sub OnTimerElapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)
    Try
    'make sure that the current tracking point index does not exceed the list index
    If m_pointIndex = (m_points.Count - 1) Then
      m_timer.Enabled = False
      Return
    End If

    'get the current and the next track location
    Dim currentPoint As WKSPoint = m_points(m_pointIndex)
    Dim nextPoint As WKSPoint = m_points(m_pointIndex + 1)

    'calculate the azimuth to the next location
    Dim azimuth As Double = (180.0 / Math.PI) * Math.Atan2(nextPoint.X - currentPoint.X, nextPoint.Y - currentPoint.Y)

    'create the navigation data structure
    Dim navigationData As NavigationData = New NavigationData(currentPoint.X, currentPoint.Y, azimuth)

    'update the map extent and rotation
    m_invokeHelper.InvokeMethod(navigationData)

    'add the navigation data to the loction layer in order to draw it
    m_objectLocation.AddNavigationData(navigationData)

    'increment the tracking point index
    m_pointIndex += 1
    Catch ex As Exception
    System.Diagnostics.Trace.WriteLine(ex.Message)
    End Try
  End Sub

  
  ''' <summary>
  ''' generate an in-memory navigation info based on an existing featureclass
  ''' </summary>
  Private Sub GenerateNavigationData()
    Try
            If Not System.IO.File.Exists(m_navigationDataFileName) Then
                Throw New Exception("File " + m_navigationDataFileName + " cannot be found!")
            End If

            Dim reader As XmlTextReader = New XmlTextReader(m_navigationDataFileName)
            Dim doc As XmlDocument = New XmlDocument()
            doc.Load(reader)
            reader.Close()

            Dim X As Double
            Dim Y As Double
            'get the navigation items
            Dim nodes As XmlNodeList = doc.DocumentElement.SelectNodes("./navigationItem")
            For Each node As XmlNode In nodes
                X = Convert.ToDouble(node.Attributes(0).Value)
                Y = Convert.ToDouble(node.Attributes(1).Value)

                Dim p As WKSPoint = New WKSPoint()
                p.X = X
                p.Y = Y
                m_points.Add(p)
            Next

        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.Message)
        End Try
  End Sub
  End Class