欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

aoeditor.cs

很好的带有编辑功能、量算功能。可在地图上进行距离、面积、角度测量
CS
第 1 页 / 共 2 页
字号:
							if (!vertex)
							{								
								pGeomColn =(IGeometryCollection) pGeom;
								pPath =(IPath) pGeomColn.get_Geometry(partIndex);
								pPointColn =(IPointCollection) pPath;
								numVertices = pPointColn.PointCount;

								if (vertexIndex == 0)
								{
									objBefore = (object) (vertexIndex + 1); 
									pPointColn.AddPoint(pPoint,ref objBefore,ref objNull);
								}
								else
								{
									objAfter = (object) vertexIndex; 
									pPointColn.AddPoint( pPoint,ref objNull , ref objAfter);
								}

								TestGeometryHit(tol, pPoint, pFeature, pHitPoint, hitDist,out partIndex,out vertexIndex,out vertex);
							}

							m_pFeedback = new PolygonMovePointFeedbackClass();
							m_pFeedback.Display = pActiveView.ScreenDisplay;
							IPolygonMovePointFeedback pPolyMove =(IPolygonMovePointFeedback) m_pFeedback;
							pPolyMove.Start((IPolygon) pGeom, vertexIndex, pPoint);
						}
						else
						{
							return false;
						}
						break;
				}
				return true ;
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
				return false;
			}
		}

		/// <summary>
		/// 编辑地图对象过程中的鼠标移动事件,
		/// 如果为点对象,进行位置移动
		/// 如果为线对象或面对象,进行节点移动
		/// 建议在Map.MouseMove事件中调用本方法
		/// </summary>
		/// <param name="x">鼠标X坐标,屏幕坐标</param>
		/// <param name="y">鼠标Y坐标,屏幕坐标</param>
		public void EditFeatureMouseMove(int x, int y)
		{			 
			try
			{
				if (m_pFeedback == null) return ; 
  
				IActiveView pActiveView =(IActiveView) m_pMap;
				IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
				m_pFeedback.MoveTo(pPoint);

//				if (m_pSelectionTracker !=null) m_pSelectionTracker.OnMouseMove(1,0,x,y); 
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}
		}

		/// <summary>
		/// 完成地图对象编辑,取得编辑后的对象,并将其更新到图层中
		/// 建议在Map.MouseUp事件中调用本方法
		/// </summary>
		public void EditFeatureEnd()
		{
			IGeometry pGeometry;
 
			try
			{
				if (m_pFeedback ==null) return;  
				
				if (m_pFeedback is IMovePointFeedback) 
				{
					IMovePointFeedback pPointMove =(IMovePointFeedback) m_pFeedback;
					pGeometry = pPointMove.Stop();
					UpdateFeature(m_pEditFeature, pGeometry);
				}
				else if (m_pFeedback is ILineMovePointFeedback)
				{
					ILineMovePointFeedback pLineMove =(ILineMovePointFeedback) m_pFeedback;
					pGeometry = pLineMove.Stop();
					UpdateFeature(m_pEditFeature, pGeometry);					 
				}
				else if (m_pFeedback is IPolygonMovePointFeedback)
				{
					IPolygonMovePointFeedback pPolyMove =(IPolygonMovePointFeedback) m_pFeedback;
					pGeometry = pPolyMove.Stop();
					UpdateFeature(m_pEditFeature, pGeometry);
				}
			
				m_pFeedback = null;
//				m_pSelectionTracker = null;
				IActiveView pActiveView = (IActiveView) m_pMap;
				pActiveView.Refresh(); 
			}
			catch( Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}
		}

		/// <summary>
		/// 移动当前图层中鼠标击中地图对象的位置(开始移动)
		/// 建议在Map.MouseDown事件中调用本方法
		/// </summary>
		/// <param name="x">鼠标X坐标,屏幕坐标</param>
		/// <param name="y">鼠标Y坐标,屏幕坐标</param>
		/// <returns></returns>
		public bool MoveFeatureMouseDown(int x, int y)
		{			
			try
			{
				m_pMap.ClearSelection();
 
				SelectMouseDown(x,y);
				IEnumFeature pSelected =(IEnumFeature) m_pMap.FeatureSelection;
				IFeature pFeature = pSelected.Next();
				if (pFeature ==null ) return false;
  
				IActiveView pActiveView =(IActiveView) m_pMap;
				IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
  
				IGeometry pGeom = pFeature.Shape;
				m_pEditFeature = pFeature;
				
				switch (pGeom.GeometryType)
				{
					case esriGeometryType.esriGeometryPoint:
						m_pFeedback = new MovePointFeedbackClass();
						m_pFeedback.Display = pActiveView.ScreenDisplay;
						IMovePointFeedback pPointMove =(IMovePointFeedback) m_pFeedback;
						pPointMove.Start((IPoint)pGeom, pPoint);  
						break;
					case esriGeometryType.esriGeometryPolyline:
						
						m_pFeedback = new MoveLineFeedbackClass() ;
						m_pFeedback.Display = pActiveView.ScreenDisplay;
						IMoveLineFeedback pLineMove =(IMoveLineFeedback) m_pFeedback;
						pLineMove.Start((IPolyline)pGeom, pPoint);
						break;
					case esriGeometryType.esriGeometryPolygon:
						m_pFeedback = new MovePolygonFeedbackClass();
						m_pFeedback.Display = pActiveView.ScreenDisplay;
						IMovePolygonFeedback pPolyMove =(IMovePolygonFeedback) m_pFeedback;
						pPolyMove.Start((IPolygon) pGeom,pPoint);						
						break;
				}
				return true ;
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
				return false;
			}
		}

		/// <summary>
		/// 移动地图对象过程中的鼠标移动事件
		/// 建议在Map.MouseMove事件中调用本方法
		/// </summary>
		/// <param name="x">鼠标X坐标,屏幕坐标</param>
		/// <param name="y">鼠标Y坐标,屏幕坐标</param>
		public void MoveFeatureMouseMove(int x, int y)
		{
			try
			{
				if (m_pFeedback == null) return ; 
  
				IActiveView pActiveView =(IActiveView) m_pMap;
				IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
				m_pFeedback.MoveTo(pPoint);
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}
		}

		/// <summary>
		/// 完成地图对象移动,取得移动后的对象,并将其更新到图层中
		/// 建议在Map.MouseUp事件中调用本方法
		/// </summary>
		public void MoveFeatureEnd()
		{
			IGeometry pGeometry;
 
			try
			{
				if (m_pFeedback ==null) return;  
				
				if (m_pFeedback is IMovePointFeedback) 
				{
					IMovePointFeedback pPointMove =(IMovePointFeedback) m_pFeedback;
					pGeometry = pPointMove.Stop();
					UpdateFeature(m_pEditFeature, pGeometry);
				}
				else if (m_pFeedback is IMoveLineFeedback)
				{
					IMoveLineFeedback pLineMove =(IMoveLineFeedback) m_pFeedback;
					pGeometry = pLineMove.Stop();
					UpdateFeature(m_pEditFeature, pGeometry);
				}
				else if (m_pFeedback is IMovePolygonFeedback)
				{
					IMovePolygonFeedback pPolyMove =(IMovePolygonFeedback) m_pFeedback;
					pGeometry = pPolyMove.Stop();
					UpdateFeature(m_pEditFeature, pGeometry);
				}
			
				m_pFeedback = null;
				IActiveView pActiveView = (IActiveView) m_pMap;
				pActiveView.Refresh(); 
			}
			catch( Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}
		}

		/// <summary>
		/// 删除当前图层中选中的地图对象
		/// </summary>
		public void DeleteSelectedFeature()
		{
			try
			{
				if (m_pCurrentLayer == null) return ;

				IFeatureCursor pFeatureCursor = GetSelectedFeatures();
				if (pFeatureCursor ==null) return ;

				m_pMap.ClearSelection();

				IWorkspaceEdit pWorkspaceEdit = GetWorkspaceEdit();
				pWorkspaceEdit.StartEditOperation();
				IFeature pFeature = pFeatureCursor.NextFeature();

				while (pFeature !=null)
				{
					pFeature.Delete();
					pFeature = pFeatureCursor.NextFeature();
				}

				pWorkspaceEdit.StopEditOperation();
  
				IActiveView pActiveView =(IActiveView) m_pMap;
				pActiveView.Refresh();  
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}			
		}

		/// <summary>
		/// 撤消以前所做的编辑
		/// </summary>
		public void UndoEdit()
		{
			bool bHasUndos =false;
    
			try
			{				
				if (m_pCurrentLayer ==null) return ;

				IFeatureLayer pFeatureLayer =(IFeatureLayer) m_pCurrentLayer;
				IDataset pDataset =(IDataset) pFeatureLayer.FeatureClass;
				if (pDataset ==null) return ;

				IWorkspaceEdit pWorkspaceEdit =(IWorkspaceEdit) pDataset.Workspace;
				pWorkspaceEdit.HasUndos(ref bHasUndos);
				if (bHasUndos)	pWorkspaceEdit.UndoEditOperation();

				IActiveView pActiveView =(IActiveView) m_pMap;
				pActiveView.Refresh();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}
		}

		/// <summary>
		/// 重做已撤消的编辑
		/// </summary>
		public void RedoEdit()
		{
			bool bHasRedos =false;
    
			try
			{				
				if (m_pCurrentLayer ==null) return ;

				IFeatureLayer pFeatureLayer = (IFeatureLayer) m_pCurrentLayer;
				IDataset pDataset =(IDataset) pFeatureLayer.FeatureClass;
				if (pDataset ==null) return ;

				IWorkspaceEdit pWorkspaceEdit =(IWorkspaceEdit) pDataset.Workspace;
				pWorkspaceEdit.HasRedos(ref bHasRedos);
				if (bHasRedos)	pWorkspaceEdit.RedoEditOperation();

				IActiveView pActiveView =(IActiveView) m_pMap;
				pActiveView.Refresh();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}
		}

		/// <summary>
		/// 向图层中添加新的地图对象,并使之处于选中状态
		/// </summary>
		/// <param name="pGeom">图形对象</param>
		private void CreateFeature(IGeometry pGeom)
		{			
			try
			{
				if (pGeom ==null) return ;
				if (m_pCurrentLayer ==null) return ;

				IWorkspaceEdit pWorkspaceEdit = GetWorkspaceEdit();
				IFeatureLayer pFeatureLayer = (IFeatureLayer) m_pCurrentLayer;
				IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

				pWorkspaceEdit.StartEditOperation();
				IFeature pFeature = pFeatureClass.CreateFeature();
				pFeature.Shape = pGeom;
				pFeature.Store();
				pWorkspaceEdit.StopEditOperation();
  
				m_pMap.SelectFeature(m_pCurrentLayer, pFeature);

				IActiveView pActiveView  =(IActiveView) m_pMap;
				pActiveView.Refresh(); 
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}
		}

		/// <summary>
		/// 瘵屏幕坐标转换为地图坐标
		/// </summary>
		/// <param name="pActiveView">地图</param>
		/// <param name="pixelUnits">屏幕坐标</param>
		/// <returns>地图坐标</returns>
		private double ConvertPixelsToMapUnits(IActiveView pActiveView , double pixelUnits)
		{
			tagRECT pRect = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame();
			int pixelExtent = pRect.right - pRect.left ;

			double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
			double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
			return pixelUnits * sizeOfOnePixel;
		}

		/// <summary>
		/// 取得当前图层所在的工作空间
		/// </summary>
		/// <returns>工作空间</returns>
		private IWorkspaceEdit GetWorkspaceEdit()
		{  
			if (m_pCurrentLayer == null) return null ;
  
			IFeatureLayer pFeatureLayer = (IFeatureLayer) m_pCurrentLayer;
			IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
			IDataset pDataset = (IDataset) pFeatureClass;
			if (pDataset ==null)
				return null;
			else
				return (IWorkspaceEdit)pDataset.Workspace;
		}

		/// <summary>
		/// 取得选中的地图对象集合
		/// </summary>
		/// <returns>地图对象游标</returns>
		private IFeatureCursor GetSelectedFeatures()
		{  
			if (m_pCurrentLayer == null) return null;

			IFeatureSelection pFeatSel = (IFeatureSelection) m_pCurrentLayer;
			ISelectionSet pSelectionSet = pFeatSel.SelectionSet;
			
			if (pSelectionSet.Count == 0)
			{
				return null;
			}

			ICursor pCursor;
			pSelectionSet.Search(null, false, out pCursor);
			return (IFeatureCursor) pCursor;
		}

		/// <summary>
		/// 测试是否击中地图对象或地图对象上的节点
		/// </summary>
		/// <param name="tolerance">查询容差</param>
		/// <param name="pPoint">点击位置</param>
		/// <param name="pFeature">测试对象</param>
		/// <param name="pHitPoint">查询目标点</param>
		/// <param name="hitDist">目标点与点击点距离</param>
		/// <param name="partIndex">节索引</param>
		/// <param name="vertexIndex">点索引</param>
		/// <param name="vertexHit">是否击中点</param>
		/// <returns>是否击中测试对象</returns>
		private bool TestGeometryHit(double tolerance, IPoint pPoint, IFeature pFeature, IPoint pHitPoint,
			double hitDist,out int partIndex,out int vertexIndex,out bool vertexHit)
		{			 
			try
			{
				IGeometry pGeom = pFeature.Shape;    
				IHitTest pHitTest =(IHitTest) pGeom;
				pHitPoint = new PointClass();
				bool bRes = true;

				partIndex =0;
				vertexIndex =0;
				vertexHit = false;
				// 检查节点是否被击中
				if (pHitTest.HitTest(pPoint, tolerance, esriGeometryHitPartType.esriGeometryPartVertex, pHitPoint, 
					ref hitDist, ref partIndex, ref vertexIndex, ref bRes))
				{
					vertexHit = true;
					return true;
				}
				// 检边界是否被击中
				else
				{                
					if (pHitTest.HitTest(pPoint, tolerance,esriGeometryHitPartType.esriGeometryPartBoundary, pHitPoint,
						ref hitDist, ref partIndex, ref vertexIndex, ref bRes))
					{
						vertexHit = false;
						return true;
					}
				}
				return false;
			}			

			catch(Exception e)
			{
				Console.WriteLine(e.Message.ToString());
				partIndex =0;
				vertexIndex =0;
				vertexHit = false;
				return false;
			}						
		}

		/// <summary>
		/// 向图层中更新新的地图对象,并使之处于选中状态
		/// </summary>
		/// <param name="pFeature"></param>
		/// <param name="pGeometry"></param>
		private void UpdateFeature(IFeature pFeature, IGeometry pGeometry)
		{ 
			try
			{
				IDataset pDataset =(IDataset) pFeature.Class;
				IWorkspaceEdit pWorkspaceEdit =(IWorkspaceEdit) pDataset.Workspace;
				if (!pWorkspaceEdit.IsBeingEdited())
				{
					MessageBox.Show("当前图层不可编辑");
					return ;
				}

				pWorkspaceEdit.StartEditOperation();
				pFeature.Shape = pGeometry;
				pFeature.Store();
				pWorkspaceEdit.StopEditOperation();
			}
			catch(Exception e)
			{
				MessageBox.Show(e.Message.ToString());
			}
		}

	}
}

⌨️ 快捷键说明

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