ArcObjects Library Reference  

RestrictionsForm

About the StreetMap routing Sample

[C#]

RestrictionsForm.cs

using System;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;

namespace RoutingSample
{
	public partial class RestrictionsForm
	{

	#region Public methods

		public RestrictionsForm() : base()
		{

			//This call is required by the Windows Form Designer.
			InitializeComponent();

			m_arrRestrictions = new System.Collections.ArrayList();
		}

        // Receives restriction from route and inits controls 
		public void Init(ISMRouter objRouter)
		{
			// clear controls
			ClearAll();

			try
			{
				this.SuspendLayout();
				m_pnlRestrictions.SuspendLayout();

				// Get Net attributes
				ISMNetAttributesCollection objAttrColl = null;
				objAttrColl = objRouter.NetAttributes;

				// attributes count
				int nCount = objAttrColl.Count;

				for (int i = 0; i < nCount; i++)
				{
					// get attribute
					SMNetAttribute objAttr = null;
					objAttr = objAttrColl.get_Item(i);

					if ((objAttr) is ISMNetAttribute2)
					{
						ISMNetAttribute2 objAttr2 = null;
						objAttr2 = objAttr as ISMNetAttribute2;

						// If Usage type is restriction
						esriSMNetAttributeUsageType eType = 0;
						eType = objAttr2.UsageType;

						if (eType == esriSMNetAttributeUsageType.esriSMNAUTRestrictionBoolean 
							|| eType == esriSMNetAttributeUsageType.esriSMNAUTRestrictionMinAllowed 
							|| eType == esriSMNetAttributeUsageType.esriSMNAUTRestrictionMaxAllowed)
						{

							// create control for restriction
							RestrictControl ctrlRestriction = null;
							ctrlRestriction = new RestrictControl();

							// create restriction info
							RestrictionsInfo objRestriction = null;
							objRestriction = new RestrictionsInfo();

							// Init info
							objRestriction.m_objAttr2 = objAttr2;
							objRestriction.m_strName = objAttr2.Name;
							objRestriction.m_bChecked = false;
							objRestriction.m_eType = RestrictControl.ERSType.eStrict;
							objRestriction.m_bUseParameter = false;

							if (eType != esriSMNetAttributeUsageType.esriSMNAUTRestrictionBoolean)
							{
								objRestriction.m_bUseParameter = true;
								objRestriction.m_dParameter = 0.0;
							}

							// Add controls (reverse order)
							ctrlRestriction.Dock = DockStyle.Top;
							m_pnlRestrictions.Controls.Add(ctrlRestriction);

							m_arrRestrictions.Add(objRestriction);
						}
					}
				}

				// Set restriction info to controls
				UpdateControls();

			}
			catch (Exception ex)
			{
				ClearAll();
			}
			finally
			{
				m_pnlRestrictions.ResumeLayout(false);
				this.ResumeLayout(false);
			}
		}

		// Setups router restrictions
		public void SetupRouter(ISMRouter objRouter)
		{
			ISMRouterSetup2 objRouterSetup = null;
			objRouterSetup = objRouter as ISMRouterSetup2;

			try
			{
				// Clear all previous changes
				objRouterSetup.ClearAllRestrictions();

				foreach (RestrictionsInfo objInfo in m_arrRestrictions)
				{
					if (objInfo.m_bChecked)
					{
						// New restriction
						ISMRestriction objRestr = new SMRestrictionClass();

						objRestr.Attribute = objInfo.m_objAttr2 as SMNetAttribute;

						// restriction type
						if (objInfo.m_eType == RestrictControl.ERSType.eStrict)
							objRestr.Type = esriSMRestrictionType.esriSMRTStrict;
						else
							objRestr.Type = esriSMRestrictionType.esriSMRTRelaxed;

                        // Parameter
						if (objInfo.m_bUseParameter)
							objRestr.Param = objInfo.m_dParameter;

						// Add restriction to router
						objRouterSetup.SetRestriction(objRestr as SMRestriction);
					}
				}
			}
			catch (Exception ex)
			{
				objRouterSetup.ClearAllRestrictions();
				MessageBox.Show("Cannot set restrictions.");
			}
		}

	#endregion

	#region Private methods

		// Clears all
		private void ClearAll()
		{
			m_arrRestrictions.Clear();
			m_pnlRestrictions.Controls.Clear();
		}

		// Updates controls values
		private void UpdateControls()
		{
			int nCount = m_arrRestrictions.Count;
			for (int i = 0; i < nCount; i++)
			{
				RestrictControl ctrlRestriction = null;
				ctrlRestriction = m_pnlRestrictions.Controls[nCount - 1 - i] as RestrictControl;

				ctrlRestriction.TabIndex = i;
				RestrictionsInfo objRestriction = m_arrRestrictions[i] as RestrictionsInfo;
				ctrlRestriction.RSName = objRestriction.m_strName;
				ctrlRestriction.RSChecked = objRestriction.m_bChecked;
				ctrlRestriction.RSType = objRestriction.m_eType;
				ctrlRestriction.RSUseParameter = objRestriction.m_bUseParameter;

				if (objRestriction.m_bUseParameter)
					ctrlRestriction.RSParameter = objRestriction.m_dParameter;
			}
		}

		// Updates restrictions info 
		private void UpdateInfo()
		{
			int nCount = m_arrRestrictions.Count;
			for (int i = 0; i < nCount; i++)
			{
				RestrictControl ctrlRestriction = null;
				ctrlRestriction = m_pnlRestrictions.Controls[nCount - 1 - i] as RestrictControl;

				RestrictionsInfo objRestriction = m_arrRestrictions[i] as RestrictionsInfo;
				objRestriction.m_strName = ctrlRestriction.RSName;
				objRestriction.m_bChecked = ctrlRestriction.RSChecked;
				objRestriction.m_eType = ctrlRestriction.RSType;

				if (objRestriction.m_bUseParameter)
					objRestriction.m_dParameter = ctrlRestriction.RSParameter;
			}
		}

		// Updates restrictions info from controls
		private void m_btnOK_Click(object sender, System.EventArgs e)
		{
			UpdateInfo();
		}

		// Updates controls from restrictions info 
		private void m_btnCancel_Click(object sender, System.EventArgs e)
		{
			UpdateControls();
		}

	#endregion

	#region Private members
		// Restrictions data for Router.
		// NOTE: Restriction controls in reverse order
		private System.Collections.ArrayList m_arrRestrictions;
	#endregion

	}

// Info for one restriction
	public class RestrictionsInfo
	{
		public ISMNetAttribute2 m_objAttr2;
		public string m_strName;
		public bool m_bChecked;
		public RestrictControl.ERSType m_eType;
		public bool m_bUseParameter;
		public double m_dParameter;
	}

} //end of root namespace
[Visual Basic .NET]

RestrictionsForm.vb

Imports System.Windows.Forms
Imports ESRI.ArcGIS.DataSourcesFile

Public Class RestrictionsForm

#Region "Public methods"

	Public Sub New()
		MyBase.New()

		'This call is required by the Windows Form Designer.
		InitializeComponent()

		m_arrRestrictions = New System.Collections.ArrayList
	End Sub

    ' Receives restriction from route and inits controls 
	Public Sub Init(ByVal objRouter As ISMRouter)
		' clear controls
		ClearAll()

		Try
			Me.SuspendLayout()
			m_pnlRestrictions.SuspendLayout()

			' Get Net attributes
			Dim objAttrColl As ISMNetAttributesCollection
			objAttrColl = objRouter.NetAttributes

			' attributes count
			Dim nCount As Integer = objAttrColl.Count

			For i As Integer = 0 To nCount - 1
				' get attribute
				Dim objAttr As ISMNetAttribute
				objAttr = objAttrColl.Item(i)

				If TypeOf (objAttr) Is ISMNetAttribute2 Then
					Dim objAttr2 As ISMNetAttribute2
					objAttr2 = objAttr

					' If Usage type is restriction
					Dim eType As esriSMNetAttributeType
					eType = objAttr2.UsageType

					If eType = esriSMNetAttributeUsageType.esriSMNAUTRestrictionBoolean Or _
						eType = esriSMNetAttributeUsageType.esriSMNAUTRestrictionMinAllowed Or _
						eType = esriSMNetAttributeUsageType.esriSMNAUTRestrictionMaxAllowed Then

						' create control for restriction
						Dim ctrlRestriction As RestrictControl
						ctrlRestriction = New RestrictControl

						' create restriction info
						Dim objRestriction As RestrictionsInfo
						objRestriction = New RestrictionsInfo

						' Init info
						objRestriction.m_objAttr2 = objAttr2
						objRestriction.m_strName = objAttr2.Name
						objRestriction.m_bChecked = False
						objRestriction.m_eType = RestrictControl.ERSType.eStrict
						objRestriction.m_bUseParameter = False

						If eType <> esriSMNetAttributeUsageType.esriSMNAUTRestrictionBoolean Then
							objRestriction.m_bUseParameter = True
							objRestriction.m_dParameter = 0.0
						End If

						' Add controls (reverse order)
						ctrlRestriction.Dock = DockStyle.Top
						m_pnlRestrictions.Controls.Add(ctrlRestriction)

						m_arrRestrictions.Add(objRestriction)
					End If
				End If
			Next

			' Set restriction info to controls
			UpdateControls()

		Catch ex As Exception
			ClearAll()
		Finally
			m_pnlRestrictions.ResumeLayout(False)
			Me.ResumeLayout(False)
		End Try
	End Sub

	' Setups router restrictions
	Public Sub SetupRouter(ByVal objRouter As ISMRouter)
        Dim objRouterSetup As ISMRouterSetup2
		objRouterSetup = objRouter

		Try
			' Clear all previous changes
			objRouterSetup.ClearAllRestrictions()

			For Each objInfo As RestrictionsInfo In m_arrRestrictions
				If objInfo.m_bChecked Then
					' New restriction
					Dim objRestr As ISMRestriction
					objRestr = New SMRestrictionClass

					objRestr.Attribute = CType(objInfo.m_objAttr2, SMNetAttribute)

					' restriction type
					If objInfo.m_eType = RestrictControl.ERSType.eStrict Then
						objRestr.Type = esriSMRestrictionType.esriSMRTStrict
					Else
						objRestr.Type = esriSMRestrictionType.esriSMRTRelaxed
					End If

					' Paramter
					If objInfo.m_bUseParameter Then
						objRestr.Param = objInfo.m_dParameter
					End If

					' Add restriction to router
					objRouterSetup.SetRestriction(objRestr)
				End If
			Next
		Catch ex As Exception
			objRouterSetup.ClearAllRestrictions()
			MessageBox.Show("Cannot set restrictions.")
		End Try
	End Sub

#End Region

#Region "Private methods"

	' Clears all
	Private Sub ClearAll()
		m_arrRestrictions.Clear()
		m_pnlRestrictions.Controls.Clear()
	End Sub

	' Updates controls values
	Private Sub UpdateControls()
		Dim nCount As Integer = m_arrRestrictions.Count
		For i As Integer = 0 To nCount - 1
			Dim ctrlRestriction As RestrictControl
			ctrlRestriction = CType(m_pnlRestrictions.Controls.Item(nCount - 1 - i), RestrictControl)

			ctrlRestriction.TabIndex = i
			Dim objRestriction As RestrictionsInfo
			objRestriction = CType(m_arrRestrictions(i), RestrictionsInfo)
			ctrlRestriction.RSName = objRestriction.m_strName
			ctrlRestriction.RSChecked = objRestriction.m_bChecked
			ctrlRestriction.RSType = objRestriction.m_eType
			ctrlRestriction.RSUseParameter = objRestriction.m_bUseParameter

			If m_arrRestrictions(i).m_bUseParameter Then
				ctrlRestriction.RSParameter = objRestriction.m_dParameter
			End If
		Next
	End Sub

	' Updates restrictions info 
	Private Sub UpdateInfo()
		Dim nCount As Integer = m_arrRestrictions.Count
		For i As Integer = 0 To nCount - 1
			Dim ctrlRestriction As RestrictControl
			ctrlRestriction = CType(m_pnlRestrictions.Controls.Item(nCount - 1 - i), RestrictControl)

			Dim objRestriction As RestrictionsInfo
			objRestriction = CType(m_arrRestrictions(i), RestrictionsInfo)

			objRestriction.m_strName = ctrlRestriction.RSName
			objRestriction.m_bChecked = ctrlRestriction.RSChecked
			objRestriction.m_eType = ctrlRestriction.RSType

			If m_arrRestrictions(i).m_bUseParameter Then
				objRestriction.m_dParameter = ctrlRestriction.RSParameter
			End If
		Next
	End Sub

	' Updates restrictions info from controls
	Private Sub m_btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_btnOK.Click
		UpdateInfo()
	End Sub

	' Updates controls from restrictions info 
	Private Sub m_btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_btnCancel.Click
		UpdateControls()
	End Sub

#End Region

#Region "Private members"
	' Restrictions data for Router.
	' NOTE: Restriction controls in reverse order
	Private m_arrRestrictions As System.Collections.ArrayList
#End Region

End Class

' Info for one restriction
Public Class RestrictionsInfo
	Public m_objAttr2 As ISMNetAttribute2
	Public m_strName As String
	Public m_bChecked As Boolean
	Public m_eType As RestrictControl.ERSType
	Public m_bUseParameter As Boolean
	Public m_dParameter As Double
End Class