In using arcpy, I have found that there are just a few lines of code needed to get a list of layers in a LYR file if more than one layer exist. How can I accomplish this same feat using ArcObjects in C#? I know that ILayerFile is the way to start this off and I can open a specific layer file using ILayerFile.Open() but how can I get back a list of layers in this layer file if more than one exist?
-
1I'm sure I have an extension method stuffed somewhere that gets this, but I'm not at my computer. I think you need to QI ICompositeLayer==>ILayerFile.Layer to get your layers: forums.esri.com/Thread.asp?c=93&f=993&t=130402#770246 (VB Example)JC5577– JC55772012年03月27日 22:08:34 +00:00Commented Mar 27, 2012 at 22:08
-
Just saw your other question (where you posted a comment that links to a Neil Clemmons post that pretty much answers your question). Is that not working?JC5577– JC55772012年03月27日 22:16:52 +00:00Commented Mar 27, 2012 at 22:16
-
Hi Jay- Your answer was correct and I would like to give you the credit since you answered first. Can you add your answer to the actual answer block below instead of the comments. Blah238 was right too and I'll give him some points but he posted after youDaBears– DaBears2012年03月28日 12:07:06 +00:00Commented Mar 28, 2012 at 12:07
-
Don't worry about it! He took the time to write a good, readable answer.JC5577– JC55772012年03月28日 18:02:40 +00:00Commented Mar 28, 2012 at 18:02
1 Answer 1
Query interface from ILayerFile.Layer
to ICompositeLayer
to determine whether the layer is a group/composite layer.
If the QI succeded, check ICompositeLayer.Count
to see if it has any sublayers.
e.g.:
ILayerFile layerFile = new LayerFileClass();
layerFile.Open(layerFilePath);
ICompositeLayer compositeLayer = layerFile.Layer as ICompositeLayer;
if (compositeLayer != null && compositeLayer.Count > 0)
{
// do something with the sublayers
}
You can then access a sublayer by its index (probably using a for loop) using the ICompositeLayer.Layer
property, which, because it takes an argument should be called as a method in .NET, e.g.: compositeLayer.get_Layer(i)
.
If you think the layer file might have multiple nested group layers, you would want to write a recursive function to loop through each sublayer and recurse if it is also a composite layer or do something else with it if not.