Common Custom EditorTask
Common_CustomEditorTask_CSharp\CustomEditorTask_CSharp\ClipFeatures.cs
// Copyright 2011 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 ClipFeatures : ESRI.ArcGIS.ADF.ArcGISServer.Editor.Tools.EditorServerToolAction
    {
        // Maintain a list of feature ids for the clipped features.
        private System.Collections.Generic.List<int> m_featureIDsList = new System.Collections.Generic.List<int>();
        public System.Collections.Generic.List<int> Features
        {
            get { return m_featureIDsList; }
        }

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

        // Implementation for clipping the selected features based on a user defined envelope.
        protected override void EditorServerAction()
        {
            // Cast the base class's input geometry into ArcObjects COM geometry
            ESRI.ArcGIS.Geometry.IEnvelope agsComEnvelope = this.Geometry as ESRI.ArcGIS.Geometry.IEnvelope;

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

            // Get an array containing the IDs of the currently selected features
            int[] featureIDArray = LayerDescription.SelectionFeatures;

            // Make sure features are selected
            if (featureIDArray != null && featureIDArray.Length > 0)
            {
                // Declare ArcGIS COM objects to use in performing the clip operation and updating the
                // features in the database underlying the resource being edited
                ESRI.ArcGIS.Geometry.ITopologicalOperator3 agsComTopologicalOperator3 = null;
                ESRI.ArcGIS.Geodatabase.IFeature agsComFeature = null;

                try
                {
                    // Start the editing operation on the workspace
                    this.StartEditOperation();

                    // Iterate through selected features and clip with user defined envelope
                    ESRI.ArcGIS.Geodatabase.IFeatureCursor agsComFeatureCursor = 
                        FeatureLayer.FeatureClass.GetFeatures(featureIDArray, false);
                    agsComFeature = agsComFeatureCursor.NextFeature();
                    
                    while (agsComFeature != null)
                    {
                        agsComTopologicalOperator3 = (ESRI.ArcGIS.Geometry.ITopologicalOperator3)agsComFeature.ShapeCopy;
                        agsComTopologicalOperator3.Clip(agsComEnvelope);
                        ESRI.ArcGIS.Geometry.IGeometry agsComGeometryClipResult = 
                            (ESRI.ArcGIS.Geometry.IGeometry)agsComTopologicalOperator3;

                        if (!agsComGeometryClipResult.IsEmpty)
                        {
                            agsComFeature.Shape = agsComGeometryClipResult;
                            agsComFeature.Store();

                            // Keep track of the clipped features
                            m_featureIDsList.Add(agsComFeature.OID);
                        }
                        agsComFeature = agsComFeatureCursor.NextFeature();
                    }

                    // Stop the editing operation on the workspace.
                    this.StopEditOperation();
                }
                catch (System.Exception exception)
                {
                    // Base class will handle clean up if exception occurs during edit operation.
                    AbortEditOperation(exception);
                }

                // Make sure there were features to update before refreshing
                if (m_featureIDsList.Count > 0)
                {
                    // Call Refresh to update the Editor, Map, vertices, etc. with the new selection set
                    this.Refresh(m_featureIDsList, true);
                }
            }
        }
    }
}