This sample will show you how you can programmatically add network locations to a network analysis class (e.g. Barriers, Stops, Incidents, Facilities) using the point feature layer selected in the table of contents as input.
How to use
- In the table of contents, select the point feature class you want to load from
- In the Network Analyst Window, select the analysis class that you want to add network locations to
- Paste this code into a VBA module in ArcMap
- Call the function AddNetworkLocationsFromSelectedLayer()
Public Sub AddNetworkLocationsFromSelectedLayer()
On Error GoTo ErrorHandler
Dim pMxDoc As IMxDocument 'Current Map Document
Dim pPointFClass As IFeatureClass 'Input Feature Class
Dim pNAExtension As INetworkAnalystExtension 'Application extension for NA
Dim pNAWindow As INAWindow 'Network Analysis Window
Dim pNALayer As INALayer 'Active Network Analysis Layer
Dim pNAContext As INAContext 'Active NALayer's context
Dim pNAWindowCategory As INAWindowCategory 'Stops, barriers, incidents, etc
'Get the current network analysis and selected category
Set pMxDoc = ThisDocument
Set pNAExtension = Application.FindExtensionByName("Network Analyst")
Set pNAWindow = pNAExtension.NAWindow
Set pNALayer = pNAWindow.ActiveAnalysis
Set pNAContext = pNALayer.Context
Set pNAWindowCategory = pNAWindow.ActiveCategory
'get the selected layer in the TOC and get a point feature class from it
Set pPointFClass = GetPointFeatureClassSelectedInTOC
If pPointFClass Is Nothing Then
MsgBox "You must select a point feature class in the table of contents"
Exit Sub
End If
'Make sure we have a valid category selected in the NAWindow
If pNAWindowCategory Is Nothing Then
MsgBox "You must have an analysis layer in your map"
Exit Sub
End If
'Setup the NAClassLoader based on the current analysis layer
Dim pNAClassLoader As INAClassLoader 'Loader that does locating
Dim pFLayer As IFeatureLayer 'Used for getting to NAClass
Dim pNAClass As INAClass 'Current NAClass (stops, barriers, etc)
Dim pNAClassFieldMap As INAClassFieldMap 'Map values from input FC to output FC
Set pNAClassLoader = New NAClassLoader
Set pFLayer = pNAWindowCategory.Layer
Set pNAClass = pFLayer.FeatureClass
Set pNAClassLoader.Locator = pNAContext.Locator
Set pNAClassLoader.NAClass = pNAClass
'use the analysis layer's default mappings and possibly add your own
Set pNAClassFieldMap = New NAClassFieldMap
pNAClassFieldMap.CreateMapping pNAClass.ClassDefinition, pPointFClass.Fields
' Uncomment and change this code to override a default value
'pNAClassFieldMap.DefaultValue("Attr_Minutes") = 5.5
' Uncomment and change this code to override a mapped field
'pNAClassFieldMap.MappedField("Name") = "RestaurantName"
Set pNAClassLoader.FieldMap = pNAClassFieldMap
'Open a cursor on the feature class you're loading from
Dim pInputCursor As ICursor
Dim rowsInCursor As Long
Dim rowsLocated As Long
Set pInputCursor = pPointFClass.Search(Nothing, False)
'Start edit operation (for undo/redo)
pNAWindow.StartOperation pNAContext
On Error GoTo ErrorHandlerWithinEditOperation
'Actually load the features and locate them
pNAClassLoader.Load pInputCursor, New CancelTracker, rowsInCursor, rowsLocated
Debug.Print "Located " & rowsLocated & " of " & rowsInCursor & " rows."
'Message all of the network analysis agents that the analysis context has changed
Dim pNAContextEdit As INAContextEdit
Set pNAContextEdit = pNAContext
pNAContextEdit.ContextChanged
pNAWindow.StopOperation pNAContext, "Loaded " & rowsInCursor & " features into " & pNAWindowCategory.Layer.Name
On Error GoTo ErrorHandler
'Make sure everything get's refreshed properly
pNAWindow.UpdateContent pNAWindowCategory
pMxDoc.ActiveView.Refresh
Exit Sub
ErrorHandlerWithinEditOperation:
MsgBox "Error: " & Err.Description
pNAWindow.AbortOperation pNAContext
Exit Sub
ErrorHandler:
MsgBox "Error: " & Err.Description
Exit Sub
End Sub
'Returns a point feature class selected in map's TOC or Nothing
Private Function GetPointFeatureClassSelectedInTOC() As IFeatureClass
Dim pMxDoc As IMxDocument
Dim pLayer As ILayer
Dim pGeoFeatureLayer As IGeoFeatureLayer
Dim pFClass As IFeatureClass
'Get the selected layer supporting IGeoFeatureLayer
Set pMxDoc = ThisDocument
Set pLayer = pMxDoc.SelectedLayer
If pLayer Is Nothing Then Exit Function
If Not TypeOf pLayer Is IGeoFeatureLayer Then Exit Function
'Get the selected layer's feature class if it's of type point
Set pGeoFeatureLayer = pLayer
Set pFClass = pGeoFeatureLayer.FeatureClass
If pFClass Is Nothing Then Exit Function
If pFClass.ShapeType = esriGeometryPoint Then
Set GetPointFeatureClassSelectedInTOC = pFClass
End If
End Function