⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pointselect.cs

📁 C# +arcengine select point
💻 CS
字号:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;

namespace PointSelect
{
    /// <summary>
    /// Summary description for PointSelect.
    /// </summary>
    [Guid("b6576ed0-9858-4bbd-b93d-4b6cb4349d70")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("PointSelect.PointSelect")]
    public sealed class PointSelect : BaseTool
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);
            ControlsCommands.Register(regKey);
        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);
            ControlsCommands.Unregister(regKey);
        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper = null;

        public PointSelect()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text 
            base.m_caption = "点选Feature";  //localizable text 
            base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl";  //localizable text
            base.m_toolTip = "点选地物工具";  //localizable text
            base.m_name = "PointSelectFeature";   //unique id, non-localizable (e.g. "MyCategory_MyTool")
            try
            {
                //
                // TODO: change resource name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
                base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overriden Class Methods

        /// <summary>
        /// Occurs when this tool is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                {
                    m_hookHelper = null;
                }
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this tool is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add PointSelect.OnClick implementation
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            IActiveView pActiveView = null;
            pActiveView = m_hookHelper.ActiveView;
            tagRECT r;
            IEnvelope pEnvelope = null;
            IPoint pPoint = new PointClass();
            IIdentify pIdentify = null;
            IArray pIDArray = new ArrayClass();
            IFeatureIdentifyObj pFeatureIdentifyObj = null;
            IIdentifyObj pIdentifyObj = null;
            IRowIdentifyObject pRowIdentifyObject = null;
            IFeature pFeature = null;
            IFeatureSelection pFeatureSelection = null;

            ICommand pCommand = new ControlsClearSelectionCommand();
            pCommand.OnCreate(m_hookHelper.Hook);
            pCommand.OnClick();

            pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y);
            IEnumLayer pEnumLayer = getAllFeatuerLayer(m_hookHelper.FocusMap);
            if(pEnumLayer!=null)
            {
                IFeatureLayer pFeatureLayer = pEnumLayer.Next() as IFeatureLayer;
                while(pFeatureLayer!=null)
                {
                    r.left = X - 5;
                    r.top = Y - 5;
                    r.right = X + 5;
                    r.bottom = Y + 5;
                    pEnvelope = new EnvelopeClass();
                    pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvelope,ref r,4);
                    pEnvelope.SpatialReference = pActiveView.FocusMap.SpatialReference;
                    pIdentify = pFeatureLayer as IIdentify;
                    pIDArray = pIdentify.Identify(pEnvelope);
                    if(pIDArray!=null && pIDArray.Count!=0)
                    {
                        pFeatureIdentifyObj = pIDArray.get_Element(0) as IFeatureIdentifyObj;
                        pIdentifyObj = pFeatureIdentifyObj as IIdentifyObj;

                        pRowIdentifyObject = pFeatureIdentifyObj as IRowIdentifyObject;
                        pFeature = pRowIdentifyObject.Row as IFeature;
                        if(pFeature!=null)
                        {
                            pFeatureSelection = pFeatureLayer as IFeatureSelection;
                            //RgbColor pcolor3 = new RgbColorClass();
                            //pcolor3.Red = 100;
                            //pcolor3.Green = 122;
                            //pcolor3.Blue = 255;
                            //pFeatureSelection.SelectionColor = pcolor3;
                            //pActiveView.FocusMap.SelectFeature(pFeatureLayer as ILayer,pFeature);
                            //MessageBox.Show(pFeature.get_Value(2).ToString());
                            pFeatureSelection.Add(pFeature);
                            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, pFeature.Extent.Envelope as IEnvelope);
                            return;
                        }
                       
                    }
                    pFeatureLayer = pEnumLayer.Next() as IFeatureLayer;
                }
            }
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add PointSelect.OnMouseMove implementation
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add PointSelect.OnMouseUp implementation
        }

        public IEnumLayer getAllFeatuerLayer(IMap pMap)
        {
            if (pMap != null)
            {
                ESRI.ArcGIS.esriSystem.UID puid = new ESRI.ArcGIS.esriSystem.UIDClass();
                puid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";  //只读取IFeaturelayer 
                IEnumLayer pEnumLayer = pMap.get_Layers(puid, true);
                pEnumLayer.Reset();
                return pEnumLayer;
            }
            return null;
        }

        //private double ConvertPixelsToMapUnits(IActiveView pActiveView, int PixelsSize)
        //{
        //    double realWorldDisplayExtent;
        //    int pixelExtent;

        //}
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -