ArcObjects Library Reference  

SelectByLineTool

About the Create a custom selection extension by extending ArcObjects Sample

[C#]

SelectByLineTool.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;

namespace SelectionCOMSample
{
  /// <summary>
  /// Summary description for SelectByLineTool.
  /// </summary>
  [Guid("15de72ff-f31f-4655-98b6-191b7348375a")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("SelectionCOMSample.SelectByLineTool")]
  public sealed class SelectByLineTool : BaseTool
  {
    #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 bool m_isMouseDown = false;
    private ESRI.ArcGIS.Display.INewLineFeedback m_lineFeedback;
    private SelectionExtension m_mainExtension;
    private IMxDocument m_doc;

    public SelectByLineTool()
    {
      base.m_category = "Developer Samples"; 
      base.m_caption = "Select ByLine Tool C#.";  
      base.m_message = "Select by line tool C#.";
      base.m_toolTip = "Select by line tool C#.\r\nSelection Sample Extension needs to be turned on in Extensions dialog.";  
      base.m_name = "ESRI_SelectionCOMSample_SelectByLineTool";   
      try
      {
        base.m_bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream("SelectionCOMSample.Images.SelectByLine.png"));
        base.m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("SelectionCOMSample.Images.SelectByLine.cur"));
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
      }
      m_mainExtension = SelectionExtension.GetExtension();
    }

    #region Overriden Class Methods

    /// <summary>
    /// Occurs when this tool is created
    /// </summary>
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
      IApplication application = hook as IApplication;
      m_doc = application.Document as IMxDocument;

      //Disable if it is not ArcMap
      if (hook is IMxApplication)
        base.m_enabled = true;
      else
        base.m_enabled = false;
    }

    /// <summary>
    /// Occurs when this tool is clicked
    /// </summary>
    public override void OnClick()
    {
    }

    public override void OnMouseDown(int Button, int Shift, int X, int Y)
    {
      IActiveView activeView = m_doc.FocusMap as IActiveView;
      IPoint point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y) as IPoint;

      if (m_lineFeedback == null)
      {
        m_lineFeedback = new ESRI.ArcGIS.Display.NewLineFeedback();
        m_lineFeedback.Display = activeView.ScreenDisplay;
        m_lineFeedback.Start(point);
      }
      else
      {
        m_lineFeedback.AddPoint(point);
      }

      m_isMouseDown = true;
    }

    public override void OnMouseMove(int Button, int Shift, int X, int Y)
    {
      if (!m_isMouseDown) return;

      IActiveView activeView = m_doc.FocusMap as IActiveView;

      IPoint point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y) as IPoint;
      m_lineFeedback.MoveTo(point);
    }


    public override void OnDblClick()
    {
      IActiveView activeView = m_doc.FocusMap as IActiveView;

      activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);

      IPolyline polyline;

      if (m_lineFeedback != null)
      {
        polyline = m_lineFeedback.Stop();
        if (polyline != null)
          m_doc.FocusMap.SelectByShape(polyline, null, false);
      }


      activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);

      m_lineFeedback = null;
      m_isMouseDown = false;
    }

    public override bool Enabled
    {
      get
      {
        if (m_mainExtension == null)
          return false;
        else
          return m_mainExtension.HasSelectableLayer() && m_mainExtension.IsExtensionEnabled;
      }
    }
    #endregion
  }
}

[Visual Basic .NET]

SelectByLineTool.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.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry

Namespace SelectionCOMSample
  ''' <summary>
  ''' Summary description for SelectByLineTool.
  ''' </summary>
  <Guid("15de72ff-f31f-4655-98b6-191b7348375a"), ClassInterface(ClassInterfaceType.None), ProgId("SelectionCOMSample.SelectByLineTool")> _
  Public NotInheritable Class SelectByLineTool
	  Inherits BaseTool
	#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_isMouseDown As Boolean = False
	Private m_lineFeedback As ESRI.ArcGIS.Display.INewLineFeedback
	Private m_mainExtension As SelectionExtension
	Private m_doc As IMxDocument

	Public Sub New()
	  MyBase.m_category = "Developer Samples"
      MyBase.m_caption = "Select ByLine Tool VB.NET."
      MyBase.m_message = "Select by line tool VB.NET."
      MyBase.m_toolTip = "Select by line tool VB.NET." & Constants.vbCrLf & "Selection Sample Extension needs to be turned on in Extensions dialog."
	  MyBase.m_name = "ESRI_SelectionCOMSample_SelectByLineTool"
	  Try
        MyBase.m_bitmap = New Bitmap(Me.GetType().Assembly.GetManifestResourceStream("SelectByLine.png"))
        MyBase.m_cursor = New System.Windows.Forms.Cursor(Me.GetType().Assembly.GetManifestResourceStream("SelectByLine.cur"))
	  Catch ex As Exception
		System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
	  End Try
	  m_mainExtension = SelectionExtension.GetExtension()
	End Sub

#Region "Overridden Class Methods"

        ''' <summary>
        ''' Occurs when this tool is created
        ''' </summary>
        ''' <param name="hook">Instance of the application</param>
        Public Overrides Sub OnCreate(ByVal hook As Object)
            Dim application As IApplication = TryCast(hook, IApplication)
            m_doc = TryCast(application.Document, IMxDocument)

            'Disable if it is not ArcMap
            If TypeOf hook Is IMxApplication Then
                MyBase.m_enabled = True
            Else
                MyBase.m_enabled = False
            End If
        End Sub

        ''' <summary>
        ''' Occurs when this tool is clicked
        ''' </summary>
        Public Overrides Sub OnClick()
        End Sub

        Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
            Dim activeView As IActiveView = TryCast(m_doc.FocusMap, IActiveView)
            Dim point As IPoint = TryCast(activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y), IPoint)

            If m_lineFeedback Is Nothing Then
                m_lineFeedback = New ESRI.ArcGIS.Display.NewLineFeedback()
                m_lineFeedback.Display = activeView.ScreenDisplay
                m_lineFeedback.Start(point)
            Else
                m_lineFeedback.AddPoint(point)
            End If

            m_isMouseDown = True
        End Sub

        Public Overrides Sub OnMouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
            If (Not m_isMouseDown) Then
                Return
            End If

            Dim activeView As IActiveView = TryCast(m_doc.FocusMap, IActiveView)

            Dim point As IPoint = TryCast(activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y), IPoint)
            m_lineFeedback.MoveTo(point)
        End Sub


        Public Overrides Sub OnDblClick()
            Dim activeView As IActiveView = TryCast(m_doc.FocusMap, IActiveView)

            activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

            Dim polyline As IPolyline

            If m_lineFeedback IsNot Nothing Then
                polyline = m_lineFeedback.Stop()
                If polyline IsNot Nothing Then
                    m_doc.FocusMap.SelectByShape(polyline, Nothing, False)
                End If
            End If


            activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

            m_lineFeedback = Nothing
            m_isMouseDown = False
        End Sub

        Public Overrides ReadOnly Property Enabled() As Boolean
            Get
                If m_mainExtension Is Nothing Then
                    Return False
                Else
                    Return m_mainExtension.HasSelectableLayer() AndAlso m_mainExtension.IsExtensionEnabled
                End If
            End Get
        End Property
#End Region
  End Class
End Namespace