About the Network Analyst Engine application Sample
[C#]
cmdLoadLocations.cs
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.NetworkAnalyst;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
// This command allows users to load locations from another point feature layer into the selected NALayer and active category.
namespace NAEngine
{
[Guid("72BDDCB7-03E8-4777-BECA-11DC47EFEDBA")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("NAEngine.LoadLocations")]
public sealed class cmdLoadLocations : BaseCommand
{
private IMapControl3 m_mapControl;
private IEngineNetworkAnalystEnvironment m_naEnv;
public cmdLoadLocations()
{
base.m_caption = "Load Locations...";
}
public override bool Enabled
{
get
{
// Enabled if the active category is an input NAClass
IEngineNAWindowCategory naWindowCategory = m_naEnv.NAWindow.ActiveCategory;
if (naWindowCategory == null)
return false;
INAClass naClass = naWindowCategory.NAClass;
if (naClass == null)
return false;
INAClassDefinition naClassDefinition = naClass.ClassDefinition;
if (naClassDefinition == null)
return false;
return naClassDefinition.IsInput;
}
}
public override void OnClick()
{
// Get the NALayer and corresponding NAContext of the layer that
// was right-clicked on in the table of contents
// m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown
INALayer naLayer = (INALayer)m_mapControl.CustomProperty;
// Set the Active Analysis layer to be the layer right-clicked on
m_naEnv.NAWindow.ActiveAnalysis = naLayer;
if (!Enabled)
return;
// Show the Property Page form for Network Analyst
frmLoadLocations loadLocations = new frmLoadLocations();
if (loadLocations.ShowModal(m_mapControl, m_naEnv))
{
// notify that the context has changed because we have added locations to a NAClass within it
INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit;
contextEdit.ContextChanged();
// If loaded locations, refresh the NAWindow and the Screen
m_mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, m_mapControl.Extent);
m_naEnv.NAWindow.UpdateContent(m_naEnv.NAWindow.ActiveCategory);
}
}
public override void OnCreate(object hook)
{
m_mapControl = (IMapControl3)hook;
// Get the Network Analyst Env
m_naEnv = new EngineNetworkAnalystEnvironmentClass();
}
}
}
[Visual Basic .NET]
cmdLoadLocations.vb
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.NetworkAnalyst
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
' This command allows users to load locations from another point feature layer into the selected NALayer and active category.
Namespace NAEngine
<Guid("24ED0B81-CB5E-48af-A069-1EB000208A7B"), ClassInterface(ClassInterfaceType.None), ProgId("NAEngine.LoadLocations")> _
Public NotInheritable Class LoadLocations : Inherits BaseCommand
Private m_mapControl As IMapControl3
Private m_naEnv As IEngineNetworkAnalystEnvironment
Public Sub New()
MyBase.m_caption = "Load Locations..."
End Sub
Public Overloads Overrides ReadOnly Property Enabled() As Boolean
Get
' Enabled if the active category is an input NAClass
Dim naWindowCategory As IEngineNAWindowCategory = m_naEnv.NAWindow.ActiveCategory
If naWindowCategory Is Nothing Then
Return False
End If
Dim naClass As INAClass = naWindowCategory.NAClass
If naClass Is Nothing Then
Return False
End If
Dim naClassDefinition As INAClassDefinition = naClass.ClassDefinition
If naClassDefinition Is Nothing Then
Return False
End If
Return naClassDefinition.IsInput
End Get
End Property
Public Overloads Overrides Sub OnClick()
' Get the NALayer and corresponding NAContext of the layer that
' was right-clicked on in the table of contents
' m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown
Dim naLayer As INALayer = CType(m_mapControl.CustomProperty, INALayer)
' Set the Active Analysis layer to be the layer right-clicked on
m_naEnv.NAWindow.ActiveAnalysis = naLayer
If (Not Enabled) Then
Return
End If
' Show the Property Page form for Network Analyst
Dim locations As frmLoadLocations = New frmLoadLocations()
If locations.ShowModal(m_mapControl, m_naEnv) Then
' notify that the context has changed because we have added an item to a NAClass within it
Dim contextEdit As INAContextEdit = CType(m_naEnv.NAWindow.ActiveAnalysis.Context, INAContextEdit)
contextEdit.ContextChanged()
' If loaded locations, refresh the NAWindow and the Screen
m_mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, m_mapControl.Extent)
m_naEnv.NAWindow.UpdateContent(m_naEnv.NAWindow.ActiveCategory)
End If
End Sub
Public Overloads Overrides Sub OnCreate(ByVal hook As Object)
m_mapControl = CType(hook, IMapControl3)
' Get the Network Analyst Env
m_naEnv = New EngineNetworkAnalystEnvironmentClass()
End Sub
End Class
End Namespace