using System; using System.Collections.Generic; using ESRI.ArcGIS.ADF; 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.Maps; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using Kingo.Plugin.EngineEditor.helper; namespace Kingo.Plugin.EngineEditor.Commands.Tools { //[PathAttribute("Kingo.Plugin.EngineEditor.Commands.Tools.CutPolygonEditTool")] public class CutPolygonEditTool : BaseToolCmd { public override void OnCreate(object hook) { base.OnCreate(hook); } private void ActiveViewEvents_Event_ItemDeleted(object Item) { if (EditorSettingClass.Instance.IsTraceing) { this.EndTraceFeedback(); } } private void ActiveViewEvents_Event_ItemAdded(object Item) { if (EditorSettingClass.Instance.IsTraceing) { this.EndTraceFeedback(); } } private void ActiveViewEvents_Event_ContentsChanged() { } private void M_pAxMapControl_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e) { if (EditorSettingClass.Instance.IsTraceing && this.m_pTraceFeedback != null) { try { this.FillFeatureCache(this.m_pPointCatched); } catch (Exception) { } this.GetBeTracedPolyline(); //this.m_pTraceFeedback.SetSplitPolyline(m_pListBeTracePolyline); this.m_pTraceFeedback.ListBeTracePolyline = this.m_pListBeTracePolyline; } } public override bool Deactivate() { base.Deactivate(); MapsManager.Instance.MapService.getAxMapControl().OnExtentUpdated -= M_pAxMapControl_OnExtentUpdated; IActiveViewEvents_Event activeViewEvents_Event = base.m_pMapControl.ActiveView as IActiveViewEvents_Event; activeViewEvents_Event.ContentsChanged -= ActiveViewEvents_Event_ContentsChanged; activeViewEvents_Event.ItemAdded -= ActiveViewEvents_Event_ItemAdded; activeViewEvents_Event.ItemDeleted -= ActiveViewEvents_Event_ItemDeleted; this.m_deactivate = true; return this.m_deactivate; } public override void OnClick() { base.OnClick(); base.m_pDisplayFeedbackMethod = DisplayFeedbackMethod.Line; MapsManager.Instance.MapService.getAxMapControl().OnExtentUpdated += M_pAxMapControl_OnExtentUpdated; IActiveViewEvents_Event activeViewEvents_Event = base.m_pMapControl.ActiveView as IActiveViewEvents_Event; activeViewEvents_Event.ContentsChanged += ActiveViewEvents_Event_ContentsChanged; activeViewEvents_Event.ItemAdded += ActiveViewEvents_Event_ItemAdded; activeViewEvents_Event.ItemDeleted += ActiveViewEvents_Event_ItemDeleted; } public override bool Enabled { get { //return editor.EditState == esriEngineEditState.esriEngineStateEditing; //白明雅 2018-11-30 bug13339 if (m_pEditor.EditState != esriEngineEditState.esriEngineStateEditing) return false; ILayer layer = (m_pEditor as EngineEditorClass).TargetLayer; if (m_pEditor.EditState == esriEngineEditState.esriEngineStateEditing && layer != null && (layer as IFeatureLayer) != null && (layer as IFeatureLayer).FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { return true; } return false; //--------------------- } } public override void OnMouseDown(int Button, int Shift, int X, int Y) { if (Button == 1) { DisplayFeedbackMethod pDisplayFeedbackMethod = this.m_pDisplayFeedbackMethod; if (this.m_pGeometry == null) { switch (pDisplayFeedbackMethod) { case DisplayFeedbackMethod.Line: this.NewPolyline(); break; case DisplayFeedbackMethod.Polygon: this.NewPolygon(); break; default: break; } } if (pDisplayFeedbackMethod != DisplayFeedbackMethod.Arc) { if (EditorSettingClass.Instance.IsTraceing && (this.m_pDisplayFeedbackMethod == DisplayFeedbackMethod.Polygon || this.m_pDisplayFeedbackMethod == DisplayFeedbackMethod.Line)) { IPoint p = this.m_pMapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); ISnappingResult snappingResult = null; if (this.m_pSnappingEnv != null && this.m_pSnappingEnv.Enabled) { snappingResult = this.m_pPntSnapper.Snap(p); } if (snappingResult == null && m_pTraceFeedback == null) { m_pTraceFeedback = null; switch (pDisplayFeedbackMethod) { case DisplayFeedbackMethod.Polygon: this.AddToFeedback(this.m_pPointCatched); this.AddPointToPolygon(this.m_pPointCatched); break; case DisplayFeedbackMethod.Line: //this.StartFeedback(this.m_pPointCatched); this.AddToFeedback(this.m_pPointCatched); this.AddPointToPolyline(this.m_pPointCatched); break; } } else if (snappingResult == null && m_pTraceFeedback != null) { IPolyline polyline = this.AddToTraceFeedBack(this.m_pPointCatched); if (polyline != null && !polyline.IsEmpty) { if (pDisplayFeedbackMethod == DisplayFeedbackMethod.Polygon) { this.AddPolylineToPolygon(polyline); this.AddToFeedback(polyline); } else if (pDisplayFeedbackMethod == DisplayFeedbackMethod.Line) { this.AddPolylineToPolyline(polyline); this.AddToFeedback(polyline); //this.StartFeedback(polyline.ToPoint); } } } else { base.ListSelectableLayer = MapsManager.Instance.MapService.GetAllVisibleLayerInMap(); //GetAllFeatureLayer(); //if (this.m_pListFeatureCache == null) //{ try { this.FillFeatureCache(this.m_pPointCatched); } catch { } this.GetBeTracedPolyline(); //} IPolyline polyline = this.AddToTraceFeedBack(this.m_pPointCatched); if (polyline != null && !polyline.IsEmpty) { if (pDisplayFeedbackMethod == DisplayFeedbackMethod.Polygon) { this.AddPolylineToPolygon(polyline); this.AddToFeedback(polyline); } else if (pDisplayFeedbackMethod == DisplayFeedbackMethod.Line) { this.AddPolylineToPolyline(polyline); this.AddToFeedback(polyline); //this.StartFeedback(polyline.ToPoint); } } } } else { m_pTraceFeedback = null; switch (pDisplayFeedbackMethod) { case DisplayFeedbackMethod.Polygon: this.AddToFeedback(this.m_pPointCatched); this.AddPointToPolygon(this.m_pPointCatched); break; case DisplayFeedbackMethod.Line: //this.StartFeedback(this.m_pPointCatched); this.AddToFeedback(this.m_pPointCatched); this.AddPointToPolyline(this.m_pPointCatched); break; } } } else if (this.m_nArcPointCount == 0) { IPointCollection pointCollection = this.m_pGeometry as IPointCollection; int pointCount = pointCollection.PointCount; if (pointCount >= 1) { IPoint pPoint = pointCollection.get_Point(pointCount - 1); this.AddToArcFeedback(pPoint); this.AddToArcFeedback(this.m_pPointCatched); } else { this.AddToArcFeedback(this.m_pPointCatched); } } else if (this.m_nArcPointCount != 1) { if (this.m_nArcPointCount == 2) { ICircularArc circularArc = this.AddToArcFeedback(this.m_pPointCatched); if (circularArc != null) { this.AddSegmentToPolygon(circularArc as ISegment); this.AddToFeedback((circularArc as ISegment).ToPoint); } } } else { this.AddToArcFeedback(this.m_pPointCatched); } if (this.m_pPointCatched != null) { this.SketchGeometry.Update(); } this.RefreshForeground(null); } } public override void OnMouseMove(int Button, int Shift, int X, int Y) { if (Button == 4) { return; } this.m_pPointCatched = this.m_pMapControl.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.m_pPointCatched = snappingResult.Location; } } string strDescription = ""; IPoint point = null; if (this.m_pSnappingEnv != null && this.m_pSnappingEnv.Enabled && SnappingSketchFeedback.Enabled) { this.RefreshForeground(null); point = base.SnappingSketch(this.m_pPointCatched, ref strDescription); if (point != null) { this.m_pPointCatched = point; } } try { DisplayFeedbackMethod pDisplayFeedbackMethod = this.m_pDisplayFeedbackMethod; if (pDisplayFeedbackMethod == DisplayFeedbackMethod.Arc) { if (this.m_pNewArcFeedback == null && this.m_pDisplayFeedback != null) { this.m_pDisplayFeedback.MoveTo(this.m_pPointCatched); } if (this.m_pNewArcFeedback != null) { if (this.m_nArcPointCount == 2) { (this.m_pNewArcFeedback as IDisplayFeedback).MoveTo(this.m_pPointCatched); } } } else { if (EditorSettingClass.Instance.IsTraceing && (this.m_pDisplayFeedbackMethod == DisplayFeedbackMethod.Polygon || this.m_pDisplayFeedbackMethod == DisplayFeedbackMethod.Line)) { if (this.m_pTraceFeedback == null && this.m_pDisplayFeedback != null) { this.m_pDisplayFeedback.MoveTo(this.m_pPointCatched); } if (this.m_pTraceFeedback != null) { this.m_pTraceFeedback.MoveTo(this.m_pPointCatched); this.RefreshForeground(null); } } else { switch (pDisplayFeedbackMethod) { case DisplayFeedbackMethod.Polygon: if (this.m_pDisplayFeedback != null) { this.m_pDisplayFeedback.MoveTo(this.m_pPointCatched); } break; case DisplayFeedbackMethod.Line: if (this.m_pDisplayFeedback != null) { this.m_pDisplayFeedback.MoveTo(this.m_pPointCatched); } break; } } } } catch (Exception ex) { //FormReportException.Report(ex); } if (point != null) { this.m_pSnappingSketchFeedback.Update(point, strDescription); } } public override void Refresh(int hDC) { if (this.m_pSnappingFeedback != null) { this.m_pSnappingFeedback.Refresh(hDC); } if (this.m_pDisplayFeedback != null) { this.m_pDisplayFeedback.Refresh(hDC); } if (EditorSettingClass.Instance.IsTraceing && this.m_pTraceFeedback != null) { this.m_pTraceFeedback.Refresh(hDC); } } public override void ReStartFeedback() { IPointCollection pointCollection = null; int num = 0; if (this.m_pGeometry != null || !this.m_pGeometry.IsEmpty) { IGeometryCollection geometryCollection = this.m_pGeometry as IGeometryCollection; int geometryCount = geometryCollection.GeometryCount; if (geometryCount > 0) { this.m_pRing = (geometryCollection.get_Geometry(geometryCollection.GeometryCount - 1) as IRing); pointCollection = (this.m_pRing as IPointCollection); num = pointCollection.PointCount; } else { this.m_pRing = null; } } this.EndFeedback(); this.EndArcFeedback(); this.EndTraceFeedback(); if (pointCollection != null) { for (int i = 0; i < num; i++) { this.AddToFeedback(pointCollection.get_Point(i)); } } this.RefreshForeground(null); } public override void OnMouseUp(int Button, int Shift, int X, int Y) { base.OnMouseUp(Button, Shift, X, Y); } public override void OnKeyDown(int keyCode, int Shift) { base.OnKeyDown(keyCode, Shift); } public override void OnKeyUp(int keyCode, int Shift) { base.OnKeyUp(keyCode, Shift); } public override void OnDblClick() { try { FinishSketch(); //通知刷新图斑查看界面 KGIS.Framework.Platform.Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { Content = "Cut", MsgType = "RefreshTBBGDetail" }); } catch (Exception ex) { LogAPI.Debug(ex.Message); MessageHelper.ShowError(ex.Message); } } public void FinishSketch() { this.EndFeedback(); this.EndArcFeedback(); this.EndTraceFeedback(); if (m_pGeometry is IPolygon) { if (!(m_pGeometry as IPolygon).IsClosed) { (m_pGeometry as IPolygon).Close(); } (this.m_pGeometry as IPolygon).SimplifyPreserveFromTo(); if (!this.m_pGeometry.IsEmpty) { CutGeometry(); } } else if (m_pGeometry is IPolyline) { if (!this.m_pGeometry.IsEmpty) { IPolyline polyline = this.m_pGeometry as IPolyline; polyline.SimplifyNetwork(); CutGeometry(); } } if (EditorSettingClass.Instance.OnDrawEnd != null) { EditorSettingClass.Instance.OnDrawEnd(this, this.m_pGeometry); } ComReleaser.ReleaseCOMObject(this.m_pGeometry); this.m_pGeometry = null; ComReleaser.ReleaseCOMObject(this.m_pRing); this.m_pRing = null; ComReleaser.ReleaseCOMObject(this.m_pPath); this.m_pPath = null; if (this.SketchGeometry != null) { this.SketchGeometry.Deactivate(); this.SketchGeometry = null; } this.RefreshForeground(null); } public void CutGeometry() { if (m_pEditor.EditState != esriEngineEditState.esriEngineStateEditing) return; if ((m_pEditor as EngineEditorClass).TargetLayer == null) return; if (m_pGeometry.IsEmpty) return; IFeatureLayer featureLayer = (m_pEditor as EngineEditorClass).TargetLayer as IFeatureLayer; IDataset dataset = featureLayer.FeatureClass as IDataset; string areapath = SysAppPath.GetAreaUpdateConfigPath(); string mjField = string.Empty; if (!string.IsNullOrWhiteSpace(areapath) && System.IO.File.Exists(areapath)) mjField = AreaConfigHelper.GetAreaPropertyByLayerName(dataset.Name); bool hasCutPolygons = false; System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; IFeatureCursor featureCursor = null; ICursor cursor = null; IFeature origFeature = null; IPointCollection collection = null; IIdentify identify = null; try { ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = m_pGeometry; spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureSelection featureSelection = featureLayer as IFeatureSelection; if (featureSelection.SelectionSet.Count > 0) { ISelectionSet selectionSet = featureSelection.SelectionSet; selectionSet.Search(spatialFilter, false, out cursor); featureCursor = cursor as IFeatureCursor; } else { featureCursor = featureLayer.FeatureClass.Search(spatialFilter, false); } origFeature = featureCursor.NextFeature(); if (origFeature == null || origFeature.ShapeCopy.IsEmpty) { return; } m_pEditor.StartOperation(); while (origFeature != null) { try { IFeatureEdit featureEdit = origFeature as IFeatureEdit; ISet newFeaturesSet = featureEdit.Split(m_pGeometry); if (newFeaturesSet != null) { newFeaturesSet.Reset(); //origFeature.Store();//修改人:李忠盼 修改时间:20180906 增加代码(解决裁剪完成后,使用选择要素报错问题) hasCutPolygons = true; if (!string.IsNullOrWhiteSpace(mjField)) { int mjIndex = origFeature.Fields.FindField(mjField); int bsmIndex = origFeature.Fields.FindField("BSM"); if (mjIndex > -1) { List fcList = new List(); fcList.Add(featureLayer.FeatureClass); IFeature pSplitFeature = null; pSplitFeature = newFeaturesSet.Next() as IFeature; string bsm = string.Empty; int count = 0; while (pSplitFeature != null) { count++; IArea area = pSplitFeature.ShapeCopy as IArea; double mj = area.Area; if (dataset.Name == "LSYD") { mj = mj * 0.0001; } mj = Math.Round(mj, 2); pSplitFeature.Value[mjIndex] = mj; pSplitFeature.Store(); pSplitFeature = newFeaturesSet.Next() as IFeature; } } } #region 白明雅 2019-05-28 加共点 //IFeature feature = newFeaturesSet.Next() as IFeature; //collection = forGdb.GeoToPoints(feature.Shape, false); //identify = featureLayer as IIdentify; //for (int i = 0; i < collection.PointCount; i++) //{ // forGdb.AddConcurrentPoint(collection.Point[i], identify); //} #endregion } } catch (Exception ex) { LogAPI.Debug("图斑裁剪失败:" + ex.Message); LogAPI.Debug("图斑裁剪失败:" + ex.StackTrace); if (ex.Message.Contains("A polygon cut operation could not classify all parts of the polygon as left or right of the cutting line")) MessageHelper.ShowError("图斑分割操作异常,不能只从图形一侧进行图斑分割!"); else MessageHelper.ShowError("图斑裁剪失败:" + ex.Message); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(origFeature); origFeature = featureCursor.NextFeature(); } } if (hasCutPolygons) { if (m_pEditor.Map.SelectionCount > 0) { m_pEditor.Map.ClearSelection(); } m_pEditor.StopOperation("Cut Polygons Without Selection"); } else { m_pEditor.AbortOperation(); } IActiveView activeView = m_pEditor.Map as IActiveView; activeView.PartialRefresh(esriViewDrawPhase.esriViewBackground | esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent); } catch (Exception e) { LogAPI.Debug(e); m_pEditor.AbortOperation(); } finally { using (ESRI.ArcGIS.ADF.ComReleaser com = new ESRI.ArcGIS.ADF.ComReleaser()) { if (origFeature != null) { com.ManageLifetime(origFeature); } if (featureCursor != null) { com.ManageLifetime(featureCursor); } if (cursor != null) { com.ManageLifetime(cursor); } if (identify == null) { com.ManageLifetime(featureCursor); } if (collection == null) { com.ManageLifetime(collection); } } m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } } } }