How to Select Feature by XY Location and Highlight it in ArcMap 9.3 programmatically?

3341
12
05-18-2011 12:46 AM
Occasional Contributor II
Greetings,

I hope to find a solution to the following questions that I really need in my project:

  1. How to find the feature under the mouse click (XY Coodinates) from a feature layer in ArcMap 9.3?

  2. How to highlight (flash) a selected feature in ArcMap 9.3 using VB.Net?

Please advise....
Reply
0 Kudos
12 Replies
Occasional Contributor
Hi,

sounds like you want to make use of the identify method?

so, I think I did something equal but for c#.
The only thing is that I did not implement a method to make a mouseclick.
In my prog when clicking the Add-In Button, the mouse coordinates where read instead of waiting for a second mousclick.
So that is what you had to implement on your own.
But when you did, may you want to post it here so can agree your mous-click-code.

public void WorkIdentify()
        {
           IMxDocument doc =  ArcMap.Application.Document as IMxDocument;
            IActiveView activeView = doc.ActiveView;
            IPoint mouseLocation = GetScreenCoordinatesFromMapCoorindates(doc.CurrentLocation, activeView);
            System.Int32 x = (System.Int32) mouseLocation.X;
            System.Int32 y = (System.Int32) mouseLocation.Y;
           
            if (activeView == null)
            {
                return;
            }
            ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap;
            ESRI.ArcGIS.CartoUI.IIdentifyDialog identifyDialog = new ESRI.ArcGIS.CartoUI.IdentifyDialogClass();
            identifyDialog.Map = map;

            //Clear the dialog on each mouse click
            identifyDialog.ClearLayers();
            ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

            ESRI.ArcGIS.Display.IDisplay display = screenDisplay; // Implicit Cast
            identifyDialog.Display = display;

            ESRI.ArcGIS.CartoUI.IIdentifyDialogProps identifyDialogProps = (ESRI.ArcGIS.CartoUI.IIdentifyDialogProps)identifyDialog; // Explicit Cast
            ESRI.ArcGIS.Carto.IEnumLayer enumLayer = identifyDialogProps.Layers;
            enumLayer.Reset();

            ESRI.ArcGIS.Carto.ILayer layer = enumLayer.Next();

            //
            while (!(layer == null))
            {
                identifyDialog.AddLayerIdentifyPoint(layer, x, y);

                layer = enumLayer.Next();
            }
            identifyDialog.Show();
        }
Reply
0 Kudos
Occasional Contributor II
Hello Kaspatoo,

I think you misunderstood my questions below, but it was a nice consideration from you.

Please note the following scenario in order to be more clear with my questions:

  • I have four layers loaded in ArcMap 9.3 and the 3rd one called "X"

  • I generated a form that contain a datagridview in order to preview the details of the selected X feature when a user click on the map. (same functionality as Identify tool)

  • I want to get the "OBJECT ID" of the X feature that lies under the xy of the mouse click  (polygon feature that covers the location of the mouse click) in order to query the details of the this X feature from my personal datatable and not from the layer in arcmap.



I think it is possible to get the "Object Id" of the feature located under the mouse click. Isn`t it?

The second think is that: I want to flash the selected feature in ArcMap programmtically, So how would be the code to do that?

Please help...
Reply
0 Kudos
New Contributor
Hello Kaspatoo,

I think you misunderstood my questions below, but it was a nice consideration from you.

Please note the following scenario in order to be more clear with my questions:

  • I have four layers loaded in ArcMap 9.3 and the 3rd one called "X"

  • I generated a form that contain a datagridview in order to preview the details of the selected X feature when a user click on the map. (same functionality as Identify tool)

  • I want to get the "OBJECT ID" of the X feature that lies under the xy of the mouse click  (polygon feature that covers the location of the mouse click) in order to query the details of the this X feature from my personal datatable and not from the layer in arcmap.



I think it is possible to get the "Object Id" of the feature located under the mouse click. Isn`t it?

The second think is that: I want to flash the selected feature in ArcMap programmtically, So how would be the code to do that?

Please help...



HI,
I'm not sure about your first question but here is the code to Flash the selected feature in ArcMap (C#):
Add the following Reference to your project:

using System.Runtime.InteropServices;

namespace Project1
{
class Class1
{
        [DllImport("kernel32.dll")]
        static extern void Sleep(uint dwMilliseconds);

public void FlashFeature(IFeature Feature, IMxDocument MxDoc)
        {
           
            try
            {
               
                MxDoc.ActiveView.ScreenDisplay.StartDrawing(0, (short)(esriScreenCache.esriNoScreenCache));

                switch (Feature.Shape.GeometryType)
                {
                    case (esriGeometryType.esriGeometryPolyline):
                        {
                            FlashLine(MxDoc.ActiveView.ScreenDisplay, Feature.Shape);
                            break;
                        }
                    case (esriGeometryType.esriGeometryPolygon):
                        {
                            FlashPolygon(MxDoc.ActiveView.ScreenDisplay, Feature.Shape);
                            break;
                        }
                    case (esriGeometryType.esriGeometryPoint):
                        {
                            FlashPoint(MxDoc.ActiveView.ScreenDisplay, Feature.Shape);
                            break;
                        }

                      
                      
                }
                MxDoc.ActiveView.ScreenDisplay.FinishDrawing();
            }
            catch (Exception Err)
            {
                MessageBox.Show("Error : " + Err.Message);

            }     

        }

public void FlashLine(IScreenDisplay Display, IGeometry Geometry)
        {

          
            ISimpleLineSymbol LineSymbol;
            ISymbol Symbol;
            IRgbColor RgbColor;

            try
            {
                LineSymbol = new SimpleLineSymbolClass();
                LineSymbol.Width = 4;

                RgbColor = new RgbColorClass();
                RgbColor.Green = 128;

                Symbol = (ISymbol)LineSymbol;
                Symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;

                Display.SetSymbol((ISymbol)(LineSymbol));
                Display.DrawPolyline(Geometry);

                Sleep(300);

                Display.DrawPolyline(Geometry);
            }
            catch (Exception Err)
            {
                LogFile.WriteLog("Error at : " + Err.Message);

            }

        }

public void FlashPolygon(IScreenDisplay Display, IGeometry Geometry)
        {
            ISimpleFillSymbol FillSymbol;
            ISymbol Symbol;
            IRgbColor RgbColor;
     
            try
            {
                FillSymbol = new SimpleFillSymbolClass();
                FillSymbol.Outline = null;

                RgbColor = new RgbColorClass();
                RgbColor.Green = 128;

                Symbol = (ISymbol)FillSymbol;
                Symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;

                Display.SetSymbol((ISymbol)(FillSymbol));
                Display.DrawPolygon(Geometry);
                Sleep(300);

                Display.DrawPolygon(Geometry);
            }
            catch (Exception Err)
            {
                LogFile.WriteLog("Error at : " + Err.Message);

            }
         
        }
public void FlashPoint(IScreenDisplay Display, IGeometry Geometry)
        {
            ISimpleMarkerSymbol MarkerSymbol;
            ISymbol Symbol;
            IRgbColor RgbColor;
       
            try
            {
                MarkerSymbol = new SimpleMarkerSymbolClass();
                MarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;

                RgbColor = new RgbColorClass();
                RgbColor.Green = 128;

                Symbol = (ISymbol)MarkerSymbol;
                Symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;

                Display.SetSymbol((ISymbol)MarkerSymbol);
                Display.DrawPoint(Geometry);
                Sleep(300);
                Display.DrawPoint(Geometry);
               
            }
            catch (Exception Err)
            {
                LogFile.WriteLog("Error : " + Err.Message);

            }          

        }
}
}
Reply
0 Kudos
Regular Contributor II
If your layer X references a featureclass then it is featureLayer class. Featurelayer classes implement the IIdentify interface.  You can use it to call the identify method with the point you clicked.  This returns an array of IIdentifyObjs, the IIdentifyObj has a flash method.  In the case of a featurelayer, the IdentifyObjs should be FeatureIdentifyObjects, IFeatureIdentifyObj has a reference to the feature, which  has the objectid.
Reply
0 Kudos
Occasional Contributor II
hello,

Thank you Syamarth very much.
I change the your code into Vb.Net syntax and it was very helpful..

As for Mr Agray1,
Can you please show me a sample code how to do what are you talking about...

Thanks you guys...
Reply
0 Kudos
New Contributor III
Hi,
This is how you will zoom to X, Y:

public static void CaptureMapCoordinates(int x, int y)
        {
            // get the map coordinates from the screen coordinates
            IPoint pScreenPoint = new ESRI.ArcGIS.Geometry.Point();
            IPoint pMapPoint = new ESRI.ArcGIS.Geometry.Point();
            IEnvelope pEnv = new EnvelopeClass();
                      
            pScreenPoint.X = x;
            pScreenPoint.Y = y;

            pMapPoint = GetMapCoordinatesFromScreenCoordinates(pScreenPoint, pActiveView);

            pEnv = pActiveView.Extent;
            pEnv.CenterAt(pMapPoint);

            pActiveView.Extent = pEnv;
            pActiveView.Refresh();
           
        }

        private static IPoint GetMapCoordinatesFromScreenCoordinates(IPoint pScreenPoint, IActiveView pActiveView)
        {
            IScreenDisplay pScreenDisplay;
            IDisplayTransformation pDisplayTransformation;

            if (pScreenPoint == null || pScreenPoint.IsEmpty || pActiveView == null)
            {
                return null;
            }

            pScreenDisplay = pActiveView.ScreenDisplay;
            pDisplayTransformation = pScreenDisplay.DisplayTransformation;

            return pDisplayTransformation.ToMapPoint((int)pScreenPoint.X, (int)pScreenPoint.Y);
        }


Hope it helps!
Reply
0 Kudos
Regular Contributor II
This sample uses IIdentify and pretty much all the objects you would need.

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/0001000004t5000000.ht...
Reply
0 Kudos
Occasional Contributor II
Thanks agray1,

Your reply is very appreciated and very helpfull..

Thanks again guys, I got waht I want....really thanks
Reply
0 Kudos
Occasional Contributor II
Thanks agray1,

Your reply is very appreciated and very helpfull..

Thanks again guys, I got what I want....really thanks
Reply
0 Kudos