版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上利用ArcEngine实现距离量测,面积量测的功能已很简单,相信众多的ArcGIS爱好者都能写. 但单纯的实现功能总觉得欠什么.本人喜欢改代码,喜欢优化代码,在原有的功能基础上总喜欢"润色",使之更好看.前一整子在玩skyline时看到 skyline 的测距功能能实时显示量测的距离,于是联想到之前自己用C#+ArcEngine 写的测距功能.何不也优化一下自己代码? 想到就做到.最近手有点痒,算练练手.废话少说,先附上效果图:说明:1.本功能的特点在于在量测的过称当中实时显示量测距离,并将结果实时显示在测距上方.在显示窗体上也显示量测的总距离.要
2、点:1. INewLineFeedback 负责画线;2. 在OnMouseDown事件中计算量测距离,并向GraphicsContainer添加线和节点的Element;3.在OnMouseUp事件中实时计算距离随鼠标移动后产生的新距离.4. 将节点,量测值,线的element都分别存入到节点组和轨迹线组(IGroupElement),即用IGroupElement同一管理这些element; 最后将这些groupelement又添加到一个总的groupelement.这样做的目的在于好控制这些element,特别是启动新的量测或取消量测功能时可以控制这些element,而不必去用IGrap
3、hicsContainer.DeleteAllElements来清除这些element,使用IGraphicsContainer.DeleteAllElements会将所有的element删除.5. 量测值element 为ITextElement, 由于他停靠在轨迹线的上方,因此需要对量测值element进行一定角度的旋转,旋转角度由轨迹线的方向角决定.6. 绘制element之后的刷新问题也是比较重要的. 刷新范围一定要控制好,太小了,添加的element显示不出来,太大了,浪费. (实在不好控制就刷新整个extent吧,呵呵)核心代码如下:1. TrackLine类TrackLine/=
4、功能测距=/描述:实时显示测量距离,节点位置,总长度/编程: Jin 开发时间:2009.8.10-2009.8.11/特点:实时计算量测距离./缺点: 由于采用element做为显示内容,地图进行放大缩小操作后并不能很好/ 控制量测值element和轨迹线element之间的距离间隔./=using System;us
5、ing System.Drawing;using System.Runtime.InteropServices;using ESRI.ArcGIS.ADF.BaseClasses;using ESRI.ArcGIS.ADF.CATIDs;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.Display;using ESRI.ArcGIS.Geometry;us
6、ing System.Windows.Forms;namespace MhGis.GisTool.Pb_Toolbar class TrackLine : BaseTool private IHookHelper m_hookHelper = null;
7、0; private INewLineFeedback m_NewLineFeedback = null; private IPointCollection m_ptColl; /记录节点 private MeasureMsgInfo _MsgInfo = null;
8、 private IPolyline m_TraceLine = null; /完整的轨迹线 / private IGroupElement m_Elements = null; /用于保存包含此功能产生的所有Element
9、60; private IGroupElement m_TraceElement = null; /测距轨迹线 private IGroupElement m_VertexElement = null; /结点 private
10、160;IGroupElement m_LabelElement = null; / 距离标记 public TrackLine() /
11、 / TODO: Define values for the public properties / base.m_category = "&qu
12、ot; /localizable text base.m_caption = "" /localizable text base.m_message = "This
13、60;should work in ArcMap/MapControl/PageLayoutControl" /localizable text base.m_toolTip = "" /localizable text
14、; base.m_name = "" /unique id, non-localizable (e.g. "MyCategory_MyTool") try
15、160; / / TODO: change resource name if necessary
16、160; / string bitmapResourceName = GetType().Name + ".bmp"
17、; base.m_bitmap = new Bitmap(GetType(), bitmapResourceName); base.m_cursor = new System.Windows.Forms.Cursor(GetType(),
18、0;GetType().Name + ".cur"); catch (Exception ex) &
19、#160; System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
20、 public MeasureMsgInfo MsgInfo set
21、 _MsgInfo = value; _MsgInfo.FormClosing += new FormClosingEventHandler(msgInfo_FromClosing);
22、; #region Overriden Class Methods / <summary>
23、60; / Occurs when this tool is created / </summary> / <param name="hook">Instance of the application</param>
24、; public override void OnCreate(object hook) try
25、 m_hookHelper = new HookHelperClass(); m_hookHelper.Hook = hook;
26、60; if (m_hookHelper.ActiveView = null) &
27、#160; m_hookHelper = null; &
28、#160; catch m_hookHelper = null;
29、160; if (m_hookHelper = null) base.m_enabled = false;
30、160;else base.m_enabled = true; / TODO: Add other initialization code
31、 void Init() /初始化 m_Elements
32、= new GroupElementClass(); m_TraceElement = new GroupElementClass(); m_VertexElement = new GroupElementClass(); &
33、#160; m_LabelElement = new GroupElementClass(); /初始化,并添加到GraphicsContainer IGraphics
34、Container g = m_hookHelper.ActiveView as IGraphicsContainer; g.AddElement(m_Elements as IElement, 0); g.AddElement(m_T
35、raceElement as IElement, 0); g.AddElement(m_VertexElement as IElement, 0); g.AddElement(m_LabelElement as IElement,
36、60;0); /添加到m_Elements中 g.MoveElementToGroup(m_VertexElement as IElement, m_Elements);
37、0; g.MoveElementToGroup(m_LabelElement as IElement, m_Elements); g.MoveElementToGroup(m_TraceElement as IElement, m_Elements);
38、160; / <summary> / Occurs when this tool is clicked / </summary> public o
39、verride void OnClick() Init(); void msgInfo_FromClosing(object sen
40、der, FormClosingEventArgs e) DeleteAllElements(); _MsgInfo = null;
41、 /throw new Exception("The method or operation is not implemented."); public override void On
42、MouseDown(int Button, int Shift, int X, int Y) if (Button = 2)
43、60; return; IPoint pt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); IGraphicsCont
44、ainer g = m_hookHelper.ActiveView.GraphicsContainer; IEnvelope pEnvBounds = null; /获取上一次轨迹线的范围,以便确定刷新范围 &
45、#160; try if (m_TraceLine != null) &
46、#160; m_TraceLine.QueryEnvelope(pEnvBounds);
47、; pEnvBounds.Expand(4, 4, true); /矩形框向四周扩大4倍(大于2倍就行),目的是为了保证有充足的刷新区域
48、160;else pEnvBounds = m_hookHelper.ActiveView.Extent;
49、0; catch pEnvBounds = m_hookHelper.ActiveView.Extent;
50、; #region 启动画线 if (m_NewLineFeedback = null)
51、0; /移除element RemoveElements(); &
52、#160; /刷新 m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
53、60; Application.DoEvents(); m_NewLineFeedback = new NewLineFeedbackClass(); m_NewLineFeedb
54、ack.Display = m_hookHelper.ActiveView.ScreenDisplay; /必须先得到symbol,后设置symbol ISimpleLineSymbol s
55、impleLineSymbol = m_NewLineFeedback.Symbol as ISimpleLineSymbol; simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDot;
56、 simpleLineSymbol.Width = 1; simpleLineSymbol.Color = TransColorToAEColor(Color.Blue);
57、0; m_NewLineFeedback.Start(pt); else
58、160; m_NewLineFeedback.AddPoint(pt); if (m_ptColl = null)
59、160; m_ptColl = new PolylineClass();
60、0; /记录节点 object obj = Type.Missing; m_ptColl.AddPoint(pt, ref obj, ref obj);
61、 #endregion #region 绘制结点 try
62、 IElement vertexElement = CreateElement(pt); / &
63、#160; g = m_hookHelper.ActiveView as IGraphicsContainer; /g.AddElement(vertexElement, 0);
64、160; /g.MoveElementToGroup(vertexElement, m_VertexElement); m_VertexElement.AddElement(vertexElement);
65、60; /刷新 m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, vertexElement, pEnvBounds);
66、160; catch #endregion
67、; try if (m_ptColl.PointCount >= 2)
68、60; IPoint fromPt = m_ptColl.get_Point(m_ptColl.PointCount - 2); /倒数第二个点 &
69、#160; IPoint toPt = m_ptColl.get_Point(m_ptColl.PointCount - 1); /最后第一个点 &
70、#160; ILine line = new LineClass(); line.PutCoords(fromPt, toPt);
71、160; #region 绘制轨迹线 try
72、; object missing = Type.Missing;
73、160; ISegmentCollection segColl = new PolylineClass(); segColl.AddSegment(line as ISegment, r
74、ef missing, ref missing); IElement traceElement = CreateElement(segColl as IPolyline); &
75、#160; / g = m_hookHelper.ActiveView
76、;as IGraphicsContainer; /g.AddElement(traceElement, 0);
77、160; /g.MoveElementToGroup(traceElement, m_TraceElement); m_TraceElement.AddElement(traceElement); &
78、#160; m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, traceElement, pEnvBounds);
79、0; catch
80、; #endregion #region 计算单线的长度,并将结果显示在单线中
81、点偏上上面 try
82、0; double angle = line.Angle; if (angle >
83、; (Math.PI / 2) && angle < (Math.PI) | (angle > -Math.PI && angle < -(Math.PI / 2) / 大于90度小于等于180
84、; angle += Math.PI; /标注点Y值偏移量
85、; double d_OffsetY = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.FromPoints(9);
86、; /标注点 double d_CenterX = (fromPt.X + toPt.X) / 2; &
87、#160; double d_CenterY = (fromPt.Y + toPt.Y) / 2 + d_OffsetY; /向上偏移
88、0; IPoint labelPt = new PointClass(); labelPt.PutCoord
89、s(d_CenterX, d_CenterY); ITextElement txtElement = CreateTextElement(line.Length.ToString("0.00");
90、; IElement labelelement = txtElement as IElement;
91、; labelelement.Geometry = labelPt; object oElement = (object)labelelement;
92、; /根据角度旋转 TransformByRotate(ref oElemen
93、t, labelPt, angle); /添加到GraphicsContainer
94、0; /g.AddElement(labelelement, 0); /移到m_LabelElement组中
95、60; /g.MoveElementToGroup(labelelement, m_LabelElement); /添加到组 &
96、#160; m_LabelElement.AddElement(labelelement);
97、60; /刷新 m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, labelelement, pEnvBounds);
98、60; catch
99、0; #endregion
100、 catch public&
101、#160;override void OnMouseMove(int Button, int Shift, int X, int Y) if (m_NewLineFeedback = null)
102、 return; IPoint pt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
103、0; m_NewLineFeedback.MoveTo(pt); if (m_ptColl.PointCount = 0) return;
104、60; double d_Total = 0; double d_segment = 0; IPoint lastPt = m_pt
105、Coll.get_Point(m_ptColl.PointCount - 1); ILine line = new LineClass(); line.PutCoords(lastPt, pt); &
106、#160; /节距离 d_segment = line.Length; _MsgInfo.Segment = d_segment;
107、160; try IPolyline polyline = m_ptColl as IPolyline;
108、60; if (polyline.IsEmpty) &
109、#160; d_Total = d_segment; else
110、60; d_Total = polyline.Length + d_segment;
111、; catch
112、 /赋值给总长度 _MsgInfo.Total = d_Total; public
113、override void OnDblClick() if (m_NewLineFeedback = null) retu
114、rn; /绘制线与多边形几何图形时,双击结束绘制 try
115、0; m_TraceLine = m_NewLineFeedback.Stop(); if (m_TraceLine = null) &
116、#160; return; catch &
117、#160; finally Recycle();
118、160; #endregion /回收 public void Recycle()
119、 m_NewLineFeedback = null; m_ptColl.RemovePoints(0, m_ptColl.PointCount); m_ptColl
120、160;= null; m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, m_hookHelper.ActiveView.Extent);
121、; / <summary> / 从组中删除所有元素 / </summary> / <param name="groupElement"></param> &
122、#160; void RemoveElementFromGroupElement(IGroupElement groupElement) if (groupElement = null | groupElement.ElementCount
123、60;= 0) return; try
124、160; IGraphicsContainer g = m_hookHelper.ActiveView.GraphicsContainer; for (int index = 0; index <
125、groupElement.ElementCount; index+) IElement tmp_Ele = groupElemen
126、t.get_Element(index); if (tmp_Ele is IGroupElement) &
127、#160; RemoveElementFromGroupElement(tmp_Ele as IGroupElement); else
128、60; try
129、 groupElement.DeleteElement(tmp_Ele);
130、 catch &
131、#160; &
132、#160; finally
133、 tmp_Ele = null;
134、; /groupElement.ClearElements();
135、; catch finally
136、; /刷新 IEnvelope pEnvBounds =
137、null; /获取上一次轨迹线的范围,以便确定刷新范围 try &
138、#160; if (m_TraceLine != null)
139、0; m_TraceLine.QueryEnvelope(pEnvBounds);
140、160; pEnvBounds.Expand(4, 4, true); /矩形框向四周扩大4倍(大于2倍就行),目的是为了保证有充足的刷新区域
141、; else pEnvBounds = m_hookHelper.ActiveView.Extent; &
142、#160; catch
143、160; pEnvBounds = m_hookHelper.ActiveView.Extent;
144、160;m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pEnvBounds); / <summary>
145、60; / 移除节点,标注和轨迹线Element / </summary> void RemoveElements() &
146、#160; try /刷新一次
147、; m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, m_hookHelper.ActiveView.Extent); IGraphicsContainer g = m_hookHelper.ActiveView.GraphicsCont
148、ainer; #region 1-new /RemoveElementFromGroupElement(m_Elements);
149、; #endregion #region 2 m_LabelElement.ClearElements(
150、); m_VertexElement.ClearElements(); m_TraceElement.ClearElements(); &
151、#160; #endregion catch
152、0; finally
153、60;/刷新一次 m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, m_hookHelper.ActiveView.Extent); &
154、#160; / <summary> / 删除所有与此相关的元素 / </summary>
155、0;public void DeleteAllElements() /m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
156、60; IGraphicsContainer g = m_hookHelper.ActiveView.GraphicsContainer; RemoveElementFromGroupElement(m_Elements); t
157、ry g.DeleteElement(m_Elements as IElement);
158、160; catch finally
159、160; m_TraceElement = null; m_LabelElement = null;
160、0; m_VertexElement = null; m_Elements = null;
161、60;/最后再刷新一次 m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
162、 / <summary> / 将系统颜色转换为IColor / </summary> / <param
163、160;name="color"></param> / <returns></returns> ESRI.ArcGIS.Display.IColor TransColorToAEColor(Color color)
164、; IRgbColor rgb = new RgbColorClass(); rgb.RGB = color.R + color.G * 256 + color.B * 65536;
165、60; return rgb as IColor; / <summary> / 按指定的角度旋转
166、160; / </summary> / <param name="obj"></param> / <param name="originPt"></param>
167、0; / <param name="rotate"></param> void TransformByRotate(ref object obj, IPoint originPt, double rotate)
168、0; if (obj = null && originPt = null) return;
169、0; try ITransform2D transform2D = obj as ITransform2D; &
170、#160; if (transform2D = null) return; transform2D.Rotate(originPt, rotate);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年秋季开学高中开学第一课(爱护公物)课件
- 2026年秋季开学幼儿园队列纪律与口令课件
- 2026年秋季开学初中稍息与立正训练课件
- 2026年秋季开学高中开学第一课(卫生健康)课件
- 2026新高二数学暑假专题:复习(6):立体几何初步
- 政治试卷黑龙江哈尔滨第三中学2024-2025学年度下学期高一学年期末考试(7.11-7.12)
- 政治试卷+答案甘肃白银市2024-2025学年高一年级下学期期未考试模拟卷(三)(GSZW)(7.12-7.13)
- 基于区域协同的新质生产力发展路径研究
- 制造业供应链中断的应对策略与快速恢复机制
- 数字化转型实施路径的系统化框架与策略分析
- 2026年泌尿外科出科试卷及答案
- 2026小学数学北师大版新教材培训:四至六年级教材解析
- AI原生数据平台研究报告(2026年)(2026.6)
- 2026年成都玉林紫荆初一入学数学分班考试真题含答案
- 工程预应力张拉与灌浆质量控制措施
- 一氧化二氮的理化性质与危险特性培训
- 2026年江苏省安全员C2证(土建安全员)考试题库及答案
- DB11-T 1610-2026 民用建筑信息模型深化设计建模细度标准
- 保安员监控岗工作制度
- (正式版)DB36∕T 1297-2020 《城市消防物联网大数据应用平台物联设施设备接口规范》
- 上海学前教育课程指南
评论
0/150
提交评论