ArcObjects Library Reference  

SubsetHelper

About the Subset network evaluators Sample

[C#]

SubsetHelper.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.ComponentModel;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;

/// <summary>
/// Utility methods for working with parameter values, and other shared utilities in setting up
/// these custom subset evaluators.
/// </summary>

namespace SubsetNetworkEvaluators
{
	[Flags]
	public enum VarType
	{
		Empty = 0x0000,		//VT_EMPTY
		Null = 0x0001,		//VT_NULL
		Short = 0x0002,		//VT_I2
		Integer = 0x0003,	//VT_I4
		Float = 0x0004,		//VT_R4
		Double = 0x0005,	//VT_R8
		Date = 0x0007,		//VT_DATE
		String = 0x0008,    //VT_BSTR
		Bool = 0x000B,		//VT_BOOL
		ComObject = 0x000D,	//VT_UNKNOWN
		Array = 0x2000      //VT_ARRAY
	}; // enum VarType

	public class SubsetHelper
	{
		public static int FindParameter(IArray netAttributeParams, string searchName)
		{
			if (netAttributeParams == null || searchName.Length <= 0)
				return -1;

			string compareName;
			INetworkAttributeParameter netAttributeParam;
			int count = netAttributeParams.Count;
			for (int i = 0; i < count; ++i)
			{
				netAttributeParam = netAttributeParams.get_Element(i) as INetworkAttributeParameter;
				if (netAttributeParam != null)
				{
					compareName = netAttributeParam.Name;
					if (String.Compare(searchName, compareName, true) == 0)
						return i;
				}
			}

			return -1;
		}

		public static List<string> GetSourceNames(List<INetworkSource> netSources)
		{
			List<string> sourceNames = new List<string>();
			if (netSources == null)
				return sourceNames;

			foreach (INetworkSource netSource in netSources)
				sourceNames.Add(netSource.Name);

			return sourceNames;
		}

		public static List<INetworkSource> GetSourceList(IArray netSourcesArray)
		{
			List<INetworkSource> netSources = new List<INetworkSource>();
			int count = netSourcesArray.Count;
			INetworkSource netSource;
			for (int i = 0; i < count; ++i)
			{
				netSource = netSourcesArray.get_Element(i) as INetworkSource;
				if (netSource != null)
					netSources.Add(netSource);
			}
			return netSources;
		}

		public static List<INetworkSource> GetSourceList(List<INetworkSource> netSources, esriNetworkElementType eType)
		{
			List<esriNetworkElementType> eTypes = new List<esriNetworkElementType>();
			eTypes.Add(eType);
			return GetSourceList(netSources, eTypes);
		}

		public static List<INetworkSource> GetSourceList(List<INetworkSource> netSources, List<esriNetworkElementType> eTypes)
		{
			List<INetworkSource> subList = new List<INetworkSource>();
			if (netSources == null || eTypes == null)
				return subList;

			foreach (INetworkSource netSource in netSources)
			{
				foreach (esriNetworkElementType eType in eTypes)
				{
					if (netSource.ElementType == eType)
					{
						subList.Add(netSource);
						break;
					}
				}
			}
			return subList;
		}

		public static IArray RemoveAttributeByName(IArray netAttributes, string name)
		{
			return RemoveAttributesByKeyName(netAttributes, name, true);
		}

		public static IArray RemoveAttributesByPrefix(IArray netAttributes, string prefix)
		{
			return RemoveAttributesByKeyName(netAttributes, prefix, true);
		}

		public static IArray RemoveAttributesBySuffix(IArray netAttributes, string suffix)
		{
			return RemoveAttributesByKeyName(netAttributes, suffix, false);
		}

		public static IArray RemoveAttributesByKeyName(IArray netAttributes, string keyName, bool keyIsPrefix)
		{
			IArray preservedNetAttributes = new ArrayClass();

			int keyNameLen = keyName.Length;
			int netAttributeNameLen;
			INetworkAttribute netAttribute;
			string netAttributeName;
			bool isKeyAttribute;
			bool ignoreCase = true;

			int count = netAttributes.Count;
			for (int i = 0; i < count; ++i)
			{
				netAttribute = netAttributes.get_Element(i) as INetworkAttribute;
				if (netAttribute == null)
					continue;

				netAttributeName = netAttribute.Name;
				netAttributeNameLen = netAttributeName.Length;

				isKeyAttribute = false;
				if (keyNameLen == 0)
					isKeyAttribute = false;
				else if (netAttributeNameLen < keyNameLen)
					isKeyAttribute = false;
				else
				{
					int startIndex = 0;
					if (!keyIsPrefix)
						startIndex = netAttributeNameLen - keyNameLen;

					if (String.Compare(netAttributeName.Substring(startIndex, keyNameLen), keyName, ignoreCase) == 0)
						isKeyAttribute = true;
				}

				if (!isKeyAttribute)
					preservedNetAttributes.Add(netAttribute);
			}

			return preservedNetAttributes;
		}

		public static List<int> FindAttributeIndexes(IArray netAttributes, esriNetworkAttributeUsageType usage, esriNetworkAttributeDataType dataType, bool searchTimeUnits, bool ignoreDataType)
		{
			INetworkAttribute2 netAttribute = null;
			esriNetworkAttributeUnits units = esriNetworkAttributeUnits.esriNAUUnknown;
			bool isSearchUnits = false;
			bool isUnknownUnits = false;
			bool isTimeUnits = false;

			List<int> netAttributeIndexes = new List<int>();
			int count = netAttributes.Count;

			for (int i = 0; i < count; ++i)
			{
				netAttribute = netAttributes.get_Element(i) as INetworkAttribute2;
				if (netAttribute == null)
					continue;

				if (netAttribute.UsageType == usage && (ignoreDataType || netAttribute.DataType == dataType))
				{
					units = netAttribute.Units;
					isSearchUnits = false;

					if (usage != esriNetworkAttributeUsageType.esriNAUTCost)
						isSearchUnits = true;
					else
					{
						isUnknownUnits = false;
						if (units == esriNetworkAttributeUnits.esriNAUUnknown)
							isUnknownUnits = true;

						isTimeUnits = false;
						if (!isUnknownUnits)
						{
							if (units == esriNetworkAttributeUnits.esriNAUMinutes ||
								units == esriNetworkAttributeUnits.esriNAUSeconds ||
								units == esriNetworkAttributeUnits.esriNAUHours ||
								units == esriNetworkAttributeUnits.esriNAUDays)
							{
								isTimeUnits = true;
							}

							if (searchTimeUnits)
								isSearchUnits = isTimeUnits;
							else
								isSearchUnits = !isTimeUnits;
						}
					}
					if (isSearchUnits)
						netAttributeIndexes.Add(i);
				}
			}

			return netAttributeIndexes;
		}

		public static List<INetworkAttribute2> FindAttributes(IArray netAttributesArray, List<int> netAttributeIndexes)
		{
			List<INetworkAttribute2> netAttributes = new List<INetworkAttribute2>();
			foreach (int i in netAttributeIndexes)
			{
				INetworkAttribute2 netAttribute = netAttributesArray.get_Element(i) as INetworkAttribute2;
				if (netAttribute != null)
					netAttributes.Add(netAttribute);
			}

			return netAttributes;
		}

		public static void SetDefaultEvaluator(IEvaluatedNetworkAttribute netAttribute, object defaultValue, esriNetworkElementType eType)
		{
			INetworkConstantEvaluator constEvaluator = new NetworkConstantEvaluatorClass();
			constEvaluator.ConstantValue = defaultValue;
			INetworkEvaluator eval = constEvaluator as INetworkEvaluator;
			netAttribute.set_DefaultEvaluator(eType, eval);
		}

		public static void SetEvaluators(IEvaluatedNetworkAttribute netAttribute, INetworkSource netSource, Type t)
		{
			esriNetworkElementType eType = netSource.ElementType;
			if (eType == esriNetworkElementType.esriNETEdge)
			{
				SetEvaluator(netAttribute, netSource, t, esriNetworkEdgeDirection.esriNEDAlongDigitized);
				SetEvaluator(netAttribute, netSource, t, esriNetworkEdgeDirection.esriNEDAgainstDigitized);
			}
			else
			{
				SetEvaluator(netAttribute, netSource, t, esriNetworkEdgeDirection.esriNEDNone);
			}
		}

		public static void SetEvaluator(IEvaluatedNetworkAttribute netAttribute, INetworkSource netSource, Type t, esriNetworkEdgeDirection dirType)
		{
			object obj = Activator.CreateInstance(t);
			INetworkEvaluator eval = obj as INetworkEvaluator;
			netAttribute.set_Evaluator(netSource, dirType, eval);
		}
	}
}
[Visual Basic .NET]

SubsetHelper.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.ComponentModel
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geodatabase

''' <summary>
''' Utility methods for working with parameter values, and other shared utilities in setting up
''' these custom subset evaluators.
''' </summary>

Namespace SubsetNetworkEvaluators
	<Flags()> _
	Public Enum VarType
		Empty = &H0	'VT_EMPTY
		Null = &H1 'VT_NULL
		[Short] = &H2 'VT_I2
		[Integer] = &H3	'VT_I4
		Float = &H4	'VT_R4
		[Double] = &H5 'VT_R8
		[Date] = &H7 'VT_DATE
		[String] = &H8 'VT_BSTR
		Bool = &HB 'VT_BOOL
		ComObject = &HD	'VT_UNKNOWN
		Array = &H2000 'VT_ARRAY
	End Enum ' enum VarType

	Public Class SubsetHelper
		Public Shared Function FindParameter(ByVal netAttributeParams As IArray, ByVal searchName As String) As Integer
			If netAttributeParams Is Nothing OrElse searchName.Length <= 0 Then
				Return -1
			End If

			Dim compareName As String
			Dim netAttributeParam As INetworkAttributeParameter
			Dim count As Integer = netAttributeParams.Count
			Dim i As Integer = 0
			Do While i < count
				netAttributeParam = TryCast(netAttributeParams.Element(i), INetworkAttributeParameter)
				If Not netAttributeParam Is Nothing Then
					compareName = netAttributeParam.Name
					If String.Compare(searchName, compareName, True) = 0 Then
						Return i
					End If
				End If
				i += 1
			Loop

			Return -1
		End Function

		Public Shared Function GetSourceNames(ByVal netSources As List(Of INetworkSource)) As List(Of String)
			Dim sourceNames As List(Of String) = New List(Of String)()
			If netSources Is Nothing Then
				Return sourceNames
			End If

			For Each netSource As INetworkSource In netSources
				sourceNames.Add(netSource.Name)
			Next netSource

			Return sourceNames
		End Function

		Public Shared Function GetSourceList(ByVal netSourcesArray As IArray) As List(Of INetworkSource)
			Dim netSources As List(Of INetworkSource) = New List(Of INetworkSource)()
			Dim count As Integer = netSourcesArray.Count
			Dim netSource As INetworkSource
			Dim i As Integer = 0
			Do While i < count
				netSource = TryCast(netSourcesArray.Element(i), INetworkSource)
				If Not netSource Is Nothing Then
					netSources.Add(netSource)
				End If
				i += 1
			Loop
			Return netSources
		End Function

		Public Shared Function GetSourceList(ByVal netSources As List(Of INetworkSource), ByVal eType As esriNetworkElementType) As List(Of INetworkSource)
			Dim eTypes As List(Of esriNetworkElementType) = New List(Of esriNetworkElementType)()
			eTypes.Add(eType)
			Return GetSourceList(netSources, eTypes)
		End Function

		Public Shared Function GetSourceList(ByVal netSources As List(Of INetworkSource), ByVal eTypes As List(Of esriNetworkElementType)) As List(Of INetworkSource)
			Dim subList As List(Of INetworkSource) = New List(Of INetworkSource)()
			If netSources Is Nothing OrElse eTypes Is Nothing Then
				Return subList
			End If

			For Each netSource As INetworkSource In netSources
				For Each eType As esriNetworkElementType In eTypes
					If netSource.ElementType = eType Then
						subList.Add(netSource)
						Exit For
					End If
				Next eType
			Next netSource
			Return subList
		End Function

		Public Shared Function RemoveAttributeByName(ByVal netAttributes As IArray, ByVal name As String) As IArray
			Return RemoveAttributesByKeyName(netAttributes, name, True)
		End Function

		Public Shared Function RemoveAttributesByPrefix(ByVal netAttributes As IArray, ByVal prefix As String) As IArray
			Return RemoveAttributesByKeyName(netAttributes, prefix, True)
		End Function

		Public Shared Function RemoveAttributesBySuffix(ByVal netAttributes As IArray, ByVal suffix As String) As IArray
			Return RemoveAttributesByKeyName(netAttributes, suffix, False)
		End Function

		Public Shared Function RemoveAttributesByKeyName(ByVal netAttributes As IArray, ByVal keyName As String, ByVal keyIsPrefix As Boolean) As IArray
			Dim preservedNetAttributes As IArray = New ArrayClass()

			Dim keyNameLen As Integer = keyName.Length
			Dim netAttributeNameLen As Integer
			Dim netAttribute As INetworkAttribute
			Dim netAttributeName As String
			Dim isKeyAttribute As Boolean
			Dim ignoreCase As Boolean = True

			Dim count As Integer = netAttributes.Count
			Dim i As Integer = 0
			For i = 0 To count - 1
				netAttribute = TryCast(netAttributes.Element(i), INetworkAttribute)
				If netAttribute Is Nothing Then
					Continue For
				End If

				netAttributeName = netAttribute.Name
				netAttributeNameLen = netAttributeName.Length

				isKeyAttribute = False
				If keyNameLen = 0 Then
					isKeyAttribute = False
				ElseIf netAttributeNameLen < keyNameLen Then
					isKeyAttribute = False
				Else
					Dim startIndex As Integer = 0
					If (Not keyIsPrefix) Then
						startIndex = netAttributeNameLen - keyNameLen
					End If

					If String.Compare(netAttributeName.Substring(startIndex, keyNameLen), keyName, ignoreCase) = 0 Then
						isKeyAttribute = True
					End If
				End If

				If (Not isKeyAttribute) Then
					preservedNetAttributes.Add(netAttribute)
				End If
			Next i

			Return preservedNetAttributes
		End Function

		Public Shared Function FindAttributeIndexes(ByVal netAttributes As IArray, ByVal usage As esriNetworkAttributeUsageType, ByVal dataType As esriNetworkAttributeDataType, ByVal searchTimeUnits As Boolean, ByVal ignoreDataType As Boolean) As List(Of Integer)
			Dim netAttribute As INetworkAttribute2 = Nothing
			Dim units As esriNetworkAttributeUnits = esriNetworkAttributeUnits.esriNAUUnknown
			Dim isSearchUnits As Boolean = False
			Dim isUnknownUnits As Boolean = False
			Dim isTimeUnits As Boolean = False

			Dim netAttributeIndexes As List(Of Integer) = New List(Of Integer)()
			Dim count As Integer = netAttributes.Count

			Dim i As Integer = 0
			For i = 0 To count - 1
				netAttribute = TryCast(netAttributes.Element(i), INetworkAttribute2)
				If netAttribute Is Nothing Then
					Continue For
				End If

				If netAttribute.UsageType = usage AndAlso (ignoreDataType OrElse netAttribute.DataType = dataType) Then
					units = netAttribute.Units
					isSearchUnits = False

					If usage <> esriNetworkAttributeUsageType.esriNAUTCost Then
						isSearchUnits = True
					Else
						isUnknownUnits = False
						If units = esriNetworkAttributeUnits.esriNAUUnknown Then
							isUnknownUnits = True
						End If

						isTimeUnits = False
						If (Not isUnknownUnits) Then
							If units = esriNetworkAttributeUnits.esriNAUMinutes OrElse units = esriNetworkAttributeUnits.esriNAUSeconds OrElse units = esriNetworkAttributeUnits.esriNAUHours OrElse units = esriNetworkAttributeUnits.esriNAUDays Then
								isTimeUnits = True
							End If

							If searchTimeUnits Then
								isSearchUnits = isTimeUnits
							Else
								isSearchUnits = Not isTimeUnits
							End If
						End If
					End If
					If isSearchUnits Then
						netAttributeIndexes.Add(i)
					End If
				End If
			Next i

			Return netAttributeIndexes
		End Function

		Public Shared Function FindAttributes(ByVal netAttributesArray As IArray, ByVal netAttributeIndexes As List(Of Integer)) As List(Of INetworkAttribute2)
			Dim netAttributes As List(Of INetworkAttribute2) = New List(Of INetworkAttribute2)()
			For Each i As Integer In netAttributeIndexes
				Dim netAttribute As INetworkAttribute2 = TryCast(netAttributesArray.Element(i), INetworkAttribute2)
				If Not netAttribute Is Nothing Then
					netAttributes.Add(netAttribute)
				End If
			Next i

			Return netAttributes
		End Function

		Public Shared Sub SetDefaultEvaluator(ByVal netAttribute As IEvaluatedNetworkAttribute, ByVal defaultValue As Object, ByVal eType As esriNetworkElementType)
			Dim constEvaluator As INetworkConstantEvaluator = New NetworkConstantEvaluatorClass()
			constEvaluator.ConstantValue = defaultValue
			Dim eval As INetworkEvaluator = TryCast(constEvaluator, INetworkEvaluator)
			netAttribute.DefaultEvaluator(eType) = eval
		End Sub

		Public Shared Sub SetEvaluators(ByVal netAttribute As IEvaluatedNetworkAttribute, ByVal netSource As INetworkSource, ByVal t As Type)
			Dim eType As esriNetworkElementType = netSource.ElementType
			If eType = esriNetworkElementType.esriNETEdge Then
				SetEvaluator(netAttribute, netSource, t, esriNetworkEdgeDirection.esriNEDAlongDigitized)
				SetEvaluator(netAttribute, netSource, t, esriNetworkEdgeDirection.esriNEDAgainstDigitized)
			Else
				SetEvaluator(netAttribute, netSource, t, esriNetworkEdgeDirection.esriNEDNone)
			End If
		End Sub

		Public Shared Sub SetEvaluator(ByVal netAttribute As IEvaluatedNetworkAttribute, ByVal netSource As INetworkSource, ByVal t As Type, ByVal dirType As esriNetworkEdgeDirection)
			Dim obj As Object = Activator.CreateInstance(t)
			Dim eval As INetworkEvaluator = TryCast(obj, INetworkEvaluator)
			netAttribute.Evaluator(netSource, dirType) = eval
		End Sub
	End Class
End Namespace