Get parent layer from Layer Extension

1088
8
04-09-2010 08:21 AM
WayneGuidry
New Contributor III
Is there anyway to find out the parent layer from a layer extension?

We have layer extensions that are tied to our layers. Instead of having to loop through each layer on our map to update the layer extensions I maintain a collection of all the layer extensions and update them when their information changes. I need to somehow pass this information back to the original layer's layer extension it came from so they receive the updates (it appears by having a collection of these layer extensions it makes a copy of them, not by reference).
0 Kudos
8 Replies
KirkKuykendall
Occasional Contributor III
If you implement ILayerExtensionDraw, the parent layer gets passed as an arg, in either of the two methods.

I found it unstable to keep a (strong) reference member varialbe to the parent layer, maybe some sort of weak reference strategy could be used though - I've never tried.
0 Kudos
WayneGuidry
New Contributor III
If you implement ILayerExtensionDraw, the parent layer gets passed as an arg, in either of the two methods.

I found it unstable to keep a (strong) reference member varialbe to the parent layer, maybe some sort of weak reference strategy could be used though - I've never tried.


Hi Kirk, I have been using a strong reference and although I haven't had any issues with it for a few months now I have a feeling it will come back to bite me later on. That's why I figured I should ask. I wasn't aware of the draw interface having the parent layer, that is extremely helpful. thanks!
0 Kudos
WayneGuidry
New Contributor III
Kirk,

I tried implementing the ILayerExtensionDraw interface. I put messagebox statements in the BeforeLayerDraw and AfterLayerDraw to see when they fire, however they never seem to fire? I noticed on the old forums you had a sample in VB and it seemed it was that easy to setup, is there something I have to do differently in .NET/C# to get these events to fire (i.e. register the layer extensions with COM or something)? Or is there another interface that I'm also supposed to setup for these events to work?

thanks,
Wayne
0 Kudos
KirkKuykendall
Occasional Contributor III
Are you sure you've added your layer extension to a layer via ILayerExtensions.AddExtension?
0 Kudos
KirkKuykendall
Occasional Contributor III
This code works for me 9.3.1 sp1 vista64

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using System.Diagnostics;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;

namespace Forums
{
    public class AddLayerExtCmd : BaseCommand
    {
        IApplication m_app;
        public AddLayerExtCmd()
        {
            m_caption = "Add LayerExtension";
        }
        public override void OnClick()
        {
            MyLayerExtension.AddExtensions(((IMxDocument)m_app.Document).FocusMap);
        }
        public override void OnCreate(object hook)
        {
            m_app = (IApplication)hook;
        }
    }

    public class MyLayerExtension: ILayerExtensionDraw 
    {
        public static void AddExtensions(IMap map)
        {
            if (map.LayerCount == 0)
                throw new Exception("no layers in map");
            IEnumLayer enumLayer = map.get_Layers(null, true);
            ILayer layer;
            while ((layer = enumLayer.Next()) != null)
            {
                ILayerExtensions exts = layer as ILayerExtensions;
                if (exts == null)
                    continue;
                for (int i = exts.ExtensionCount - 1; i > -1; i--)
                {
                    if (exts.get_Extension(i) is MyLayerExtension)
                    {
                        Debug.Print("removing old extension from {0}", layer.Name);
                        exts.RemoveExtension(i);
                    }
                }
                exts.AddExtension(new MyLayerExtension());
            }
        }
        #region ILayerExtensionDraw Members

        public void AfterLayerDraw(ILayer pLayer, esriDrawPhase DrawPhase, 
            IDisplay pDisplay, ITrackCancel pTrackCancel)
        {
            Debug.Print("Afterlayerdraw {0} {1}", pLayer.Name, DrawPhase);
        }

        public void BeforeLayerDraw(ILayer pLayer, esriDrawPhase DrawPhase, 
            IDisplay pDisplay, ITrackCancel pTrackCancel)
        {
            Debug.Print("Beforelayerdraw {0} {1}", pLayer.Name, DrawPhase);
        }

        #endregion
    }
}
0 Kudos
WayneGuidry
New Contributor III
Kirk,

sorry for taking so long to get back to this... I'm running my code in Engine 9.3 (not 9.3.1) and even tried it in Desktop at 9.3 and it doesn't work. I'll copy your code over and try yours instead and see if it works in 9.3, maybe I'm overlooking something... if not, perhaps it was a bug in 9.3 fixed in 9.3.1? I'll do some more test to find out and let you know... thanks
0 Kudos
WayneGuidry
New Contributor III
Just realized that I wasn't clear on what kind of layers I'm using... we are using Temporal layers to attach layer extensions to. If I run your code (or mine) on feature layers, you are correct, the events fire as expected, however I'm noticing on temporal layers in 9.3 that the events never fire with the exact same code to add the layer extension.
0 Kudos
WayneGuidry
New Contributor III
FYI... for anyone who runs into this. Regarding this problem, this was a quote from an ESRI technical lead:

"All we do internally for regular FeatureLayers and NetworkLayers is
loop through each layer and check its extensions. The temporal layers
simply do not contain this loop for the extensions and thus are unable
to fire the events."

It appears they will also re-evaluate in 10 to see if this can be fixed.
0 Kudos