不多BB,直接上代码
public sealed class Snap : 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; ISnappingFeedback feedback;//展示捕捉 IPointSnapper pointsnapper; //点捕捉器 ISnappingEnvironment pSnappingEnvironment; public Snap() { // // 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; } /// <summary> /// Occurs when this tool is clicked /// </summary> public override void OnClick() { //获取捕捉环境 IExtensionManager extensionManager = (m_hookHelper as IHookHelper2).ExtensionManager; if (extensionManager != null) { IExtension extension = extensionManager.FindExtension(new UIDClass { Value = "{E07B4C52-C894-4558-B8D4-D4050018D1DA}" //捕捉 }); pSnappingEnvironment = extension as ISnappingEnvironment; pSnappingEnvironment.Enabled = true; //开启捕捉 pSnappingEnvironment.IgnoreIMSLayers = true; //忽略服务底图 pointsnapper = pSnappingEnvironment.PointSnapper; //获取点捕捉器 feedback = new SnappingFeedbackClass(); //捕捉反馈,初始化 feedback.Initialize(m_hookHelper.Hook, pSnappingEnvironment, true); } } public override void OnMouseDown(int Button, int Shift, int X, int Y) { IPoint point = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); //地图点 ISnappingResult snapResult = this.pointsnapper.Snap(point); //根据地图点获取捕捉结果 if (snapResult != null) point = snapResult.Location; /**************** 后续可以使用point执行相关操作 *****************/ } public override void OnMouseMove(int Button, int Shift, int X, int Y) { IPoint pointcurrentcoords = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); ISnappingResult snapResult = this.pointsnapper.Snap(pointcurrentcoords); this.feedback.Update(snapResult, 0); //显示捕捉到的点 } public override void OnMouseUp(int Button, int Shift, int X, int Y) { // TODO: Add Snap.OnMouseUp implementation } #endregion }
注:
扩展的UID可以从AO帮助Names and IDs----Extensions里查看。