1

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?

asked Mar 27, 2012 at 20:52
4
  • 1
    I'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) Commented 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? Commented 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 you Commented Mar 28, 2012 at 12:07
  • Don't worry about it! He took the time to write a good, readable answer. Commented Mar 28, 2012 at 18:02

1 Answer 1

3

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.

answered Mar 27, 2012 at 22:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.