⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addstopbypolygon.cs

📁 AE+vb来实现的最短路径的实现功能!gis
💻 CS
字号:
using System;

using System.Runtime.InteropServices;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Utility.BaseClasses;
using ESRI.ArcGIS.Utility.CATIDs;
using ESRI.ArcGIS.ControlCommands;

using ESRI.ArcGIS.ToolbarControl;
using ESRI.ArcGIS.MapControl;
using ESRI.ArcGIS.SceneControl;
using ESRI.ArcGIS.Analyst3D;

using ESRI.ArcGIS.NetworkAnalyst;

//Ismael Chivite
//ESRI Redlands
//March 2005
namespace EvacuationRoutesTools
{
	[ClassInterface(ClassInterfaceType.None)]
	[Guid("D47057E3-9403-48cc-86F3-B762C01CDC18")]

	public sealed class AddBarriersByPolygon:  BaseTool
	{
		private IHookHelper m_pHookHelper = new HookHelperClass();
		private ISceneHookHelper m_pSceneHookHelper = new SceneHookHelperClass();
		private bool m_is2D = true;
		private IDisplayFeedback m_pNewPolygonFeedback = null;
		private IDisplayFeedback m_pNewPolylineFeedback = null;
		private int m_TotalVertices = 0;
		private IPoint m_LastPoint = null;
		private bool m_IsMoving = false;
		private bool m_Enabled = true;

		#region "Component Category Registration"
		[ComRegisterFunction()]
		static void Reg(string regKey)
		{
			MxCommands.Register(regKey);
			ControlsCommands.Register(regKey);
		}

		[ComUnregisterFunction()]
		static void Unreg(string regKey)
		{
			MxCommands.Unregister(regKey);
			ControlsCommands.Unregister(regKey);
		}
		#endregion 

		public AddBarriersByPolygon()
		{
			base.m_category = "Network_3D";
			base.m_caption = "Adds barriers by polygon";
			base.m_message = "Locks areas in the building";
			base.m_toolTip = "Locks areas in the building";
			base.m_name = "AddPolygonBarrier";
			try
			{
				string [] res = GetType().Assembly.GetManifestResourceNames();
				if(res.GetLength(0)>0)
				{
					base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream("EvacuationRoutesTools.AddStopByPolygon.bmp"));
				}
			}
			catch(Exception exx)
			{
				//System.Windows.Forms.MessageBox.Show(exx.Message);
			}
		}
	
		public override void OnCreate(object hook)
		{
			if (hook!=null)
			{
				if (hook is IToolbarControl)
				{
					IToolbarControl pToolBarControl = hook as IToolbarControl;
					if (pToolBarControl.Buddy is MapControl)
					{
						m_is2D = true;
						m_pHookHelper.Hook = hook; //This is a toolbar if the app is an ArcEngine app.
					}
					else
					{
						m_is2D = false;
						m_pSceneHookHelper.Hook = hook ;
					}
				}
				else
				{
					m_Enabled = false;
				}
			}
		}
	
	
		public override void OnMouseDown(int Button, int Shift, int X, int Y)
		{
			
			if (m_is2D==false)
			{
				//MouseDown3D(Button,Shift,X,Y);
				System.Windows.Forms.MessageBox.Show("This tool does not work in 3D","Evacuation Routes Demo",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
			}
			else
			{
				//MouseDown2D(Button,Shift,X,Y);
				m_IsMoving = true;
			}
		}
	
		public override bool Enabled
		{
			get
			{
				return m_Enabled;
			}
		}
		private void MouseDown2D(int Button,int Shift,int X,int Y)
		{
			try
			{
				m_TotalVertices++;

				IToolbarControl pToolBar = m_pHookHelper.Hook as IToolbarControl;
				IPoint pPoint = m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y);

				if (m_TotalVertices>3)
				{
					if (m_pNewPolygonFeedback == null)
					{
						IPointCollection pPointCol = ((INewLineFeedback)m_pNewPolylineFeedback).Stop() as IPointCollection;
						m_pNewPolygonFeedback = new NewPolygonFeedbackClass() as IDisplayFeedback;
						m_pNewPolygonFeedback.Display = m_pHookHelper.ActiveView.ScreenDisplay;
						((INewPolygonFeedback)m_pNewPolygonFeedback).Start(pPointCol.get_Point(0));
						for(int i =1;i<pPointCol.PointCount;i++)
						{
							((INewPolygonFeedback)m_pNewPolygonFeedback).AddPoint(pPointCol.get_Point(i));
						}
						m_pNewPolylineFeedback = null;
					}
					((INewPolygonFeedback)m_pNewPolygonFeedback).AddPoint(pPoint);
				}
				else
				{
					if (m_pNewPolylineFeedback == null)
					{
						m_pNewPolylineFeedback = new NewLineFeedbackClass() as IDisplayFeedback;
						m_pNewPolylineFeedback.Display = m_pHookHelper.ActiveView.ScreenDisplay;
						((INewLineFeedback)m_pNewPolylineFeedback).Start(pPoint);
					}
					else
					{
						((INewLineFeedback)m_pNewPolylineFeedback).AddPoint(pPoint);
					}
				}
			}
			catch(Exception exx)
			{
				System.Windows.Forms.MessageBox.Show("Add barriers by polygon tool: " + exx.Message);
			}

		}
	
		public override void OnDblClick()
		{
			
			//Get the polygon (NewPolygon)
			IPolygon pPolygon = null;
			if (m_pNewPolygonFeedback == null)
			{
				return;
			}
			else
			{
				pPolygon = ((INewPolygonFeedback)m_pNewPolygonFeedback).Stop();
				pPolygon.SimplifyPreserveFromTo();
			}
			IPolygon pNewPolygon = ((ESRI.ArcGIS.esriSystem.IClone)pPolygon).Clone() as IPolygon;
			if (pNewPolygon==null)return;

			if (((IPointCollection)pNewPolygon).PointCount < 10) return;

			//Get the active floor and NALayer
			IToolbarControl pToolBar = m_pHookHelper.Hook as IToolbarControl;
			IMapControl3 pMapControl = pToolBar.Buddy as IMapControl3;

			AppProperties.AppProperties pAppProps =  pMapControl.CustomProperty as AppProperties.AppProperties;
			string sActiveFloor = pAppProps.ActiveFloor;
			ESRI.ArcGIS.NetworkAnalyst.INALayer pNALayer = pAppProps.NetworkLayer;

			//Add the graphical representation of the barriers in 2D and 3D
			double dHeight = pAppProps.FloorsHeight;

			IFeatureLayer pPolygonBarriersFL = pAppProps.PolygonBarrier3DLayer as IFeatureLayer;
			IFeatureClass pPolygonBarriersFC = pPolygonBarriersFL.FeatureClass;
			IFeature pF = pPolygonBarriersFC.CreateFeature();
			
			((IZAware)pNewPolygon).ZAware = true;
			((IZ)pNewPolygon).SetConstantZ(dHeight);
			((IPolycurve)pNewPolygon).Smooth(0.02);   

			pF.Shape = pNewPolygon as IGeometry;
			pF.set_Value(pF.Fields.FindField("Floor"),sActiveFloor);
			pF.Store();

			//Refresh 2D
			m_pHookHelper.ActiveView.Refresh();
			//Refresh 3D
			ISceneGraph pSceneGraph = pAppProps.SceneGraph;
			pSceneGraph.Invalidate(pAppProps.PolygonBarrier3DLayer,true,false);
			pSceneGraph.RefreshViewers();

			//Add the barriers
			AddBarriers(pNALayer,sActiveFloor,pNewPolygon);


			//Cleanup
			m_pNewPolylineFeedback = null;
			m_pNewPolygonFeedback = null;
			m_TotalVertices = 0;
		}
		private void AddBarriers(ESRI.ArcGIS.NetworkAnalyst.INALayer pNALayer, string sFloorName,IPolygon pPolygonBarrier)
		{
//			try
//			{
//				//Simplified method. 
//				INetworkDataset pNetworkDataset = pNALayer.Context.NetworkDataset;
//				//Get the Floor Featureclass
//				IFeatureWorkspace pFWS = ((IDataset)pNetworkDataset).Workspace as IFeatureWorkspace;
//				IFeatureClass pFCFloor = pFWS.OpenFeatureClass(sFloorName);
//				//Search
//				ISpatialFilter pSF = new SpatialFilterClass();
//				pSF.Geometry = ((ITopologicalOperator)pPolygonBarrier).Boundary;
//				pSF.AddField(pFCFloor.OIDFieldName);
//				pSF.GeometryField = pFCFloor.ShapeFieldName;
//				pSF.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects;
//
//				IFeatureCursor pFCursor = pFCFloor.Search(pSF,true);
//				IFeature pFEdge = pFCursor.NextFeature();
//				while(pFEdge!=null)
//				{
//					ICurve pCurve = pFEdge.Shape as ICurve;
//					IPoint pOutPoint = new PointClass();
//					pCurve.QueryPoint(ESRI.ArcGIS.Geometry.esriSegmentExtension.esriNoExtension,0.5,true,pOutPoint);
//					bool added = APL.NetworkUtils.AddNetWorkLocation(pNALayer,"Barriers",pOutPoint,sFloorName,"FromPolygonBarrier",1);
//
//					pFEdge = pFCursor.NextFeature();
//				}
//			}
//			catch(Exception exx)
//			{
//				System.Diagnostics.Debug.WriteLine(exx.Message);
//			}

			//Configure Network Locator
			INAContext pNAContext = pNALayer.Context;
			INetworkDataset pNetworkDataset = pNAContext.NetworkDataset;

			INALocator pNALocator = pNAContext.Locator;
			pNALocator.SnapTolerance = 1;
				
			int iTotalAgents = pNALocator.LocatorAgentCount;
			for (int n=0;n<iTotalAgents;n++)
			{
				INALocatorAgent pNALocAgent = pNALocator.get_LocatorAgent(n);
				INALocatorFeatureAgent pNALocFeatAgent = pNALocAgent as INALocatorFeatureAgent;
				if (pNALocAgent!=null)
				{
					if (pNALocFeatAgent.SourceName==sFloorName)
					{
						pNALocFeatAgent.SnapType = ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex;
					}
					else
					{
						pNALocFeatAgent.SnapType = ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartNone;
					}
				}
			}

			//Get the Floor Featureclass
			IFeatureWorkspace pFWS = ((IDataset)pNetworkDataset).Workspace as IFeatureWorkspace;
			IFeatureClass pFCFloor = pFWS.OpenFeatureClass(sFloorName);
			//Search
			ISpatialFilter pSF = new SpatialFilterClass();
			pSF.Geometry = pPolygonBarrier;
			pSF.AddField(pFCFloor.OIDFieldName);
			pSF.GeometryField = pFCFloor.ShapeFieldName;
			pSF.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects;

			IFeatureCursor pFCursor = pFCFloor.Search(pSF,true);
			IFeature pFEdge = pFCursor.NextFeature();
			while(pFEdge!=null)
			{
				ITopologicalOperator pTopOp = pFEdge.Shape as ITopologicalOperator;
				IMultipoint pIntersectionPoints = pTopOp.Intersect(pPolygonBarrier,ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry0Dimension)as IMultipoint;
				for (int i = 0;i<((IPointCollection)pIntersectionPoints).PointCount;i++)
				{
					IPoint pPoint = ((IPointCollection)pIntersectionPoints).get_Point(i);
					APL.NetworkUtils.AddBarrier(pNALayer,pPoint,sFloorName,true,2);

				}
				pFEdge = pFCursor.NextFeature();
			}
			((INAContextEdit)pNAContext).ContextChanged();
		}
	
		public override void OnMouseMove(int button, int shift, int x, int y)
		{
			if(!m_IsMoving)return;
			try
			{
				

				IToolbarControl pToolBar = m_pHookHelper.Hook as IToolbarControl;
				IPoint pPoint = m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x,y);

				if (m_LastPoint!=null)
				{
					if (((IProximityOperator)m_LastPoint).ReturnDistance(pPoint)<2) return;
				}

				m_TotalVertices++;
				if (m_TotalVertices>3)
				{
					if (m_pNewPolygonFeedback == null)
					{
						IPointCollection pPointCol = ((INewLineFeedback)m_pNewPolylineFeedback).Stop() as IPointCollection;
						m_pNewPolygonFeedback = new NewPolygonFeedbackClass() as IDisplayFeedback;
						m_pNewPolygonFeedback.Display = m_pHookHelper.ActiveView.ScreenDisplay;
						((INewPolygonFeedback)m_pNewPolygonFeedback).Start(pPointCol.get_Point(0));
						for(int i =1;i<pPointCol.PointCount;i++)
						{
							((INewPolygonFeedback)m_pNewPolygonFeedback).AddPoint(pPointCol.get_Point(i));
						}
						m_pNewPolylineFeedback = null;
					}
					((INewPolygonFeedback)m_pNewPolygonFeedback).AddPoint(pPoint);
				}
				else
				{
					if (m_pNewPolylineFeedback == null)
					{
						m_pNewPolylineFeedback = new NewLineFeedbackClass() as IDisplayFeedback;
						m_pNewPolylineFeedback.Display = m_pHookHelper.ActiveView.ScreenDisplay;
						((INewLineFeedback)m_pNewPolylineFeedback).Start(pPoint);
					}
					else
					{
						((INewLineFeedback)m_pNewPolylineFeedback).AddPoint(pPoint);
					}
				}
			}
			catch(Exception exx)
			{
				System.Windows.Forms.MessageBox.Show("Add barriers by polygon tool: " + exx.Message);
			}

		}
	
		public override void OnMouseUp(int button, int shift, int x, int y)
		{
			m_IsMoving = false;
			this.OnDblClick();

		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -