using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using KGIS.Framework.EngineEditor; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using System; using System.Collections.Generic; namespace Kingo.Plugin.EngineEditor.Commands.Tools { public class MoveSharedPointTool : BaseToolCmd { private EngineEditorClass editor = new EngineEditorClass(); private IHookHelper m_hookHelper; private bool m_isDrawing = false; //private IEngineSnapEnvironment snapEnv = null; //public string LayerName { get; set; } private IPoint snapPoint = null; private IPoint firstPoint = null; private List firstSnapPointList = null; private INewLineFeedback m_NewPolygonFeedback; public MoveSharedPointTool() { //this.LayerName = "DLTB"; this.m_caption = "共点移动工具"; this.m_category = "共点移动工具"; this.m_name = "共点移动工具"; this.m_message = "同时移动当前图层公用改点的图形端点"; base.m_cursor = System.Windows.Forms.Cursors.Cross; } public override void OnCreate(object hook) { try { base.OnCreate(hook); if (this.m_hookHelper == null) { this.m_hookHelper = base.m_pHookHelper; } //IHookHelper2 hookHelper2 = m_hookHelper as IHookHelper2; //if (hookHelper2 != null) //{ // IExtensionManager extensionManager = hookHelper2.ExtensionManager; // if (extensionManager != null) // { // IExtension extension = extensionManager.FindExtension(new UIDClass // { // Value = "{E07B4C52-C894-4558-B8D4-D4050018D1DA}"//捕捉 // }); // if (extension != null) // { // this.m_pSnappingEnv = (extension as ISnappingEnvironment); // this.m_pPntSnapper = this.m_pSnappingEnv.PointSnapper; // this.m_pSnappingFeedback = new SnappingFeedbackClass(); // this.m_pSnappingFeedback.Initialize(hook, this.m_pSnappingEnv, true); // } // } //} //snapEnv = (IEngineSnapEnvironment)editor; //IEngineFeatureSnapAgent featureSnapAgent = new EngineFeatureSnap(); //featureSnapAgent.FeatureClass = (editor.TargetLayer as IFeatureLayer).FeatureClass; //featureSnapAgent.HitType = esriGeometryHitPartType.esriGeometryPartVertex; //snapEnv.AddSnapAgent(featureSnapAgent); editor.OnStopEditing += Editor_OnStopEditing; base.OnCreate(hook); } catch (Exception ex) { LogAPI.Debug("共点移动工具初始化OnCreate异常:" + ex.Message); } } private void Editor_OnStopEditing(bool saveChanges) { try { m_isDrawing = false; (m_hookHelper.Hook as IMapControl2).CurrentTool = null; } catch (Exception ex) { LogAPI.Debug(ex); } } public override bool Deactivate() { m_isDrawing = false; return base.Deactivate(); } public override void OnKeyDown(int keyCode, int shift) { try { if (keyCode == (int)System.Windows.Forms.Keys.Back)//修改为退格删除 { this.m_NewPolygonFeedback.Stop(); firstPoint = null; } base.OnKeyDown(keyCode, shift); } catch (Exception ex) { LogAPI.Debug("共点移动工具OnKeyDown异常:" + ex.Message); } } public override void OnMouseMove(int Button, int Shift, int X, int Y) { try { if (!m_isDrawing) { return; } base.OnMouseMove(Button, Shift, X, Y); this.m_pPointCatched = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); if (this.m_pSnappingEnv != null && this.m_pSnappingEnv.Enabled) { ISnappingResult snappingResult = this.m_pPntSnapper.Snap(this.m_pPointCatched); this.m_pSnappingFeedback.Update(snappingResult, 0); if (snappingResult != null) { this.snapPoint = snappingResult.Location; } else { this.snapPoint = null; } } IPoint point = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); //if (snapEnv != null) //{ // this.SnapPoint(m_hookHelper.ActiveView, point, snapEnv); // if (snapEnv.SnapPoint(point)) // { // snapPoint = point; // } // else // { // snapPoint = null; // } //} if (firstPoint != null) { //已经捕获到一个点之后,后续不管是否捕获到点都可以使用 if (snapPoint == null) snapPoint = point; this.m_NewPolygonFeedback.MoveTo(snapPoint); } } catch (Exception ex) { LogAPI.Debug("共点移动工具OnMouseMove异常:" + ex.Message); } } public override void OnMouseUp(int Button, int Shift, int X, int Y) { try { if (Button != 1) { return; } //开始移动 editor.StartOperation(); try { if (!m_isDrawing) return; //只有第一次没有捕获到点的时候才为空 if (snapPoint == null) return; //第一个点 if (firstPoint == null) { firstPoint = snapPoint; firstSnapPointList = Snapping(snapPoint); this.m_NewPolygonFeedback.Start(firstPoint); return; } if (firstSnapPointList != null) { //第二个点 IPoint moveToPoint = snapPoint; foreach (var item in firstSnapPointList) { IGeometryCollection collection = item.Geometry as IGeometryCollection; IGeometry geo = collection.get_Geometry(item.PartIndex); IPointCollection points = geo as IPointCollection; IPoint pt = points.get_Point(item.VertexIndex); points.UpdatePoint(item.VertexIndex, moveToPoint); item.Feature.Shape = item.Geometry; item.Feature.Store(); } } editor.StopOperation("共点移动"); this.m_pHookHelper.ActiveView.Refresh(); //this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, this.m_featureLayer, this.m_hookHelper.ActiveView.Extent); //重置状态 firstPoint = null; firstSnapPointList = null; this.m_NewPolygonFeedback.Stop(); } catch (Exception ex) { LogAPI.Debug(ex); editor.AbortOperation(); //重置状态 firstPoint = null; firstSnapPointList = null; this.m_NewPolygonFeedback.Stop(); MessageHelper.ShowError(ex.Message); } } catch (Exception ex) { LogAPI.Debug("共点移动工具OnMouseMove异常:" + ex.Message); } } public override bool Enabled { get { ESRI.ArcGIS.Carto.ILayer layer = (editor as EngineEditorClass).TargetLayer; if (editor.EditState == esriEngineEditState.esriEngineStateEditing && layer != null && (layer as ESRI.ArcGIS.Carto.IFeatureLayer).FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { return true; } else { return false; } } } //public override enumProductType AttachProductType { get { return enumProductType.KDB; } } private IFeatureLayer m_featureLayer = null; public override void OnClick() { try { if (editor.EditState != esriEngineEditState.esriEngineStateEditing) return; //if (this.m_hookHelper == null) //this.OnCreate(m_hookHelper.Hook);//return;//edit by cmj 2018/4/25 #region 设置目标图层 //IFeatureLayer selectFeatureLayer = Env.Instance.KMap.GetFeatureLayerByName(this.m_hookHelper.ActiveView.FocusMap, Env.Instance.DefaultValue.DLTB_TableName) as IFeatureLayer; //if (selectFeatureLayer != null) //{ // IEngineEditLayers pEditLayer = editor as IEngineEditLayers; // pEditLayer.SetTargetLayer(selectFeatureLayer, 0); //} #endregion m_featureLayer = editor.TargetLayer as IFeatureLayer; if (m_featureLayer == null) return; this.m_NewPolygonFeedback = new NewLineFeedbackClass() { Display = this.m_hookHelper.ActiveView.ScreenDisplay }; this.m_NewPolygonFeedback.Stop(); m_isDrawing = true; base.OnClick(); } catch (Exception ex) { LogAPI.Debug("MoveSharedPointTool共点移动工具的OnClick异常:" + ex.Message); } } /// /// 捕捉 /// /// /// /// /// /// private List Snapping(IPoint point) { double tol = 0.001; ITopologicalOperator pTopo = point as ITopologicalOperator; IGeometry pGeometry = pTopo.Buffer(tol).Envelope as IGeometry; IIdentify indentify = this.m_featureLayer as IIdentify; if (indentify == null) return null; IArray pIDs = indentify.Identify(pGeometry); if (pIDs == null || pIDs.Count == 0) return null; List pointList = new List(); for (int index = 0; index < pIDs.Count; index++) { IFeatureIdentifyObj pFeatIdObj = pIDs.get_Element(index) as IFeatureIdentifyObj; IRowIdentifyObject pRowObj = pFeatIdObj as IRowIdentifyObject; IFeature iF = pRowObj.Row as IFeature; IPoint iHitPt = new ESRI.ArcGIS.Geometry.Point(); IHitTest iHitTest = iF.Shape as IHitTest; double hitDist = 0; int partIndex = 0; int vertexIndex = 0; bool bRightSide = false; if (iHitTest.HitTest(point, tol, esriGeometryHitPartType.esriGeometryPartVertex, iHitPt, ref hitDist, ref partIndex, ref vertexIndex, ref bRightSide)) { point = iHitPt; pointList.Add(new MovePointStruct() { Feature = iF, Geometry = iF.Shape, PartIndex = partIndex, VertexIndex = vertexIndex }); } } this.m_hookHelper.ActiveView.Refresh(); return pointList; } private class MovePointStruct { public IFeature Feature { get; set; } public IGeometry Geometry { get; set; } public int PartIndex { get; set; } public int VertexIndex { get; set; } } } }