About the Load network layer from active analysis to the table of contents Sample
[C#]
LoadNetworkLayerFromActiveAnalysisToTOC.cs
//************************************************************************************* // Network Analyst - Load network layer from active analysis to the table of contents // // This simple code shows how to load the network layer and its associated sources // to a map // //************************************************************************************ using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.NetworkAnalyst; using ESRI.ArcGIS.NetworkAnalystUI; namespace LoadNetworkLayerFromActiveAnalysisToTOC { public class LoadNetworkLayerFromActiveAnalysisToTOC : ESRI.ArcGIS.Desktop.AddIns.Button { public LoadNetworkLayerFromActiveAnalysisToTOC() { } /// <summary> /// OnClick is the main function for the add-in. When the button is clicked in ArcMap, /// this code will execute /// </summary> protected override void OnClick() { // Verify that the network extension has been loaded properly. var naExt = ArcMap.Application.FindExtensionByName("Network Analyst") as INetworkAnalystExtension; if (naExt == null || naExt.NAWindow == null) return; // There must be an active analysis layer INALayer naLayer = naExt.NAWindow.ActiveAnalysis; if (naLayer == null || naLayer.Context == null) return; // Use the network layer factory to generate the layers ILayerFactory layerFactory = new EngineNetworkLayerFactoryClass(); var dataset = naLayer.Context.NetworkDataset as IDataset; if (layerFactory.get_CanCreate(dataset.FullName)) { // Calling create will open a popup asking the user if the network sources // should be added as well. If the user clicks NO, then the returned // IEnumLayer will only return a network layer for the network dataset. // If the user clicks YES, then IEnumLayer will also return layers for // the network sources IEnumLayer enumLayer = layerFactory.Create(dataset.FullName); ILayer layer = enumLayer.Next(); while (layer != null) { ArcMap.Document.FocusMap.AddLayer(layer); layer = enumLayer.Next(); } } } protected override void OnUpdate() { Enabled = (ArcMap.Application != null); } } }
[Visual Basic .NET]
LoadNetworkLayerFromActiveAnalysisToTOC.vb
'************************************************************************************* ' Network Analyst - Load network layer from active analysis to the table of contents ' ' This simple code shows how to load the network layer and its associated sources ' to a map ' '************************************************************************************ Imports ESRI.ArcGIS.Carto Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.NetworkAnalyst Imports ESRI.ArcGIS.NetworkAnalystUI Namespace LoadNetworkLayerFromActiveAnalysisToTOC Public Class LoadNetworkLayerFromActiveAnalysisToTOC Inherits ESRI.ArcGIS.Desktop.AddIns.Button Public Sub New() End Sub ''' <summary> ''' OnClick is the main function for the add-in. When the button is clicked in ArcMap, ''' this code will execute ''' </summary> Protected Overrides Sub OnClick() ' Verify that the network extension has been loaded properly. Dim naExt As INetworkAnalystExtension = TryCast(ArcMap.Application.FindExtensionByName("Network Analyst"), INetworkAnalystExtension) If naExt Is Nothing OrElse naExt.NAWindow Is Nothing Then Return End If ' There must be an active analysis layer Dim naLayer As INALayer = naExt.NAWindow.ActiveAnalysis If naLayer Is Nothing OrElse naLayer.Context Is Nothing Then Return End If ' Use the network layer factory to generate the layers Dim layerFactory As ILayerFactory = New EngineNetworkLayerFactoryClass() Dim dataset As IDataset = TryCast(naLayer.Context.NetworkDataset, IDataset) If layerFactory.CanCreate(dataset.FullName) Then ' Calling create will open a popup asking the user if the network sources ' should be added as well. If the user clicks NO, then the returned ' IEnumLayer will only return a network layer for the network dataset. ' If the user clicks YES, then IEnumLayer will also return layers for ' the network sources Dim enumLayer As IEnumLayer = layerFactory.Create(dataset.FullName) Dim layer As ILayer = enumLayer.Next() Do While Not layer Is Nothing ArcMap.Document.FocusMap.AddLayer(layer) layer = enumLayer.Next() Loop End If End Sub Protected Overrides Sub OnUpdate() Enabled = (Not ArcMap.Application Is Nothing) End Sub End Class End Namespace