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

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ADF.ArcGISServer;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.ADF.ArcGISServer.Editor.Tools;
using ESRI.ArcGIS.ADF.ArcGISServer.Editor;
using ESRI.ArcGIS.Carto;

namespace CustomEditorTask_CSharp
{
    class PolygonToLine : EditorServerCommandAction
    {
        private List<int> features = new List<int>();
        private IFeatureClass destinationFeatureClass;

        // Maintain a list of new polyline features
        public List<int> Features
        {
            get { return features; }
        }

        // Reference to feature class for IPolyline layer
        public IFeatureClass DestinationFeatureClass
        {
            get
            {
                if (destinationFeatureClass == null)
                    destinationFeatureClass = GetDestinationFeatureClass();
                return destinationFeatureClass;
            }
        }

        // Clear the feature id list during initialization
        protected override bool Init(Editor editor)
        {
            features.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()
        {
            ESRI.ArcGIS.ADF.ArcGISServer.MapDescription agsSOAPMapDescription = 
                Editor.MapFunctionality.MapDescription;
            int[] fidSet = LayerDescription.SelectionFeatures;

            if (fidSet != null && fidSet.Length > 0)
            {
                if (DestinationFeatureClass == null)
                    return;

                ITopologicalOperator3 agsCOMTopoOp3 = null;
                IFeature agsCOMFeature = null;
                IFeature agsCOMNewFeature = null;

                try
                {
                    StartEditOperation();
                    IFeatureCursor agsCOMFeatureCursor = FeatureLayer.FeatureClass.GetFeatures(fidSet, false);
                    agsCOMFeature = agsCOMFeatureCursor.NextFeature();
                    while (agsCOMFeature != null)
                    {
                        agsCOMTopoOp3 = (ITopologicalOperator3)agsCOMFeature.ShapeCopy;
                        IGeometry agsCOMGeometry = (IGeometry)agsCOMTopoOp3;
                        if (!agsCOMGeometry.IsEmpty)
                        {
                            agsCOMNewFeature = DestinationFeatureClass.CreateFeature();
                            // Store the boundary of selected polygon 
                            agsCOMNewFeature.Shape = agsCOMTopoOp3.Boundary;
                            agsCOMNewFeature.Store();

                            // Keep track of converted features
                            features.Add(agsCOMNewFeature.OID);
                        }

                        agsCOMFeature = agsCOMFeatureCursor.NextFeature();
                    }

                    StopEditOperation();

                }
                catch (Exception e)
                {
                    AbortEditOperation(e);
                }

                if (features.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 Editor, PolylineLayerID.
        private IFeatureClass GetDestinationFeatureClass()
        {
            int destinationLayerID = ((CustomEditor)Editor).PolylineLayerID;
            if (destinationLayerID == -1)
                return null;

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