Convert part to feature command
ConvertPart.cs
// Copyright 2012 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.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;

namespace Convert_Part_To_FeatureCS
{
  /// <summary>
  /// The command must be in the edit sketch context menu 
  /// and converts a selected part to its own feature.
  /// </summary>
  [Guid("f766682c-3a1c-4b97-8354-af3c12706e1f")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("Convert_Part_To_FeatureCS.ConvertPart")]
  public sealed class ConvertPart : BaseCommand
  {
    #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            SketchMenuCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            SketchMenuCommands.Unregister(regKey);

        }

        #endregion

    #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);
        }
        #endregion

    private IApplication m_application;

    public ConvertPart()
    {
      base.m_category = "Developer Samples";
      base.m_caption = "Convert Part to Feature";
      base.m_message = "Command must be run from Edit Sketch Context Menu only";
      base.m_toolTip = "Creates a new feature from a part";
      base.m_name = "Convert Part to Feature";
    }

    public IEditor m_editor;
    public IEditSketch m_editSketch;
    public IFeatureClass featureClass;
    public IMap map = new Map();       

    #region Overriden Class Methods
      
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
      if (hook == null)
        return;

      m_application = hook as IApplication;
      if (m_application == null)
        return;

      UID uID = new UID();
      uID.Value = "esriEditor.Editor";
      m_editor = m_application.FindExtensionByCLSID(uID) as IEditor;

      if (m_editor == null)
        return;

      m_editSketch = m_editor as IEditSketch;
    }   
    /// <summary>
    /// Command Enabled called to determine enable status.
    /// </summary>
    public override bool Enabled
    {
      get
      {
        if (m_editor == null)
          return false;

        if (!m_editSketch.Geometry.IsEmpty && m_editor.SelectionCount == 1)
        {
          return true;
        }
        else
        {
          return false;
        }
      }
    }
    /// <summary>
    /// After enabling the edit sketch of a multipart feature right click on the part to be converted and
    /// select Convert Part to Feature, to make the part it's own feature.
    /// </summary>
      public override void OnClick()
      {
          IActiveView activeView;

          if (m_editor == null)
              return;

          m_editor.StartOperation();

          //if the sketch only has one part to begin with - exit.
          IGeometryCollection geometryCollection = m_editSketch.Geometry as IGeometryCollection;
          {
              if (geometryCollection.GeometryCount == 1)
              {
                  return;
              }

              //get the part, this is the one the user right-clicked on.
              int Part = m_editSketch.Part;

              IEnumFeature enumFeature = m_editor.EditSelection;
              enumFeature.Reset();

              IFeature origFeature = enumFeature.Next();

              if (origFeature == null)
              {
                  m_editor.AbortOperation();
                  return;
              }

              featureClass = origFeature.Class as IFeatureClass;
              IFeature newFeature = featureClass.CreateFeature();

              geometryCollection = origFeature.Shape as IGeometryCollection;
              IGeometry origPartGeometry = geometryCollection.get_Geometry(Part);

              //delete the original part.
              geometryCollection.RemoveGeometries(Part, 1);
              geometryCollection.GeometriesChanged();
              origFeature.Shape = geometryCollection as IGeometry;
              origFeature.Store();

              //check the type of geometry.
              IPolygon polygon = new PolygonClass();
              IPolyline polyline = new PolylineClass();
              IMultipoint multiPoint = new MultipointClass();
              object Missing = Type.Missing;

              //make sure the new geometry is z aware, set a flag for later use.
              IGeometryDef fcGeoDef = CheckZGeometryDef(featureClass);
              //if the feature class is z aware set the flag to true.
              Boolean isZAware = true;
              IZAware zAware;
              if (fcGeoDef.HasZ == false)
              {
                isZAware = false;
              }
              switch (origPartGeometry.GeometryType)
              {
                case esriGeometryType.esriGeometryRing:
                  if (isZAware == true)
                  {
                    zAware = polygon as IZAware;
                    zAware.ZAware = true;
                  }
                  geometryCollection = polygon as IGeometryCollection;
                  geometryCollection.AddGeometry
                    (origPartGeometry, ref Missing, ref Missing);
                  break;

                case esriGeometryType.esriGeometryPath:
                  if (isZAware == true)
                  {
                    zAware = polyline as IZAware;
                    zAware.ZAware = true;
                  }
                  geometryCollection = polyline as IGeometryCollection;
                  geometryCollection.AddGeometry
                    (origPartGeometry, ref Missing, ref Missing);
                  break;

                case esriGeometryType.esriGeometryPoint:
                  if (isZAware == true)
                  {
                    zAware = multiPoint as IZAware;
                    zAware.ZAware = true;
                  }
                    geometryCollection = multiPoint as IGeometryCollection;
                    geometryCollection.AddGeometry
                    (origPartGeometry, ref Missing, ref Missing);
                  break;

                default:
                  m_editor.AbortOperation();
                  break;
                }

                newFeature.Shape = geometryCollection as IGeometry;

                //copy the attributes of the orig feature the new feature.
                IField field = new FieldClass();
                IFields fields = origFeature.Fields;

                //skip OID and geometry.
                for (int fieldCount = 0; fieldCount < fields.FieldCount; fieldCount++)
                {
                    field = fields.get_Field(fieldCount);

                    if ((field.Type != esriFieldType.esriFieldTypeGeometry) &&
                      (field.Type != esriFieldType.esriFieldTypeOID) && field.Editable)
                    {
                        newFeature.set_Value(fieldCount, origFeature.get_Value(fieldCount));
                    }
                }

                newFeature.Store();

                m_editor.StopOperation("Convert Part to Feature");

                //refresh map according to old and new selections.
                activeView = m_editor.Map as IActiveView;
                activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                m_editor.Map.ClearSelection();

                ILayer featLayer = GetFeatureLayer(newFeature);
                if (featLayer == null)
                    return;

                m_editor.Map.SelectFeature(featLayer, newFeature);
                activeView.PartialRefresh
                  (esriViewDrawPhase.esriViewGeoSelection, null, null);
              }
          }
      //}
    /// <summary>
    /// Function to obtain the feature layer from the selected feature.
    /// </summary>
    public ILayer GetFeatureLayer(IFeature feature)
    {
      if (feature == null)
        return null;

      map = m_editor.Map;

      featureClass = feature.Class as IFeatureClass;

      for (int layerCount = 0; layerCount < map.LayerCount; layerCount++)
      {
        // Search the layers for the layer in question and get the name.
        if (featureClass.AliasName == map.get_Layer(layerCount).Name)
        {
          return map.get_Layer(layerCount);
        }
      }
      return null;
    }

    public IGeometryDef CheckZGeometryDef(IFeatureClass featureClass)
    {
      string shapeFieldName = featureClass.ShapeFieldName;
      IFields fields = featureClass.Fields;
      int geometryIndex = fields.FindField(shapeFieldName);
      IField field = fields.get_Field(geometryIndex);
      IGeometryDef geometryDef = field.GeometryDef;
      return geometryDef;
    } 
  }
}
#endregion