How to acces to a layer in a groupLayer

5542
5
Jump to solution
07-16-2014 01:28 AM
JonGarrido
New Contributor III

Hi,

I'm attempting to access to a FeatureLayer in a GropuLayer.  I'm looping through layers in the TOC and when getting the IGroupLayer I don't know what to do next.

IMap myMap = ArcMap.Document.ActivatedView.FocusMap;

for (int i = 0; i < myMap.LayerCount; i++)

            {

                if (myMap.Layer is IGroupLayer)

                {

                    IGroupLayer igroup = myMap.Layer as IGroupLayer;

                    //what to do...

                   

                }

}

0 Kudos
1 Solution

Accepted Solutions
JavierArtero
New Contributor III

Ok, i'll just modify your code replacing IGroupLayer for ICompositeLayer. Not my type of solution but I guess it'll do the job...

IMap myMap = ArcMap.Document.ActivatedView.FocusMap;

for (int i = 0; i < myMap.LayerCount; i++)

{

     if (myMap.Layer is ICompositeLayer)

     {

           ICompositeLayer igroup = myMap.Layer as ICompositeLayer;

           for (int j = 0; j < igroup.Count; j++)

           {

                 IFeatureLayer ilayer = igroup.Layer(j) as IFeatureLayer;

                 // Do whatever you need.

           }

     }

}

Does it works for you now?

View solution in original post

0 Kudos
5 Replies
JavierArtero
New Contributor III

Two methods are available (as far as I know) to get a particular layer from the ToC.

1.- Traversing the Layer collection obtained from a Map instance

2.- Iterating the enumerator Layers obtained from a Map instance

The first, the one that you are showing on your code, forces you to deal with group layers. This is usually solved by using a recursive function.

If you are only interested on Feature layers, I would advise to face the problem busing the second option.

I'll give you a sample code in vb.net (which you should be able to translate to C# in a breeze)

Dim enumLayer As IEnumLayer

Dim uidGeo As UID = New UIDClass()

// GUID for IGeoFeatureLayer

uidGeo.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"

// Request IGeoFeatureLayers recursing into group layers

enumLayer = FocusMap.Layers(uidGeo, True)

enumLayer.Reset()

// You will need a cast into IFeatureLayer here, since the enumLayer.Next returns a ILayer interface

Dim featureLayer as IFeatureLayer = enumLayer.Next

While featureLayer IsNot Nothing

     // Do whatever you need...

     // Next feature layer...

     featureLayer = enumLayer.Next()

End While

Hope this works for you.

PS: Group layers are better dealt with by using the ICompositeLayer interface, which will provide you with access to its contained layers. So instead of using the IGroupLayer, you should use the ICompositeLayer... in case you didn't like the solution I provided

0 Kudos
JonGarrido
New Contributor III

Hi Alberto,

I haven't nay problem on looping through the "normal" layers of the TOC. Only with the ones are inside a group layer.

The groupeLayer responses like normal layer, but inside there can be multiple layers and I can't access to them...

Thanks for your response in any case...

BR

Jon

0 Kudos
JavierArtero
New Contributor III

Ok, i'll just modify your code replacing IGroupLayer for ICompositeLayer. Not my type of solution but I guess it'll do the job...

IMap myMap = ArcMap.Document.ActivatedView.FocusMap;

for (int i = 0; i < myMap.LayerCount; i++)

{

     if (myMap.Layer is ICompositeLayer)

     {

           ICompositeLayer igroup = myMap.Layer as ICompositeLayer;

           for (int j = 0; j < igroup.Count; j++)

           {

                 IFeatureLayer ilayer = igroup.Layer(j) as IFeatureLayer;

                 // Do whatever you need.

           }

     }

}

Does it works for you now?

0 Kudos
JonGarrido
New Contributor III

HI Javier,

I'll test it tomorrow. Thank you very much!!

0 Kudos
JonGarrido
New Contributor III

Thanks to everybody,

Javier was true, ICompositeLayer gave me access to the layers inside the GroupLayer

Gracias Javier!!!

0 Kudos