ArcObjects Library Reference  

NAClassToTextfileCmd

About the Export any network analysis class to a text file Sample

[C#]

NAClassToTextfileCmd.cs

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.NetworkAnalyst;
using ESRI.ArcGIS.NetworkAnalystUI;

namespace ExportNAClass
{
	/// <summary>
	/// This sample command allows you export a text file version
	/// of the active class in the ArcGIS Network Analyst extension window after 
	/// completion of a successful solve.
	/// </summary>
	/// 
	[ClassInterface(ClassInterfaceType.None)]
	[Guid("08CE5834-8267-4a73-AFDD-2821B4B1F6EC")]
	[ProgId("ExportNAClass.NAClassToTextfileCmd")]
	public sealed class NAClassToTextfileCmd : BaseCommand, INAWindowCommand
	{
		private const string DELIMITER = "\t";
		private INetworkAnalystExtension m_naExt;

		public NAClassToTextfileCmd()
		{
			base.m_category = "Developer Samples";
			base.m_caption = "Export To text file...";
			base.m_message = "Export a network analysis class to a text file.";
			base.m_toolTip = "Export a network analysis class to a text file.";
			base.m_name = "NAClassToTextFileCmd";

			try
			{
				string bitmapResourceName = GetType().Name + ".bmp";
				base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
			}
			catch (Exception ex)
			{
				System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
			}
		}

		#region COM Registration Function(s)

		[ComRegisterFunction()]
		[ComVisible(false)]
		static void RegisterFunction(Type registerType)
		{
			// Required for ArcGIS Component Category Registrar support
			ArcGISCategoryRegistration(registerType);
		}

		[ComUnregisterFunction()]
		[ComVisible(false)]
		static void UnregisterFunction(Type registerType)
		{
			// Required for ArcGIS Component Category Registrar support
			ArcGISCategoryUnregistration(registerType);
		}

		#region ArcGIS Component Category Registrar generated code
		/// <summary>
		/// Required method for ArcGIS Component Category registration -
		/// Do not modify the contents of this method with the code editor.
		/// </summary>
		private static void ArcGISCategoryRegistration(Type registerType)
		{
			string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
			ESRI.ArcGIS.ADF.CATIDs.MxCommands.Register(regKey);
			// Register with NetworkAnalystWindowItemsCommand to get the 
			// command to show up when you right click on the class in the NAWindow
			ESRI.ArcGIS.ADF.CATIDs.NetworkAnalystWindowCategoryCommand.Register(regKey);
		}

		/// <summary>
		/// Required method for ArcGIS Component Category unregistration -
		/// Do not modify the contents of this method with the code editor.
		/// </summary>
		private static void ArcGISCategoryUnregistration(Type registerType)
		{
			string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
			ESRI.ArcGIS.ADF.CATIDs.MxCommands.Unregister(regKey);
			ESRI.ArcGIS.ADF.CATIDs.NetworkAnalystWindowCategoryCommand.Unregister(regKey);
		}

		#endregion

		#endregion

		#region "NAWindow Interaction"
		private INALayer GetActiveAnalysisLayer()
		{
			if (m_naExt != null)
				return m_naExt.NAWindow.ActiveAnalysis;
			else
				return null;
		}

		private INAWindowCategory2 GetActiveCategory()
		{
			if (m_naExt != null)
				return m_naExt.NAWindow.ActiveCategory as INAWindowCategory2;
			else
				return null;
		}
		#endregion

		#region "Overridden INAWindowCommand Methods"
		public bool Applies(INALayer naLayer, INAWindowCategory Category)
		{
			return true;
		}
		#endregion

		#region "Overridden BaseCommand Methods"
		public override void OnCreate(object hook)
		{
			// Try to get the ArcGIS Network Analyst extension from the desktop app's extensions
			IApplication app;
			app = hook as IApplication;
			if (app != null)
				m_naExt = app.FindExtensionByName("Network Analyst") as INetworkAnalystExtension;
		}

		/// <summary>
		/// This command will be enabled only for a NAClass
		/// associated with a successful solve
		/// </summary>
		public override bool Enabled
		{
			get
			{
				// there must be an active analysis layer
				INALayer naLayer = GetActiveAnalysisLayer();
				if (naLayer != null)
				{
					// the context must be valid
					INAContext naContext = naLayer.Context;
					if (naContext != null)
					{
						return true;
					}
				}
				return false;
			}
		}

		public override void OnClick()
		{
			try
			{
				ExportToText();
			}
			catch (Exception exception)
			{
				MessageBox.Show(exception.Message, "Error");
			}

		}
		#endregion

		private void ExportToText()
		{
			SaveFileDialog sfDialog = new SaveFileDialog();
			SetUpSaveDialog(ref sfDialog);
			// generate the dialog and verify the user successfully clicked save
			DialogResult dResult = sfDialog.ShowDialog();

			if (dResult == DialogResult.OK)
			{
				// set up the text file to be written
				FileInfo t = new FileInfo(sfDialog.FileName);
				StreamWriter swText = t.CreateText();

				ITable table = GetActiveCategory().DataLayer as ITable;

				// write the first line of the text file as column headers
				swText.WriteLine(GenerateColumnHeaderString(ref table));

				// iterate through the table associated with the class
				// to write out each line of data into the text file
				ICursor cursor = table.Search(null, true);
				IRow row = cursor.NextRow();
				while (row != null)
				{
					swText.WriteLine(GenerateDataString(ref row));
					row = cursor.NextRow();
				}
				swText.Close();
			}
		}

		private void SetUpSaveDialog(ref SaveFileDialog sfDialog)
		{
			sfDialog.AddExtension = true;
			sfDialog.Title = "Save an export of the specified class in the active analysis...";
			sfDialog.DefaultExt = "txt";
			sfDialog.OverwritePrompt = true;
			sfDialog.FileName = "ClassExport.txt";
			sfDialog.Filter = "Text files (*.txt;*.csv;*.asc;*.tab)|*.txt;*.tab;*.asc;*.csv";
			sfDialog.InitialDirectory = "c:\\";
		}

		private string GenerateColumnHeaderString(ref ITable table)
		{
			IField field = null;

			// export the names of the fields (tab delimited) as the first line of the export
			string fieldNames = "";
			for (int i = 0; i < table.Fields.FieldCount; i++)
			{
				field = table.Fields.get_Field(i);
				if (i > 0) fieldNames += DELIMITER;

				string columnName = field.Name.ToString();

				// point classes have a special output of X and Y, other classes just output "Shape"
				if (field.Type == esriFieldType.esriFieldTypeGeometry)
				{
					if (field.GeometryDef.GeometryType == esriGeometryType.esriGeometryPoint)
					{
						columnName = "X";
						columnName += DELIMITER;
						columnName += "Y";
					}
				}
				fieldNames += columnName;
			}
			return fieldNames;
		}

		private string GenerateDataString(ref IRow row)
		{
			string textOut = "";

			// On a zero-based index, iterate through the fields in the collection.
			for (int i = 0; i < row.Fields.FieldCount; i++)
			{
				if (i > 0) textOut += DELIMITER;
				IField field = row.Fields.get_Field(i);

				// for shape fields in a point layer, export the associated X and Y coordinates
				if (field.Type == esriFieldType.esriFieldTypeGeometry)
				{
					if (field.GeometryDef.GeometryType == esriGeometryType.esriGeometryPoint)
					{
						// x y location information must be retrieved from the Feature
						IPoint point = row.get_Value(i) as ESRI.ArcGIS.Geometry.Point;
						textOut += point.X.ToString();
						textOut += DELIMITER;
						textOut += point.Y.ToString();
					}
					else
					{
						textOut += "Shape";
					}
				}
				else
				{
					textOut += row.get_Value(i).ToString();
				}
			}
			return textOut;
		}
	}
}

[Visual Basic .NET]

NAClassToTextfileCmd.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.NetworkAnalyst
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.NetworkAnalystUI

Namespace ExportNAClass
	''' <summary>
	''' This sample command allows you export a text file version
	''' of the active class in the Network Analyst window after 
	''' completion of a successful solve.
	''' </summary>
	''' 
	<ClassInterface(ClassInterfaceType.None), Guid("7C12A530-759A-4B12-9241-2215403483E8"), ProgId("ExportNAClass.NAClassToTextfileCmd")> _
	Public NotInheritable Class NAClassToTextfileCmd : Inherits ESRI.ArcGIS.ADF.BaseClasses.BaseCommand : Implements INAWindowCommand
		Private Const DELIMITER As String = Constants.vbTab
		Private m_naExt As INetworkAnalystExtension

		' set up the bitmap for the command icon
		<DllImport("gdi32.dll")> _
		Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
		End Function
		Private Shadows m_bitmap As System.Drawing.Bitmap
		Private m_hBitmap As IntPtr

		Public Sub New()

			ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)

			' set up the bitmap transparency
			Dim res As String() = Me.GetType().Assembly.GetManifestResourceNames()
			If res.GetLength(0) > 0 Then
				m_bitmap = New System.Drawing.Bitmap(Me.GetType().Assembly.GetManifestResourceStream(res(0)))
				If Not m_bitmap Is Nothing Then
					m_bitmap.MakeTransparent(m_bitmap.GetPixel(0, 0))
					m_hBitmap = m_bitmap.GetHbitmap()
				End If
			End If
		End Sub

		Protected Overrides Sub Finalize()
			If m_hBitmap.ToInt32() <> 0 Then
				DeleteObject(m_hBitmap)
			End If
		End Sub

#Region "Component Category Registration"
		<ComRegisterFunction()> _
		Private Shared Sub Reg(ByVal regKey As String)
			ESRI.ArcGIS.ADF.CATIDs.ControlsCommands.Register(regKey)
			ESRI.ArcGIS.ADF.CATIDs.MxCommands.Register(regKey)
			' Register with NetworkAnalystWindowCategoryCommand to get the 
			' command to show up when you right click on the class in the NAWindow
			ESRI.ArcGIS.ADF.CATIDs.NetworkAnalystWindowCategoryCommand.Register(regKey)
		End Sub

		<ComUnregisterFunction()> _
		Private Shared Sub Unreg(ByVal regKey As String)
			ESRI.ArcGIS.ADF.CATIDs.ControlsCommands.Unregister(regKey)
			ESRI.ArcGIS.ADF.CATIDs.MxCommands.Unregister(regKey)
		End Sub
#End Region

#Region "NAWindow Interaction"
		Private Function GetActiveAnalysisLayer() As INALayer
			If Not m_naExt Is Nothing Then
				Return m_naExt.NAWindow.ActiveAnalysis
			Else
				Return Nothing
			End If
		End Function

		Private Function GetActiveCategory() As INAWindowCategory2
			' Remove the next 2 lines for an engine only install
			If Not m_naExt Is Nothing Then
				Return TryCast(m_naExt.NAWindow.ActiveCategory, INAWindowCategory2)
			Else
				Return Nothing
			End If
		End Function
#End Region

#Region "Overridden BaseCommand Methods"
		Public Overrides Sub OnCreate(ByVal hook As Object)
			' Try to get the network analyst extension from the desktop app's extensions
			Dim app As IApplication
			app = TryCast(hook, IApplication)
			If Not app Is Nothing Then
				m_naExt = TryCast(app.FindExtensionByName("Network Analyst"), INetworkAnalystExtension)
			End If
		End Sub

		''' <summary>
		''' This command will be enabled only for a NAClass
		''' associated with a successful solve
		''' </summary>
		Public Overrides ReadOnly Property Enabled() As Boolean
			Get
				' there must be an active analysis layer
				Dim naLayer As INALayer = GetActiveAnalysisLayer()
				If Not naLayer Is Nothing Then
					' the context must be valid
					Dim naContext As INAContext = naLayer.Context
					If Not naContext Is Nothing Then
						Return True
					End If
				End If
				Return False
			End Get
		End Property

		Public Overrides ReadOnly Property Message() As String
			Get
				Return "Export a network analysis class to a text file."
			End Get
		End Property

		Public Overrides ReadOnly Property Bitmap() As Integer
			Get
				Return m_hBitmap.ToInt32()
			End Get
		End Property

		Public Overrides ReadOnly Property Tooltip() As String
			Get
				Return "Export a network analysis class to a text file."
			End Get
		End Property

		Public Overrides ReadOnly Property Name() As String
			Get
				Return "NAClassToTextFileCmd"
			End Get
		End Property

		Public Overrides ReadOnly Property Caption() As String
			Get
				Return "Export To text file..."
			End Get
		End Property

		Public Overrides ReadOnly Property Category() As String
			Get
				Return "Developer Samples"
			End Get
		End Property

		Public Overrides Sub OnClick()
			Try
				ExportToText()
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Error")
			End Try

		End Sub
#End Region

#Region "Overridden INAWindowCommand Methods"
		Public Function Applies(ByVal naLayer As INALayer, ByVal Category As INAWindowCategory) As Boolean Implements ESRI.ArcGIS.NetworkAnalystUI.INAWindowCommand.Applies
			Return True
		End Function
#End Region

		Private Sub ExportToText()
			Dim sfDialog As SaveFileDialog = New SaveFileDialog()
			SetUpSaveDialog(sfDialog)
			' generate the dialog and verify the user successfully clicked save
			Dim dResult As DialogResult = sfDialog.ShowDialog()

			If dResult = System.Windows.Forms.DialogResult.OK Then
				' set up the text file to be written
				Dim t As FileInfo = New FileInfo(sfDialog.FileName)
				Dim swText As StreamWriter = t.CreateText()

				Dim table As ITable = TryCast(GetActiveCategory().DataLayer, ITable)

				' write the first line of the text file as column headers
				swText.WriteLine(GenerateColumnHeaderString(table))

				' iterate through the table associated with the class
				' to write out each line of data into the text file
				Dim cursor As ICursor = table.Search(Nothing, True)
				Dim row As IRow = cursor.NextRow()
				Do While Not row Is Nothing
					swText.WriteLine(GenerateDataString(row))
					row = cursor.NextRow()
				Loop
				swText.Close()
			End If
		End Sub

		Private Sub SetUpSaveDialog(ByRef sfDialog As SaveFileDialog)
			sfDialog.AddExtension = True
			sfDialog.Title = "Save an export of the specified class in the active analysis..."
			sfDialog.DefaultExt = "txt"
			sfDialog.OverwritePrompt = True
			sfDialog.FileName = "ClassExport.txt"
			sfDialog.Filter = "Text files (*.txt;*.csv;*.asc;*.tab)|*.txt;*.tab;*.asc;*.csv"
			sfDialog.InitialDirectory = "c:\"
		End Sub

		Private Function GenerateColumnHeaderString(ByRef table As ITable) As String
			Dim field As IField = Nothing

			' export the names of the fields (tab delimited) as the first line of the export
			Dim fieldNames As String = ""
			Dim i As Integer = 0
			Do While i < table.Fields.FieldCount
				field = table.Fields.Field(i)
				If i > 0 Then
					fieldNames &= DELIMITER
				End If

				Dim columnName As String = field.Name.ToString()

				' point classes have a special output of X and Y, other classes just output "Shape"
				If field.Type = esriFieldType.esriFieldTypeGeometry Then
					If field.GeometryDef.GeometryType = esriGeometryType.esriGeometryPoint Then
						columnName = "X"
						columnName &= DELIMITER
						columnName &= "Y"
					End If
				End If
				fieldNames &= columnName
				i += 1
			Loop
			Return fieldNames
		End Function

		Private Function GenerateDataString(ByRef row As IRow) As String
			Dim textOut As String = ""

			' On a zero-based index, iterate through the fields in the collection.
			Dim i As Integer = 0
			Do While i < row.Fields.FieldCount
				If i > 0 Then
					textOut &= DELIMITER
				End If
				Dim field As IField = row.Fields.Field(i)

				' for shape fields in a point layer, export the associated X and Y coordinates
				If field.Type = esriFieldType.esriFieldTypeGeometry Then
					If field.GeometryDef.GeometryType = esriGeometryType.esriGeometryPoint Then
						' x y location information must be retrieved from the Feature
						Dim point As IPoint = TryCast(row.Value(i), ESRI.ArcGIS.Geometry.Point)
						textOut &= point.X.ToString()
						textOut &= DELIMITER
						textOut &= point.Y.ToString()
					Else
						textOut &= "Shape"
					End If
				Else
					textOut &= row.Value(i).ToString()
				End If
				i += 1
			Loop
			Return textOut
		End Function
	End Class
End Namespace