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
    }
}





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
客户最近打电话过来,说我们的软件在量距离的时候不能平移地图。我们的软件是使用ArcEngine来开发的,在网上输入关键字“arcengine 距离测量”,可以搜索到一大堆内容基本相似的文章或代码,基本上都是采用INewLineFeedback来实现的,我们的软件也是使用此种方法。方法大致如下:写一个距离测量工具,继承自BaseTool,在该类中使用INewLineFeedback动态绘制多段折线,最后转化为Element添加到地图上。如果要增加平移功能,我们仿造ArcMap使用鼠标中键平移,在OnMouseDown、OnMouseMove、OnMouseUp事件中增加判断鼠标中键的语句,然后分别使用PanStart、PanMoveTo、PanStop实现地图的平移。 此时,出现了一个新问题,一旦移动地图后,之前使用INewLineFeedback绘制的线完全乱了方寸,有一部分线丢失了。之后又发现一个更可笑的问题,在绘制的过程中,如果使用alt+tab键切换到其它窗口,然后再切换回地图窗口的时候,会多出一条线,INewLineFeedback把切换窗口前的鼠标位置记录了下来。 对于这个问题,我使用ArcMap的测量工具检查了一下,发现不存在上述问题。但是INewLineFeedback为什么会产生这个bug,难道是本人的使用方法不对。在网上搜了一下其他类似代码进行测试,都存在这个bug。ArcEngine的SDK文档上也没有特别强调INewLineFeedback的使用细节。 本来打算用gdi进行解决,最后发现INewLineFeedback有一个Refresh函数,那么应该在什么地方使用它呢?在ArcMapControl的诸多事件中,尝试了OnAfterDraw、OnAfterScreenDraw、OnViewRefresh等,发现OnAfterScreenDraw是OK的。具体是在OnAfterScreenDraw事件中使用Refresh函数刷新INewLineFeedback,完美解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值