数据编辑中 实现要素捕捉

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 description for SnapTool.  
    ///   
    [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  
        ///   
        /// Required method for ArcGIS Component Category registration -  
        /// Do not modify the contents of this method with the code editor.  
        ///   
        private static void ArcGISCategoryRegistration(Type registerType)  
        {  
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);  
            MxCommands.Register(regKey);  
            ControlsCommands.Register(regKey);  
        }  
        ///   
        /// Required method for ArcGIS Component Category unregistration -  
        /// Do not modify the contents of this method with the code editor.  
        ///   
        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  
  
        ///   
        /// Occurs when this tool is created  
        ///   
        /// Instance of the application  
        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  
        }  
  
        ///   
        /// Occurs when this tool is clicked  
        ///   
        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  
    }  
}  

你可能感兴趣的:(ArcEngine)