You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							494 lines
						
					
					
						
							20 KiB
						
					
					
				
			
		
		
	
	
							494 lines
						
					
					
						
							20 KiB
						
					
					
				using ESRI.ArcGIS.Carto; | 
						|
using ESRI.ArcGIS.Controls; | 
						|
using ESRI.ArcGIS.Geometry; | 
						|
using KGIS.Framework.Maps; | 
						|
using KGIS.Framework.Platform; | 
						|
using KGIS.Framework.Utils; | 
						|
using Kingo.Plugin.MapView.Interface; | 
						|
using System; | 
						|
using System.Collections.Generic; | 
						|
using System.Linq; | 
						|
using System.Text; | 
						|
using System.Threading.Tasks; | 
						|
 | 
						|
namespace Kingo.Plugin.MapView.Commands | 
						|
{ | 
						|
    public class BaseSnapCommand : ICheckBarItem | 
						|
    { | 
						|
        private IHookHelper m_hookHelper; | 
						|
        private EngineEditorClass m_editor = new EngineEditorClass(); | 
						|
        private IEngineSnapEnvironment snapEnv = null; | 
						|
        private bool IsChecked = false; | 
						|
        List<IFeatureLayer> layers = new List<IFeatureLayer>(); | 
						|
        public virtual ESRI.ArcGIS.Geometry.esriGeometryHitPartType HitPartType { get { return ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartNone; } } | 
						|
 | 
						|
        public bool Checked | 
						|
        { | 
						|
            get | 
						|
            { | 
						|
                if (snapEnv.SnapAgentCount != 0) | 
						|
                { | 
						|
                    //List<IFeatureLayer> layers = GetVisibleLayer(m_hookHelper.FocusMap); | 
						|
                    //layers = FrmSnapSetting.checkedLayers; | 
						|
                    //若未进行捕捉设置,则获取当前显示图层 | 
						|
                    if (layers.Count <= 0) | 
						|
                    { | 
						|
                        layers = MapsManager.Instance.MapService.GetAllVisibleLayerInMap<IFeatureLayer>(); | 
						|
                    } | 
						|
                    if (layers.Count != snapEnv.SnapAgentCount) | 
						|
                    { | 
						|
                        ESRI.ArcGIS.Geometry.esriGeometryHitPartType partType = (snapEnv.get_SnapAgent(0) as IEngineFeatureSnapAgent).HitType; | 
						|
                        snapEnv.ClearSnapAgents(); | 
						|
                        foreach (IFeatureLayer item in layers) | 
						|
                        { | 
						|
                            IEngineFeatureSnapAgent featureSnapAgent = new EngineFeatureSnap(); | 
						|
                            featureSnapAgent.FeatureClass = item.FeatureClass; | 
						|
                            featureSnapAgent.HitType = partType; | 
						|
                            snapEnv.AddSnapAgent(featureSnapAgent); | 
						|
                        } | 
						|
                    } | 
						|
                } | 
						|
                return IsChecked; | 
						|
            } | 
						|
        } | 
						|
 | 
						|
        public virtual void OnCreate(object hook) | 
						|
        { | 
						|
            try | 
						|
            { | 
						|
                snapEnv = (IEngineSnapEnvironment)m_editor; | 
						|
                if (hook == null) | 
						|
                { | 
						|
                    return; | 
						|
                } | 
						|
                if (m_hookHelper == null) | 
						|
                { | 
						|
                    m_hookHelper = new HookHelper(); | 
						|
                    m_hookHelper.Hook = hook; | 
						|
                } | 
						|
 | 
						|
                if (m_hookHelper.ActiveView == null) | 
						|
                { | 
						|
                    m_hookHelper = null; | 
						|
                    return; | 
						|
                } | 
						|
                layers = null; | 
						|
            } | 
						|
            catch (Exception ex) | 
						|
            { | 
						|
                LogAPI.Debug("加载 BaseSnap 类时失败,异常原因: " + ex + " ; "); | 
						|
                m_hookHelper = null; | 
						|
            } | 
						|
        } | 
						|
 | 
						|
        private IMapService _MapService; | 
						|
 | 
						|
        public virtual void OnClick() | 
						|
        { | 
						|
            try | 
						|
            { | 
						|
                snapEnv.SnapTolerance = 15; | 
						|
                this.IsChecked = !this.IsChecked; | 
						|
                if (this.IsChecked) | 
						|
                { | 
						|
                    layers = MapsManager.Instance.MapService.GetAllVisibleLayerInMap<IFeatureLayer>(); | 
						|
                    //layers = FrmSnapSetting.checkedLayers; | 
						|
                    if (layers.Count <= 0) | 
						|
                    { | 
						|
                        layers = MapsManager.Instance.MapService.GetAllVisibleLayerInMap<IFeatureLayer>(); | 
						|
                    } | 
						|
                    if (snapEnv.SnapAgentCount != layers.Count) | 
						|
                    { | 
						|
                        snapEnv.ClearSnapAgents(); | 
						|
                        foreach (IFeatureLayer item in layers) | 
						|
                        { | 
						|
                            IEngineFeatureSnapAgent featureSnapAgent = new EngineFeatureSnap(); | 
						|
                            featureSnapAgent.FeatureClass = item.FeatureClass; | 
						|
                            featureSnapAgent.HitType = HitPartType; | 
						|
                            snapEnv.AddSnapAgent(featureSnapAgent); | 
						|
                        } | 
						|
                    } | 
						|
                    else | 
						|
                    { | 
						|
                        for (int i = 0; i < snapEnv.SnapAgentCount; i++) | 
						|
                        { | 
						|
                            IEngineFeatureSnapAgent featureSnapAgent = snapEnv.get_SnapAgent(i) as IEngineFeatureSnapAgent; | 
						|
                            if (featureSnapAgent != null && (featureSnapAgent.HitType & HitPartType) != HitPartType) | 
						|
                            { | 
						|
                                featureSnapAgent.HitType = featureSnapAgent.HitType | HitPartType; | 
						|
                            } | 
						|
                        } | 
						|
                    } | 
						|
                } | 
						|
                else | 
						|
                { | 
						|
                    if (snapEnv.SnapAgentCount > 0) | 
						|
                    { | 
						|
                        IEngineFeatureSnapAgent featureSnapAgent = snapEnv.get_SnapAgent(0) as IEngineFeatureSnapAgent; | 
						|
                        if (featureSnapAgent != null && (featureSnapAgent.HitType & HitPartType) == HitPartType) | 
						|
                        { | 
						|
                            if ((featureSnapAgent.HitType ^ HitPartType) == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartNone) | 
						|
                            { | 
						|
                                snapEnv.ClearSnapAgents(); | 
						|
                            } | 
						|
                            else | 
						|
                            { | 
						|
                                for (int i = 0; i < snapEnv.SnapAgentCount; i++) | 
						|
                                { | 
						|
                                    IEngineFeatureSnapAgent snapAgent = snapEnv.get_SnapAgent(i) as IEngineFeatureSnapAgent; | 
						|
                                    if (snapAgent != null && (snapAgent.HitType & HitPartType) == HitPartType) | 
						|
                                    { | 
						|
                                        snapAgent.HitType = snapAgent.HitType ^ HitPartType; | 
						|
                                    } | 
						|
                                } | 
						|
                            } | 
						|
                        } | 
						|
                    } | 
						|
                } | 
						|
            } | 
						|
            catch (Exception ex) | 
						|
            { | 
						|
                LogAPI.Debug(ex); | 
						|
            } | 
						|
            finally | 
						|
            { | 
						|
 | 
						|
            } | 
						|
        } | 
						|
        //public virtual void OnClick() | 
						|
        //{ | 
						|
        //    try | 
						|
        //    { | 
						|
        //        _MapService = Platform.Instance.MapsService; | 
						|
 | 
						|
        //        IPoint IPointNow = m_editor.LastPoint; | 
						|
 | 
						|
        //        //double dX = IPointNow.X; | 
						|
        //        //double dY = IPointNow.Y; | 
						|
 | 
						|
        //        int iMark = 0; | 
						|
 | 
						|
        //        if (iMark == 1) | 
						|
        //        { | 
						|
        //            OnClickReal(); | 
						|
        //            return; | 
						|
        //        } | 
						|
 | 
						|
        //        snapEnv.SnapTolerance = 15; | 
						|
        //        this.IsChecked = !this.IsChecked; | 
						|
        //        if (this.IsChecked)//点击了 | 
						|
        //        { | 
						|
        //            layers = KGIS.Framework.AE.IFeatureLayerAPI.GetLayers(m_hookHelper.FocusMap, ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryNull, true); | 
						|
        //            //if (snapEnv.SnapAgentCount != layers.Count) | 
						|
        //            //{ | 
						|
        //            int iCTemp1 = snapEnv.SnapAgentCount; | 
						|
        //            snapEnv.ClearSnapAgents(); | 
						|
        //            int iCTemp2 = snapEnv.SnapAgentCount; | 
						|
        //            foreach (IFeatureLayer item in layers) | 
						|
        //            { | 
						|
        //                //public static IPoint getSnapPoint(AxMapControl mainMap, IPoint QueryPoint, IFeatureLayer pFeatureLayer, string snapStyle) | 
						|
 | 
						|
        //                if(IPointNow!=null) | 
						|
        //                { | 
						|
        //                    var v1 = (m_hookHelper.Hook as AxMapControl); | 
						|
        //                    var v2 = (m_hookHelper as AxMapControl); | 
						|
        //                    var v3 = (_MapService.getAxMapControl() as AxMapControl); | 
						|
 | 
						|
        //                    IPoint ipO = GetSnapPointBZ.getSnapPoint(v3, IPointNow, item, "线中点"); | 
						|
        //                    snapEnv.SnapPoint(ipO); | 
						|
        //                } | 
						|
 | 
						|
        //                IEngineFeatureSnapAgent featureSnapAgent = new EngineFeatureSnap(); | 
						|
        //                featureSnapAgent.FeatureClass = item.FeatureClass; | 
						|
        //                featureSnapAgent.HitType = HitPartType; | 
						|
        //                snapEnv.AddSnapAgent(featureSnapAgent); | 
						|
        //            } | 
						|
 | 
						|
        //            m_hookHelper.ActiveView.Refresh();//刷新图层 | 
						|
 | 
						|
        //            //} | 
						|
        //            //else | 
						|
        //            //{ | 
						|
        //            //    for (int i = 0; i < snapEnv.SnapAgentCount; i++) | 
						|
        //            //    { | 
						|
        //            //        IEngineFeatureSnapAgent featureSnapAgent = snapEnv.get_SnapAgent(i) as IEngineFeatureSnapAgent; | 
						|
        //            //        if (featureSnapAgent != null) | 
						|
        //            //        { | 
						|
        //            //            featureSnapAgent.HitType = ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartMidpoint; | 
						|
        //            //        } | 
						|
        //            //    } | 
						|
        //            //} | 
						|
        //        } | 
						|
        //        else | 
						|
        //        { | 
						|
        //            if (snapEnv.SnapAgentCount > 0) | 
						|
        //            { | 
						|
        //                snapEnv.ClearSnapAgents(); | 
						|
        //            } | 
						|
        //        } | 
						|
        //    } | 
						|
        //    catch (Exception ex) | 
						|
        //    { | 
						|
        //        LogAPI.Debug(ex); | 
						|
        //    } | 
						|
        //    finally | 
						|
        //    { | 
						|
 | 
						|
        //    } | 
						|
        //} | 
						|
 | 
						|
        public virtual void OnClickReal() | 
						|
        { | 
						|
            snapEnv.SnapTolerance = 150; | 
						|
            this.IsChecked = !this.IsChecked; | 
						|
            if (this.IsChecked)//点击了 | 
						|
            { | 
						|
                layers = MapsManager.Instance.MapService.GetAllVisibleLayerInMap<IFeatureLayer>(); | 
						|
                if (snapEnv.SnapAgentCount != layers.Count) | 
						|
                { | 
						|
                    snapEnv.ClearSnapAgents(); | 
						|
                    foreach (IFeatureLayer item in layers) | 
						|
                    { | 
						|
                        IEngineFeatureSnapAgent featureSnapAgent = new EngineFeatureSnap(); | 
						|
                        featureSnapAgent.FeatureClass = item.FeatureClass; | 
						|
                        featureSnapAgent.HitType = HitPartType; | 
						|
                        snapEnv.AddSnapAgent(featureSnapAgent); | 
						|
                    } | 
						|
                } | 
						|
                else | 
						|
                { | 
						|
                    for (int i = 0; i < snapEnv.SnapAgentCount; i++) | 
						|
                    { | 
						|
                        IEngineFeatureSnapAgent featureSnapAgent = snapEnv.get_SnapAgent(i) as IEngineFeatureSnapAgent; | 
						|
 | 
						|
                        //LogAPI.Debug("featureSnapAgent.HitType 为 " + featureSnapAgent.HitType + " ; "); | 
						|
                        //LogAPI.Debug("HitPartType 为 " + HitPartType + " ; "); | 
						|
 | 
						|
                        //var vTemp11 = featureSnapAgent.HitType & HitPartType; | 
						|
                        //LogAPI.Debug("featureSnapAgent.HitType & HitPartType 为 " + vTemp11 + " ; "); | 
						|
 | 
						|
                        //var vTemp12 = featureSnapAgent.HitType | HitPartType; | 
						|
                        //LogAPI.Debug("featureSnapAgent.HitType | HitPartType 为 " + vTemp12 + " ; "); | 
						|
 | 
						|
 | 
						|
                        //bool b11 = (featureSnapAgent.HitType & HitPartType) != HitPartType; | 
						|
                        //LogAPI.Debug(" (featureSnapAgent.HitType & HitPartType) != HitPartType 为 " + b11 + " ; "); | 
						|
 | 
						|
 | 
						|
                        if (featureSnapAgent != null && (featureSnapAgent.HitType & HitPartType) != HitPartType) | 
						|
                        { | 
						|
                            featureSnapAgent.HitType = featureSnapAgent.HitType | HitPartType; | 
						|
 | 
						|
                            //if(featureSnapAgent.HitType== ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartBoundary) | 
						|
                            //{ | 
						|
                            //    int i55 = 1; | 
						|
                            //} | 
						|
 | 
						|
                            //if (featureSnapAgent.HitType == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex) | 
						|
                            //{ | 
						|
                            //    int i55 = 1; | 
						|
                            //} | 
						|
 | 
						|
                            //if (featureSnapAgent.HitType == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartEndpoint) | 
						|
                            //{ | 
						|
                            //    int i55 = 1; | 
						|
                            //} | 
						|
 | 
						|
                            //if (featureSnapAgent.HitType == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartMidpoint) | 
						|
                            //{ | 
						|
                            //    int i55 = 1; | 
						|
                            //} | 
						|
                        } | 
						|
                    } | 
						|
                } | 
						|
            } | 
						|
            else//未点击 | 
						|
            { | 
						|
                if (snapEnv.SnapAgentCount > 0) | 
						|
                { | 
						|
                    IEngineFeatureSnapAgent featureSnapAgent = snapEnv.get_SnapAgent(0) as IEngineFeatureSnapAgent; | 
						|
 | 
						|
                    //LogAPI.Debug("featureSnapAgent.HitType 为 " + featureSnapAgent.HitType + " ; "); | 
						|
                    //LogAPI.Debug("HitPartType 为 " + HitPartType + " ; "); | 
						|
 | 
						|
                    //var vTemp21 = featureSnapAgent.HitType ^ HitPartType; | 
						|
                    //LogAPI.Debug("featureSnapAgent.HitType ^ HitPartType 为 " + vTemp21 + " ; "); | 
						|
 | 
						|
                    //var b21 = (featureSnapAgent.HitType & HitPartType) == HitPartType; | 
						|
                    //LogAPI.Debug(" (featureSnapAgent.HitType & HitPartType) == HitPartType 为 " + b21 + " ; "); | 
						|
 | 
						|
                    if (featureSnapAgent != null && (featureSnapAgent.HitType & HitPartType) == HitPartType) | 
						|
                    { | 
						|
                        //var vTemp22 = featureSnapAgent.HitType ^ HitPartType; | 
						|
                        //LogAPI.Debug("featureSnapAgent.HitType ^ HitPartType 为 " + vTemp22 + " ; "); | 
						|
 | 
						|
                        if ((featureSnapAgent.HitType ^ HitPartType) == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartNone) | 
						|
                        { | 
						|
                            snapEnv.ClearSnapAgents(); | 
						|
                        } | 
						|
                        else | 
						|
                        { | 
						|
                            for (int i = 0; i < snapEnv.SnapAgentCount; i++) | 
						|
                            { | 
						|
                                IEngineFeatureSnapAgent snapAgent = snapEnv.get_SnapAgent(i) as IEngineFeatureSnapAgent; | 
						|
 | 
						|
                                //var vTemp23 = snapAgent.HitType & HitPartType; | 
						|
                                //LogAPI.Debug("snapAgent.HitType & HitPartType 为 " + vTemp23 + " ; "); | 
						|
 | 
						|
                                //var b22 = (snapAgent.HitType & HitPartType) == HitPartType; | 
						|
                                //LogAPI.Debug(" (snapAgent.HitType & HitPartType) == HitPartType 为 " + b22 + " ; "); | 
						|
 | 
						|
                                if (snapAgent != null && (snapAgent.HitType & HitPartType) == HitPartType) | 
						|
                                { | 
						|
                                    //var vTemp24 = snapAgent.HitType ^ HitPartType; | 
						|
                                    //LogAPI.Debug("snapAgent.HitType ^ HitPartType 为 " + vTemp24 + " ; "); | 
						|
 | 
						|
                                    snapAgent.HitType = snapAgent.HitType ^ HitPartType; | 
						|
 | 
						|
                                    //if (snapAgent.HitType == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartBoundary) | 
						|
                                    //{ | 
						|
                                    //    int i55 = 1; | 
						|
                                    //} | 
						|
 | 
						|
                                    //if (snapAgent.HitType == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex) | 
						|
                                    //{ | 
						|
                                    //    int i55 = 1; | 
						|
                                    //} | 
						|
 | 
						|
                                    //if (snapAgent.HitType == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartEndpoint) | 
						|
                                    //{ | 
						|
                                    //    int i55 = 1; | 
						|
                                    //} | 
						|
 | 
						|
                                    //if (snapAgent.HitType == ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartMidpoint) | 
						|
                                    //{ | 
						|
                                    //    int i55 = 1; | 
						|
                                    //} | 
						|
                                } | 
						|
                            } | 
						|
                        } | 
						|
                    } | 
						|
                } | 
						|
            } | 
						|
        } | 
						|
 | 
						|
        public int Bitmap | 
						|
        { | 
						|
            get { return 0; } | 
						|
        } | 
						|
 | 
						|
        public string Caption | 
						|
        { | 
						|
            get { return ""; } | 
						|
        } | 
						|
 | 
						|
        public string Category | 
						|
        { | 
						|
            get { return ""; } | 
						|
        } | 
						|
 | 
						|
        public bool Enabled | 
						|
        { | 
						|
            get { return m_editor.EditState == esriEngineEditState.esriEngineStateEditing; } | 
						|
        } | 
						|
 | 
						|
        public int HelpContextID | 
						|
        { | 
						|
            get { return 0; } | 
						|
        } | 
						|
 | 
						|
        public string HelpFile | 
						|
        { | 
						|
            get { return ""; } | 
						|
        } | 
						|
 | 
						|
        public string Message | 
						|
        { | 
						|
            get { return ""; } | 
						|
        } | 
						|
 | 
						|
        public string Name | 
						|
        { | 
						|
            get { return ""; } | 
						|
        } | 
						|
 | 
						|
        public virtual string Tooltip | 
						|
        { | 
						|
            get { return ""; } | 
						|
        } | 
						|
        #region 获取所有可见图层 | 
						|
        ///// <summary> | 
						|
        ///// 获取当前地图中可见的矢量图层 | 
						|
        ///// </summary> | 
						|
        ///// <param name="pMap"></param> | 
						|
        ///// <returns></returns> | 
						|
        //private List<IFeatureLayer> GetVisibleLayer(IMap pMap) | 
						|
        //{ | 
						|
        //    List<IFeatureLayer> result = new List<IFeatureLayer>(); | 
						|
        //    if (pMap == null) | 
						|
        //        return result; | 
						|
        //    try | 
						|
        //    { | 
						|
        //        for (int i = 0; i < pMap.LayerCount; i++) | 
						|
        //        { | 
						|
        //            ILayer layer = pMap.get_Layer(i); | 
						|
        //            if (layer is IFeatureLayer) | 
						|
        //            { | 
						|
        //                if ((layer as IFeatureLayer).Visible) | 
						|
        //                { | 
						|
        //                    result.Add(layer as IFeatureLayer); | 
						|
        //                } | 
						|
        //            } | 
						|
        //            else if (layer is ICompositeLayer) | 
						|
        //            { | 
						|
        //                if ((layer as IGroupLayer).Visible) | 
						|
        //                { | 
						|
        //                    result.AddRange(GetVisibleLayerByGroupLayer(layer as ICompositeLayer)); | 
						|
        //                } | 
						|
        //            } | 
						|
        //        } | 
						|
        //    } | 
						|
        //    catch (Exception ex) | 
						|
        //    { | 
						|
        //        LogAPI.Debug("获取当前地图中可见的矢量图层时失败,异常原因:" + ex + " ; "); | 
						|
        //    } | 
						|
        //    return result; | 
						|
        //} | 
						|
 | 
						|
        //private List<IFeatureLayer> GetVisibleLayerByGroupLayer(ICompositeLayer pGroupLayer) | 
						|
        //{ | 
						|
        //    List<IFeatureLayer> result = new List<IFeatureLayer>(); | 
						|
        //    if (pGroupLayer != null && pGroupLayer.Count > 0) | 
						|
        //    { | 
						|
        //        for (int i = 0; i < pGroupLayer.Count; i++) | 
						|
        //        { | 
						|
        //            ILayer layer = pGroupLayer.get_Layer(i); | 
						|
        //            if (layer is IAnnotationLayer) | 
						|
        //            { | 
						|
 | 
						|
        //            } | 
						|
        //            else if (layer is IGroupLayer) | 
						|
        //            { | 
						|
        //                if ((layer as IGroupLayer).Visible) | 
						|
        //                { | 
						|
        //                    result.AddRange(GetVisibleLayerByGroupLayer(layer as ICompositeLayer)); | 
						|
        //                } | 
						|
        //            } | 
						|
        //            else | 
						|
        //            { | 
						|
        //                if (layer is IFeatureLayer) | 
						|
        //                { | 
						|
        //                    if ((layer as IFeatureLayer).Visible) | 
						|
        //                    { | 
						|
        //                        result.Add((IFeatureLayer)layer); | 
						|
        //                    } | 
						|
        //                } | 
						|
        //            } | 
						|
        //        } | 
						|
        //    } | 
						|
        //    return result; | 
						|
        //} | 
						|
        #endregion | 
						|
 | 
						|
    } | 
						|
}
 | 
						|
 |