About the Network Analyst Engine application Sample
[C#]
frmLoadLocations.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.NetworkAnalyst;
// This form allows users to load locations from another point feature layer into the selected NALayer and active category.
namespace NAEngine
{
/// <summary>
/// Summary description for frmLoadLocations.
/// </summary>
public class frmLoadLocations : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblInputData;
private System.Windows.Forms.CheckBox chkUseSelection;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnOK;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.ComboBox cboInputData;
bool m_okClicked;
System.Collections.IList m_listDisplayTable;
public frmLoadLocations()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cboInputData = new System.Windows.Forms.ComboBox();
this.lblInputData = new System.Windows.Forms.Label();
this.chkUseSelection = new System.Windows.Forms.CheckBox();
this.btnCancel = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cboInputData
//
this.cboInputData.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboInputData.Location = new System.Drawing.Point(82, 9);
this.cboInputData.Name = "cboInputData";
this.cboInputData.Size = new System.Drawing.Size(381, 21);
this.cboInputData.TabIndex = 0;
this.cboInputData.SelectedIndexChanged += new System.EventHandler(this.cboInputData_SelectedIndexChanged);
//
// lblInputData
//
this.lblInputData.Location = new System.Drawing.Point(12, 12);
this.lblInputData.Name = "lblInputData";
this.lblInputData.Size = new System.Drawing.Size(64, 24);
this.lblInputData.TabIndex = 1;
this.lblInputData.Text = "Input Data";
//
// chkUseSelection
//
this.chkUseSelection.Location = new System.Drawing.Point(15, 39);
this.chkUseSelection.Name = "chkUseSelection";
this.chkUseSelection.Size = new System.Drawing.Size(419, 16);
this.chkUseSelection.TabIndex = 2;
this.chkUseSelection.Text = "Use Selection";
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.Location = new System.Drawing.Point(351, 61);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(112, 32);
this.btnCancel.TabIndex = 6;
this.btnCancel.Text = "&Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// btnOK
//
this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnOK.Location = new System.Drawing.Point(223, 61);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(112, 32);
this.btnOK.TabIndex = 5;
this.btnOK.Text = "&OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// frmLoadLocations
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(479, 100);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.chkUseSelection);
this.Controls.Add(this.lblInputData);
this.Controls.Add(this.cboInputData);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "frmLoadLocations";
this.ShowInTaskbar = false;
this.Text = "Load Locations";
this.ResumeLayout(false);
}
#endregion
public bool ShowModal(IMapControl3 mapControl, IEngineNetworkAnalystEnvironment naEnv)
{
// Initialize variables
m_okClicked = false;
m_listDisplayTable = new System.Collections.ArrayList();
// Get the NALayer and NAContext
INALayer naLayer = naEnv.NAWindow.ActiveAnalysis;
INAContext naContext = naLayer.Context;
//Get the active category
IEngineNAWindowCategory2 activeCategory = naEnv.NAWindow.ActiveCategory as IEngineNAWindowCategory2;
if (activeCategory == null)
return false;
INAClass naClass = activeCategory.NAClass;
IDataset naDataset = naClass as IDataset;
IDataLayer pDataLayer = activeCategory.DataLayer;
ILayer pLayer = pDataLayer as ILayer;
IFeatureLayer pFeatureLayer = pDataLayer as IFeatureLayer;
IStandaloneTable pStandaloneTable = pDataLayer as IStandaloneTable;
esriGeometryType targetGeoType = esriGeometryType.esriGeometryNull;
String dataLayerName = "";
if (pFeatureLayer != null)
{
if (pLayer.Valid)
{
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
if (pFeatureClass != null)
{
targetGeoType = pFeatureClass.ShapeType;
dataLayerName = pLayer.Name;
}
}
}
else if (pStandaloneTable != null)
{
dataLayerName = pStandaloneTable.Name;
}
if (dataLayerName.Length == 0)
return false;
this.Text = "Load Items into " + dataLayerName;
// Loop through all the sourcedisplayTables having targetGeoType to the combo box and the
// list of displayTables
IEnumLayer sourceLayers = null;
ILayer sourceLayer = null;
IDisplayTable sourceDisplayTable = null;
UID searchInterfaceUID = new UID();
if (targetGeoType != esriGeometryType.esriGeometryNull)
{
searchInterfaceUID.Value = typeof(IFeatureLayer).GUID.ToString("B");
sourceLayers = mapControl.Map.get_Layers(searchInterfaceUID, true);
sourceLayer = sourceLayers.Next();
while (sourceLayer != null)
{
IFeatureLayer sourceFeatureLayer = sourceLayer as IFeatureLayer;
sourceDisplayTable = sourceLayer as IDisplayTable;
if ((sourceFeatureLayer != null) && (sourceDisplayTable != null))
{
IFeatureClass sourceFeatureClass = sourceFeatureLayer.FeatureClass;
esriGeometryType sourceGeoType = sourceFeatureClass.ShapeType;
if ((sourceGeoType == targetGeoType) ||
(targetGeoType == esriGeometryType.esriGeometryPoint && sourceGeoType == esriGeometryType.esriGeometryMultipoint))
{
// Add the layer name to the combobox and the layer to the list
cboInputData.Items.Add(sourceLayer.Name);
m_listDisplayTable.Add(sourceDisplayTable);
}
}
sourceLayer = sourceLayers.Next();
}
}
else //if (targetGeoType == esriGeometryType.esriGeometryNull)
{
IStandaloneTableCollection sourceStandaloneTables = mapControl.Map as IStandaloneTableCollection;
IStandaloneTable sourceStandaloneTable = null;
sourceDisplayTable = null;
int count = 0;
if (sourceStandaloneTables != null)
count = sourceStandaloneTables.StandaloneTableCount;
for (int i = 0; i < count; ++i)
{
sourceStandaloneTable = sourceStandaloneTables.get_StandaloneTable(i);
sourceDisplayTable = sourceStandaloneTable as IDisplayTable;
if ((sourceStandaloneTable != null) && (sourceDisplayTable != null))
{
// Add the table name to the combobox and the layer to the list
cboInputData.Items.Add(sourceStandaloneTable.Name);
m_listDisplayTable.Add(sourceDisplayTable);
}
}
searchInterfaceUID.Value = typeof(INALayer).GUID.ToString("B");
sourceLayers = mapControl.Map.get_Layers(searchInterfaceUID, true);
sourceLayer = sourceLayers.Next();
while (sourceLayer != null)
{
INALayer sourceNALayer = sourceLayer as INALayer;
if (sourceNALayer != null)
{
sourceStandaloneTables = sourceNALayer as IStandaloneTableCollection;
sourceStandaloneTable = null;
sourceDisplayTable = null;
count = 0;
if (sourceStandaloneTables != null)
count = sourceStandaloneTables.StandaloneTableCount;
for (int i = 0; i < count; ++i)
{
sourceStandaloneTable = sourceStandaloneTables.get_StandaloneTable(i);
sourceDisplayTable = sourceStandaloneTable as IDisplayTable;
if ((sourceStandaloneTable != null) && (sourceDisplayTable != null))
{
// Add the table name to the combobox and the layer to the list
cboInputData.Items.Add(sourceStandaloneTable.Name);
m_listDisplayTable.Add(sourceDisplayTable);
}
}
}
sourceLayer = sourceLayers.Next();
}
}
//Select the first display table from the list
if (cboInputData.Items.Count > 0)
cboInputData.SelectedIndex = 0;
// Show the window
this.ShowDialog();
// If we selected a layer and clicked OK, load the locations
if (m_okClicked && (cboInputData.SelectedIndex >= 0))
{
// Get a cursor on the source display table (either though the selection set or table)
// Use IDisplayTable because it accounts for joins, querydefs, etc.
// IDisplayTable is implemented by FeatureLayers and StandaloneTables.
//
IDisplayTable displayTable = m_listDisplayTable[cboInputData.SelectedIndex] as IDisplayTable;
ICursor cursor;
if (chkUseSelection.Checked)
{
ISelectionSet selSet;
selSet = displayTable.DisplaySelectionSet;
selSet.Search(null, false, out cursor);
}
else
{
cursor = displayTable.SearchDisplayTable(null, false);
}
// Setup NAClassLoader and Load Locations
INAClassLoader2 naClassLoader = new NAClassLoader() as INAClassLoader2;
naClassLoader.Initialize(naContext, naDataset.Name, cursor);
// Avoid loading network locations onto non-traversable portions of elements
INALocator3 locator = naContext.Locator as INALocator3;
locator.ExcludeRestrictedElements = true;
locator.CacheRestrictedElements(naContext);
int rowsIn = 0;
int rowsLocated = 0;
naClassLoader.Load(cursor, null, ref rowsIn, ref rowsLocated);
return true;
}
return false;
}
private void btnOK_Click(object sender, System.EventArgs e)
{
m_okClicked = true;
this.Close();
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
m_okClicked = false;
this.Close();
}
private void cboInputData_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Set the chkUseSelecteFeatures control based on if anything is selected or not
if (cboInputData.SelectedIndex >= 0)
{
IDisplayTable displayTable = m_listDisplayTable[cboInputData.SelectedIndex] as IDisplayTable;
chkUseSelection.Checked = (displayTable.DisplaySelectionSet.Count > 0);
chkUseSelection.Enabled = (displayTable.DisplaySelectionSet.Count > 0);
}
}
}
}
[Visual Basic .NET]
frmLoadLocations.vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.NetworkAnalyst
' This form allows users to load locations from another point feature layer into the selected NALayer and active category.
Namespace NAEngine
''' <summary>
''' Summary description for frmLoadLocations.
''' </summary>
Public Class frmLoadLocations : Inherits System.Windows.Forms.Form
Private WithEvents btnCancel As System.Windows.Forms.Button
Private WithEvents btnOK As System.Windows.Forms.Button
''' <summary>
''' Required designer variable.
''' </summary>
Private components As System.ComponentModel.Container = Nothing
Private WithEvents cboInputData As System.Windows.Forms.ComboBox
Private m_okClicked As Boolean
Private WithEvents chkUseSelection As System.Windows.Forms.CheckBox
Private WithEvents lblInputData As System.Windows.Forms.Label
Private m_listDisplayTable As System.Collections.IList
Public Sub New()
'
' Required for Windows Form Designer support
'
InitializeComponent()
End Sub
''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
''' <summary>
''' Required method for Designer support - do not modify
''' the contents of this method with the code editor.
''' </summary>
Private Sub InitializeComponent()
Me.cboInputData = New System.Windows.Forms.ComboBox
Me.btnCancel = New System.Windows.Forms.Button
Me.btnOK = New System.Windows.Forms.Button
Me.chkUseSelection = New System.Windows.Forms.CheckBox
Me.lblInputData = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'cboInputData
'
Me.cboInputData.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboInputData.Location = New System.Drawing.Point(88, 16)
Me.cboInputData.Name = "cboInputData"
Me.cboInputData.Size = New System.Drawing.Size(352, 21)
Me.cboInputData.TabIndex = 0
'
'btnCancel
'
Me.btnCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnCancel.Location = New System.Drawing.Point(328, 104)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(112, 32)
Me.btnCancel.TabIndex = 6
Me.btnCancel.Text = "&Cancel"
'
'btnOK
'
Me.btnOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnOK.Location = New System.Drawing.Point(200, 104)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(112, 32)
Me.btnOK.TabIndex = 5
Me.btnOK.Text = "&OK"
'
'chkUseSelection
'
Me.chkUseSelection.Location = New System.Drawing.Point(21, 43)
Me.chkUseSelection.Name = "chkUseSelection"
Me.chkUseSelection.Size = New System.Drawing.Size(419, 16)
Me.chkUseSelection.TabIndex = 7
Me.chkUseSelection.Text = "Use Selection"
'
'lblInputData
'
Me.lblInputData.Location = New System.Drawing.Point(18, 19)
Me.lblInputData.Name = "lblInputData"
Me.lblInputData.Size = New System.Drawing.Size(64, 24)
Me.lblInputData.TabIndex = 8
Me.lblInputData.Text = "Input Data"
'
'frmLoadLocations
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(456, 148)
Me.Controls.Add(Me.lblInputData)
Me.Controls.Add(Me.chkUseSelection)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.btnOK)
Me.Controls.Add(Me.cboInputData)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Name = "frmLoadLocations"
Me.ShowInTaskbar = False
Me.Text = "Load Locations"
Me.ResumeLayout(False)
End Sub
#End Region
Public Function ShowModal(ByVal mapControl As IMapControl3, ByVal naEnv As IEngineNetworkAnalystEnvironment) As Boolean
' Initialize variables
m_okClicked = False
m_listDisplayTable = New System.Collections.ArrayList()
' Get the NALayer and NAContext
Dim naLayer As INALayer = naEnv.NAWindow.ActiveAnalysis
Dim naContext As INAContext = naLayer.Context
' Get the active category
Dim activeCategory As IEngineNAWindowCategory2 = TryCast(naEnv.NAWindow.ActiveCategory, IEngineNAWindowCategory2)
If (activeCategory Is Nothing) Then
Return False
End If
Dim naClass As INAClass = activeCategory.NAClass
Dim naDataset As IDataset = TryCast(naClass, IDataset)
Dim pDataLayer As IDataLayer = activeCategory.DataLayer
Dim pLayer As ILayer = TryCast(pDataLayer, ILayer)
Dim pFeatureLayer As IFeatureLayer = TryCast(pDataLayer, IFeatureLayer)
Dim pStandaloneTable As IStandaloneTable = TryCast(pDataLayer, IStandaloneTable)
Dim targetGeoType As esriGeometryType = esriGeometryType.esriGeometryNull
Dim dataLayerName As String = ""
If Not pFeatureLayer Is Nothing Then
If (pLayer.Valid) Then
Dim pFeatureClass As IFeatureClass = pFeatureLayer.FeatureClass
If Not pFeatureClass Is Nothing Then
targetGeoType = pFeatureClass.ShapeType
dataLayerName = pLayer.Name
End If
End If
ElseIf Not pStandaloneTable Is Nothing Then
dataLayerName = pStandaloneTable.Name
End If
If dataLayerName.Length = 0 Then
Return False
End If
Me.Text = "Load Items into " + dataLayerName
' Loop through all the sourcedisplayTables having targetGeoType to the combo box and the
' list of displayTables
Dim sourceLayers As IEnumLayer = Nothing
Dim sourceLayer As ILayer = Nothing
Dim sourceDisplayTable As IDisplayTable = Nothing
Dim searchInterfaceUID As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UID()
If targetGeoType <> esriGeometryType.esriGeometryNull Then
searchInterfaceUID.Value = GetType(IFeatureLayer).GUID.ToString("B")
sourceLayers = mapControl.Map.Layers(searchInterfaceUID, True)
sourceLayer = sourceLayers.Next()
While (Not sourceLayer Is Nothing)
Dim sourceFeatureLayer As IFeatureLayer = TryCast(sourceLayer, IFeatureLayer)
sourceDisplayTable = TryCast(sourceLayer, IDisplayTable)
If ((Not sourceFeatureLayer Is Nothing) And (Not sourceDisplayTable Is Nothing)) Then
Dim sourceFeatureClass As IFeatureClass = sourceFeatureLayer.FeatureClass
Dim sourceGeoType As esriGeometryType = sourceFeatureClass.ShapeType
If ((sourceGeoType = targetGeoType) Or (targetGeoType = esriGeometryType.esriGeometryPoint And sourceGeoType = esriGeometryType.esriGeometryMultipoint)) Then
' Add the layer name to the combobox and the layer to the list
cboInputData.Items.Add(sourceLayer.Name)
m_listDisplayTable.Add(sourceDisplayTable)
End If
End If
sourceLayer = sourceLayers.Next()
End While
Else 'if (targetGeoType == esriGeometryType.esriGeometryNull)
Dim sourceStandaloneTables As IStandaloneTableCollection = TryCast(mapControl.Map, IStandaloneTableCollection)
Dim sourceStandaloneTable As IStandaloneTable = Nothing
sourceDisplayTable = Nothing
Dim count As Integer = 0
If Not sourceStandaloneTables Is Nothing Then
count = sourceStandaloneTables.StandaloneTableCount
End If
For i As Integer = 0 To count - 1 Step 1
sourceStandaloneTable = sourceStandaloneTables.StandaloneTable(i)
sourceDisplayTable = TryCast(sourceStandaloneTable, IDisplayTable)
If ((Not sourceStandaloneTable Is Nothing) And (Not sourceDisplayTable Is Nothing)) Then
' Add the table name to the combobox and the layer to the list
cboInputData.Items.Add(sourceStandaloneTable.Name)
m_listDisplayTable.Add(sourceDisplayTable)
End If
Next
searchInterfaceUID.Value = GetType(INALayer).GUID.ToString("B")
sourceLayers = mapControl.Map.Layers(searchInterfaceUID, True)
sourceLayer = sourceLayers.Next()
While (Not sourceLayer Is Nothing)
Dim sourceNALayer As INALayer = TryCast(sourceLayer, INALayer)
If Not sourceNALayer Is Nothing Then
sourceStandaloneTables = TryCast(sourceNALayer, IStandaloneTableCollection)
sourceStandaloneTable = Nothing
sourceDisplayTable = Nothing
count = 0
If Not sourceStandaloneTables Is Nothing Then
count = sourceStandaloneTables.StandaloneTableCount
End If
For i As Integer = 0 To count - 1 Step 1
sourceStandaloneTable = sourceStandaloneTables.StandaloneTable(i)
sourceDisplayTable = TryCast(sourceStandaloneTable, IDisplayTable)
If ((Not sourceStandaloneTable Is Nothing) And (Not sourceDisplayTable Is Nothing)) Then
' Add the table name to the combobox and the layer to the list
cboInputData.Items.Add(sourceStandaloneTable.Name)
m_listDisplayTable.Add(sourceDisplayTable)
End If
Next
End If
sourceLayer = sourceLayers.Next()
End While
End If
'Select the first point feature layer from the list
If cboInputData.Items.Count > 0 Then
cboInputData.SelectedIndex = 0
End If
' Show the window
Me.ShowDialog()
' If we selected a layer and clicked OK, load the locations
If m_okClicked AndAlso (cboInputData.SelectedIndex >= 0) Then
' Get a cursor on the source display table (either though the selection set or table)
' Use IDisplayTable because it accounts for joins, querydefs, etc.
' IDisplayTable is implemented by FeatureLayers and StandaloneTables.
Dim displayTable As IDisplayTable = TryCast(m_listDisplayTable(cboInputData.SelectedIndex), IDisplayTable)
Dim cursor As ICursor = Nothing
If chkUseSelection.Checked Then
Dim selSet As ISelectionSet
selSet = displayTable.DisplaySelectionSet
selSet.Search(Nothing, False, cursor)
Else
cursor = displayTable.SearchDisplayTable(Nothing, False)
End If
' Setup NAClassLoader and Load Locations
Dim naClassLoader As INAClassLoader2 = New NAClassLoader()
naClassLoader.Initialize(naContext, naDataset.Name, cursor)
' Avoid loading network locations onto non-traversable portions of elements
Dim locator As INALocator3 = TryCast(naContext.Locator, INALocator3)
locator.ExcludeRestrictedElements = True
locator.CacheRestrictedElements(naContext)
Dim rowsIn As Integer = 0
Dim rowsLocated As Integer = 0
naClassLoader.Load(cursor, Nothing, rowsIn, rowsLocated)
Return True
Else
Return False
End If
End Function
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
m_okClicked = True
Me.Close()
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
m_okClicked = False
Me.Close()
End Sub
Private Sub cboPointLayers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboInputData.SelectedIndexChanged
' Set the chkUseSelecteFeatures control based on if anything is selected or not
If cboInputData.SelectedIndex >= 0 Then
Dim displayTable As IDisplayTable = TryCast(m_listDisplayTable(cboInputData.SelectedIndex), IDisplayTable)
chkUseSelection.Checked = (displayTable.DisplaySelectionSet.Count > 0)
chkUseSelection.Enabled = (displayTable.DisplaySelectionSet.Count > 0)
End If
End Sub
End Class
End Namespace