I am trying to get the selected features from axMapControl
and put them in a new feature class. I can get the selected features but when i'm trying to put them in a IFeatureClass and then cast it as IFeatureLayer I get the null reference exception
.
Can any one help?
List<IFeature> features = GetSelectedFeatures(axMapControl1.get_Layer(0) as IFeatureLayer);
IPointArray pa = new PointArrayClass();
for (int i = 0; i < features.Count; i++)
{
p.Add(features[i].ShapeCopy as IPoint);
pa.Add(p[i]);
}
IPolyline pl = new PolylineClass();
pl.FromPoint = pa.Element[0];
pl.ToPoint = pa.Element[1];
pl.ToPoint = pa.Element[2];
pl.SpatialReference = p[0].SpatialReference;
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile(@"C:\", 0);
IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
IFeatureClass FC = CreateFeatureClass("FC", featureWorkspace, pa.get_Element(0).SpatialReference);
IFeatureCursor fCur = FC.Insert(false);
IFeatureBuffer buff = FC.CreateFeatureBuffer();
buff.Shape = pl;
fCur.InsertFeature(buff);
fCur.Flush();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(fCur);
ILayer layer =FC as ILayer;
axMapControl1.AddLayer(layer);
axMapControl1.Refresh();