+ Reply to Thread
Results 1 to 9 of 9

Thread: Get parent layer from Layer Extension

  1. #1
    Wayne Guidry
    Join Date
    Apr 2010
    Posts
    27
    Points
    0
    Answers Provided
    0


    0

    Default Get parent layer from Layer Extension

    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).

  2. #2
    Kirk Kuykendall

    Join Date
    Oct 2009
    Posts
    295
    Points
    3
    Answers Provided
    0


    0

    Default

    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.
    Kirk Kuykendall
    AmberGIS Programming Services
    Now answering questions at http://gis.stackexchange.com

  3. #3
    Wayne Guidry
    Join Date
    Apr 2010
    Posts
    27
    Points
    0
    Answers Provided
    0


    0

    Default

    Quote Originally Posted by kirkktx View Post
    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!

  4. #4
    Wayne Guidry
    Join Date
    Apr 2010
    Posts
    27
    Points
    0
    Answers Provided
    0


    0

    Default

    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

  5. #5
    Kirk Kuykendall

    Join Date
    Oct 2009
    Posts
    295
    Points
    3
    Answers Provided
    0


    0

    Default

    Are you sure you've added your layer extension to a layer via ILayerExtensions.AddExtension?
    Kirk Kuykendall
    AmberGIS Programming Services
    Now answering questions at http://gis.stackexchange.com

  6. #6
    Kirk Kuykendall

    Join Date
    Oct 2009
    Posts
    295
    Points
    3
    Answers Provided
    0


    0

    Default

    This code works for me 9.3.1 sp1 vista64

    Code:
    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
        }
    }
    Kirk Kuykendall
    AmberGIS Programming Services
    Now answering questions at http://gis.stackexchange.com

  7. #7
    Wayne Guidry
    Join Date
    Apr 2010
    Posts
    27
    Points
    0
    Answers Provided
    0


    0

    Default

    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

  8. #8
    Wayne Guidry
    Join Date
    Apr 2010
    Posts
    27
    Points
    0
    Answers Provided
    0


    0

    Default

    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.

  9. #9
    Wayne Guidry
    Join Date
    Apr 2010
    Posts
    27
    Points
    0
    Answers Provided
    0


    0

    Default Re: Get parent layer from Layer Extension

    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.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts