How I can get object of the map document  when I click on it?

779
5
02-23-2011 09:28 PM
AnatoliiTerentiev
Occasional Contributor III
How I can in arcgis Engine get the object on wich I click ?
How I can get X,Y coordinates of that object (using shape field?)?
Thanks for any advice or reference!
0 Kudos
5 Replies
TomGiles
New Contributor
How I can in arcgis Engine get the object on wich I click ?
How I can get X,Y coordinates of that object (using shape field?)?
Thanks for any advice or reference!



Hi, this might not be perfect, but may be a start for you.  The first function gets the selected feature (only one allowed) and stores it in pFeat. The second function is passed pFeat, and calculates decimal degrees. You can see in the second function that I have set the coord system to use, so you could report the coordinates of To and From points (starting and ending points) in any units you wish. The working coordinate system for this project is always an UTM zone (PCS). Thanks goes out to Neil C. for a previous post that helped me with this!

I apologize, there is no indenting.

Cheers



Public Function Form_Initialize(ByVal m_application As ESRI.ArcGIS.Framework.IApplication) As Boolean
        'MsgBox("Form_Initialize Outside of the try statement")

        Try
            'MsgBox("Form_Initialize Function Inside of the try statement")

            pMxDoc = m_application.Document

            Dim pMap As IMap
            pMap = pMxDoc.FocusMap

            ' Get the selected layer
            Dim pLayer As IGeoFeatureLayer
            pLayer = pMxDoc.SelectedLayer


            ' Make sure a layer was selected
            If pLayer Is Nothing Then
                MsgBox("You must select a Layer to Attribute")
                Close()
                Exit Function
            End If

            ' Get the feature class to get the correct
            ' oid field name for the data type (oid,fid,objectid)
            'Dim pFc As IFeatureClass 'declared up top
            Dim strOIDName As String
            pFc = pLayer.FeatureClass
            strOIDName = pFc.OIDFieldName

            ' Get the selected features from the layer
            Dim pFSel As IFeatureSelection
            pFSel = pLayer
            Dim pSelSet As ISelectionSet
            pSelSet = pFSel.SelectionSet
            'Dim pFCur As IFeatureCursor 'declared up top
            pSelSet.Search(Nothing, False, pFCur)


            ' ensure only 1 feature selected
            If Not pSelSet.Count = 1 Then
                MsgBox("You must select one and only one feature.")
                Close() 'Exit Function
            Else
                'setup the combo boxes
                fillComboBoxes()
                pFeat = pFCur.NextFeature
                loadSegment(pFeat)

            End If

        Catch ex As Exception
            MsgBox("Exception in Form_Initialize")
            System.Windows.Forms.MessageBox.Show(ex, "Initialize")
            Return False
        End Try
    End Function





Private Sub getLatLong(ByVal pLineF As ESRI.ArcGIS.Geodatabase.IFeature)
        'MsgBox("Function: getLatLong()")

        'MsgBox("pLineF (passed) is of type: " + pLineF.Shape.GeometryType.ToString)
        If Not (pLineF.Shape.GeometryType = esriGeometryType.esriGeometryPolyline) Then
            MsgBox("Geometry type is not as expected. Select a single polyline segment.")
            Exit Sub
        End If

        'otherwise continue with polyline...

        'not used - bounding box of selected line segment
        'Dim anExtent As IEnvelope
        'anExtent = pLineF.Shape.Envelope
        'MsgBox("Bounding box: YMax:" + anExtent.YMax.ToString + " YMin: " + anExtent.YMin.ToString)


        Try
            Dim pPL As IPolyline
            pPL = pLineF.ShapeCopy 'QI

            'not directly used - start and end points of selected line segment
            'MsgBox("Selected IPolyline has From Point: X: " + pPL.FromPoint.X.ToString + " Y: " + pPL.FromPoint.Y.ToString)
            'MsgBox("                   and   To Point: X: " + pPL.ToPoint.X.ToString + " Y: " + pPL.ToPoint.Y.ToString)

            Dim pFromPoint As IPoint
            Dim pToPoint As IPoint

            pFromPoint = pPL.FromPoint
            pToPoint = pPL.ToPoint

            Dim pFromGeometry As IGeometry
            Dim pToGeometry As IGeometry
            pFromGeometry = pFromPoint 'QI
            pToGeometry = pToPoint 'QI

            Dim pSpatialReferenceFactory As ISpatialReferenceFactory
            Dim pSpatialRef As ISpatialReference

            pSpatialReferenceFactory = New SpatialReferenceEnvironment

            Dim pGeographicCoordinateSys As GeographicCoordinateSystem
            pGeographicCoordinateSys = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_WGS1984)

            pSpatialRef = pGeographicCoordinateSys 'QI
            pSpatialRef.SetFalseOriginAndUnits(-180, -90, 1000000)

            pFromGeometry.Project(pSpatialRef)
            pToGeometry.Project(pSpatialRef)

            MsgBox("Line segment spans from: " + pFromPoint.X.ToString + ", " + pFromPoint.Y.ToString + " Decimal Degrees To: " + pToPoint.X.ToString + ", " + pToPoint.Y.ToString + " Decimal Degrees")

        Catch ex As Exception
            MsgBox("Exception in getLatLong()")
        End Try


    End Sub
0 Kudos
AnatoliiTerentiev
Occasional Contributor III
Thank you very much!
And what about to get attribute of the point(poligon) in some layer after click on the correspond symbol ?
(I work in .NET +c#+arcgis Engine) . On axMapControl on  layer1 (point or polygon) there is some symbols. When operator click on some symbol I need to get attributes of the corresponding point (poligon). Can't I?  What event handler I must use? What property of the axMapControl or event handler may give information about corresponding point(poligon)?
0 Kudos
AnatoliiTerentiev
Occasional Contributor III
Can anyone say what to read - about what kind of event I must to use, how I can get Selected layer?
Thank you for any reference!
0 Kudos
AnatoliiTerentiev
Occasional Contributor III
�?ол�?�?а�? ActiveView след�?�?�?им об�?азом
    
private ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler m_ActiveViewEventsSelectionChanged;
m_map = (ESRI.ArcGIS.Carto.IMap)axMapControl1.ActiveView.FocusMap; // Explict Cast
//Create an instance of the delegate, add it to SelectionChanged event
m_ActiveViewEventsSelectionChanged = new ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler(OnActiveViewEventsSelectionChanged);
((ESRI.ArcGIS.Carto.IActiveViewEvents_Event)(m_map)).SelectionChanged += m_ActiveViewEventsSelectionChanged;

private void OnActiveViewEventsSelectionChanged()
{
            // Сode here
        }


�?ожно ли в э�?ой си�?�?а�?ии поня�?�? на каком символе клик или н�?жно �?�?о-�?о е�?е?
0 Kudos
AnatoliiTerentiev
Occasional Contributor III
�?о�? �?ак �?або�?ае�?
        private void OnActiveViewEventsSelectionChanged()
        {
            ICompositeLayer layer1;
            IFeatureLayer layer;
            layer1 = axMapControl1.get_Layer(2) as ESRI.ArcGIS.Carto.ICompositeLayer;
            layer = layer1.get_Layer(0) as ESRI.ArcGIS.Carto.IFeatureLayer;

            IBasicMap map = null;
            ILayer layer2 = null;
            layer.Selectable = true;
            Object other = null;
            Object index = null;

            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
            axTOCControl1.GetSelectedItem(ref item, ref map, ref layer2, ref other, ref index);
            IGeoFeatureLayer player; 
            player    = layer2 as IGeoFeatureLayer;
            player.Selectable = true;
            ESRI.ArcGIS.Geodatabase.IFeatureClass pFc;// = player as ESRI.ArcGIS.Geodatabase.IFeatureClass;
            string strOIDName;
            pFc = player.FeatureClass;
            if(pFc!=null)
               strOIDName = pFc.OIDFieldName;
           ESRI.ArcGIS.Carto.IFeatureSelection pFSel;
           pFSel = player as ESRI.ArcGIS.Carto.IFeatureSelection;
           ESRI.ArcGIS.Geodatabase.ISelectionSet pSelSet;
           pSelSet = pFSel.SelectionSet;

           ESRI.ArcGIS.Geodatabase.ICursor pFCur;
           pSelSet.Search(null, false, out pFCur);

           int nlay = pFCur.FindField("Centr_lesn");
           string centr = pFCur.NextRow().get_Value(nlay).ToString();
           this.label2.Text = centr;
           IFeatureSelection featureSelection = layer2 as IFeatureSelection;
           ESRI.ArcGIS.Geodatabase.ISelectionSet selectionSet = featureSelection.SelectionSet;
        }

�?�?о�?�? п�?о�?ения если �?�?о-�?о ли�?нее. Спасибо за помо�?�?!!!
0 Kudos