Common Custom EditorTask
Common_CustomEditorTask_VBNet\CustomEditorTask_VBNet\ClipFeatures.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.
' 

Imports Microsoft.VisualBasic
Imports System
Namespace CustomEditorTask_VBNet
  Friend Class ClipFeatures
    Inherits ESRI.ArcGIS.ADF.ArcGISServer.Editor.Tools.EditorServerToolAction
    ' Maintain a list of feature ids for the clipped features.
    Private m_featureIDsList As System.Collections.Generic.List(Of Integer) = New System.Collections.Generic.List(Of Integer)()
    Public ReadOnly Property Features() As System.Collections.Generic.List(Of Integer)
      Get
        Return m_featureIDsList
      End Get
    End Property

    ' Clears the feature id list.
    Protected Overrides Function Init(ByVal editor As ESRI.ArcGIS.ADF.ArcGISServer.Editor.Editor) As Boolean
      m_featureIDsList.Clear()
      Return MyBase.Init(editor)
    End Function

    ' Implementation for clipping the selected features based on a user defined envelope.
    Protected Overrides Sub EditorServerAction()
      ' Cast the base class's input geometry into ArcObjects COM geometry
      Dim agsComEnvelope As ESRI.ArcGIS.Geometry.IEnvelope = TryCast(Me.Geometry, ESRI.ArcGIS.Geometry.IEnvelope)

      ' Get the map description underlying the editor's map
      Dim agsSoapMapDescription As ESRI.ArcGIS.ADF.ArcGISServer.MapDescription = Editor.MapFunctionality.MapDescription

      ' Get an array containing the IDs of the currently selected features
      Dim featureIDArray As Integer() = LayerDescription.SelectionFeatures

      ' Make sure features are selected
      If Not featureIDArray Is Nothing AndAlso featureIDArray.Length > 0 Then
        ' Declare ArcGIS COM objects to use in performing the clip operation and updating the
        ' features in the database underlying the resource being edited
        Dim agsComTopologicalOperator3 As ESRI.ArcGIS.Geometry.ITopologicalOperator3 = Nothing
        Dim agsComFeature As ESRI.ArcGIS.Geodatabase.IFeature = Nothing

        Try
          ' Start the editing operation on the workspace
          Me.StartEditOperation()

          ' Iterate through selected features and clip with user defined envelope
          Dim agsComFeatureCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor = FeatureLayer.FeatureClass.GetFeatures(featureIDArray, False)
          agsComFeature = agsComFeatureCursor.NextFeature()

          Do While Not agsComFeature Is Nothing
            agsComTopologicalOperator3 = CType(agsComFeature.ShapeCopy, ESRI.ArcGIS.Geometry.ITopologicalOperator3)
            agsComTopologicalOperator3.Clip(agsComEnvelope)
            Dim agsComGeometryClipResult As ESRI.ArcGIS.Geometry.IGeometry = CType(agsComTopologicalOperator3, ESRI.ArcGIS.Geometry.IGeometry)

            If (Not agsComGeometryClipResult.IsEmpty) Then
              agsComFeature.Shape = agsComGeometryClipResult
              agsComFeature.Store()

              ' Keep track of the clipped features
              m_featureIDsList.Add(agsComFeature.OID)
            End If
            agsComFeature = agsComFeatureCursor.NextFeature()
          Loop

          ' Stop the editing operation on the workspace.
          Me.StopEditOperation()
        Catch exception As System.Exception
          ' Base class will handle clean up if exception occurs during edit operation.
          AbortEditOperation(exception)
        End Try

        ' Make sure there were features to update before refreshing
        If m_featureIDsList.Count > 0 Then
          ' Call Refresh to update the Editor, Map, vertices, etc. with the new selection set
          Me.Refresh(m_featureIDsList, True)
        End If
      End If
    End Sub
  End Class
End Namespace