void IEditTask.OnFinishSketch() { if (m_editSketch == null) { return; } bool HasCutPolygons = false; //Change the cursor to be hourglass shape. System.Windows.Forms.Cursor.Current = Cursors.WaitCursor; try { //Get the geometry that performs the cut from the edit sketch. IGeometry cutGeometry = m_editSketch.Geometry; //The sketch geometry is simplified to deal with a multi-part sketch as well //as the case where the sketch loops back over itself. ITopologicalOperator2 topoOperator = cutGeometry as ITopologicalOperator2; topoOperator.IsKnownSimple_2 = false; topoOperator.Simplify(); //Get ready to invalidate the features. IInvalidArea refreshArea = new InvalidAreaClass(); refreshArea.Add(cutGeometry.Envelope); //Create the spatial filter to search for features in the target feature class. //The spatial relationship we care about is whether the interior of the line //intesects the interior of the polygon. ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = m_editSketch.Geometry; spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; //Find the polygon features that cross the sketch. IFeatureClass featureClass = m_editLayer.CurrentLayer.FeatureClass; IFeatureCursor featureCursor = featureClass.Search(spatialFilter, false); //Only do work if there are features that intersect the edit sketch. IFeature origFeature = featureCursor.NextFeature(); if (origFeature != null) { //Check the first feature to see if it is ZAware and if it needs to make the //cut geometry ZAware. IZAware zAware = origFeature.Shape as IZAware; if (zAware.ZAware) { zAware = cutGeometry as IZAware; zAware.ZAware = true; } //The result selection set will be the new features created. This means //we need to add the currently selected features to the refresh area so //they are correctly invalidated. if (m_editor.SelectionCount > 0) { IEnumFeature selectedFeatures = m_editor.EditSelection; selectedFeatures.Reset(); IFeature selectedFeature = selectedFeatures.Next(); while (selectedFeature != null) { refreshArea.Add(selectedFeature); selectedFeature = selectedFeatures.Next(); } } //Initialize the selection set for the new features. IFeatureSelection featureSelect = m_editLayer.CurrentLayer as IFeatureSelection; featureSelect.Clear(); //Cache the new features created. ISelectionSet selectionSet = featureSelect.SelectionSet; //Start an edit operation so we can have undo/redo. m_editor.StartOperation(); //Cycle through the features, cutting with the sketch. while (origFeature != null) { try { //Add the feature to the invalidate object. refreshArea.Add(origFeature); //Split the feature. Use the IFeatureEdit::Split method which ensures //the attributes are correctly dealt with. IFeatureEdit featureEdit = origFeature as IFeatureEdit; //Set to hold the new features that are created by the Split. ISet newFeaturesSet = featureEdit.Split(cutGeometry); //New features have been created, loop through the set and flash //each feature and get it's OID for the final selection set. if (newFeaturesSet != null) { newFeaturesSet.Reset(); //A list to hold the selected features' OIDs. List <int> iOIDList = new List <int>(); for (int featureCount = 0; featureCount < newFeaturesSet.Count; featureCount++) { HasCutPolygons = true; //Flash the new features. IFeature newFeature = newFeaturesSet.Next() as IFeature; FlashGeometry(newFeature.Shape, m_editor.Display); //Add the feature to the new selection set iOIDList.Add(newFeature.OID); } int[] iOIDArray = iOIDList.ToArray(); selectionSet.AddList(newFeaturesSet.Count, ref iOIDArray[0]); } } catch (COMException) { } finally { //Continue to work on the next feature if it fails to split the current one. origFeature = featureCursor.NextFeature(); } } //If any polygons were cut, invalidate the display and stop the edit operation. if (HasCutPolygons) { //Clear the map's selection. m_mxDoc.FocusMap.ClearSelection(); //Select the new features. featureSelect.SelectionSet = selectionSet; //Refresh the display. refreshArea.Display = m_editor.Display; refreshArea.Invalidate((short)esriScreenCache.esriAllScreenCaches); //Complete the edit operation. m_editor.StopOperation("Cut Polygons Without Selection"); //As we changed the selection set, the map has no idea we did this. //Fire the event to inform others the selection has changed. featureSelect.SelectionChanged(); } else { m_editor.AbortOperation(); } } } catch (Exception e) { MessageBox.Show("Unable to perform the cut task.\n" + e.ToString()); //In the event of an error, abort the operation. m_editor.AbortOperation(); } finally { //Change the cursor shape to default. System.Windows.Forms.Cursor.Current = Cursors.Default; } }