This sample will show you how you can programmatically create a network analysis layer (NALayer) and add it to ArcMap within an undo/redo operation. This example creates a route layer but this can be easily modified to create a closest facility, service area, or origin-destination cost matrix layer.
How to use
- Add a Network Dataset to ArcMap
- Ensure you have the Network Analyst extension enabled
- Paste this code into a VBA module in ArcMap
- Call the Sub CreateAnalysisLayer()
Public Sub CreateAnalysisLayer()
On Error GoTo ErrorHandler
'Get the network dataset from the current Network Layer
Dim pNetworkDataset As INetworkDataset
Set pNetworkDataset = GetNetworkDataset
If pNetworkDataset Is Nothing Then
MsgBox "Could not find a network dataset."
Exit Sub
End If
'Create a solver corresponding to the type of analysis you want to perform.
'Change "NARouteSolver" to be one of the following for a different type of analysis layer:
' "NAClosestFacilitySolver", "NAODCostMatrixSolver", "NAServiceAreaSolver"
Dim pNASolver As INASolver
Set pNASolver = New NARouteSolver
'Get the Network Dataset's DataElement
Dim pDENetworkDataset As IDENetworkDataset
Set pDENetworkDataset = GetDataElement(pNetworkDataset)
'Create a context to hold the analysis and bind (initialize) it
Dim pNAContext As INAContext
Dim pNAContextEdit As INAContextEdit
Set pNAContext = pNASolver.CreateContext(pDENetworkDataset, "My Network Analysis Layer")
Set pNAContextEdit = pNAContext
pNAContextEdit.Bind pNetworkDataset, Nothing
'Create an analysis layer
Dim pNALayer As INALayer
Set pNALayer = pNASolver.CreateLayer(pNAContext)
'Set the layer name
Dim pLayer As ILayer
Set pLayer = pNALayer
pLayer.Name = pNAContext.Name
'Add the network analysis layer to ArcMap
AddLayerInOperation pNALayer, ThisDocument
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub
Public Function GetNetworkDataset() As INetworkDataset
Dim pNetworkAnalystExtension As INetworkAnalystExtension
Dim pNetworkLayer As INetworkLayer
'Get the Network Analyst extension
'Return the current network layer's network dataset
Set pNetworkAnalystExtension = Application.FindExtensionByName("Network Analyst")
If pNetworkAnalystExtension Is Nothing Then
MsgBox "Could not get the Network Analyst extension"
Exit Function
End If
Set pNetworkLayer = pNetworkAnalystExtension.CurrentNetworkLayer
If pNetworkLayer Is Nothing Then
MsgBox "There is no network dataset in the map"
Exit Function
End If
Set GetNetworkDataset = pNetworkLayer.NetworkDataset
End Function
Public Function GetDataElement(ByVal pDatasetComponent As IDatasetComponent) As IDEDataset
'Network Dataset implements IDatasetComponent.
'Return it's DataElement
Set GetDataElement = pDatasetComponent.DataElement
End Function
Public Sub AddLayerInOperation(ByVal pLayer As ILayer, ByVal pMxDoc As IMxDocument)
'Add the layer within an undo/redo operation
Dim pAddLayersOperation As IAddLayersOperation
Set pAddLayersOperation = New AddLayersOperation
pAddLayersOperation.AddLayer pLayer
pAddLayersOperation.ArrangeLayers = False
pAddLayersOperation.Name = pLayer.Name
pAddLayersOperation.SetDestinationInfo 0, pMxDoc.FocusMap, Nothing
pMxDoc.OperationStack.Do pAddLayersOperation
End Sub