ArcObjects Library Reference  

PolygonTool

About the Creating a toolbar of globe tools Sample

[C#]

PolygonTool.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;

namespace GlobeGraphicsToolbar
{
    public class PolygonTool : ESRI.ArcGIS.Desktop.AddIns.Tool
    {
        private PolygonGeometry _polygonGeometry = null;
        private const int LeftButton = 1;
        private const esriSRGeoCSType GeographicCoordinateSystem = esriSRGeoCSType.esriSRGeoCS_WGS1984;
        private const double PointElementSize = 1;
        private const esriSimpleMarkerStyle PointElementStyle = esriSimpleMarkerStyle.esriSMSCircle;
        private const double PolylineElementWidth = 1000;
        private const esriSimpleLineStyle PolylineElementStyle = esriSimpleLineStyle.esriSLSSolid;
        private const string GraphicsLayerName = "Globe Graphics";
        public PolygonTool()
        {
        }

        protected override void OnUpdate()
        {

        }

        protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            if (arg.Button == MouseButtons.Left)
            {
                GeographicCoordinates geographicCoordinates = new GeographicCoordinates(ArcGlobe.Globe, arg.X, arg.Y);

                SpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceFactory((int)GeographicCoordinateSystem);

                PointGeometry pointGeometry = new PointGeometry(geographicCoordinates.Longitude, geographicCoordinates.Latitude, geographicCoordinates.AltitudeInKilometers, spatialReferenceFactory.SpatialReference);

                if (_polygonGeometry == null)
                {
                    _polygonGeometry = new PolygonGeometry(spatialReferenceFactory.SpatialReference);
                }

                _polygonGeometry.AddPoint(pointGeometry.Geometry as IPoint);

                TableOfContents tableOfContents = new TableOfContents(ArcGlobe.Globe);

                if (!tableOfContents.LayerExists(GraphicsLayerName))
                {
                    tableOfContents.ConstructLayer(GraphicsLayerName);
                }

                Layer layer = new Layer(tableOfContents[GraphicsLayerName]);

                if (_polygonGeometry.PointCount == 1)
                {
                    PointElement pointElement = new PointElement(pointGeometry.Geometry, PointElementSize, PointElementStyle);

                    layer.AddElement(pointElement.Element, pointElement.ElementProperties);
                }
                else
                {
                    layer.RemoveElement(layer.ElementCount - 1);

                    PolylineGeometry polylineGeometry = new PolylineGeometry(_polygonGeometry.Geometry);

                    PolylineElement polylineElement = new PolylineElement(polylineGeometry.Geometry, PolylineElementWidth, PolylineElementStyle);

                    layer.AddElement(polylineElement.Element, polylineElement.ElementProperties);
                }

                ArcGlobe.Globe.GlobeDisplay.RefreshViewers();
            }
        }

        protected override void OnDoubleClick()
        {
            if (_polygonGeometry.PointCount > 2)
            {
                TableOfContents tableOfContents = new TableOfContents(ArcGlobe.Globe);

                if (tableOfContents.LayerExists("Globe Graphics"))
                {
                    Layer layer = new Layer(tableOfContents["Globe Graphics"]);

                    layer.RemoveElement(layer.ElementCount - 1);

                    _polygonGeometry.Close();

                    PolygonElement polygonElement = new PolygonElement(_polygonGeometry.Geometry, esriSimpleFillStyle.esriSFSSolid);

                    layer.AddElement(polygonElement.Element, polygonElement.ElementProperties);

                    _polygonGeometry = null;

                    ArcGlobe.Globe.GlobeDisplay.RefreshViewers();
                }
            }
        }
    }

}

[Visual Basic .NET]

PolygonTool.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Display

Namespace GlobeGraphicsToolbar
	Public Class PolygonTool
		Inherits ESRI.ArcGIS.Desktop.AddIns.Tool
		Private _polygonGeometry As PolygonGeometry = Nothing
		Private Const LeftButton As Integer = 1
        Private Const GeographicCoordinateSystem As ESRI.ArcGIS.Geometry.esriSRGeoCSType = ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_WGS1984
		Private Const PointElementSize As Double = 1
        Private Const PointElementStyle As ESRI.ArcGIS.Display.esriSimpleMarkerStyle = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle
		Private Const PolylineElementWidth As Double = 1000
        Private Const PolylineElementStyle As ESRI.ArcGIS.Display.esriSimpleLineStyle = ESRI.ArcGIS.Display.esriSimpleLineStyle.esriSLSSolid
		Private Const GraphicsLayerName As String = "Globe Graphics"
		Public Sub New()
		End Sub

		Protected Overrides Sub OnUpdate()

		End Sub

		Protected Overrides Sub OnMouseDown(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
			If arg.Button = MouseButtons.Left Then
				Dim geographicCoordinates As New GeographicCoordinates(ArcGlobe.Globe, arg.X, arg.Y)

				Dim spatialReferenceFactory As New SpatialReferenceFactory(CInt(Fix(GeographicCoordinateSystem)))

				Dim pointGeometry As New PointGeometry(geographicCoordinates.Longitude, geographicCoordinates.Latitude, geographicCoordinates.AltitudeInKilometers, spatialReferenceFactory.SpatialReference)

				If _polygonGeometry Is Nothing Then
					_polygonGeometry = New PolygonGeometry(spatialReferenceFactory.SpatialReference)
				End If

				_polygonGeometry.AddPoint(TryCast(pointGeometry.Geometry, IPoint))

				Dim tableOfContents As New TableOfContents(ArcGlobe.Globe)

				If (Not tableOfContents.LayerExists(GraphicsLayerName)) Then
					tableOfContents.ConstructLayer(GraphicsLayerName)
				End If

				Dim layer As New Layer(tableOfContents(GraphicsLayerName))

				If _polygonGeometry.PointCount = 1 Then
					Dim pointElement As New PointElement(pointGeometry.Geometry, PointElementSize, PointElementStyle)

					layer.AddElement(pointElement.Element, pointElement.ElementProperties)
				Else
					layer.RemoveElement(layer.ElementCount - 1)

					Dim polylineGeometry As New PolylineGeometry(_polygonGeometry.Geometry)

					Dim polylineElement As New PolylineElement(polylineGeometry.Geometry, PolylineElementWidth, PolylineElementStyle)

					layer.AddElement(polylineElement.Element, polylineElement.ElementProperties)
				End If

				ArcGlobe.Globe.GlobeDisplay.RefreshViewers()
			End If
		End Sub

		Protected Overrides Sub OnDoubleClick()
			If _polygonGeometry.PointCount > 2 Then
				Dim tableOfContents As New TableOfContents(ArcGlobe.Globe)

				If tableOfContents.LayerExists("Globe Graphics") Then
					Dim layer As New Layer(tableOfContents("Globe Graphics"))

					layer.RemoveElement(layer.ElementCount - 1)

					_polygonGeometry.Close()

                    Dim polygonElement As New PolygonElement(_polygonGeometry.Geometry, ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSSolid)

					layer.AddElement(polygonElement.Element, polygonElement.ElementProperties)

					_polygonGeometry = Nothing

					ArcGlobe.Globe.GlobeDisplay.RefreshViewers()
				End If
			End If
		End Sub
	End Class

End Namespace