Selected Features from multiple layers in table of contents

2050
3
Jump to solution
02-07-2013 12:28 PM
RyanHawkins
New Contributor II
I am able to get the ObjectID for the features only in the layer selected in the table of contents when I click the StartButton. I would like to be able to gather the selected items for multiple layers. This is because there are multiple layers that have selected features and need to grab information for each. Below I have added the code I used to put the ObjectID into 3 comboboxes. I realize the line IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer; is selecting the layer in the table of contents that is selected. I am wondering how others would solve this. I have tried to find a way to use code to select a different layer by its layer name but I could not find any information on it. If I can select a layer by name then I can merely change the layer before the IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer; line and run the method GetSelectedValues for each layer I would like. Any suggestions will be greatly appreciated!

public void StartButton_Click(object sender, EventArgs e)
{
IApplication app = (IApplication)this.Hook;
IMxDocument mapDoc = (IMxDocument)app.Document;
IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer;
GetSelectedValues(featureLayer);
}

public void GetSelectedValues(IFeatureLayer featureLayer)
{
try
{
IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
ISelectionSet selectionSet = featureSelection.SelectionSet;

ICursor cursor;
selectionSet.Search(null, false, out cursor);
IRow row = cursor.NextRow();

//IFeatureCursor featureCursor = (IFeatureCursor)cursor;
IFeatureClass featureClass = featureLayer.FeatureClass;

// Gets the feature from the OID found from row
IFeature feature = featureClass.GetFeature(row.OID);

while (row != null)
{
feature = featureClass.GetFeature(row.OID);

//Adds the selected object ID's to the drop down menus
middlePole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString());
leftPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString());
rightPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString());
//textBox3.Text = feature.get_Value(featureClass.FindField("LASTUPDATESESSIONID")).ToString();

//Goes to the NextRow so that the while loop can catch if the next row is a Null.
row = cursor.NextRow();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
}
0 Kudos
1 Solution

Accepted Solutions
MarcinDruzgala
Occasional Contributor
Please use CODE tags!

Firts of all:
while (row != null) { feature = featureClass.GetFeature(row.OID);  //Adds the selected object ID's to the drop down menus middlePole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); leftPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); rightPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); //textBox3.Text = feature.get_Value(featureClass.FindField("LASTUPDATESESSIONID")).ToString();  //Goes to the NextRow so that the while loop can catch if the next row is a Null. row = cursor.NextRow(); }

Isn't it simpler this way:
{  while(row != null)  {                 string objectID = GetStringValueFromField(row, "OBJECTID");   middlePole.Items.Add(objectID);   leftPole.Items.Add(objectID);   rightPole.Items.Add(objectID);   row = cursor.NextRow();  } } private string GetStringValueFromField(IRow row, string fieldName) {  int index = row.Fields.FindField(fieldName);  string fieldValue;  if (index > -1)  {   fieldValue = row.get_Value(index).ToString();   return fieldValue;  }  else   return fieldValue = ""; }

By the way you don't have to get feature to get value from field(as you can see in GetStringValueFromField method).

To relate to topic of your thread. Enumerate through the layers in the TOC, example:
IUID uid = new UIDClass(); uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}"; IEnumLayer enumLayer = map.get_Layers((UID)uid, true); enumLayer.Reset(); IFeatureLayer featLayer = (IFeatureLayer)enumLayer.Next(); while(featLayer != null) {  //your logic, for example  IFeatureSelection featureSelection = (IFeatureSelection)featLayer;  ICursor cursor;  selectionSet.Search(null, false, out cursor);  IRow row = cursor.NextRow();  ...etc         //////////////          featLayer = (IFeatureLayer)enumLayer.Next(); }


Cheers
MDruzgala

View solution in original post

0 Kudos
3 Replies
MarcinDruzgala
Occasional Contributor
Please use CODE tags!

Firts of all:
while (row != null) { feature = featureClass.GetFeature(row.OID);  //Adds the selected object ID's to the drop down menus middlePole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); leftPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); rightPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); //textBox3.Text = feature.get_Value(featureClass.FindField("LASTUPDATESESSIONID")).ToString();  //Goes to the NextRow so that the while loop can catch if the next row is a Null. row = cursor.NextRow(); }

Isn't it simpler this way:
{  while(row != null)  {                 string objectID = GetStringValueFromField(row, "OBJECTID");   middlePole.Items.Add(objectID);   leftPole.Items.Add(objectID);   rightPole.Items.Add(objectID);   row = cursor.NextRow();  } } private string GetStringValueFromField(IRow row, string fieldName) {  int index = row.Fields.FindField(fieldName);  string fieldValue;  if (index > -1)  {   fieldValue = row.get_Value(index).ToString();   return fieldValue;  }  else   return fieldValue = ""; }

By the way you don't have to get feature to get value from field(as you can see in GetStringValueFromField method).

To relate to topic of your thread. Enumerate through the layers in the TOC, example:
IUID uid = new UIDClass(); uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}"; IEnumLayer enumLayer = map.get_Layers((UID)uid, true); enumLayer.Reset(); IFeatureLayer featLayer = (IFeatureLayer)enumLayer.Next(); while(featLayer != null) {  //your logic, for example  IFeatureSelection featureSelection = (IFeatureSelection)featLayer;  ICursor cursor;  selectionSet.Search(null, false, out cursor);  IRow row = cursor.NextRow();  ...etc         //////////////          featLayer = (IFeatureLayer)enumLayer.Next(); }


Cheers
MDruzgala
0 Kudos
NeilClemmons
Regular Contributor III
If you want the feature selection for all layers in the map then use IMap.FeatureSelection.
0 Kudos
RyanHawkins
New Contributor II
Thank you both for the responses! I really appreciate critiquing my code, I am learning c# on the fly and am missing some basic concepts so I really appreciate that. The code you provided has allowed me to do exactly what I asked!
0 Kudos