Copying polylines from one layer to another one results in a multi part feature

511
3
04-28-2010 04:56 AM
LuisPenedo
New Contributor
I'm using this code to copy polyline features from one layer to another one but the undisired results is a multipart features.  What am I missing or doing wrong?

public void selectOrphanFeatures(ILayer selLayer, string orphans)
        {
            if (selLayer != null)
            {
                //Get the MxDocument and Map
                if (pMxDoc == null) { pMxDoc = m_app.Document as IMxDocument; }
                if (pMap == null) { pMap = pMxDoc.FocusMap as IMap; }

                String[] linkPVIDs = orphans.Split(',');
                for (int p = 0; p<linkPVIDs.Length; p++)
                {
                    // 5.10.2.18 ----  The orphaned PVIDs records are added to the fallout layer
                    IFeatureClass featureClassOut = OpenFeatureClass(@"d:\", "FALLOUT LAYER");
                    if (featureClassOut == null)
                    {
                        throw new Exception("Cannot Open Output FeatureClass");
                    }

                    IFeatureLayer featureLayer = selLayer as IFeatureLayer;
                    IFeatureClass featureClass = featureLayer.FeatureClass;

                    IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
                    IFeatureWorkspace pFWS = (IFeatureWorkspace)workspaceFactory.OpenFromFile(@"d:\", 0);

                    IFeatureCursor featureCursorInsert = featureClassOut.Insert(true);
                    IFeatureBuffer featureBufferInsert = featureClassOut.CreateFeatureBuffer();
                   
                    IFeatureCursor featCursor = featureClass.Search(null, false);
                    IFeature feature;
                    while ((feature = (IFeature)featCursor.NextFeature()) != null)
                    {
                        if (linkPVIDs

== feature.get_Value(feature.Fields.FindField("LINK_ID")).ToString())


                        {
                            featureBufferInsert.Shape = feature.Shape;
                            // Add all the original feature's fields to the feature buffer.
                            AddFields(featureBufferInsert, feature);
                            // Insert the feature into the cursor.
                            featureCursorInsert.InsertFeature(featureBufferInsert);
                            featureCursorInsert.Flush();
                        }
                    }
                }
            }
        }
0 Kudos
3 Replies
LuisPenedo
New Contributor
What I am getting is a layer with all features I want to copy right there (that is OK) but when I select one, all of them become selected like if they are a multi part feature.  When I try to explode them with the arcmap advance editing tool, it is not able to do it.  What is wrong?
0 Kudos
JohnHauck
Occasional Contributor II
Not sure what's happening in your AddFields method. Try adding both feature classes to a map and hard coding a subset of your orphan list to see if it will help track down the problem.  Here is the code I tested from your sample code and had no problem:

IMap pMap = m_hookHelper.FocusMap;

String[] linkPVIDs = new String[] { "1", "2", "3" };
for (int p = 0; p<linkPVIDs.Length; p++)
{
    IFeatureLayer fallOutLayer = (IFeatureLayer)pMap.get_Layer(1);
    IFeatureClass featureClassOut = fallOutLayer.FeatureClass;
    IFeatureCursor featureCursorInsert = featureClassOut.Insert(true);
    IFeatureBuffer featureBufferInsert = featureClassOut.CreateFeatureBuffer();

    IFeatureLayer featureLayer = (IFeatureLayer)pMap.get_Layer(0);
    IFeatureClass featureClass = featureLayer.FeatureClass;
    IFeatureCursor featCursor = featureClass.Search(null, false);
    IFeature feature;
    while ((feature = (IFeature)featCursor.NextFeature()) != null)
    {
        if (linkPVIDs

== feature.get_Value(feature.Fields.FindField("Id")).ToString())


        {
            featureBufferInsert.Shape = feature.Shape;
            featureCursorInsert.InsertFeature(featureBufferInsert);
            featureCursorInsert.Flush();
        }
    }
}
0 Kudos
LuisPenedo
New Contributor
Thanks!

That made it.  I don't know why I created a workspace when I already had the layer in the table of contents.

Thank you very much again.

Luis F. Penedo
0 Kudos