1、精选c# ArcEngine93 实现实时显示测距结果功能c#+ArcEngine93 实现实时显示测距结果功能利用ArcEngine实现距离量测,面积量测的功能已很简单,相信众多的ArcGIS爱好者都能写. 但单纯的实现功能总觉得欠什么.本人喜欢改代码,喜欢优化代码,在原有的功能基础上总喜欢润色,使之更好看.前一整子在玩skyline时看到 skyline 的测距功能能实时显示量测的距离,于是联想到之前自己用C#+ArcEngine 写的测距功能.何不也优化一下自己代码? 想到就做到.最近手有点痒,算练练手.废话少说,先附上效果图:说明:1.本功能的特点在于在量测的过称当中实时显示量测距离,
2、并将结果实时显示在测距上方.在显示窗体上也显示量测的总距离.要点:1. INewLineFeedback 负责画线;2. 在OnMouseDown事件中计算量测距离,并向GraphicsContainer添加线和节点的Element;3.在OnMouseUp事件中实时计算距离随鼠标移动后产生的新距离.4. 将节点,量测值,线的element都分别存入到节点组和轨迹线组(IGroupElement),即用IGroupElement同一管理这些element; 最后将这些groupelement又添加到一个总的groupelement.这样做的目的在于好控制这些element,特别是启动新的量测或
3、取消量测功能时可以控制这些element,而不必去用IGraphicsContainer.DeleteAllElements来清除这些element,使用IGraphicsContainer.DeleteAllElements会将所有的element删除.5. 量测值element 为ITextElement, 由于他停靠在轨迹线的上方,因此需要对量测值element进行一定角度的旋转,旋转角度由轨迹线的方向角决定.6. 绘制element之后的刷新问题也是比较重要的. 刷新范围一定要控制好,太小了,添加的element显示不出来,太大了,浪费. (实在不好控制就刷新整个extent吧,呵呵)
4、核心代码如下:1. TrackLine类TrackLine/=功能测距=/描述:实时显示测量距离,节点位置,总长度/编程:Jin开发时间:2009.8.10-2009.8.11/特点:实时计算量测距离./缺点:由于采用element做为显示内容,地图进行放大缩小操作后并不能很好/控制量测值element和轨迹线element之间的距离间隔./=usingSystem;usingSystem.Drawing;usingSystem.Runtime.InteropServices;usingESRI.ArcGIS.ADF.BaseClasses;usingESRI.ArcGIS.ADF.CATID
5、s;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Geodatabase;usingESRI.ArcGIS.Display;usingESRI.ArcGIS.Geometry;usingSystem.Windows.Forms;namespaceMhGis.GisTool.Pb_ToolbarclassTrackLine:BaseToolprivateIHookHelperm_hookHelper=null;privateINewLineFeedbackm_NewLineFeedback=null;priva
6、teIPointCollectionm_ptColl;/记录节点privateMeasureMsgInfo_MsgInfo=null;privateIPolylinem_TraceLine=null;/完整的轨迹线/privateIGroupElementm_Elements=null;/用于保存包含此功能产生的所有ElementprivateIGroupElementm_TraceElement=null;/测距轨迹线privateIGroupElementm_VertexElement=null;/结点privateIGroupElementm_LabelElement=null;/距离标
7、记publicTrackLine()/TODO:Definevaluesforthepublicproperties/base.m_category=;/localizabletextbase.m_caption=;/localizabletextbase.m_message=ThisshouldworkinArcMap/MapControl/PageLayoutControl;/localizabletextbase.m_toolTip=;/localizabletextbase.m_name=;/uniqueid,non-localizable(e.g.MyCategory_MyTool)
8、try/TODO:changeresourcenameifnecessary/stringbitmapResourceName=GetType().Name+.bmp;base.m_bitmap=newBitmap(GetType(),bitmapResourceName);base.m_cursor=newSystem.Windows.Forms.Cursor(GetType(),GetType().Name+.cur);catch(Exceptionex)System.Diagnostics.Trace.WriteLine(ex.Message,InvalidBitmap);publicM
9、easureMsgInfoMsgInfoset_MsgInfo=value;_MsgInfo.FormClosing+=newFormClosingEventHandler(msgInfo_FromClosing);#regionOverridenClassMethods/Occurswhenthistooliscreated/InstanceoftheapplicationpublicoverridevoidOnCreate(objecthook)trym_hookHelper=newHookHelperClass();m_hookHelper.Hook=hook;if(m_hookHelp
10、er.ActiveView=null)m_hookHelper=null;catchm_hookHelper=null;if(m_hookHelper=null)base.m_enabled=false;elsebase.m_enabled=true;/TODO:AddotherinitializationcodevoidInit()/初始化m_Elements=newGroupElementClass();m_TraceElement=newGroupElementClass();m_VertexElement=newGroupElementClass();m_LabelElement=ne
11、wGroupElementClass();/初始化,并添加到GraphicsContainerIGraphicsContainerg=m_hookHelper.ActiveViewasIGraphicsContainer;g.AddElement(m_ElementsasIElement,0);g.AddElement(m_TraceElementasIElement,0);g.AddElement(m_VertexElementasIElement,0);g.AddElement(m_LabelElementasIElement,0);/添加到m_Elements中g.MoveElement
12、ToGroup(m_VertexElementasIElement,m_Elements);g.MoveElementToGroup(m_LabelElementasIElement,m_Elements);g.MoveElementToGroup(m_TraceElementasIElement,m_Elements);/Occurswhenthistoolisclicked/publicoverridevoidOnClick()Init();voidmsgInfo_FromClosing(objectsender,FormClosingEventArgse)DeleteAllElement
13、s();_MsgInfo=null;/thrownewException(Themethodoroperationisnotimplemented.);publicoverridevoidOnMouseDown(intButton,intShift,intX,intY)if(Button=2)return;IPointpt=m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y);IGraphicsContainerg=m_hookHelper.ActiveView.GraphicsContainer
14、;IEnvelopepEnvBounds=null;/获取上一次轨迹线的范围,以便确定刷新范围tryif(m_TraceLine!=null)m_TraceLine.QueryEnvelope(pEnvBounds);pEnvBounds.Expand(4,4,true);/矩形框向四周扩大4倍(大于2倍就行),目的是为了保证有充足的刷新区域elsepEnvBounds=m_hookHelper.ActiveView.Extent;catchpEnvBounds=m_hookHelper.ActiveView.Extent;#region启动画线if(m_NewLineFeedback=nul
15、l)/移除elementRemoveElements();/刷新m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);Application.DoEvents();m_NewLineFeedback=newNewLineFeedbackClass();m_NewLineFeedback.Display=m_hookHelper.ActiveView.ScreenDisplay;/必须先得到symbol,后设置symbolISimpleLineSymbolsimpleLineSym
16、bol=m_NewLineFeedback.SymbolasISimpleLineSymbol;simpleLineSymbol.Style=esriSimpleLineStyle.esriSLSDot;simpleLineSymbol.Width=1;simpleLineSymbol.Color=TransColorToAEColor(Color.Blue);m_NewLineFeedback.Start(pt);elsem_NewLineFeedback.AddPoint(pt);if(m_ptColl=null)m_ptColl=newPolylineClass();/记录节点objec
17、tobj=Type.Missing;m_ptColl.AddPoint(pt,refobj,refobj);#endregion#region绘制结点tryIElementvertexElement=CreateElement(pt);/g=m_hookHelper.ActiveViewasIGraphicsContainer;/g.AddElement(vertexElement,0);/g.MoveElementToGroup(vertexElement,m_VertexElement);m_VertexElement.AddElement(vertexElement);/刷新m_hook
18、Helper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,vertexElement,pEnvBounds);catch#endregiontryif(m_ptColl.PointCount=2)IPointfromPt=m_ptColl.get_Point(m_ptColl.PointCount-2);/倒数第二个点IPointtoPt=m_ptColl.get_Point(m_ptColl.PointCount-1);/最后第一个点ILineline=newLineClass();line.PutCoords(f
19、romPt,toPt);#region绘制轨迹线tryobjectmissing=Type.Missing;ISegmentCollectionsegColl=newPolylineClass();segColl.AddSegment(lineasISegment,refmissing,refmissing);IElementtraceElement=CreateElement(segCollasIPolyline);/g=m_hookHelper.ActiveViewasIGraphicsContainer;/g.AddElement(traceElement,0);/g.MoveEleme
20、ntToGroup(traceElement,m_TraceElement);m_TraceElement.AddElement(traceElement);m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,traceElement,pEnvBounds);catch#endregion#region计算单线的长度,并将结果显示在单线中点偏上上面trydoubleangle=line.Angle;if(angle(Math.PI/2)&angle-Math.PI&angle-(Math.PI/2)/
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1