Get selected Feature OID in ArcMap Addin - Java

1893
3
06-29-2013 01:46 AM
IshanKaushik
New Contributor
Hi,

I am trying to built an arcMap Addin tool which on MousePressed event tells me about the selected feature OID.
I have followed following question in the forum but without any luck--

http://forums.arcgis.com/threads/76873-Getting-the-OID-from-a-selected-feature

Till now this is wat my tool do ---

                          public void mousePressed(MouseEvent me) {
        try
        {
          mxDoc = (IMxDocument) app.getDocument();
          IActiveView activeView = mxDoc.getActivatedView();
          IMap map = activeView.getFocusMap();
                        
          IFeatureLayer featureLayer = (IFeatureLayer) mxDoc.getSelectedLayer();

                                  //Getting problem here how to get selected feature in featureLayer
   
   
          mxDoc.getActiveView().refresh();
        }
        catch(Exception ex)
        {
          ex.printStackTrace();
        }
    }


Additionally i tried following methord but nothing happens---

                                      public void mousePressed(MouseEvent me) {
        try
        {
          mxDoc = (IMxDocument) app.getDocument();
          IActiveView activeView = mxDoc.getActivatedView();
          IMap map = activeView.getFocusMap();
    IEnumFeature iefeature = (IEnumFeature) map.getFeatureSelection();
    IFeature ifeature = iefeature.next();
    while(ifeature != null)
    {
     int oid = ifeature.getOID();
     list.add(oid);
     ifeature = iefeature.next();
    }
         
         JOptionPane.showMessageDialog(null, list.get(0));
          mxDoc.getActiveView().refresh();
        }
        catch(Exception ex)
        {
          ex.printStackTrace();
        }
    }

There are less nos of java examples to follow for arcmap addin in java , Can anybody tell me how to get OID of the feature selected.
0 Kudos
3 Replies
LeoDonahue
Occasional Contributor III
In your second example, change IEnumFeature to MapSelection.

Read the API for class Map.getFeatureSelection()

http://help.arcgis.com/en/sdk/10.0/java_ao_adf/api/arcobjects/com/esri/arcgis/carto/Map.html#getFeat...

IEnumFeature works on all of the FeatureLayers as a whole. Each  FeatureLayer has a ISelectionSet and IEnumFeature steps through all of  these as though there was only one. Because IEnumFeature works with all  the FeatureLayers, you cannot use it to loop through the features  belonging to just one FeatureLayer.
0 Kudos
IshanKaushik
New Contributor
I tried by changing the IEnumFeature to MapSelection . My Code Looks like this---

                              
public void mousePressed(MouseEvent me) {
        try
        {
          mxDoc = (IMxDocument) app.getDocument();
          IActiveView pactiveView = mxDoc.getActivatedView();
          IMap pMap = pactiveView.getFocusMap();
          MapSelection iefeature = (MapSelection) pMap.getFeatureSelection();
          IFeature ifeature = iefeature.next();
          while(ifeature != null)
          {
          int oid = ifeature.getOID();
          list.add(oid);
          ifeature = iefeature.next();
          }

         JOptionPane.showMessageDialog(null,"Found :"+ list.size());
          mxDoc.getActiveView().refresh();
        }
        catch(Exception ex)
        {
          ex.printStackTrace();
          JOptionPane.showMessageDialog(null, "Error :"+ex);
        }
        }


But i am receiving error again as follows---

[ATTACH=CONFIG]25606[/ATTACH]
0 Kudos
LeoDonahue
Occasional Contributor III
ISelection implements the following:  ElementSelection, ISelectionProxy, MapSelection, SimpleElementSelection

Which one do you probably have before trying to cast to MapSelection?

From what I can tell about your code, your first concrete class is MapSelection.  The rest of your variables appear to be interfaces.
0 Kudos