ArcObjects Library Reference  

MainForm

About the Decluttering MOLE graphics using leadering and stacking Sample

[C#]

MainForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DefenseSolutions;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;

namespace MoleLeaderStack
{
    public partial class MainForm : Form
    {
        private Random m_Random = new Random();
        private IMoleGroupElement m_MoleGroup = null;
		private IElement m_element;
        private String[] m_sicArray = { "SFAPC----------", "SFAPCF---------", "SFAPFH---------", "SFAPCL---------", "SFAPM----------" };
		
        public MainForm()
        {
            InitializeComponent();
            LoadDefaultMapData();
        }

		// Adds multiple MOLE graphics using IGroupElement.
		private void btnAddGraphics_Click( object sender, EventArgs e )
		{
			m_MoleGroup = new MoleGroupElementClass();
			if (m_MoleGroup == null)
				return;
			IGroupElement ge = m_MoleGroup as IGroupElement;
			if (ge != null)
			{
				IElement el = null;
				for (int i = 0; i < m_sicArray.Length; ++i)
				{
					IPoint pPoint = CreateRandomPoint();
					string sic = m_sicArray[ i ];
					el = BuildElement( pPoint, sic );
					ge.AddElement( el );
				}
			}

			m_element = m_MoleGroup as IElement;

			if (m_element != null)
				Draw();
		}

		// Applies leadering on the graphics in the group element.
		private void btnLeader_Click( object sender, EventArgs e )
		{
			if (m_MoleGroup != null)
			{
				// base options
				m_MoleGroup.DeclutterOption = moleDeclutterOptionEnum.moleDeclutterLeader;
				m_MoleGroup.EnableDeclutter = true;

				// leadering option
				(m_MoleGroup as IMoleLeaderElement).LeaderQuadrant = moleQuadrantEnum.moleQuadrantUR;
				(m_MoleGroup as IMoleLeaderElement).Anchor = CreateRandomPoint();
				(m_MoleGroup as IMoleLeaderElement).Base = CreateRandomPoint();

				m_element = m_MoleGroup as IElement;

				if (m_element != null)
					Draw();
			}
		}

		// Applies stacking on the graphics in the group element.
        private void btnStack_Click(object sender, EventArgs e)
        {
			if (m_MoleGroup != null)
			{
				m_MoleGroup.DeclutterOption = moleDeclutterOptionEnum.moleDeclutterStack;
				m_MoleGroup.EnableDeclutter = true;

				// stacking option
				(m_MoleGroup as IMoleStackElement).StackQuadrant = moleQuadrantEnum.moleQuadrantLR;

				m_element = m_MoleGroup as IElement;
				if (m_element != null)
					Draw();
			}
        }

		/// <summary>
		/// Draws the IElement to the map.
		/// </summary>
		private void Draw()
		{
			this.axMapControl1.ActiveView.GraphicsContainer.AddElement( m_element, 0 );

			// update the view
			this.axMapControl1.ActiveView.PartialRefresh( esriViewDrawPhase.esriViewGraphics, null, null );
		}
         
        /// <summary>
        /// Builds an IElement from an IPoint.
        /// </summary>
        /// <param name="loki">IPoint object</param>
        /// <param name="sic">symbol ID code</param>
        /// <returns>IElement object</returns>
		private IElement BuildElement(IPoint loki, string sic)
        {
            IMoleSymbol ms = new MoleMarkerSymbolClass();
            ms.SymbolID = sic;

            // create the MarkerElement
            IMarkerElement me = new MarkerElementClass();
            me.Symbol = ms as IMarkerSymbol;
            IElement m_element = me as IElement;
            m_element.Geometry = loki as IGeometry;

            return m_element;
        }
        
        /// <summary>
        /// Generates a point with random coordinates.
        /// </summary>
        /// <returns>IPoint object</returns>
		public IPoint CreateRandomPoint()
        {
            IPoint point = new PointClass();
            point.PutCoords(m_Random.Next(-30, 30), m_Random.Next(-30, 30));
            return point;
        }

        private string GetSdkDataPath()
        {
            //get the ArcGIS path from the registry
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ESRI\ArcGIS_SXS_SDK");
            string path = Convert.ToString(key.GetValue("InstallDir"));

            //set the of the logo
            string str = System.IO.Path.Combine(path, @"Samples\data\");

            if (!System.IO.Directory.Exists(str))
            {
                MessageBox.Show("Path :" + str + " does not exist!");
                return string.Empty;
            }

            return str;
        }

        private void LoadDefaultMapData()
        {
            string dataPath = GetSdkDataPath() + @"MilitaryOverlayEditor\";
            string defaultMxDoc = dataPath + "molebasemap.mxd";

            object missing = System.Reflection.Missing.Value;

            if (this.axMapControl1.CheckMxFile(defaultMxDoc))
                this.axMapControl1.LoadMxFile(defaultMxDoc, missing, missing);
            else
            {
                string errorMsg = "Could not load default map document - Application may not work!";
                errorMsg += "\n" + defaultMxDoc;
                System.Diagnostics.Trace.WriteLine(errorMsg);
                MessageBox.Show(errorMsg);
            }
        }

		// Clears all graphics from the map control.
		private void btnClear_Click( object sender, EventArgs e )
		{
			axMapControl1.ActiveView.GraphicsContainer.DeleteAllElements();
			axMapControl1.ActiveView.Refresh();
		}
		
    }
}
[Visual Basic .NET]

MainForm.vb


Public Class MoleLeaderStack

    Dim m_Random As New Random
    Dim checkStack As Boolean
    Dim sicArray() As String = {"SFAPC----------", "SFAPCF---------", "SFAPFH---------", "SFAPCL---------", "SFAPM----------"}
    Dim m_mge As ESRI.ArcGIS.DefenseSolutions.IMoleGroupElement
    Dim m_element As ESRI.ArcGIS.Carto.IElement

#Region "class constructor"
    Public Sub New()
        ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine)
        InitializeComponent()
    End Sub
#End Region

    ' Adds multiple MOLE graphics using IGroupElement.
    Private Sub btnAddGraphics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGraphics.Click
        m_mge = New ESRI.ArcGIS.DefenseSolutions.MoleGroupElementClass()

        Dim groupElement As ESRI.ArcGIS.Carto.IGroupElement
        groupElement = m_mge

        If Not groupElement Is Nothing Then
            Dim element As ESRI.ArcGIS.Carto.IElement
            Dim i As String
            For Each i In sicArray
                Dim point As ESRI.ArcGIS.Geometry.Point
                point = CreateRandomPoint()
                element = BuildElement(point, i)
                groupElement.AddElement(element)
            Next i
        End If

        m_element = m_mge
        Draw()
    End Sub

    ' Generates a point with random coordinates
    Private Function CreateRandomAnchorPoint() As ESRI.ArcGIS.Geometry.IPoint
        'Create a new point and set its properties
        Dim point As ESRI.ArcGIS.Geometry.IPoint
        point = New ESRI.ArcGIS.Geometry.PointClass()
        point.PutCoords(m_Random.Next(-10, 10), m_Random.Next(-10, 10))
        Return point
    End Function

    Public Function CreateRandomPoint() As ESRI.ArcGIS.Geometry.IPoint
        ' Create a new point and set its properties
        Dim point As ESRI.ArcGIS.Geometry.IPoint
        point = New ESRI.ArcGIS.Geometry.PointClass()
        point.PutCoords(m_Random.Next(-30, 30), m_Random.Next(-30, 30))
        Return point
    End Function

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

        'set the of the logo
        Dim str As String = System.IO.Path.Combine(path, "Samples\data\")
        If (Not System.IO.Directory.Exists(str)) Then
            MessageBox.Show("Path :" & str & " does not exist!")
            Return String.Empty
        End If

        Return str
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AxMapControl1.LoadMxFile(GetSdkDataPath() + "MilitaryOverlayEditor\molebasemap.mxd")
    End Sub

    ' Clears the map of all graphics. 
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        AxMapControl1.ActiveView.GraphicsContainer.DeleteAllElements()
        AxMapControl1.ActiveView.Refresh()
    End Sub

    ' Applies stacking on the graphics in the group element.
    Private Sub btnAddStacked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddStacked.Click
        If Not m_mge Is Nothing Then
            m_mge.DeclutterOption = ESRI.ArcGIS.DefenseSolutions.moleDeclutterOptionEnum.moleDeclutterStack
            m_mge.EnableDeclutter = True

            ' stacking option
            Dim stackElem As ESRI.ArcGIS.DefenseSolutions.IMoleStackElement
            stackElem = m_mge
            stackElem.StackQuadrant = ESRI.ArcGIS.DefenseSolutions.moleQuadrantEnum.moleQuadrantLR
            m_element = stackElem
            Draw()
        End If
    End Sub

    ' Applies leadering on the graphics in the group element.
    Private Sub btnLeader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeader.Click
        If Not m_mge Is Nothing Then
            m_mge.DeclutterOption = ESRI.ArcGIS.DefenseSolutions.moleDeclutterOptionEnum.moleDeclutterLeader
            m_mge.EnableDeclutter = True

            Dim mle As ESRI.ArcGIS.DefenseSolutions.IMoleLeaderElement
            mle = m_mge
            mle.LeaderQuadrant = ESRI.ArcGIS.DefenseSolutions.moleQuadrantEnum.moleQuadrantUR
            mle.Anchor = CreateRandomPoint()
            mle.Base = CreateRandomPoint()
            m_element = m_mge
            Draw()
        End If
    End Sub

    Private Function BuildElement(ByVal loki As ESRI.ArcGIS.Geometry.Point, ByVal sic As String) As ESRI.ArcGIS.Carto.IElement
        Dim ms As ESRI.ArcGIS.DefenseSolutions.IMoleSymbol
        ms = New ESRI.ArcGIS.DefenseSolutions.MoleMarkerSymbolClass()
        ms.SymbolID = sic

        ' create the MarkerElement
        Dim markerEl As ESRI.ArcGIS.Carto.IMarkerElement = New ESRI.ArcGIS.Carto.MarkerElementClass()
        markerEl.Symbol = ms
        Dim element As ESRI.ArcGIS.Carto.IElement
        element = markerEl
        element.Geometry = loki

        Return element
    End Function

    Sub Draw()
        AxMapControl1.ActiveView.GraphicsContainer.AddElement(m_element, 0)
        'update the view
        AxMapControl1.ActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
    End Sub
End Class