Track Shapes
DockWindow.cs
// 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.
// 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGISExplorer.Application;
using ESRI.ArcGISExplorer.Geometry;
using ESRI.ArcGISExplorer.Mapping;

namespace TrackShapesCS
{
    public partial class DockWindow : ESRI.ArcGISExplorer.Application.DockWindow
    {

        MapDisplay _md = null;
        GraphicCollection _graphics = null;

        public DockWindow()
        {
            InitializeComponent();

            _md = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay;

            _graphics = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Graphics;
        }

        private void DockWindow_Load(object sender, EventArgs e)
        {

            //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"];


        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            //Track Point
            ESRI.ArcGISExplorer.Geometry.Point trackedPoint = _md.TrackPoint();
            //Turn Point To Graphic
            Graphic TemporaryPointGraphic = GeometryToGraphic(trackedPoint);
            //Add Graphic to TreeView
            string msg = trackedPoint.GeometryType.ToString() + " with center at " + trackedPoint.X + ", " + trackedPoint.Y;
            GraphicToTreeView(TemporaryPointGraphic, msg);
        }

        private void btnLine_Click(object sender, EventArgs e)
        {
            Polyline trackedLine = _md.TrackPolyline();
            Graphic TemporaryPolylineGraphic = GeometryToGraphic(trackedLine);
            string len = GetLength(trackedLine);
            string msg = trackedLine.GeometryType.ToString() + " of length " + len;
            GraphicToTreeView(TemporaryPolylineGraphic, msg);
        }

        private string GetLength(Polyline pLine)
        {
            bool angular = pLine.CoordinateSystem.Unit.UnitType == UnitType.Angular;
            double len = angular ? GeodesicUtilities.Length(pLine, Unit.Linear.Meters) : pLine.Length;
            Unit lengthUnit = angular ? Unit.Linear.Meters : pLine.CoordinateSystem.Unit;
            return len + " " + lengthUnit.GetPluralName(UnitNameOptions.Simple);
        }

        private void btnPolygon_Click(object sender, EventArgs e)
        {
            Polygon trackedPolygon = _md.TrackPolygon();
            Graphic TemporaryPolygonGraphic = GeometryToGraphic(trackedPolygon);
            string area = GetArea(trackedPolygon);
            string msg = trackedPolygon.GeometryType.ToString() + " with an area of " + area;
            GraphicToTreeView(TemporaryPolygonGraphic, msg);
        }

        private string GetArea(Polygon pGon)
        {
            bool angular = pGon.CoordinateSystem.Unit.UnitType == UnitType.Angular;
            double area = angular ? GeodesicUtilities.Area(pGon, Unit.Area.SquareMeters) : pGon.Area;
            string areaUnitName = angular ? "square meters" : "square " + pGon.CoordinateSystem.Unit.GetPluralName(UnitNameOptions.Simple);
            return area + " " + areaUnitName;
        }

        private void btnEnvelope_Click(object sender, EventArgs e)
        {
          Envelope trackedEnvelope = _md.TrackEnvelope();
          Polygon envelopePolygon = trackedEnvelope.GetPolygon();
          Graphic temporaryPolygonGraphic = GeometryToGraphic(envelopePolygon);
          string area = GetArea(envelopePolygon);
          string msg = envelopePolygon.GeometryType.ToString() + " with an area of " + area;
          GraphicToTreeView(temporaryPolygonGraphic, msg);
        }

        private void btnCircle_Click(object sender, EventArgs e)
        {
          Polygon trackedCircle = _md.TrackCircle();
          Graphic temporaryPolygonGraphic = GeometryToGraphic(trackedCircle);
          string area = GetArea(trackedCircle);
          string msg = trackedCircle.GeometryType.ToString() + " with an approximate area of " + area;
          GraphicToTreeView(temporaryPolygonGraphic, msg);
        }

        private void btnArrow_Click(object sender, EventArgs e)
        {
          Polygon trackedArrow = _md.TrackArrow();
          Graphic temporaryPolygonGraphic = GeometryToGraphic(trackedArrow);
          string area = GetArea(trackedArrow);
          string msg = trackedArrow.GeometryType.ToString() + " with an area of " + area;
          GraphicToTreeView(temporaryPolygonGraphic, msg);
        }


        private Graphic GeometryToGraphic(Geometry geom)
        {

            Graphic trackedGraphic = new Graphic(geom);

            switch (Symbol.GeometryTypeToSymbolType(geom.GeometryType))
            {
                case SymbolType.Fill:
                    trackedGraphic.Symbol = Symbol.Fill.Solid.Red;
                    break;
                case SymbolType.Line:
                    trackedGraphic.Symbol = Symbol.Line.Solid.Red;
                    break;
                case SymbolType.Marker:
                    trackedGraphic.Symbol = Symbol.Marker.Pushpin.Red;
                    break;
                case SymbolType.Unknown:
                    break;
            }

            return trackedGraphic;

        }

        private void GraphicToTreeView(Graphic trackedGraphic, string title)
        {

            tvTrackedGeometries.BeginUpdate();
            TreeNode tn = new TreeNode(title) { Tag = trackedGraphic };


            tvTrackedGeometries.Nodes.Insert(tvTrackedGeometries.Nodes.Count, tn);
            tvTrackedGeometries.Nodes[tvTrackedGeometries.Nodes.Count - 1].Checked = true;
            tvTrackedGeometries.EndUpdate();
            tvTrackedGeometries.Refresh();
        }

        //TreeView Events//

        private void tvTrackedGeometries_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node == null)
                return;

          e.Node.Checked = true; 

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

            Graphic zoomGraphic = e.Node.Tag as Graphic;
            _md.ZoomTo(zoomGraphic.Geometry);
        }

        private void tvTrackedGeometries_AfterCheck(object sender, TreeViewEventArgs e)
        {
            Graphic g = e.Node.Tag as Graphic;
            if (g == null) return;

            // if the node is checked, show the graphic on the map, otherwise, hide it.
            if (_graphics.Contains(g))
            {
                g.Visible = (e.Node.Checked);
            }
            else
            {
                // If this is the first time that the graphic is shown, then add it to the Map.
                // It will be visible by default.
                _graphics.Add(g);
            }
        }

        private void cmtv_Opening(object sender, CancelEventArgs e)
        {
            // enable/disable the context menu items
            deleteMenuItem.Enabled = tvTrackedGeometries.SelectedNode.IsSelected;
            moveToMapMenuItem.Enabled = tvTrackedGeometries.SelectedNode.IsSelected;
        }

        private void deleteMenuItem_Click(object sender, EventArgs e)
        {

            tvTrackedGeometries.SelectedNode.Checked = false;  // force removal of graphic thru the generated event
            tvTrackedGeometries.SelectedNode.Remove();

        }

        private void moveToMapMenuItem_Click(object sender, EventArgs e)
        {
            Graphic tempGraphic = tvTrackedGeometries.SelectedNode.Tag as Graphic;
            string noteInfo = tvTrackedGeometries.SelectedNode.Text;

            tvTrackedGeometries.SelectedNode.Checked = false;  // force removal of graphic thru the generated event
           

            Note newNote = new Note(noteInfo, tempGraphic.Geometry, tempGraphic.Symbol);
            _md.Map.ChildItems.Add(newNote);
 tvTrackedGeometries.SelectedNode.Remove();


        }

        private void tvTrackedGeometries_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                e.Node.ContextMenuStrip = cmtv;
                e.Node.ContextMenuStrip.Show(tvTrackedGeometries, e.Location);
            }
        }

        

    }
}