Common Custom EditorTask
Common_CustomEditorTask_VBNet\CustomEditorTask_VBNet\PolygonToLine.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 PolygonToLine
    Inherits ESRI.ArcGIS.ADF.ArcGISServer.Editor.Tools.EditorServerCommandAction
    Private m_featureIDsList As System.Collections.Generic.List(Of Integer) = New System.Collections.Generic.List(Of Integer)()
    Private m_destinationFeatureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass

    ' Maintain a list of new polyline features
    Public ReadOnly Property Features() As System.Collections.Generic.List(Of Integer)
      Get
        Return m_featureIDsList
      End Get
    End Property

    ' Reference to feature class for IPolyline layer
    Public ReadOnly Property DestinationFeatureClass() As ESRI.ArcGIS.Geodatabase.IFeatureClass
      Get
        If m_destinationFeatureClass Is Nothing Then
          m_destinationFeatureClass = GetDestinationFeatureClass()
        End If
        Return m_destinationFeatureClass
      End Get
    End Property

    ' Clear the feature ID list during initialization
    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 code to convert selected polygon features to polylines and store in user defined feature layer. 
    Protected Overrides Sub EditorServerAction()
      ' Get the MapDescription for the editor's map and an array containing the IDs of features currently selected
      ' for editing.
      Dim agsSoapMapDescription As ESRI.ArcGIS.ADF.ArcGISServer.MapDescription = Editor.MapFunctionality.MapDescription
      Dim featureIDArray As Integer() = LayerDescription.SelectionFeatures

      ' Make sure there are features selected
      If Not featureIDArray Is Nothing AndAlso featureIDArray.Length > 0 Then
        ' Make sure there is a destination featuer class
        If DestinationFeatureClass Is Nothing Then
          Return
        End If

        ' Declare ArcGIS COM objects to use in creating the new features
        Dim agsComTopologicalOperator3 As ESRI.ArcGIS.Geometry.ITopologicalOperator3 = Nothing
        Dim agsComFeature As ESRI.ArcGIS.Geodatabase.IFeature = Nothing
        Dim agsComNewFeature As ESRI.ArcGIS.Geodatabase.IFeature = Nothing

        Try
          ' Start the edit operation on the editor's workspace
          Me.StartEditOperation()

          ' Iterate through the features referenced by the feature ID array, referencing an ArcGIS COM
          ' feature object for each
          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)
            Dim agsComGeometry As ESRI.ArcGIS.Geometry.IGeometry = CType(agsComTopologicalOperator3, ESRI.ArcGIS.Geometry.IGeometry)
            If (Not agsComGeometry.IsEmpty) Then
              ' Create a new feature in the destination feature class to store the converted geometry
              agsComNewFeature = DestinationFeatureClass.CreateFeature()

              ' Store the boundary of selected polygon in the new feature
              agsComNewFeature.Shape = agsComTopologicalOperator3.Boundary
              agsComNewFeature.Store()

              ' Keep track of converted features
              m_featureIDsList.Add(agsComNewFeature.OID)
            End If

            ' Get the next feature
            agsComFeature = agsComFeatureCursor.NextFeature()
          Loop

          ' Stop the edit operation on the editor's workspace
          Me.StopEditOperation()

        Catch exception As System.Exception
          AbortEditOperation(exception)
        End Try

        If m_featureIDsList.Count > 0 Then
          ' Refresh the Map
          Editor.Map.Refresh()
        End If
      End If
    End Sub

    ' Retreive polyline feature class using drop down list in custom PolygonToLineLayerEditorPanel.
    ' Drop down sets a custom property on the CustomEditor called PolylineLayerID.
    Private Function GetDestinationFeatureClass() As ESRI.ArcGIS.Geodatabase.IFeatureClass
      Dim destinationLayerID As Integer = (CType(Editor, CustomEditor)).PolylineLayerID
      If destinationLayerID = -1 Then
        Return Nothing
      End If

      Dim agsComMapServerObjects2 As ESRI.ArcGIS.Carto.IMapServerObjects2 = TryCast(Me.IMapServer, ESRI.ArcGIS.Carto.IMapServerObjects2)
            Dim agsCOMFeatureLayerDestination As ESRI.ArcGIS.Carto.IFeatureLayer = CType(agsComMapServerObjects2.Layer(Resource.DataFrame, destinationLayerID), ESRI.ArcGIS.Carto.IFeatureLayer)
      If Not agsCOMFeatureLayerDestination Is Nothing Then
        Return (agsCOMFeatureLayerDestination.FeatureClass)
      Else
        Return (Nothing)
      End If
    End Function
  End Class
End Namespace