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 } }
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.
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.