Common Custom EditorTask
Common_CustomEditorTask_CSharp\CustomEditorTask_CSharp\PolygonToLine.cs
// 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.
// 

namespace CustomEditorTask_CSharp
{
    class PolygonToLine : ESRI.ArcGIS.ADF.ArcGISServer.Editor.Tools.EditorServerCommandAction
    {
        private System.Collections.Generic.List<int> m_featureIDsList = new System.Collections.Generic.List<int>();
        private ESRI.ArcGIS.Geodatabase.IFeatureClass m_destinationFeatureClass;

        // Maintain a list of new polyline features
        public System.Collections.Generic.List<int> Features
        {
            get { return m_featureIDsList; }
        }

        // Reference to feature class for IPolyline layer
        public ESRI.ArcGIS.Geodatabase.IFeatureClass DestinationFeatureClass
        {
            get
            {
                if (m_destinationFeatureClass == null)
                    m_destinationFeatureClass = GetDestinationFeatureClass();
                return m_destinationFeatureClass;
            }
        }

        // Clear the feature ID list during initialization
        protected override bool Init(ESRI.ArcGIS.ADF.ArcGISServer.Editor.Editor editor)
        {
            m_featureIDsList.Clear();
            return base.Init(editor);
        }

        // Implementation code to convert selected polygon features to polylines and store in user defined feature layer. 
        protected override void EditorServerAction()
        {
            // Get the MapDescription for the editor's map and an array containing the IDs of features currently selected
            // for editing.
            ESRI.ArcGIS.ADF.ArcGISServer.MapDescription agsSoapMapDescription = 
                Editor.MapFunctionality.MapDescription;
            int[] featureIDArray = LayerDescription.SelectionFeatures;

            // Make sure there are features selected
            if (featureIDArray != null && featureIDArray.Length > 0)
            {
                // Make sure there is a destination featuer class
                if (DestinationFeatureClass == null)
                    return;

                // Declare ArcGIS COM objects to use in creating the new features
                ESRI.ArcGIS.Geometry.ITopologicalOperator3 agsComTopologicalOperator3 = null;
                ESRI.ArcGIS.Geodatabase.IFeature agsComFeature = null;
                ESRI.ArcGIS.Geodatabase.IFeature agsComNewFeature = null;

                try
                {
                    // Start the edit operation on the editor's workspace
                    this.StartEditOperation();

                    // Iterate through the features referenced by the feature ID array, referencing an ArcGIS COM
                    // feature object for each
                    ESRI.ArcGIS.Geodatabase.IFeatureCursor agsComFeatureCursor = 
                        FeatureLayer.FeatureClass.GetFeatures(featureIDArray, false);
                    agsComFeature = agsComFeatureCursor.NextFeature();
                    while (agsComFeature != null)
                    {
                        agsComTopologicalOperator3 = 
                            (ESRI.ArcGIS.Geometry.ITopologicalOperator3)agsComFeature.ShapeCopy;
                        ESRI.ArcGIS.Geometry.IGeometry agsComGeometry = 
                            (ESRI.ArcGIS.Geometry.IGeometry)agsComTopologicalOperator3;
                        if (!agsComGeometry.IsEmpty)
                        {
                            // 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);
                        }

                        // Get the next feature
                        agsComFeature = agsComFeatureCursor.NextFeature();
                    }

                    // Stop the edit operation on the editor's workspace
                    this.StopEditOperation();

                }
                catch (System.Exception exception)
                {
                    AbortEditOperation(exception);
                }

                if (m_featureIDsList.Count > 0)
                {
                    // Refresh the Map
                    Editor.Map.Refresh();
                }
            }
        }

        // Retreive polyline feature class using drop down list in custom PolygonToLineLayerEditorPanel.
        // Drop down sets a custom property on the CustomEditor called PolylineLayerID.
        private ESRI.ArcGIS.Geodatabase.IFeatureClass GetDestinationFeatureClass()
        {
            int destinationLayerID = ((CustomEditor)Editor).PolylineLayerID;
            if (destinationLayerID == -1)
                return null;

            ESRI.ArcGIS.Carto.IMapServerObjects2 agsComMapServerObjects2 = 
                this.IMapServer as ESRI.ArcGIS.Carto.IMapServerObjects2;
            ESRI.ArcGIS.Carto.IFeatureLayer agsCOMFeatureLayerDestination = 
                (ESRI.ArcGIS.Carto.IFeatureLayer)agsComMapServerObjects2.get_Layer(
                Resource.DataFrame, destinationLayerID);
            return (agsCOMFeatureLayerDestination != null ? agsCOMFeatureLayerDestination.FeatureClass : null);
        }
    }
}