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


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