1

Hi I have the following code below for zooming into an arcGIS object based on the attribute now all I need is to be able to highlight that area with a select feature (The feature where you right-click on the area on the map and do select feature).

Currently I have an event which will do the zoom.I want to add this select to the same attribute as well.

Thank you in advance!!!

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;

    IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
    IQueryFilter pFilter = new QueryFilterClass();
    pFilter.WhereClause = "RightID = '" + selectedRightID.ToString() + "'";

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

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

    IApplication m_application = ArcMap.Application;
    IMxDocument pMxDoc = (IMxDocument)m_application.Document;
    IActiveView pActiveView = (IActiveView)pMxDoc.FocusMap;

    IEnvelope pEnv = pFeature.Shape.Envelope;
    pEnv.Expand(1.1, 1.1, true);

    pActiveView.Extent = pEnv;

    pActiveView.Refresh();

I tried by adding this code which I think will add the particular feature to the selection. but no luck with that as well.

IFeatureSelection pfeatSelect = pFeatureLayer as IFeatureSelection;
pfeatSelect.Add(pFeature);
George Brighton
  • 5,131
  • 9
  • 27
  • 36
sss111
  • 349
  • 1
  • 7
  • 27
  • 1
    IMap.SelectFeature(pLayer,pFeature) http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/SelectFeature_Method/001200000mm6000000/ or IMap.FeatureSelection = (ISelection)pfeatSelect; http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/FeatureSelection_Property/001200000m9n000000/ – Michael Stimson Dec 08 '14 at 22:05

2 Answers2

1

If I understand you correctly, all you need is this:

IFeatureSelection featSelect = pFeatureLayer as IFeatureSelection;
featSelect.SelectFeatures(pFilter, esriSelectionResultEnum.esriSelectionResultNew, false);

This will select all Features that match your filter.

germi
  • 4,628
  • 1
  • 21
  • 38
  • I did try this code and did a IFeatureSelection before the "if" condition but still not able to get it highlighted(when I do it interactively with the right-click it is highlighted with a blue border). – sss111 Nov 05 '13 at 16:18
  • Is there another approach to achieve this?? – sss111 Nov 06 '13 at 16:07
  • @sss111, Here is the solution for the problem using "spatialfilter" on http://www.gisremotesensing.com/2017/04/code-snippet-select-feature-polygons-on.html – EvilInside Apr 14 '17 at 22:25
0

This solution is working for me

            IFeatureLayer PFeaLayer = (IFeatureLayer)pLayer;
            IFeatureSelection PFeaSel = (IFeatureSelection)PFeaLayer;
            int OIDIndex = PFeature.Fields.FindField("OBJECTID");
            PFeaSel.SelectionSet.Add(Convert.ToInt32(PFeature.get_Value(OIDIndex)));
            ISelectionSet PFeaSelSet = PFeaSel.SelectionSet;
            IEnumGeometry pEnumGeom = new EnumFeatureGeometryClass();
            IEnumGeometryBind pEnumGeomBind = (IEnumGeometryBind)pEnumGeom;
            pEnumGeomBind.BindGeometrySource(null, PFeaSelSet);
            IGeometryFactory pGeomFactory = new GeometryEnvironmentClass();
            IGeometry pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom);
            IMxDocument pMxDoc = ArcMap.Document as IMxDocument;
            IActiveView activeView = pMxDoc.ActiveView;
            double midX = (pGeom.Envelope.XMax + pGeom.Envelope.XMin) / 2;
            double midY = (pGeom.Envelope.YMax + pGeom.Envelope.YMin) / 2;
            IPoint pPoint = new PointClass();
            pPoint.SpatialReference = pGeom.Envelope.SpatialReference;
            pPoint.PutCoords(midX, midY);
            pPoint.Project(activeView.Extent.SpatialReference);
            IEnvelope pCurrentEnvelope = activeView.Extent;
            pCurrentEnvelope.CenterAt(pPoint);
            activeView.Extent = pCurrentEnvelope;
            activeView.Refresh();
  • Thank you for your contribution to the community. This may be a correct answer, but it’d be useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who may not be familiar with the syntax. Further, it can help reduce the need for follow-up questions. – Jeremy Caney May 06 '20 at 07:18