using ESRI.ArcGIS.ADF.BaseClasses; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Geometry; using System; using System.Collections.Generic; namespace Kingo.Plugin.General.Tool { public class DrawPolylineTool : BaseTool { private EngineEditorClass editor = new EngineEditorClass(); //private IEngineSnapEnvironment snapEnv = null; private IHookHelper m_hookHelper; //private EngineSnapAgentClass m_SnapAgent; private INewLineFeedback m_NewPolygonFeedback; private bool IsDrawing = false; public event Action DrawCompleted; private List points = new List(); //private IPoint snapPoint; //private bool m_isHasSnap; public DrawPolylineTool() { base.m_caption = "绘制线"; base.m_toolTip = "左键开始绘制,双击结束绘制"; base.m_cursor = System.Windows.Forms.Cursors.Cross; } public override void OnCreate(object hook) { try { if (hook == null) { return; } try { if (m_hookHelper == null) { m_hookHelper = new HookHelperClass(); m_hookHelper.Hook = hook; } } catch { m_hookHelper = null; return; } //snapEnv = (IEngineSnapEnvironment)editor; this.m_NewPolygonFeedback = new NewLineFeedbackClass() { Display = this.m_hookHelper.ActiveView.ScreenDisplay }; } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug(ex); } } public override void OnClick() { //this.m_SnapAgent.Reset(); IsDrawing = true; points.Clear(); } public override void OnDblClick() { try { if (!IsDrawing) return; IPolyline line = this.m_NewPolygonFeedback.Stop(); if (points.Count > 1) { OnDrawCompleted(line); } points.Clear(); //(this.m_hookHelper.Hook as IToolbarBuddy).CurrentTool = new ESRI.ArcGIS.Controls.ControlsMapPanToolClass(); } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug(ex); } } public override void OnMouseDown(int Button, int Shift, int X, int Y) { try { if (!IsDrawing) return; if (Button == 1) { //左键开始绘制 IPoint point = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); //if (snapEnv != null) //{ // snapEnv.SnapPoint(point); //} if (points.Count == 0) { this.m_NewPolygonFeedback.Start(point); } else { this.m_NewPolygonFeedback.AddPoint(point); } points.Add(point); } else if (Button == 2) { } } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug(ex); } } public override bool Deactivate() { //右键结束绘制 IsDrawing = false; return base.Deactivate(); } public override void OnMouseMove(int Button, int Shift, int X, int Y) { try { if (!IsDrawing) return; IPoint point = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); //if (snapEnv != null) //{ // this.SnapPoint(m_hookHelper.ActiveView, point, snapEnv); //} if (points.Count > 0) { this.m_NewPolygonFeedback.MoveTo(point); } } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug(ex); } } public void SnapPoint(IActiveView activeView, IPoint inPoint, IEngineSnapEnvironment m_editEnvironment) { try { //if (this.m_isHasSnap && (this.snapPoint != null)) //{ // this.DrawSnapPoint(activeView, this.snapPoint); //} //this.m_isHasSnap = ((IEngineSnapEnvironment)m_editEnvironment).SnapPoint(inPoint); //if (this.m_isHasSnap) //{ // this.snapPoint = inPoint; // this.DrawSnapPoint(activeView, this.snapPoint); //} } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug(ex); } } private void DrawSnapPoint(IActiveView activeView, IPoint point) { try { IScreenDisplay screenDisplay = activeView.ScreenDisplay; screenDisplay.StartDrawing(screenDisplay.hDC, -1); IEngineEditProperties engProperties = new EngineEditorClass(); screenDisplay.SetSymbol((ISymbol)engProperties.SnapSymbol); screenDisplay.DrawPoint(point); screenDisplay.FinishDrawing(); screenDisplay = null; } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug(ex); } } public virtual void OnDrawCompleted(IPolyline polygon) { try { if (this.DrawCompleted != null) DrawCompleted(polygon); } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug(ex); } } } }