Track Shapes
DockWindow.vb
' Copyright 2011 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.
' 

' Copyright 2010 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 at <your ArcGIS Explorer install location>/DeveloperKit/userestrictions.txt.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Imports ESRI.ArcGISExplorer.Application
Imports ESRI.ArcGISExplorer.Geometry
Imports ESRI.ArcGISExplorer.Mapping

Partial Public Class DockWindow
  Inherits ESRI.ArcGISExplorer.Application.DockWindow

    Private _md As MapDisplay = Nothing
    Private _graphics As GraphicCollection = Nothing

  Public Sub New()
    InitializeComponent()

    _md = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay
  End Sub

  Private Sub DockWindow_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

    'Populate Buttons with Images
    btnPoint.Image = imageList1.Images("GetPoint.bmp")
    btnLine.Image = imageList1.Images("GetLine.bmp")
    btnPolygon.Image = imageList1.Images("GetPolygon.bmp")
    btnArrow.Image = imageList1.Images("GetArrow.bmp")
    btnCircle.Image = imageList1.Images("GetCircle.bmp")
    btnEnvelope.Image = imageList1.Images("GetEnvelope.bmp")

  End Sub

  Private Sub btnPoint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPoint.Click
    'Track Point
    Dim trackedPoint As ESRI.ArcGISExplorer.Geometry.Point = _md.TrackPoint()
    'Turn Point To Graphic
    Dim TemporaryPointGraphic As Graphic = GeometryToGraphic(trackedPoint)
    'Add Graphic to TreeView
    Dim msg As String = trackedPoint.GeometryType.ToString() & " with center at " & trackedPoint.X & ", " & trackedPoint.Y
    GraphicToTreeView(TemporaryPointGraphic, msg)
  End Sub


  Private Sub btnLine_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLine.Click
    Dim trackedLine As Polyline = _md.TrackPolyline()
    Dim TemporaryPolylineGraphic As Graphic = GeometryToGraphic(trackedLine)
    Dim len As String = GetLength(trackedLine)
    Dim msg As String = trackedLine.GeometryType.ToString() + " of length " + len
    GraphicToTreeView(TemporaryPolylineGraphic, msg)
  End Sub

  Private Function GetLength(ByRef pLine As Polyline) As String
    Dim len As Double
    Dim lengthUnit As Unit
    If (pLine.CoordinateSystem.Unit.UnitType = UnitType.Angular) Then
      len = GeodesicUtilities.Length(pLine, Unit.Linear.Meters)
      lengthUnit = Unit.Linear.Meters
    Else
      len = pLine.Length
      lengthUnit = pLine.CoordinateSystem.Unit
    End If
    Return len.ToString() + " " + lengthUnit.GetPluralName(UnitNameOptions.Simple)
  End Function

  Private Sub btnPolygon_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPolygon.Click
    Dim trackedPolygon As Polygon = _md.TrackPolygon()
    Dim TemporaryPolygonGraphic As Graphic = GeometryToGraphic(trackedPolygon)
    Dim area As String = GetArea(trackedPolygon)
    Dim msg As String = trackedPolygon.GeometryType.ToString() + " with an area of " + area
    GraphicToTreeView(TemporaryPolygonGraphic, msg)
  End Sub

  Private Function GetArea(ByRef pGon As Polygon) As String
    Dim area As Double
    Dim areaUnitName As String
    If (pGon.CoordinateSystem.Unit.UnitType = UnitType.Angular) Then
      area = GeodesicUtilities.Area(pGon, Unit.Area.SquareMeters)
      areaUnitName = "square meters"
    Else
      area = pGon.Area
      areaUnitName = "square " + pGon.CoordinateSystem.Unit.GetPluralName(UnitNameOptions.Simple)
    End If
    Return area.ToString() + " " + areaUnitName
  End Function

  Private Sub btnEnvelope_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnvelope.Click
    Dim trackedEnvelope As Envelope = _md.TrackEnvelope()
    Dim envelopePolygon As Polygon = trackedEnvelope.GetPolygon()
    Dim temporaryPolygonGraphic As Graphic = GeometryToGraphic(envelopePolygon)
    Dim area As String = GetArea(envelopePolygon)
    Dim msg As String = envelopePolygon.GeometryType.ToString() + " with an area of " + area
    GraphicToTreeView(temporaryPolygonGraphic, msg)
  End Sub

  Private Sub btnCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCircle.Click
    Dim trackedCircle As Polygon = _md.TrackCircle()
    Dim temporaryPolygonGraphic As Graphic = GeometryToGraphic(trackedCircle)
    Dim area As String = GetArea(trackedCircle)
    Dim msg As String = trackedCircle.GeometryType.ToString() + " with an approximate area of " + area
    GraphicToTreeView(temporaryPolygonGraphic, msg)
  End Sub

  Private Sub btnArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArrow.Click
    Dim trackedArrow As Polygon = _md.TrackArrow()
    Dim temporaryPolygonGraphic As Graphic = GeometryToGraphic(trackedArrow)
    Dim area As String = GetArea(trackedArrow)
    Dim msg As String = trackedArrow.GeometryType.ToString() + " with an area of " + area
    GraphicToTreeView(temporaryPolygonGraphic, msg)
  End Sub


  Private Function GeometryToGraphic(ByVal geom As Geometry) As Graphic

    Dim trackedGraphic As Graphic = New Graphic(geom)

    Select Case Symbol.GeometryTypeToSymbolType(geom.GeometryType)
      Case SymbolType.Fill
        trackedGraphic.Symbol = Symbol.Fill.Solid.Red
      Case SymbolType.Line
        trackedGraphic.Symbol = Symbol.Line.Solid.Red
      Case SymbolType.Marker
        trackedGraphic.Symbol = Symbol.Marker.Pushpin.Red
      Case SymbolType.Unknown
    End Select

    Return trackedGraphic

  End Function

  Private Sub GraphicToTreeView(ByVal trackedGraphic As Graphic, ByVal title As String)

    tvTrackedGeometries.BeginUpdate()
    Dim tn As TreeNode = New TreeNode(title)
    tn.Tag = trackedGraphic

    tvTrackedGeometries.Nodes.Insert(tvTrackedGeometries.Nodes.Count, tn)
    tvTrackedGeometries.Nodes(tvTrackedGeometries.Nodes.Count - 1).Checked = True
    tvTrackedGeometries.EndUpdate()
    tvTrackedGeometries.Refresh()
  End Sub

  'TreeView Events//

  Private Sub tvTrackedGeometries_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles tvTrackedGeometries.NodeMouseDoubleClick
    If e.Node Is Nothing Then
      Return
    End If

    e.Node.Checked = True

    tvTrackedGeometries.Refresh() ' redraw the tree before the UI thread gets blocked

    Dim zoomGraphic As Graphic = TryCast(e.Node.Tag, Graphic)
    _md.ZoomTo(zoomGraphic.Geometry)
  End Sub

  Private Sub tvTrackedGeometries_AfterCheck(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles tvTrackedGeometries.AfterCheck
        Dim _graphics As GraphicCollection = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Graphics
        Dim g As Graphic = TryCast(e.Node.Tag, Graphic)
        If _graphics.Contains(g) Then
            g.Visible = e.Node.Checked
        Else
            _graphics.Add(g)
        End If
  End Sub

  Private Sub cmtv_Opening(ByVal sender As Object, ByVal e As CancelEventArgs) Handles cmtv.Opening
    ' enable/disable the context menu items
    deleteMenuItem.Enabled = tvTrackedGeometries.SelectedNode.IsSelected
    moveToMapMenuItem.Enabled = tvTrackedGeometries.SelectedNode.IsSelected
  End Sub

  Private Sub deleteMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteMenuItem.Click

    tvTrackedGeometries.SelectedNode.Checked = False ' force removal of graphic thru the generated event
    tvTrackedGeometries.SelectedNode.Remove()

  End Sub

  Private Sub moveToMapMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles moveToMapMenuItem.Click
    Dim tempGraphic As Graphic = TryCast(tvTrackedGeometries.SelectedNode.Tag, Graphic)
    Dim noteInfo As String = tvTrackedGeometries.SelectedNode.Text

        tvTrackedGeometries.SelectedNode.Checked = False


    Dim newNote As Note = New Note(noteInfo, tempGraphic.Geometry, tempGraphic.Symbol)
        _md.Map.ChildItems.Add(newNote)

        tvTrackedGeometries.SelectedNode.Remove()

  End Sub

  Private Sub tvTrackedGeometries_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles tvTrackedGeometries.NodeMouseClick
    If e.Button = MouseButtons.Right Then
      e.Node.ContextMenuStrip = cmtv
      e.Node.ContextMenuStrip.Show(tvTrackedGeometries, e.Location)
    End If
  End Sub

  
End Class