arcengine 捕捉笔记

IEngineEditor pEngineEditor=new EngineEditor();

//Get the snap enviroment from editor

IEngineSnapEnviroment psnapEnvironment = pEngineEditor as IEngineSnapEnviorment();

if(pSnapEnvironment.SnapAgentCount==0)

{

IEngineFeatureSnapAgent pFeatureSnapAgent=new EngineFeatureAgent();

pFeatureSnapAgent.FeatureClass=pFeatureClass;

psnapEnvironment.AddSnapAgent(pFeatureSnapAgent);

}

psnapEnvironment.SnapToleranceUnits=esriEngineToleranceUnits.esriEngineTolerancePixels;

psnapEnviroment.SnapTolerance=10;

IEngineEditorProperties pEnginePro=pEngineEditor as IEngineEditProperties2;

pEginePro.SnapTips=true;

//用法 MoveMove下

IEngineSnapEnviroment psnapEnvironment = pEngineEditor as IEngineSnapEnviorment();

psnapEnvironment .SnapPoint(Point);

//0——————————————————简便高级的另外的一种捕捉 不需要打开编辑 效果同arcmap一致—————————–

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.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;

namespace WindowsFormsApplication1.tool
{
    /// <summary>
    /// Summary description for SnapTool.
    /// </summary>
    [Guid("192f72b4-5043-45cb-8e1a-743ea964fa97")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("WindowsFormsApplication1.tool.SnapTool")]
    public sealed class SnapTool : 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;
        private IPoint m_snapPt;
        private ISnappingEnvironment m_SnapEnv;
        private IPointSnapper m_Snapper;
        private ISnappingFeedback m_SnapFeedback;
        private INewLineFeedback m_feedback;
        private ISnappingResult m_snapRst;

        public SnapTool()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text 
            base.m_caption = "";  //localizable text 
            base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl";  //localizable text
            base.m_toolTip = "";  //localizable text
            base.m_name = "";   //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 Overridden 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()
        {
            UID uid = new UIDClass();
            uid.Value = "{E07B4C52-C894-4558-B804-D4050018D1DA}";
            IExtensionManager extMar = (m_hookHelper as IHookHelper2).ExtensionManager;
            IExtension ext=extMar.FindExtension(uid);
            m_SnapEnv = ext as ISnappingEnvironment;
            m_Snapper = m_SnapEnv.PointSnapper;
            m_SnapFeedback = new SnappingFeedbackClass();
            m_SnapFeedback.Initialize(m_hookHelper.Hook, m_SnapEnv, true);
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            IPoint ppt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            if (m_snapPt != null)
            {
                ppt = m_snapPt;
            }

            if (m_feedback != null)
            {
                m_feedback = new NewLineFeedbackClass();
                m_feedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                m_feedback.Start(ppt);
            }
            else
            {
                m_feedback.AddPoint(ppt);
            }
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            IPoint ppt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            m_snapPt = ppt;
            m_snapRst = m_Snapper.Snap(m_snapPt);
            m_SnapFeedback.Update(m_snapRst, 0);
            if (m_snapRst != null)
            {
                m_snapPt = m_snapRst.Location;
            }

            if (m_feedback != null)
            {
                m_feedback.MoveTo(m_snapPt);
            }
        }

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

转载自:https://blog.csdn.net/wangtao510/article/details/51610785

You may also like...