Get 'Parent' Group Layers for an ILayer

3544
4
09-24-2013 05:21 AM
BrendanDwyer
Occasional Contributor
I'm creating an add-in for 10.0 in C#.  Users will be working in MXD's that have nested group layers.  They'll select a layer in the TOC to work on.  In code, I get the selected layer in the TOC, and what I need to do is get the parent layers of the selected layer. 

I have a good handle on looping through each top level layer in the map, and then each sublayer and so on, but that's going from the 'top down,' so to speak.  I want to go from the 'bottom up.'  From the child layer, get each parent. 

For example, say I have the following structure in my table of contents.

Main Campus
-> Engineering Compound            (Group Layer)
----> Building 101                      (Group Layer)
---------> Floor 1                     (Group Layer)
---------------> Classrooms       (Feature Class)
---------------> Offices            (Feature Class)
---------------> Storage Rooms (Feature Class)

Imagine there are many compounds and many buildings in each compound.  If a user selects Classrooms, I want to get Floor 1/Building 101/Engineering Compound/Main Campus.

Any help would be appreciated.

-Brendan
0 Kudos
4 Replies
NeilClemmons
Regular Contributor III
There isn't any way to do this through ArcObjects because a layer doesn't have any kind of "parent" property.  You can, however, create your own custom class that has a parent property and use it to build a lookup index of some type.
0 Kudos
AlexanderGray
Occasional Contributor III
I have always found this annoying, there is no way to go back up.  You have to go top down until you find your layer and then look at the previous level in your code.
0 Kudos
BrendanDwyer
Occasional Contributor
There isn't any way to do this through ArcObjects because a layer doesn't have any kind of "parent" property.  You can, however, create your own custom class that has a parent property and use it to build a lookup index of some type.


Yeah, that's what I was thinking of, too.  I created a class that contains a list called children which contains a list of that class.  I use this in a recursive loop that will populate each object (group layer) with it's own children.

The problem is that I only need the parents of the selected layer.  So I'd much rather go up from the selected layer than down from each top level group, then try to figure out when I get to the selected layer.
0 Kudos
BrendanDwyer
Occasional Contributor
I have always found this annoying, there is no way to go back up.  You have to go top down until you find your layer and then look at the previous level in your code.


Right.  I'm surprised that this isn't built in.  Maybe in 10.3....
0 Kudos