Select to view content in your preferred language

How to get selected features on the map for a custom geoprocessing tool

393
1
05-13-2010 08:35 PM
JosephSu
New Contributor
Hello,

I am working on a custom geoprocessing tool that need to obtain the selected feature(s) on a feature in ArcMap.

One of my parameters from IGPFunction2 is IGPFeatureLayer, however, its FIDSet is always null. Is there any way that I can get the selected feature from the this point?

Thanks
0 Kudos
1 Reply
JosephSu
New Contributor
Nevermind, I figured it out

if (pGPValue is IGPFeatureLayer)
{
    IFeatureLayer pFeatureLayer = pGPUtilities.DecodeLayer(pGPValue) as IFeatureLayer;
    ISelectionSet pSelectionSet = (pFeatureLayer as IFeatureSelection).SelectionSet;

    if (pSelectionSet != null && pSelectionSet.Count > 0)
    {
        IEnumIDs pEnumIDs = pSelectionSet.IDs;

        for (int j = 0; j < pSelectionSet.Count; ++j)
        {
            IEnvelope pCurrentExtent = pFeatureLayer.FeatureClass.GetFeature(pEnumIDs.Next()).Extent;

            if (pCurrentExtent != null)
            {
                pCurrentExtent.Project(pGeographicCoordinateSystem);
                pInputExtent.Union(pCurrentExtent);
            }
        }
    }
    else
    {
        IQueryFilter pQueryFilter = null;
        int iFeatureCount = pFeatureLayer.FeatureClass.FeatureCount(pQueryFilter);

        for (int j = 0; j < iFeatureCount; ++j)
        {
            IEnvelope pCurrentExtent = pFeatureLayer.FeatureClass.GetFeature(j).Extent;

            if (pCurrentExtent != null)
            {
                pCurrentExtent.Project(pGeographicCoordinateSystem);
                pInputExtent.Union(pCurrentExtent);
            }
        }
    }
}
0 Kudos