How to set the view extent when selecting multiple features

2k views Asked by At

I am trying to figure out what is the best way to set the active view extent to be able to view all the selected features on the Arc Map.

1.The Map has only one layer. 2.The features are filtered with attributes.

Below is the code where I tried,stuck with the part where I can loop through the features in the feature selection set but not able to set the active view extent to zoom to show all the selected ones.

P.S: this is similar to doing the right click on the attributes table and selecting multiple rows and doing a Zoom to selected.

private void ZoomToMultipleDGVSelection(List<int> selectedRightIDs)
{

    IMxDocument pMxDoc = ArcMap.Document;
    IMap pMap = (IMap)pMxDoc.ActiveView;

    ESRI.ArcGIS.Carto.ILayer layer = GetLayersClass.GetFieldBoundaryLayer;
    if (layer is ESRI.ArcGIS.Carto.IGroupLayer)
    {

        ESRI.ArcGIS.Carto.IGroupLayer groupLayer = layer as    ESRI.ArcGIS.Carto.IGroupLayer;
        ICompositeLayer pCompositeLayer = layer as ICompositeLayer;
        int layers = pCompositeLayer.Count;
        ILayer pLayer = pCompositeLayer.Layer[0];
        IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;

        IFeatureSelection pFeatureSelection = (IFeatureSelection)pLayer;
        ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet;

        IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
        IQueryFilter pFilter = new QueryFilterClass();

        foreach(int ID  in selectedRightIDs)
        {
        pFilter.WhereClause = "RightID = " + ID.ToString();

        IFeatureCursor pFeatureCursor = pFeatureClass.Search(pFilter, false);
        IFeature pFeature = pFeatureCursor.NextFeature();

        pFeatureSelection.Add(pFeature);

        }


        //if (pFeature == null)
        //{
        //    System.Windows.Forms.MessageBox.Show("This section doesn't exist");
        //    return;
        //}


        IGeometry pgeom = (IGeometry)pFeature.Shape;
        pMap.SelectByShape(pgeom, null, false);
        IEnvelope pEnv = pgeom.Envelope;
        pMxDoc.ActiveView.Extent = pEnv;
        pMxDoc.ActiveView.Refresh();
    }
}

Thank you in advance!

2

There are 2 answers

0
JTran On

The simpler way would probably be

IDocument d = ArcMap.Document as IDocument;
IUID ud = new UIDClass();
ud.Value = "esriArcMapUI.ZoomToSelectedCommand";
ICommandItem ci = d.CommandBars.Find(ud);
ci.Execute();

Otherwise, you can add all your Shape to a GeometryBag and get the extent (envelope) of that to pass to the ActiveView.

0
pvdev On

Add your geometry objects to a geometrybag and then call

pMxDoc.ActiveView.Extent = pGeometryBag.envelope

edit: just noticed the previous answer already suggested that, silly me!