Select multiple feature classes

3236
8
12-21-2010 11:16 AM
GregRieck
Frequent Contributor
Hello,

How do I select several features from several different feature classes at one time? I know which features I want to select from within the separate feature classes. So, how do I put that together to perform a selection?

Once I have all of the features selected do I need to do something special to get them to show up in the attribute editor window? I'm trying to select several different spatial objects and have each one appear in the attribute editor window like it does if I were to manually draw a selection rectangle around several objects on the map.

Is this a BUG? Does anyone have any sample code that allows the programmatic selection of several spatial features and have those same features appear in the attribute editor window? Can anyone else reproduce this problem?

Greg
0 Kudos
8 Replies
Venkata_RaoTammineni
Regular Contributor
Hello,

How do I select several features from several different feature classes at one time? I know which features I want to select from within the separate feature classes. So, how do I put that together to perform a selection?

Once I have all of the features selected do I need to do something special to get them to show up in the attribute editor window? I'm trying to select several different spatial objects and have each one appear in the attribute editor window like it does if I were to manually draw a selection rectangle around several objects on the map.

Is this a BUG? Does anyone have any sample code that allows the programmatic selection of several spatial features and have those same features appear in the attribute editor window? Can anyone else reproduce this problem?

Greg


I think you suppose to switch on all layers as selectable ?
0 Kudos
SherryVotava
Emerging Contributor
Hi Greg,

If I understand your question correctly, yes this can be done.  Your first selection must be esriSelectionResultEnum.esriSelectionResultNew and the subsequent selections be of esriSelectionResultEnum.esriSelectionResultAdd.  Here's a sample...  not a spatial query, but it should get you going in the right direction.

      pMap.ClearSelection() 'clear the selection set
      pQueryFilter = New ESRI.ArcGIS.Geodatabase.QueryFilter
      pQueryFilter.WhereClause = "POLENUMBER = '" & m_Criteria & "'"
      pFeatureSelection = Nothing
      selectionresult = ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultNew

      For x = 0 To m_count - 1
        If Not m_pFlayer(x) Is Nothing Then
          pFeatureLayer = m_pFlayer(x)
          pFeatureSelection = pFeatureLayer 
          pActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)
          pFeatureSelection.SelectFeatures(pQueryFilter, selectionresult, False)
          selectionresult = ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultAdd
        End If
      Next x

I hope this helps,
Sherry
0 Kudos
GregRieck
Frequent Contributor
Venkat and Sherry,

Thank you so much for responding to my thread. Everything you have said is true and is what I am currently doing. I was afraid I had something seriously wrong in my code. However, it appears the way I am selecting data is correct. Venka, I'm using your suggestion to make sure the layer is selectable, visible, editable. Sherry, I saw the IFeatureSelection example and implemented it pretty much as provided.

But, what is weird is the first time I call my selection routine, via command button, it selects all the correct data and displays the selected data correctly in the attribute window. But then if I try a new selection sending it through the same code it selects the data but won't allow it to appear in the attribute window. I physically have to stop and restart ArcMap to get it to select and display the data in the attribute window.

What I'm trying to do is select several different feature classes on the map programmatically, but also have those selected features appear in the attribute window. It is similar to using the select by rectangle and having the attribute window refresh itself each time a selection changes. Would either of you have time to confirm or test this behavior to see if you can reproduce it? Is there something else I need to do to refresh the attribute window?

Greg
0 Kudos
Venkata_RaoTammineni
Regular Contributor
Venkat and Sherry,

Thank you so much for responding to my thread. Everything you have said is true and is what I am currently doing. I was afraid I had something seriously wrong in my code. However, it appears the way I am selecting data is correct. Venka, I'm using your suggestion to make sure the layer is selectable, visible, editable. Sherry, I saw the IFeatureSelection example and implemented it pretty much as provided.

But, what is weird is the first time I call my selection routine, via command button, it selects all the correct data and displays the selected data correctly in the attribute window. But then if I try a new selection sending it through the same code it selects the data but won't allow it to appear in the attribute window. I physically have to stop and restart ArcMap to get it to select and display the data in the attribute window.

What I'm trying to do is select several different feature classes on the map programmatically, but also have those selected features appear in the attribute window. It is similar to using the select by rectangle and having the attribute window refresh itself each time a selection changes. Would either of you have time to confirm or test this behavior to see if you can reproduce it? Is there something else I need to do to refresh the attribute window?

Greg


Please send me the code so that we can work it out...
0 Kudos
SherryVotava
Emerging Contributor
Greg,

You probably just need to force a refresh.  Here's what I usually use...

pActiveView.Refresh() 'sometimes a .PartialRefresh will do
pSelEvents = pMap
pSelEvents.SelectionChanged()

Sherry
0 Kudos
GregRieck
Frequent Contributor
Venkat and Sherry,
Thank you SO MUCH for your continued support on this it really has me confused.

I've included my code below. I make the selection through an attribute query.
Keep in mind the selection works just fine. The first time through the routine
it selects and displays in the attribute window fine. However, if you try to make
a second selection it will select but it won't display in the attribute window.
I've tried partial and full refreshes and nothing helps. I don't know what else
to try.

Greg

public static void SelectMyID(int MyID)
{
  using (ComReleaser comReleaser = new ComReleaser())
  try
  {
    MyMap.ClearSelection();
    IFeatureWorkspace featworkspace = MyEditor3.EditWorkspace as IFeatureWorkspace;
    IQueryFilter2 queryfilter2 = new QueryFilterClass();
    queryfilter2.WhereClause = "ID = " + MyID;
    IFeatureClass featClass = null;
    ITable MyTable = featworkspace.OpenTable("MyTable");
    ICursor cur = MyTable.Search(queryfilter2, false);
    comReleaser.ManageLifetime(cur);  
    IRow row = null;
    while ((row = cur.NextRow()) != null)
    {
      featClass = featworkspace.OpenFeatureClass(row.get_Value((int)row.Fields.FindField("MyTable")).ToString());
      if (featClass != null)
      {
        switch (featClass.AliasName)
 {
   case "Feature1":
   case "Feature2":
   case "Feature3":
   case "Feature4":
     SelectFeature(featClass, (int)row.get_Value(row.Fields.FindField("ClassID")), 0, false, true);
     break;
 }
      }
    }
    MyFindCommandAndExecute("{AB073B49-DE5E-11D1-AA80-00C04FA37860}", -1); //Zoom to selected
    MyMxDoc.ActiveView.Refresh();
  }
  catch (Exception ex)
  {
    MyRecordError(ex);
  }
}

public static void SelectFeature(IFeatureClass FC, int MyID, int AltID, bool ZoomTo, bool MultiSel)
{
  try
  {
    if(!MultiSel)
      MyMap.ClearSelection();
    IFeatureSelection vFeatSel = GetCurrentLayer(FC.AliasName, 0) as IFeatureSelection;
    if (vFeatSel == null)
      return;
    IQueryFilter2 queryfilt2 = new QueryFilterClass();
    if (AltID > 0)
      queryfilt2.WhereClause = "OtherID = " + AltID;
    else
      queryfilt2.WhereClause = "ClassID = " + MyID;
    if(MultiSel)
      vFeatSel.SelectFeaturesqueryfilt2, esriSelectionResultEnum.esriSelectionResultAdd, false);
    else
      vFeatSel.SelectFeatures(queryfilt2, esriSelectionResultEnum.esriSelectionResultNew, false);
    vFeatSel.SelectionChanged();
    if (ZoomTo)
      FindCommandAndExecute("{AB073B49-DE5E-11D1-AA80-00C04FA37860}", -1);//esriArcMapUI.ZoomToSelectedCommand
  }
  catch (Exception ex)
  {
    MyRecordError(ex);
  }
}
 
public static IFeatureLayer GetCurrentLayer(string sLayerName, int SubType)
{
  try
  {
    ESRI.ArcGIS.esriSystem.UID pID = new ESRI.ArcGIS.esriSystem.UIDClass();
    pID.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}";// IFeatureLayer
    IMap myMap = myMxDoc.Activeview.FocusMap as IMap;
    IEnumLayer pEnumLayer = MyMap.get_Layers(pID, true) as IEnumLayer;
    pEnumLayer.Reset();
    IFeatureLayer pFL;
    while ((pFL = pEnumLayer.Next() as IFeatureLayer) != null)
    {
      if (pFL.FeatureClass.AliasName.ToLower() == sLayerName.ToLower())
      {
 return pFL;
      }
    }
  }
  catch (Exception ex)
  {
    MyRecordError(ex, "Layer Name = " + sLayerName);
  }
  return null;
}
0 Kudos
GregRieck
Frequent Contributor
I wrote another c# solution using the USA data found in the developer kit and was able to reproduce the problem. I have submitted an incident to ESRI technical support on this problem. I'll post my findings if and when I get them.

Greg
0 Kudos
GregRieck
Frequent Contributor
Here is the solution to this problem:

http://resources.arcgis.com/content/kbase?fa=articleShow&d=20165


Adding the following three lines of code to the bottom of my selection routine solved the problem.
ISelectionEvents pSelEvents;
pSelEvents = myMap as ISelectionEvents;
pSelEvents.SelectionChanged();
0 Kudos