ArcObjects Library Reference  

TAPurgeRuleCmd

About the Updating the purge rule on a real-time temporal layer Sample

[C#]

TAPurgeRuleCmd.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;

namespace TAPurgeRuleCommand
{
	/// <summary>
	/// Command that works in ArcMap/Map/PageLayout, ArcScene/SceneControl
	/// or ArcGlobe/GlobeControl
	/// </summary>
	[Guid("4a9ae2c3-dfdb-4b55-922d-558a1a9ccfe1")]
	[ClassInterface(ClassInterfaceType.None)]
	[ProgId("TAPurgeRuleCommand.TAPurgeRuleCmd")]
	public sealed class TAPurgeRuleCmd : 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);
			GMxCommands.Register(regKey);
			MxCommands.Register(regKey);
			SxCommands.Register(regKey);
			ControlsCommands.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);
			GMxCommands.Unregister(regKey);
			MxCommands.Unregister(regKey);
			SxCommands.Unregister(regKey);
			ControlsCommands.Unregister(regKey);
		}

		#endregion
		#endregion

		private IHookHelper m_hookHelper = null;
		private IGlobeHookHelper m_globeHookHelper = null;
		private const string TEMPORALLAYERCLSID = "{78C7430C-17CF-11D5-B7CF-00010265ADC5}"; //CLSID for ITemporalLayer
		private PurgeRuleForm m_PRForm;

		public TAPurgeRuleCmd()
		{
			//
			// TODO: Define values for the public properties
			//
			base.m_category = ".NET Samples"; //localizable text
			base.m_caption = "Change the purge rule for temporal layers";  //localizable text
			base.m_message = "Change the purge rule for temporal layers"; //localizable text 
			base.m_toolTip = "Change the purge rule for temporal layers";  //localizable text 
			base.m_name = "TAPurgeRuleCommand_TAPurgeRuleCmd";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

			m_PRForm = new PurgeRuleForm();

			try
			{
				//
				// TODO: change bitmap name if necessary
				//
				string bitmapResourceName = GetType().Name + ".bmp";
				base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
			}
			catch (Exception ex)
			{
				System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
			}
		}

		#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;

			// Test the hook that calls this command and disable if nothing is valid
			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)
			{
				//Can be globe
				try
				{
					m_globeHookHelper = new GlobeHookHelperClass();
					m_globeHookHelper.Hook = hook;
					if (m_globeHookHelper.ActiveViewer == null)
					{
						m_globeHookHelper = null;
					}
				}
				catch
				{
					m_globeHookHelper = null;
				}
			}

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

			//TODO: Add other initialization code
		}

		/// <summary>
		/// Occurs when this command is clicked
		/// </summary>
		public override void OnClick()
		{
			//Show the dialog, pass it the temporal layers from the map, have it initialize the dialog
			if (!m_PRForm.Visible)
			{
				m_PRForm.Show();
				m_PRForm.TrackingLayers = GetAllTrackingLayers();
				m_PRForm.PopulateDialog();
			}
			else
			{
				m_PRForm.Hide();
			}
		}

		//Show the command as depressed when the dialog is visible
		public override bool Checked
		{
			get
			{
				return m_PRForm.Visible;
			}
		}
		#endregion

		//Query the map for all the tracking layers in it
		private IEnumLayer GetAllTrackingLayers()
		{
			IEnumLayer eLayers = null;
			try
			{
				IBasicMap basicMap = null;
				IUID uidTemoralLayer = new UIDClass();
				uidTemoralLayer.Value = TEMPORALLAYERCLSID;

				if (m_hookHelper != null)
				{

					basicMap = m_hookHelper.FocusMap as IBasicMap;
				}
				else if (m_globeHookHelper != null)
				{
					basicMap = m_globeHookHelper.Globe as IBasicMap;
				}

				//This call throws an E_FAIL exception if the map has no layers, caught below
				if (basicMap != null)
				{
					eLayers = basicMap.get_Layers((UID)uidTemoralLayer, true);
				}
			}
			catch
			{
			}
			
			return eLayers;
		}

	}
}

[Visual Basic .NET]

TAPurgeRuleCmd.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Carto

Namespace TAPurgeRuleCommand
	''' <summary>
	''' Command that works in ArcMap/Map/PageLayout, ArcScene/SceneControl
	''' or ArcGlobe/GlobeControl
	''' </summary>
	<Guid("4a9ae2c3-dfdb-4b55-922d-558a1a9ccfe1"), ClassInterface(ClassInterfaceType.None), ProgId("TAPurgeRuleCommand.TAPurgeRuleCmd")> _
	Public NotInheritable Class TAPurgeRuleCmd : 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)
			GMxCommands.Register(regKey)
			MxCommands.Register(regKey)
			SxCommands.Register(regKey)
			ControlsCommands.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)
			GMxCommands.Unregister(regKey)
			MxCommands.Unregister(regKey)
			SxCommands.Unregister(regKey)
			ControlsCommands.Unregister(regKey)
		End Sub

		#End Region
		#End Region

		Private m_hookHelper As IHookHelper = Nothing
		Private m_globeHookHelper As IGlobeHookHelper = Nothing
		Private Const TEMPORALLAYERCLSID As String = "{78C7430C-17CF-11D5-B7CF-00010265ADC5}" 'CLSID for ITemporalLayer
		Private m_PRForm As PurgeRuleForm

		Public Sub New()
			'
			' TODO: Define values for the public properties
			'
			MyBase.m_category = ".NET Samples" 'localizable text
			MyBase.m_caption = "Change the purge rule for temporal layers" 'localizable text
			MyBase.m_message = "Change the purge rule for temporal layers" 'localizable text
			MyBase.m_toolTip = "Change the purge rule for temporal layers" 'localizable text
			MyBase.m_name = "TAPurgeRuleCommand_TAPurgeRuleCmd" 'unique id, non-localizable (e.g. "MyCategory_MyCommand")

			m_PRForm = New PurgeRuleForm()

			Try
				'
				' TODO: change bitmap name if necessary
				'
				Dim bitmapResourceName As String = Me.GetType().Name & ".bmp"
				MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
			Catch ex As Exception
				System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
			End Try
		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

            ' Test the hook that calls this command and disable if nothing is valid
            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
                'Can be globe
                Try
                    m_globeHookHelper = New GlobeHookHelperClass()
                    m_globeHookHelper.Hook = hook
                    If m_globeHookHelper.ActiveViewer Is Nothing Then
                        m_globeHookHelper = Nothing
                    End If
                Catch
                    m_globeHookHelper = Nothing
                End Try
            End If

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

            'TODO: Add other initialization code
        End Sub

        ''' <summary>
        ''' Occurs when this command is clicked
        ''' </summary>
        Public Overrides Sub OnClick()
            'Show the dialog, pass it the temporal layers from the map, have it initialize the dialog
            If (Not m_PRForm.Visible) Then
                m_PRForm.Show()
                m_PRForm.TrackingLayers = GetAllTrackingLayers()
                m_PRForm.PopulateDialog()
            Else
                m_PRForm.Hide()
            End If
        End Sub

        'Show the command as depressed when the dialog is visible
        Public Overrides ReadOnly Property Checked() As Boolean
            Get
                Return m_PRForm.Visible
            End Get
        End Property
#End Region

		'Query the map for all the tracking layers in it
		Private Function GetAllTrackingLayers() As IEnumLayer
			Dim eLayers As IEnumLayer = Nothing
			Try
				Dim basicMap As IBasicMap = Nothing
				Dim uidTemoralLayer As IUID = New UIDClass()
				uidTemoralLayer.Value = TEMPORALLAYERCLSID

				If Not m_hookHelper Is Nothing Then

					basicMap = TryCast(m_hookHelper.FocusMap, IBasicMap)
				ElseIf Not m_globeHookHelper Is Nothing Then
					basicMap = TryCast(m_globeHookHelper.Globe, IBasicMap)
				End If

				'This call throws an E_FAIL exception if the map has no layers, caught below
				If Not basicMap Is Nothing Then
                    eLayers = basicMap.Layers(CType(uidTemoralLayer, UID), True)
				End If
			Catch
			End Try

			Return eLayers
		End Function

	End Class
End Namespace