ArcObjects Library Reference  

cmdMoveGraphicAlongPath

About the Move a graphic along a path in ArcMap Sample

[C#]

cmdMoveGraphicAlongPath.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Animation;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Framework;

namespace AnimationDeveloperSamples
{
    [Guid("96EA1F27-4394-4997-ADC0-065792082D1D")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("AnimationDeveloperSamples.cmdMoveGraphicAlongPath")]
    public sealed class cmdMoveGraphicAlongPath : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper = null;
        private IAnimationExtension animExt;

        public cmdMoveGraphicAlongPath()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "Animation Developer Samples"; //localizable text
            base.m_caption = "Move Graphic along Path...";  //localizable text 
            base.m_message = "Move graphic along a selected line graphic or line feature";  //localizable text
            base.m_toolTip = "Move graphic along path";  //localizable text
            base.m_name = "AnimationDeveloperSamples_cmdMoveGraphicAlongPath";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            string[] res = GetType().Assembly.GetManifestResourceNames();
            if (res.GetLength(0) > 0)
            {
                base.m_bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream(GetType(), "cmdMoveGraphicAlongPath.bmp"));
            }
        }

        #region Overriden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                    m_hookHelper = null;
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            if (hook is IApplication)
            {
                IApplication app = (IApplication)hook;
                UID pUID = new UIDClass();
                pUID.Value = "esriAnimation.AnimationExtension";
                animExt = (IAnimationExtension)app.FindExtensionByCLSID(pUID);
            }
        }

        public override void OnClick()
        {
            frmCreateGraphicTrackOptions optionsForm = new frmCreateGraphicTrackOptions();
            IGeometry selectedPath = GetSelectedLineFeature();
            IElement lineElement = (IElement)GetSelectedLineElement();
            IElement selectedElement = GetSelectedPointElement();
            optionsForm.lineFeature = selectedPath;
            optionsForm.lineGraphic = (ILineElement)lineElement;
            optionsForm.pointGraphic = selectedElement;
            optionsForm.AnimationExtension = animExt;
            optionsForm.RefreshPathSourceOptions();
            optionsForm.ShowDialog();
        }

        public override bool Enabled
        {
            get
            {
                if (m_hookHelper != null)
                {
                    IGeometry selectedPath = GetSelectedLineFeature();
                    IElement lineElement = (IElement)GetSelectedLineElement();
                    IElement selectedElement = GetSelectedPointElement();

                    if (selectedElement != null && (lineElement != null || selectedPath != null))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                    return false;
            }
        }

        #endregion

        #region private methods
        private IGeometry GetSelectedLineFeature()
        {
            IMap pMap = m_hookHelper.FocusMap;
            IEnumFeature enumFeature = (IEnumFeature)pMap.FeatureSelection;
            IGeometry selectedPath = null;

            IFeature pFeat = enumFeature.Next();
            while (pFeat != null)
            {
                if (pFeat.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                {
                    selectedPath = (IGeometry)pFeat.Shape;
                    break;
                }
                pFeat = enumFeature.Next();
            }
            return selectedPath;
        }

        private IElement GetSelectedPointElement()
        {
            IMap activeFrame = m_hookHelper.FocusMap;
            IGraphicsContainerSelect graphicsSel = activeFrame as IGraphicsContainerSelect;
            IElement selectedElement = null;
            IAGAnimationType graphicAnimationType = new AnimationTypeMapGraphic();
            if (graphicsSel.ElementSelectionCount > 0)
            {
                IEnumElement enumElem = graphicsSel.SelectedElements;
                selectedElement = enumElem.Next();
                while (selectedElement != null)
                {
                    if (graphicAnimationType.get_AppliesToObject(selectedElement))
                        break;
                    selectedElement = enumElem.Next();
                }
            }
            else
            {
                selectedElement = null;
            }
            return selectedElement;
        }

        private ILineElement GetSelectedLineElement()
        {
            IMap activeFrame = m_hookHelper.FocusMap;
            IGraphicsContainerSelect graphicsSel = activeFrame as IGraphicsContainerSelect;
            IElement selectedElement = null;
            ILineElement lineElement;
            if (graphicsSel.ElementSelectionCount > 0)
            {
                IEnumElement enumElem = graphicsSel.SelectedElements;
                selectedElement = enumElem.Next();
                while (selectedElement != null)
                {
                    if (selectedElement is ILineElement)
                        break;
                    selectedElement = enumElem.Next();
                }
            }
            lineElement = (ILineElement)selectedElement;
            return lineElement;
        }
        #endregion
    }
}

[Visual Basic .NET]

cmdMoveGraphicAlongPath.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Animation
Imports ESRI.ArcGIS.ADF
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Framework

<Guid("64794603-561D-48A5-B0F1-286DC26ADFA6"), ClassInterface(ClassInterfaceType.None), ProgId("AnimationDeveloperSamples.cmdMoveGraphicAlongPath")> _
Public NotInheritable Class cmdMoveGraphicAlongPath : Inherits BaseCommand
#Region "COM Registration Function(s)"
    <ComRegisterFunction(), ComVisible(False)> _
    Private Shared Sub RegisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryRegistration(registerType)

        '
        ' TODO: Add any COM registration code here
        '
    End Sub

    <ComUnregisterFunction(), ComVisible(False)> _
    Private Shared Sub UnregisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryUnregistration(registerType)

        '
        ' TODO: Add any COM unregistration code here
        '
    End Sub

#Region "ArcGIS Component Category Registrar generated code"
    ''' <summary>
    ''' Required method for ArcGIS Component Category registration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        MxCommands.Register(regKey)

    End Sub
    ''' <summary>
    ''' Required method for ArcGIS Component Category unregistration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        MxCommands.Unregister(regKey)

    End Sub

#End Region
#End Region

    Private m_hookHelper As IHookHelper = Nothing
    Private animExt As IAnimationExtension

    Public Sub New()
        '
        ' TODO: Define values for the public properties
        '
        MyBase.m_category = "Animation Developer Samples" 'localizable text
        MyBase.m_caption = "Move Graphic along Path..." 'localizable text
        MyBase.m_message = "Move graphic along a selected line graphic or line feature" 'localizable text
        MyBase.m_toolTip = "Move graphic along path" 'localizable text
        MyBase.m_name = "AnimationDeveloperSamples_cmdMoveGraphicAlongPath" 'unique id, non-localizable (e.g. "MyCategory_MyCommand")

        Dim res As String() = Me.GetType().Assembly.GetManifestResourceNames()
        If res.GetLength(0) > 0 Then
            MyBase.m_bitmap = New Bitmap(Me.GetType().Assembly.GetManifestResourceStream(Me.GetType(), "cmdMoveGraphicAlongPath.bmp"))
        End If

        'Dim bitmapResourceName As String = Me.GetType().Name & ".bmp"
        'MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
    End Sub

#Region "Overridden Class Methods"

    ''' <summary>
    ''' Occurs when this command is created
    ''' </summary>
    ''' <param name="hook">Instance of the application</param>
    Public Overrides Sub OnCreate(ByVal hook As Object)
        If hook Is Nothing Then
            Return
        End If

        Try
            m_hookHelper = New HookHelperClass()
            m_hookHelper.Hook = hook
            If m_hookHelper.ActiveView Is Nothing Then
                m_hookHelper = Nothing
            End If
        Catch
            m_hookHelper = Nothing
        End Try

        If m_hookHelper Is Nothing Then
            MyBase.m_enabled = False
        Else
            MyBase.m_enabled = True
        End If

        If TypeOf hook Is IApplication Then
            Dim app As IApplication = CType(hook, IApplication)
            Dim pUID As UID = New UIDClass()
            pUID.Value = "esriAnimation.AnimationExtension"
            animExt = CType(app.FindExtensionByCLSID(pUID), IAnimationExtension)
        End If
    End Sub

    Public Overrides Sub OnClick()
        Dim optionsForm As frmCreateGraphicTrackOptions = New frmCreateGraphicTrackOptions()
        Dim selectedPath As IGeometry = GetSelectedLineFeature()
        Dim lineElement As IElement = CType(GetSelectedLineElement(), IElement)
        Dim selectedElement As IElement = GetSelectedPointElement()
        optionsForm.lineFeature = selectedPath
        optionsForm.lineGraphic = CType(lineElement, ILineElement)
        optionsForm.pointGraphic = selectedElement
        optionsForm.AnimationExtension = animExt
        optionsForm.RefreshPathSourceOptions()
        optionsForm.ShowDialog()
    End Sub

    Public Overrides ReadOnly Property Enabled() As Boolean
        Get
            If Not m_hookHelper Is Nothing Then
                Dim selectedPath As IGeometry = GetSelectedLineFeature()
                Dim lineElement As IElement = CType(GetSelectedLineElement(), IElement)
                Dim selectedElement As IElement = GetSelectedPointElement()

                If Not selectedElement Is Nothing AndAlso (Not lineElement Is Nothing OrElse Not selectedPath Is Nothing) Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        End Get
    End Property

#End Region

#Region "private methods"
    Private Function GetSelectedLineFeature() As IGeometry
        Dim pMap As IMap = m_hookHelper.FocusMap
        Dim enumFeature As IEnumFeature = CType(pMap.FeatureSelection, IEnumFeature)
        Dim selectedPath As IGeometry = Nothing

        Dim pFeat As IFeature = enumFeature.Next()
        Do While Not pFeat Is Nothing
            If pFeat.Shape.GeometryType = esriGeometryType.esriGeometryPolyline Then
                selectedPath = CType(pFeat.Shape, IGeometry)
                Exit Do
            End If
            pFeat = enumFeature.Next()
        Loop
        Return selectedPath
    End Function

    Private Function GetSelectedPointElement() As IElement
        Dim activeFrame As IMap = m_hookHelper.FocusMap
        Dim graphicsSel As IGraphicsContainerSelect = TryCast(activeFrame, IGraphicsContainerSelect)
        Dim selectedElement As IElement = Nothing
        Dim graphicAnimationType As IAGAnimationType = New AnimationTypeMapGraphic()
        If graphicsSel.ElementSelectionCount > 0 Then
            Dim enumElem As IEnumElement = graphicsSel.SelectedElements
            selectedElement = enumElem.Next()
            Do While Not selectedElement Is Nothing
                If graphicAnimationType.AppliesToObject(selectedElement) Then
                    Exit Do
                End If
                selectedElement = enumElem.Next()
            Loop
        Else
            selectedElement = Nothing
        End If
        Return selectedElement
    End Function

    Private Function GetSelectedLineElement() As ILineElement
        Dim activeFrame As IMap = m_hookHelper.FocusMap
        Dim graphicsSel As IGraphicsContainerSelect = TryCast(activeFrame, IGraphicsContainerSelect)
        Dim selectedElement As IElement = Nothing
        Dim lineElement As ILineElement
        If graphicsSel.ElementSelectionCount > 0 Then
            Dim enumElem As IEnumElement = graphicsSel.SelectedElements
            selectedElement = enumElem.Next()
            Do While Not selectedElement Is Nothing
                If TypeOf selectedElement Is ILineElement Then
                    Exit Do
                End If
                selectedElement = enumElem.Next()
            Loop
        End If
        lineElement = CType(selectedElement, ILineElement)
        Return lineElement
    End Function
#End Region
End Class