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 ESRI.ArcGIS.SystemUI; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using KGIS.PlatformPlugin.Commands.Tools.TraceTool.Enum; using System; using System.Collections.Generic; using System.Reflection; using ICommand = ESRI.ArcGIS.SystemUI.ICommand; namespace KGIS.PlatformPlugin.Commands.Tools.TraceTool { public class NewPolygonTraceTool : ICommand, ITool { protected IGeometry m_pGeometry; private IRing m_pRing; private List m_pListFeatureCache; private List m_pListBeTracePolyline; List lstTraceLayer { get; set; } private IDisplayFeedback m_pDisplayFeedback { get; set; } private ITraceFeedback m_pTraceFeedback; private IFeatureLayer featureLayer { get; set; } private IEngineEditor m_engineEditor; private bool m_checked; private IHookHelper m_hookHelper; private IGeometryCollection geometryCollection { get; set; } protected ISnappingFeedback m_pSnappingFeedback; protected ISnappingEnvironment m_pSnappingEnv; protected IPointSnapper m_pPntSnapper; private IPoint m_pPointCatched = null; public void OnCreate(object hook) { try { if (m_hookHelper == null) { m_hookHelper = new HookHelperClass(); m_hookHelper.Hook = hook; } //System.IO.Stream delVerStream = GetType().Assembly.GetManifestResourceStream("KGIS.PlatformPlugin.Cur.InsertVertexCursor.cur"); //if (delVerStream != null) //{ // m_Cursor = new System.Windows.Forms.Cursor(delVerStream); //} if (m_engineEditor == null) { m_engineEditor = new EngineEditorClass(); } 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); } } } IActiveViewEvents_Event activeViewEvents_Event = this.m_hookHelper.ActiveView as IActiveViewEvents_Event; //activeViewEvents_Event.ContentsChanged += new IActiveViewEvents_ContentsChangedEventHandler(this, (UIntPtr)ldftn(pActiveViewEvents_Event_ContentsChanged)); activeViewEvents_Event.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(pActiveViewEvents_Event_ItemAdded); activeViewEvents_Event.ItemDeleted += new IActiveViewEvents_ItemDeletedEventHandler(pActiveViewEvents_Event_ItemDeleted); } catch (Exception ex) { LogAPI.Debug("NewPolygonTool OnCreate异常:" + ex.Message); } } private void pActiveViewEvents_Event_ItemDeleted(object Item) { //if (this.m_pDisplayFeedbackMethod == DisplayFeedbackMethod.Trace) //{ this.EndTraceFeedback(); //} } private void pActiveViewEvents_Event_ItemAdded(object Item) { //if (this.m_pDisplayFeedbackMethod == DisplayFeedbackMethod.Trace) //{ this.EndTraceFeedback(); //} } private void pActiveViewEvents_Event_ContentsChanged() { } private void axMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e) { //this.m_pDisplayFeedbackMethod == DisplayFeedbackMethod.Trace && if (this.m_pTraceFeedback != null) { this.FillFeatureCache(this.m_pPointCatched); this.GetBeTracedPolyline(); this.m_pTraceFeedback.ListBeTracePolyline = this.m_pListBeTracePolyline; } } public void OnClick() { try { //featureLayer = (m_engineEditor as IEngineEditLayers).TargetLayer; //if (featureLayer == null || featureLayer.FeatureClass == null) //{ // m_checked = false; // return; //} //IsUseing = true; m_checked = true; } catch (Exception ex) { LogAPI.Debug(ex.Message); MessageHelper.ShowError(ex.Message); } } public bool Enabled { get { return m_engineEditor.EditState == esriEngineEditState.esriEngineStateEditing; } } public bool Checked { get { return m_checked; } } public string Name { get { return "追踪绘面"; } } public string Caption { get { return ""; } } public string Tooltip { get { return "追踪绘面"; } } public string Message { get { return ""; } } public string HelpFile { get { return ""; } } public int HelpContextID { get { return 0; } } public int Bitmap { get { return 0; } } public string Category { get { return ""; } } public void OnMouseDown(int button, int shift, int x, int y) { try { if (button != 1) { return; } if (this.m_pGeometry == null) { this.NewPolygon(); } if (this.m_pListFeatureCache == null) { this.FillFeatureCache(this.m_pPointCatched); this.GetBeTracedPolyline(); } IPolyline polyline = this.AddToTraceFeedBack(this.m_pPointCatched); if (polyline != null) { this.AddPolylineToPolygon(polyline); this.AddToFeedback(polyline); } //AddToFeedback(m_pPointCatched); //AddPointToPolygon(m_pPointCatched); this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, this.m_hookHelper.ActiveView.Extent); } catch (Exception ex) { LogAPI.Debug(ex.Message); MessageHelper.ShowError(ex.Message); } } private void NewPolygon() { this.m_pGeometry = new PolygonClass(); this.m_pGeometry.SpatialReference = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.SpatialReference; //this.m_pEditor.SketchGeometry = new SketchGeometry(this.m_pGeometry, null, true); IEngineEditSketch editSketch = m_engineEditor as IEngineEditSketch; editSketch.Geometry = m_pGeometry; //editSketch.FinishSketch(); } private void EndPolygon() { if (this.m_pRing != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(this.m_pRing); this.m_pRing = null; } } private void AddToFeedback(IPoint pPoint) { if (this.m_pDisplayFeedback == null) { m_pDisplayFeedback = new NewPolygonFeedbackClass(); m_pDisplayFeedback.Display = this.m_hookHelper.ActiveView.ScreenDisplay; (this.m_pDisplayFeedback as INewPolygonFeedback).Start(pPoint); return; } if (this.m_pDisplayFeedback is INewPolygonFeedback) { (this.m_pDisplayFeedback as INewPolygonFeedback).AddPoint(pPoint); } } public void FinishSketch() { this.EndFeedback(); //this.EndArcFeedback(); this.EndTraceFeedback(); this.EndPolygon(); if (this.m_pGeometry != null && (this.m_pGeometry as IPointCollection).PointCount > 2) { (this.m_pGeometry as IPolygon).SimplifyPreserveFromTo(); //if (this.OnDrawEnd != null) //{ // this.OnDrawEnd(this, this.m_pGeometry); //} IEngineEditSketch editSketch = m_engineEditor as IEngineEditSketch; editSketch.Geometry = m_pGeometry; editSketch.FinishSketch(); } System.Runtime.InteropServices.Marshal.ReleaseComObject(this.m_pGeometry); this.m_pGeometry = null; //if (this.m_pEditor.SketchGeometry != null) //{ // this.m_pEditor.SketchGeometry.Deactivate(); // this.m_pEditor.SketchGeometry = null; //} //this.m_pEditor.RefreshForeground(null); this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, this.m_hookHelper.ActiveView.Extent); } private void AddPointToPolygon(IPoint pPoint) { if (this.m_pRing != null) { IPointCollection arg_24_0 = this.m_pRing as IPointCollection; object value = Missing.Value; object value2 = Missing.Value; arg_24_0.AddPoint(pPoint, ref value, ref value2); return; } this.NewPolygonPart(); IPointCollection arg_4C_0 = this.m_pRing as IPointCollection; object value3 = Missing.Value; object value4 = Missing.Value; arg_4C_0.AddPoint(pPoint, ref value3, ref value4); } private void NewPolygonPart() { if (this.m_pRing == null) { this.m_pRing = new RingClass(); IGeometryCollection geometryCollection = this.m_pGeometry as IGeometryCollection; IGeometryCollection arg_36_0 = geometryCollection; IGeometry arg_36_1 = this.m_pRing; object value = Missing.Value; object value2 = Missing.Value; arg_36_0.AddGeometry(arg_36_1, ref value, ref value2); } } private void EndFeedback() { if (this.m_pDisplayFeedback != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(this.m_pDisplayFeedback); this.m_pDisplayFeedback = null; } } //private void AddPointToPolygon(IPoint pPoint) //{ // if (this.m_pRing != null) // { // IPointCollection arg_24_0 = this.m_pRing as IPointCollection; // object value = Missing.Value; // object value2 = Missing.Value; // arg_24_0.AddPoint(pPoint, ref value, ref value2); // return; // } // this.NewPolygonPart(); // IPointCollection arg_4C_0 = this.m_pRing as IPointCollection; // object value3 = Missing.Value; // object value4 = Missing.Value; // arg_4C_0.AddPoint(pPoint, ref value3, ref value4); //} public void OnMouseMove(int button, int shift, int x, int y) { try { 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.m_pPointCatched = snappingResult.Location; } } //if (m_pDisplayFeedback != null) //{ // m_pDisplayFeedback.MoveTo(m_pPointCatched); //} 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); RefreshForeground(this.m_hookHelper.ActiveView.Extent); } } catch (Exception ex) { LogAPI.Debug(ex.Message); MessageHelper.ShowError(ex.Message); } } public void RefreshForeground(IEnvelope pEnvelope = null) { this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForeground, null, pEnvelope); KGIS.Framework.Platform.Platform.Instance.GetMapsManager().GetMapService().getAxMapControl().Update(); } public void OnMouseUp(int button, int shift, int x, int y) { } public void OnDblClick() { try { FinishSketch(); } catch (Exception ex) { LogAPI.Debug(ex.Message); MessageHelper.ShowError(ex.Message); } } public void OnKeyDown(int keyCode, int shift) { if (keyCode == (int)ConsoleKey.F9 || keyCode == (int)ConsoleKey.Escape) { } } public void OnKeyUp(int keyCode, int shift) { } public bool OnContextMenu(int x, int y) { return true; } public void Refresh(int hdc) { if (this.m_pDisplayFeedback != null) { this.m_pDisplayFeedback.Refresh(hdc); } } public bool Deactivate() { try { m_checked = false; if (m_pDisplayFeedback != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(m_pDisplayFeedback); m_pDisplayFeedback = null; } } catch (Exception ex) { LogAPI.Debug(ex.Message); } return true; } public int Cursor { get { return System.Windows.Forms.Cursors.Cross.Handle.ToInt32(); } } private void FillFeatureCache(IPoint pPoint) { try { List listAllLayer = KGIS.Framework.Platform.Platform.Instance.GetMapsManager().GetMapService().GetAllLayerInMap(); lstTraceLayer = new List(); foreach (IFeatureLayer item in listAllLayer) { if (item == null || !item.Visible || !item.Selectable || item.FeatureClass == null) { continue; } if (item.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon || item.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline) { lstTraceLayer.Add(item); } } } catch (Exception ex) { LogAPI.Debug(ex); } IEnvelope arg_41_0 = this.m_hookHelper.ActiveView.Extent; int width = KGIS.Framework.Platform.Platform.Instance.GetMapsManager().GetMapService().getAxMapControl().Size.Width; double size = ConvertPixelsToMapUnits((double)width); if (this.m_pListFeatureCache != null) { this.ReleaseFeatureCache(); } this.m_pListFeatureCache = new List(); foreach (IFeatureLayer current in lstTraceLayer) { IFeatureCache2 featureCache = new FeatureCacheClass(); featureCache.Initialize(pPoint, size); featureCache.AddFeatures(current.FeatureClass, null); this.m_pListFeatureCache.Add(featureCache); } } private void ReleaseFeatureCache() { if (this.m_pListFeatureCache != null) { using (List.Enumerator enumerator = this.m_pListFeatureCache.GetEnumerator()) { if (enumerator.MoveNext()) { IFeatureCache2 current = enumerator.Current; ComReleaser.ReleaseCOMObject(current); } } this.m_pListFeatureCache.Clear(); this.m_pListFeatureCache = null; } } public double ConvertPixelsToMapUnits(double dPixelUnits = 0.0) { if (dPixelUnits == 0.0) { dPixelUnits = 10.0; } int num = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().right - this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().left; double width = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width; double num2 = width / (double)num; return dPixelUnits * num2; } private void GetBeTracedPolyline() { if (this.m_pListBeTracePolyline != null) { this.ReleaseBeTracedPolyline(); } this.m_pListBeTracePolyline = new List(); foreach (IFeatureCache2 current in this.m_pListFeatureCache) { int count = current.Count; for (int i = 0; i < count; i++) { IFeature feature = current.get_Feature(i); IGeometry shapeCopy = feature.ShapeCopy; ITopologicalOperator topologicalOperator = shapeCopy as ITopologicalOperator; if (shapeCopy.GeometryType == esriGeometryType.esriGeometryPolygon) { IPolyline polyline = topologicalOperator.Boundary as IPolyline; IGeometryCollection geometryCollection = polyline as IGeometryCollection; int geometryCount = geometryCollection.GeometryCount; for (int j = 0; j < geometryCount; j++) { IPath path = geometryCollection.get_Geometry(j) as IPath; IPolyline item = ToPolyline(path); this.m_pListBeTracePolyline.Add(item); ComReleaser.ReleaseCOMObject(path); } ComReleaser.ReleaseCOMObject(polyline); } else { if (shapeCopy.GeometryType == esriGeometryType.esriGeometryPolyline) { IPolyline polyline2 = shapeCopy as IPolyline; IGeometryCollection geometryCollection2 = polyline2 as IGeometryCollection; int geometryCount2 = geometryCollection2.GeometryCount; for (int k = 0; k < geometryCount2; k++) { IPath path2 = geometryCollection2.get_Geometry(k) as IPath; IPolyline item2 = ToPolyline(path2); this.m_pListBeTracePolyline.Add(item2); ComReleaser.ReleaseCOMObject(path2); } } } ComReleaser.ReleaseCOMObject(shapeCopy); } } } private IPolyline AddToTraceFeedBack(IPoint pPoint) { if (this.m_pTraceFeedback == null) { this.m_pTraceFeedback = new TraceFeedbackExClass(); m_pTraceFeedback.Display = this.m_hookHelper.ActiveView.ScreenDisplay; this.m_pTraceFeedback.ListBeTracePolyline = this.m_pListBeTracePolyline; if (this.m_pGeometry.IsEmpty) { this.m_pTraceFeedback.Start(pPoint, null, null); } else { IPoint fromPoint = this.m_pRing.FromPoint; IPoint toPoint = this.m_pRing.ToPoint; this.m_pTraceFeedback.Start(pPoint, fromPoint, toPoint); } return null; } IPolyline result = this.m_pTraceFeedback.Stop(pPoint); this.m_pTraceFeedback = null; return result; } private void ReleaseBeTracedPolyline() { if (this.m_pListBeTracePolyline != null) { foreach (IGeometry current in this.m_pListBeTracePolyline) { ComReleaser.ReleaseCOMObject(current); } this.m_pListBeTracePolyline.Clear(); this.m_pListBeTracePolyline = null; } } public static IPolyline ToPolyline(IPath pPath) { IPolyline polyline = new PolylineClass(); polyline.SpatialReference = pPath.SpatialReference; ISegmentCollection segments = pPath as ISegmentCollection; ISegmentCollection segmentCollection = polyline as ISegmentCollection; segmentCollection.AddSegmentCollection(segments); polyline.SimplifyNetwork(); return polyline; } private void AddPolylineToPolygon(IPolyline pPolyline) { if (pPolyline != null && !pPolyline.IsEmpty) { if (this.m_pRing == null) { this.NewPolygonPart(); } ISegmentCollection segmentCollection = this.m_pRing as ISegmentCollection; if (!this.m_pRing.IsEmpty && this.m_pRing.ToPoint != null) { ILine line = new LineClass(); line.PutCoords(this.m_pRing.ToPoint, pPolyline.FromPoint); ISegmentCollection arg_7B_0 = segmentCollection; ISegment arg_7B_1 = line as ISegment; object value = Missing.Value; object value2 = Missing.Value; arg_7B_0.AddSegment(arg_7B_1, ref value, ref value2); } ISegmentCollection segmentCollection2 = pPolyline as ISegmentCollection; int segmentCount = segmentCollection2.SegmentCount; for (int i = 0; i < segmentCount; i++) { ISegmentCollection arg_AE_0 = segmentCollection; ISegment arg_AE_1 = segmentCollection2.get_Segment(i); object value3 = Missing.Value; object value4 = Missing.Value; arg_AE_0.AddSegment(arg_AE_1, ref value3, ref value4); } return; } } private void AddToFeedback(IPolyline pPolyline) { if (this.m_pDisplayFeedback != null) { IPointCollection pointCollection = pPolyline as IPointCollection; int pointCount = pointCollection.PointCount; for (int i = 0; i < pointCount; i++) { (this.m_pDisplayFeedback as INewPolygonFeedback).AddPoint(pointCollection.get_Point(i)); } return; } this.m_pDisplayFeedback = new NewPolygonFeedbackClass(); m_pDisplayFeedback.Display = this.m_hookHelper.ActiveView.ScreenDisplay; if (pPolyline.IsEmpty) { return; } IPointCollection pointCollection2 = pPolyline as IPointCollection; int pointCount2 = pointCollection2.PointCount; (this.m_pDisplayFeedback as INewPolygonFeedback).Start(pointCollection2.get_Point(0)); for (int j = 1; j < pointCount2; j++) { (this.m_pDisplayFeedback as INewPolygonFeedback).AddPoint(pointCollection2.get_Point(j)); } } private void EndTraceFeedback() { if (this.m_pTraceFeedback != null) { this.m_pTraceFeedback.Stop(null); this.m_pTraceFeedback = null; } this.ReleaseFeatureCache(); this.ReleaseBeTracedPolyline(); } } }