




已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
利用ArcEngine实现距离量测,面积量测的功能已很简单,相信众多的ArcGIS爱好者都能写. 但单纯的实现功能总觉得欠什么.本人喜欢改代码,喜欢优化代码,在原有的功能基础上总喜欢润色,使之更好看.前一整子在玩skyline时看到 skyline 的测距功能能实时显示量测的距离,于是联想到之前自己用C#+ArcEngine 写的测距功能.何不也优化一下自己代码? 想到就做到.最近手有点痒,算练练手.废话少说,先附上效果图:说明:1.本功能的特点在于在量测的过称当中实时显示量测距离,并将结果实时显示在测距上方.在显示窗体上也显示量测的总距离.要点:1. INewLineFeedback 负责画线;2. 在OnMouseDown事件中计算量测距离,并向GraphicsContainer添加线和节点的Element;3.在OnMouseUp事件中实时计算距离随鼠标移动后产生的新距离.4. 将节点,量测值,线的element都分别存入到节点组和轨迹线组(IGroupElement),即用IGroupElement同一管理这些element; 最后将这些groupelement又添加到一个总的groupelement.这样做的目的在于好控制这些element,特别是启动新的量测或取消量测功能时可以控制这些element,而不必去用IGraphicsContainer.DeleteAllElements来清除这些element,使用IGraphicsContainer.DeleteAllElements会将所有的element删除.5. 量测值element 为ITextElement, 由于他停靠在轨迹线的上方,因此需要对量测值element进行一定角度的旋转,旋转角度由轨迹线的方向角决定.6. 绘制element之后的刷新问题也是比较重要的. 刷新范围一定要控制好,太小了,添加的element显示不出来,太大了,浪费. (实在不好控制就刷新整个extent吧,呵呵)核心代码如下: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.CATIDs;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;privateIPointCollectionm_ptColl;/记录节点privateMeasureMsgInfo_MsgInfo=null;privateIPolylinem_TraceLine=null;/完整的轨迹线/privateIGroupElementm_Elements=null;/用于保存包含此功能产生的所有ElementprivateIGroupElementm_TraceElement=null;/测距轨迹线privateIGroupElementm_VertexElement=null;/结点privateIGroupElementm_LabelElement=null;/距离标记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)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);publicMeasureMsgInfoMsgInfoset_MsgInfo=value;_MsgInfo.FormClosing+=newFormClosingEventHandler(msgInfo_FromClosing);#regionOverridenClassMethods/Occurswhenthistooliscreated/InstanceoftheapplicationpublicoverridevoidOnCreate(objecthook)trym_hookHelper=newHookHelperClass();m_hookHelper.Hook=hook;if(m_hookHelper.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=newGroupElementClass();/初始化,并添加到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.MoveElementToGroup(m_VertexElementasIElement,m_Elements);g.MoveElementToGroup(m_LabelElementasIElement,m_Elements);g.MoveElementToGroup(m_TraceElementasIElement,m_Elements);/Occurswhenthistoolisclicked/publicoverridevoidOnClick()Init();voidmsgInfo_FromClosing(objectsender,FormClosingEventArgse)DeleteAllElements();_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;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=null)/移除elementRemoveElements();/刷新m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);Application.DoEvents();m_NewLineFeedback=newNewLineFeedbackClass();m_NewLineFeedback.Display=m_hookHelper.ActiveView.ScreenDisplay;/必须先得到symbol,后设置symbolISimpleLineSymbolsimpleLineSymbol=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();/记录节点objectobj=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_hookHelper.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(fromPt,toPt);#region绘制轨迹线tryobjectmissing=Type.Missing;ISegmentCollectionsegColl=newPolylineClass();segColl.AddSegment(lineasISegment,refmissing,refmissing);IElementtraceElement=CreateElement(segCollasIPolyline);/g=m_hookHelper.ActiveViewasIGraphicsContainer;/g.AddElement(traceElement,0);/g.MoveElementToGroup(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)/大于90度小于等于180angle+=Math.PI;/标注点Y值偏移量doubled_OffsetY=m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.FromPoints(9);/标注点doubled_CenterX=(fromPt.X+toPt.X)/2;doubled_CenterY=(fromPt.Y+toPt.Y)/2+d_OffsetY;/向上偏移IPointlabelPt=newPointClass();labelPt.PutCoords(d_CenterX,d_CenterY);ITextElementtxtElement=CreateTextElement(line.Length.ToString(0.00);IElementlabelelement=txtElementasIElement;labelelement.Geometry=labelPt;objectoElement=(object)labelelement;/根据角度旋转TransformByRotate(refoElement,labelPt,angle);/添加到GraphicsContainer/g.AddElement(labelelement,0);/移到m_LabelElement组中/g.MoveElementToGroup(labelelement,m_LabelElement);/添加到组m_LabelElement.AddElement(labelelement);/刷新m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,labelelement,pEnvBounds);catch#endregioncatchpublicoverridevoidOnMouseMove(intButton,intShift,intX,intY)if(m_NewLineFeedback=null)return;IPointpt=m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y);m_NewLineFeedback.MoveTo(pt);if(m_ptColl.PointCount=0)return;doubled_Total=0;doubled_segment=0;IPointlastPt=m_ptColl.get_Point(m_ptColl.PointCount-1);ILineline=newLineClass();line.PutCoords(lastPt,pt);/节距离d_segment=line.Length;_MsgInfo.Segment=d_segment;tryIPolylinepolyline=m_ptCollasIPolyline;if(polyline.IsEmpty)d_Total=d_segment;elsed_Total=polyline.Length+d_segment;catch/赋值给总长度_MsgInfo.Total=d_Total;publicoverridevoidOnDblClick()if(m_NewLineFeedback=null)return;/绘制线与多边形几何图形时,双击结束绘制trym_TraceLine=m_NewLineFeedback.Stop();if(m_TraceLine=null)return;catchfinallyRecycle();#endregion/回收publicvoidRecycle()m_NewLineFeedback=null;m_ptColl.RemovePoints(0,m_ptColl.PointCount);m_ptColl=null;m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,m_hookHelper.ActiveView.Extent);/从组中删除所有元素/voidRemoveElementFromGroupElement(IGroupElementgroupElement)if(groupElement=null|groupElement.ElementCount=0)return;tryIGraphicsContainerg=m_hookHelper.ActiveView.GraphicsContainer;for(intindex=0;indexgroupElement.ElementCount;index+)IElementtmp_Ele=groupElement.get_Element(index);if(tmp_EleisIGroupElement)RemoveElementFromGroupElement(tmp_EleasIGroupElement);elsetrygroupElement.DeleteElement(tmp_Ele);catchfinallytmp_Ele=null;/groupElement.ClearElements();catchfinally/刷新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;m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,pEnvBounds);/移除节点,标注和轨迹线Element/voidRemoveElements()try/刷新一次m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,m_hookHelper.ActiveView.Extent);IGraphicsContainerg=m_hookHelper.ActiveView.GraphicsContainer;#region1-new/RemoveElementFromGroupElement(m_Elements);#endregion#region2m_LabelElement.ClearElements();m_VertexElement.ClearElements();m_TraceElement.ClearElements();#endregioncatchfinally/刷新一次m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,m_hookHelper.ActiveView.Extent);/删除所有与此相关的元素/publicvoidDeleteAllElements()/m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);IGraphicsContainerg=m_hookHelper.ActiveView.GraphicsContainer;RemoveElementFromGroupElement(m_Elements);tryg.DeleteElement(m_ElementsasIElement);catchfinallym_TraceElement=null;m_LabelElement=null;m_VertexElement=null;m_Elements=null;/最后再刷新一次m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);/将系统颜色转换为I
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025生物识别技术在医疗健康领域的市场竞争态势研究报告
- 2025年医疗器械国产化:产业链上下游协同创新与市场前景研究报告
- 太阳粒子辐射特性-洞察及研究
- 聚焦2025年新能源汽车整车动力系统创新研究报告
- 文件系统在智慧交通流量管理中的应用-洞察及研究
- 智能运维数据分析-洞察及研究
- 个性化医疗伦理框架构建-洞察及研究
- 安全性缺陷的动态监测-洞察及研究
- AR电影观众感知分析-洞察及研究
- 仓储智能穿戴集成-洞察及研究
- 版大学习、大培训、大考试专项行动工作方案
- 校企联建支部活动方案
- 2025至2030年中国医用激光光纤行业市场全景分析及产业前景研判报告
- 2025年兵团职工考试试题及答案
- 如何使用第四种检查器文档
- 硫酸盐酸安全管理制度
- 2025秋部编版(2024)八年级上册语文上课课件 第二单元 阅读综合实践
- lng燃气安全管理制度
- 2022年全国青少年禁毒知识竞赛题库附答案(共470题)
- 2025年高考数学全国新课标Ⅱ卷试卷评析及备考策略(课件)
- 钙钛矿型稀土氧化物的制备与红外发射率性能研究
评论
0/150
提交评论