ArcObjects Library Reference  

ReshapePolylineEditTask

About the Custom reshape polyline edit task Sample

[C#]

ReshapePolylineEditTask.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;

namespace ReshapePolylineEditTask_CS
{
    [Guid("89467aa7-76c1-4531-a160-e160e8d782f7")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("ReshapePolylineEditTask_CS.ReshapePolylineEditTask")]
    public class ReshapePolylineEditTask : ESRI.ArcGIS.Controls.IEngineEditTask
    {
        #region Private Members
        IEngineEditor m_engineEditor;
        IEngineEditSketch m_editSketch;
        IEngineEditLayers m_editLayer;
        #endregion

        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);
        }

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

        #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);
            EngineEditTasks.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);
            EngineEditTasks.Unregister(regKey);

        }

        #endregion
        #endregion

        #region IEngineEditTask Implementations

        public void Activate(ESRI.ArcGIS.Controls.IEngineEditor editor, ESRI.ArcGIS.Controls.IEngineEditTask oldTask)
        {
            if (editor == null)
                return;

            m_engineEditor = editor;
            m_editSketch = m_engineEditor as IEngineEditSketch;
            m_editSketch.GeometryType = esriGeometryType.esriGeometryPolyline;
            m_editLayer = m_editSketch as IEngineEditLayers; 
           
            //Listen to engine editor events
            ((IEngineEditEvents_Event)m_editSketch).OnTargetLayerChanged += new IEngineEditEvents_OnTargetLayerChangedEventHandler(OnTargetLayerChanged);
            ((IEngineEditEvents_Event)m_editSketch).OnSelectionChanged += new IEngineEditEvents_OnSelectionChangedEventHandler(OnSelectionChanged);
            ((IEngineEditEvents_Event)m_editSketch).OnCurrentTaskChanged += new IEngineEditEvents_OnCurrentTaskChangedEventHandler(OnCurrentTaskChanged);
        }

     
        public void Deactivate()
        {
            m_editSketch.RefreshSketch();
            
            //Stop listening to engine editor events.
            ((IEngineEditEvents_Event)m_editSketch).OnTargetLayerChanged -= OnTargetLayerChanged;
            ((IEngineEditEvents_Event)m_editSketch).OnSelectionChanged -= OnSelectionChanged;
            ((IEngineEditEvents_Event)m_editSketch).OnCurrentTaskChanged -= OnCurrentTaskChanged;

            //Release object references.
            m_engineEditor = null;
            m_editSketch = null;
            m_editLayer = null;
        }

        public string GroupName
        {
            get
            {    
                //This property allows groups to be created/used in the EngineEditTaskToolControl treeview.
                //If an empty string is supplied the task will be appear in an "Other Tasks" group. 
                //In this example the Reshape Polyline_CSharp task will appear in the existing Modify Tasks group.
                return "Modify Tasks";
            }
        }

        public string Name
        {
            get
            {    
                return "Reshape Polyline_CSharp"; //unique edit task name
            }
        }

        public void OnDeleteSketch()
        {
        }

        public void OnFinishSketch()
        {
            //get reference to featurelayer being edited
            IFeatureLayer featureLayer = m_editLayer.TargetLayer as IFeatureLayer;
            //get reference to the sketch geometry
            IGeometry reshapeGeom = m_editSketch.Geometry;

            if (reshapeGeom.IsEmpty == false)
            {
                //get the currently selected feature    
                IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
                ISelectionSet selectionSet = featureSelection.SelectionSet;
                ICursor cursor;
                selectionSet.Search(null, true, out cursor);
                IFeatureCursor featureCursor = cursor as IFeatureCursor;
                //the PerformSketchToolEnabledChecks property has already checked that only 1 feature is selected
                IFeature feature = featureCursor.NextFeature();
                 
                //Take a copy of geometry for the selected feature
                IGeometry editShape = feature.ShapeCopy;

                //create a path from the editsketch geometry
                IPointCollection reshapePath = new PathClass();
                reshapePath.AddPointCollection(reshapeGeom as IPointCollection);

                //reshape the selected feature
                IPolyline polyline = editShape as IPolyline;
                polyline.Reshape(reshapePath as IPath);

                #region Perform an edit operation to store the new geometry for selected feature

                try
                {
                    m_engineEditor.StartOperation();
                    feature.Shape = editShape;
                    feature.Store();
                    m_engineEditor.StopOperation("Reshape Feature");
                }
                catch (Exception ex)
                {
                    m_engineEditor.AbortOperation();
                    System.Diagnostics.Trace.WriteLine(ex.Message, "Reshape Geometry Failed");
                }

                #endregion
            }

            //refresh the display 
            IActiveView activeView = m_engineEditor.Map as IActiveView;
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, (object)featureLayer, activeView.Extent);
          

        }

        public string UniqueName
        {      
            get 
            { 
                return "ReshapePolylineEditTask_Reshape Polyline_CSharp" ;
            }
        }

        #endregion

        #region Event Listeners
        public void OnTargetLayerChanged()
        {
            PerformSketchToolEnabledChecks();
        }

        public void OnSelectionChanged()
        {
            PerformSketchToolEnabledChecks();
        }


        void OnCurrentTaskChanged()
        {
            if (m_engineEditor.CurrentTask.Name == "Reshape Polyline_CSharp")
            {
                PerformSketchToolEnabledChecks();
            }
        }
        #endregion

        #region private methods

        private void PerformSketchToolEnabledChecks()
        {
            if (m_editLayer == null)
                return;

            //Only enable the sketch tool if there is a polyline target layer.
            if (m_editLayer.TargetLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)
            {
                m_editSketch.GeometryType = esriGeometryType.esriGeometryNull;
                return;
            }

            //check that only one feature in the target layer is currently selected
            IFeatureSelection featureSelection = m_editLayer.TargetLayer as IFeatureSelection;
            ISelectionSet selectionSet = featureSelection.SelectionSet;
            if (selectionSet.Count != 1)
            {
                m_editSketch.GeometryType = esriGeometryType.esriGeometryNull;
                return;
            }

            m_editSketch.GeometryType = esriGeometryType.esriGeometryPolyline;

        }

        #endregion

    }
}

[Visual Basic .NET]

ReshapePolylineEditTask.vb

Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.esriSystem

<ComClass(ReshapePolylineEditTask.ClassId, ReshapePolylineEditTask.InterfaceId, ReshapePolylineEditTask.EventsId), _
 ProgId("ReshapePolylineEditTask_VB.ReshapePolylineEditTask")> _
Public Class ReshapePolylineEditTask

    Implements ESRI.ArcGIS.Controls.IEngineEditTask


#Region "COM Registration Function(s)"
    <ComRegisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub RegisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryRegistration(registerType)
    End Sub

    <ComUnregisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub UnregisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryUnregistration(registerType)
    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)
        EngineEditTasks.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)
        EngineEditTasks.Unregister(regKey)

    End Sub

#End Region
#End Region

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "3ee19d65-bcb9-4823-995a-a2779d664332"
    Public Const InterfaceId As String = "2f536850-a5b7-48b7-a3e6-337dd5d3d9ec"
    Public Const EventsId As String = "c83dddf2-f231-4f7d-96b0-630d06e49f97"
#End Region

#Region "Private Members"
    Private m_engineEditor As IEngineEditor
    Private m_editSketch As IEngineEditSketch
    Private m_editLayer As IEngineEditLayers
    Private m_ActiveViewEventsAfterDraw As IActiveViewEvents_AfterDrawEventHandler
#End Region

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub Activate(ByVal editor As ESRI.ArcGIS.Controls.IEngineEditor, ByVal oldTask As ESRI.ArcGIS.Controls.IEngineEditTask) Implements ESRI.ArcGIS.Controls.IEngineEditTask.Activate

        If (editor Is Nothing) Then
            Return
        End If

        m_engineEditor = editor
        m_editSketch = TryCast(m_engineEditor, IEngineEditSketch)
        m_editSketch.GeometryType = esriGeometryType.esriGeometryPolyline
        m_editLayer = TryCast(m_editSketch, IEngineEditLayers)

        'Listen to engine editor events
        AddHandler (CType(m_engineEditor, IEngineEditEvents_Event)).OnTargetLayerChanged, AddressOf OnTargetLayerChanged
        AddHandler (CType(m_engineEditor, IEngineEditEvents_Event)).OnSelectionChanged, AddressOf OnSelectionChanged
        AddHandler (CType(m_engineEditor, IEngineEditEvents_Event)).OnCurrentTaskChanged, AddressOf OnCurrentTaskChanged

    End Sub

    Public Sub Deactivate() Implements ESRI.ArcGIS.Controls.IEngineEditTask.Deactivate

        m_editSketch.RefreshSketch()

        'Stop listening for engine editor events.
        RemoveHandler (CType(m_engineEditor, IEngineEditEvents_Event)).OnTargetLayerChanged, AddressOf OnTargetLayerChanged
        RemoveHandler (CType(m_engineEditor, IEngineEditEvents_Event)).OnSelectionChanged, AddressOf OnSelectionChanged
        RemoveHandler (CType(m_engineEditor, IEngineEditEvents_Event)).OnCurrentTaskChanged, AddressOf OnCurrentTaskChanged

        'Release object references.
        m_engineEditor = Nothing
        m_editSketch = Nothing
        m_editLayer = Nothing

    End Sub

    Public ReadOnly Property GroupName() As String Implements ESRI.ArcGIS.Controls.IEngineEditTask.GroupName
        Get
            'This property allows groups to be created/used in the EngineEditTaskToolControl treeview.
            'If an empty string is supplied the task will be appear in an "Other Tasks" group. 
            'In this example the Reshape Feature_VB task will appear in the existing Modify Tasks group.
            Return "Modify Tasks"
        End Get
    End Property

    Public ReadOnly Property Name() As String Implements ESRI.ArcGIS.Controls.IEngineEditTask.Name
        Get
            Return "Reshape Polyline_VB"
        End Get
    End Property

    Public Sub OnDeleteSketch() Implements ESRI.ArcGIS.Controls.IEngineEditTask.OnDeleteSketch

    End Sub

    Public Sub OnFinishSketch() Implements ESRI.ArcGIS.Controls.IEngineEditTask.OnFinishSketch

        'get reference to the FeatureLayer being edited
        Dim featureLayer As IFeatureLayer = CType(m_editLayer.TargetLayer, IFeatureLayer)
        'get reference to the sketch geometry
        Dim reshapeGeom As IGeometry = m_editSketch.Geometry

        If (reshapeGeom.IsEmpty = False) Then

            'get the currently selected feature  
            Dim featureSelection As IFeatureSelection = featureLayer
            Dim selectionSet As ISelectionSet = featureSelection.SelectionSet
            Dim featureCursor As IFeatureCursor = Nothing

            selectionSet.Search(Nothing, True, featureCursor)

            'the enabled property has already checked that only 1 feature is selected
            Dim feature As IFeature = featureCursor.NextFeature()

            'Take a copy of geometry for the selected feature
            Dim editShape As IGeometry = feature.ShapeCopy

            'create a path from the editsketch geometry
            Dim reshapePath As IPointCollection = New PathClass()
            reshapePath.AddPointCollection(reshapeGeom)

            'reshape the selected feature
            Dim polyline As IPolyline = editShape
            polyline.Reshape(reshapePath)

            Try

                m_engineEditor.StartOperation()
                feature.Shape = editShape
                feature.Store()
                m_engineEditor.StopOperation("Reshape Feature")

            Catch ex As Exception

                m_engineEditor.AbortOperation()
                System.Diagnostics.Trace.WriteLine(ex.Message, "Edit Reshape Feature Failed")

            End Try

        End If

        'refresh the display 
        Dim activeView As IActiveView = CType(m_engineEditor.Map, IActiveView)
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, featureLayer, activeView.Extent)

    End Sub

    Public ReadOnly Property UniqueName() As String Implements ESRI.ArcGIS.Controls.IEngineEditTask.UniqueName
        Get
            Return "ReshapePolylineEditTask_Reshape Polyline_VB"
        End Get
    End Property

#Region "Event Handlers"

    Private Sub OnTargetLayerChanged()
        PerformSketchToolEnabledChecks()
    End Sub

    Private Sub OnSelectionChanged()
        PerformSketchToolEnabledChecks()
    End Sub

    Private Sub OnCurrentTaskChanged()
        If (m_engineEditor.CurrentTask.Name = "Reshape Polyline_VB") Then
            PerformSketchToolEnabledChecks()
        End If

    End Sub

#End Region

#Region "Private Methods"
    Private Sub PerformSketchToolEnabledChecks()

        If m_editLayer Is Nothing Then
            Return
        End If

        'Only enable the sketch tool if there is a polyline target layer.
        If m_editLayer.TargetLayer.FeatureClass.ShapeType <> esriGeometryType.esriGeometryPolyline Then
            m_editSketch.GeometryType = esriGeometryType.esriGeometryNull
            Return
        End If

        'check that only one feature in the target layer is currently selected
        Dim featureSelection As IFeatureSelection = CType(m_editLayer.TargetLayer, IFeatureSelection)
        Dim selectionSet As ISelectionSet = featureSelection.SelectionSet

        If (selectionSet.Count <> 1) Then
            m_editSketch.GeometryType = esriGeometryType.esriGeometryNull
            Return
        End If


        m_editSketch.GeometryType = esriGeometryType.esriGeometryPolyline

    End Sub
#End Region

End Class