Decluttering MOLE graphics using leadering and stacking
MainForm.cs
// 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.
// 

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();
    }
    
    }
}