1

I am trying to figure out how to set the active view extent to be able to view all the selected features on the ArcMap.

  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.

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();
        }
    }
2

Since you are already touching each feature that will be added to the selection set you can union each geometry to a new envelope and then set the active view to this new envelope.

Prior to the loop:

IEnvelope selectionFootprint = new EnvelopeClass();

Inside the loop:

selectionFootprint.Union(pFeature.ShapeCopy.Envelope);

After the loop:

ArcMap.Document.ActiveView.Extent = selectionFootprint;
ArcMap.Document.ActiveView.Refresh();

There is also the IGeometryFactory.CreateGeometryFromEnumerator method but i'm not certain it works with all geometry types.

User's edited code:

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;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IQueryFilter pFilter = new QueryFilterClass();
            IEnvelope pEnv = new EnvelopeClass();

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

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

                pFeatureSelection.Add(pFeature);
                pEnv.Union(pFeature.ShapeCopy.Envelope);

                IGeometry pgeom = (IGeometry)pFeature.Shape;
                pMap.SelectByShape(pgeom, null, false);

            }

            pMxDoc.ActiveView.Extent = pEnv;
            pMxDoc.ActiveView.Refresh();
        }
    }
  • I have edited the my code as shown above and works great. The problem is that it would only select one feature which is not what i wanted to achieve. – sudhansh Dec 2 '13 at 19:58
  • I tried to do that but no luck either I know that I have to use the select by attribute rather than select by shape but I had problem with select by attribute(feature) which would not highlight the selections. – sudhansh Dec 2 '13 at 20:38
4

You could use the build-in command to Zoom to Selected features. This is from the Find Command and Execute Snippet

public void FindCommandAndExecute(ESRI.ArcGIS.Framework.IApplication application, System.String commandName)
{
  ESRI.ArcGIS.Framework.ICommandBars commandBars = application.Document.CommandBars;
  ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
  uid.Value = commandName; // Example: "esriArcMapUI.MxSelectionMenu" or "{AB073B49-DE5E-11D1-AA80-00C04FA37860}"
  ESRI.ArcGIS.Framework.ICommandItem commandItem = commandBars.Find(uid, false, false);

  if (commandItem != null)
    commandItem.Execute();
}
  • I am new to arcmap and I am not able to figure out how to use this snippet.Thanks! – sudhansh Dec 2 '13 at 19:59
  • This is a function where you supply a reference to the application and a string representing the command you want to run. – kenbuja Dec 2 '13 at 20:41
  • Agree, this method takes less lines of code. I anwsered a similar question on this before, gis.stackexchange.com/questions/20681/… – artwork21 Dec 6 '13 at 14:39
1

per kenbuja's method... add this line.

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

A list of ArcMap ids are here (the ones I've been using still works in 10 and 10.1). http://resources.esri.com/help/9.3/ArcGISDesktop/com/shared/desktop/reference/ArcMapIds.htm

1

Simply get the activeview envelope and CenterAt() the point shape you want

var point = (IPoint)myItem.Shape;
var envelope = theActiveView.Extent;
envelope.CenterAt(point);                    
theActiveView.Extent = envelope;
theActiveView.Refresh();

That's it !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.