ArcEngine编辑模块——移动单个要素的实现方法

58 篇文章 73 订阅

1、前言

ArcEngine中,移动单个点、线、面要素主要使用以下三个接口:

  • IMovePointFeedback
  • IMoveLineFeedback
  • IMovePolygonFeedback

需要注意的是:这三个接口每次只能移动一个要素,要素的批量移动将会在下篇博客给出实现方法。

2、移动单个面要素

以移动单个面要素为例,首先构建如下图所示的界面:

在这里插入图片描述
2.1、主界面代码

using System;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using EditApp.Command;
using EditApp.Tool;

namespace EditApp
{
    public partial class MainForm : Form
    {
        // 构造函数
        public MainForm()
        {
            InitializeComponent();
            btnStartEditing.Enabled = true;
            btnStopEditing.Enabled = false;
            btnSelect.Enabled = false;
            btnMove.Enabled = false;
            axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
            axMapControl1.Extent = axMapControl1.FullExtent;
        }

        // 开始编辑
        private void btnStartEditing_Click(object sender, EventArgs e)
        {
            ICommand command = new StartEditingCommand();
            command.OnCreate(axMapControl1.Object);
            command.OnClick();

            // 设置按钮可用性
            btnStartEditing.Enabled = false;
            btnStopEditing.Enabled = true;
            btnSelect.Enabled = true;
            btnMove.Enabled = true;
        }

        // 结束编辑
        private void btnStopEditing_Click(object sender, EventArgs e)
        {
            ICommand command = new StopEditingCommand();
            command.OnCreate(axMapControl1.Object);
            command.OnClick();

            // 设置按钮可用性
            btnStartEditing.Enabled = true;
            btnStopEditing.Enabled = false;
            btnSelect.Enabled = false;
            btnMove.Enabled = false;
        }

        // 选择要素
        private void btnSelect_Click(object sender, EventArgs e)
        {
            ICommand command = new ControlsSelectFeaturesTool();
            command.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = command as ITool;
        }

        // 移动要素
        private void btnMove_Click(object sender, EventArgs e)
        {
            ICommand command = new MovePolygonTool();
            command.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = command as ITool;
        }
    }
}

2.2、开始编辑

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace EditApp.Command
{
    /// <summary>
    /// Summary description for StartEditingCommand.
    /// </summary>
    [Guid("e65d004c-4cbc-4577-8676-11c72454caf2")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EditApp.Command.StartEditingCommand")]
    public sealed class StartEditingCommand : BaseCommand
    {
        #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);
            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);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;

        public StartEditingCommand()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text
            base.m_caption = "";  //localizable text
            base.m_message = "";  //localizable text 
            base.m_toolTip = "";  //localizable text 
            base.m_name = "";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // 刷新地图
            m_hookHelper.FocusMap.ClearSelection();
            m_hookHelper.ActiveView.Refresh();

            // 获取数据集
            IFeatureLayer pFeatureLayer =  m_hookHelper.FocusMap.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IDataset pDataset = pFeatureClass as IDataset;

            // 开始编辑
            IWorkspace pWorkspace = pDataset.Workspace;
            IWorkspaceEdit pWorkspaceEdit = pWorkspace as IWorkspaceEdit;
            pWorkspaceEdit.StartEditing(true);
        }
        #endregion
    }
}

2.3、结束编辑

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace EditApp.Command
{
    /// <summary>
    /// Summary description for StopEditingCommand.
    /// </summary>
    [Guid("61ba0f95-3a1c-4a3a-b802-4a0194496d47")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EditApp.Command.StopEditingCommand")]
    public sealed class StopEditingCommand : BaseCommand
    {
        #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);
            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);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;

        public StopEditingCommand()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text
            base.m_caption = "";  //localizable text
            base.m_message = "";  //localizable text 
            base.m_toolTip = "";  //localizable text 
            base.m_name = "";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // 刷新地图
            m_hookHelper.FocusMap.ClearSelection();
            m_hookHelper.ActiveView.Refresh();

            // 获取数据集
            IFeatureLayer pFeatureLayer = m_hookHelper.FocusMap.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IDataset pDataset = pFeatureClass as IDataset;

            // 结束编辑
            IWorkspace pWorkspace = pDataset.Workspace;
            IWorkspaceEdit pWorkspaceEdit = pWorkspace as IWorkspaceEdit;
            pWorkspaceEdit.StopEditing(true);
        }
        #endregion
    }
}

2.4、移动单个面要素

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace EditApp.Tool
{
    /// <summary>
    /// Summary description for MovePolygonTool.
    /// </summary>
    [Guid("9d3a7901-b8a0-42b1-9f0b-25e2f25d9ba1")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EditApp.Tool.MovePolygonTool")]
    public sealed class MovePolygonTool : 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);
            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);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;
        private IMovePolygonFeedback pMovePolygonFeedback;
        private ISet pMoveSet;

        public MovePolygonTool()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text 
            base.m_caption = "";  //localizable text 
            base.m_message = "";  //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)
        {
            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add MovePolygonTool.OnCreate implementation
        }

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

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            if (m_hookHelper.FocusMap.SelectionCount == 0)
            {
                MessageBox.Show("请选择需要移动的要素", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pMovePolygonFeedback == null)
            {
                IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
                pEnumFeature.Reset();
                IFeature pFeature = pEnumFeature.Next();
                pMoveSet = new Set();
                pMoveSet.Add(pFeature);

                // 开始移动
                IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMovePolygonFeedback = new MovePolygonFeedback();
                pMovePolygonFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                pMovePolygonFeedback.Start(pFeature.Shape as IPolygon, pPoint);
            }
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            if (pMovePolygonFeedback != null)
            {
                IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMovePolygonFeedback.MoveTo(pPoint);
            }
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            if (pMovePolygonFeedback != null)
            {
                pMoveSet.Reset();
                IFeature pFeature = pMoveSet.Next() as IFeature;
                pFeature.Shape = pMovePolygonFeedback.Stop();
                pFeature.Store();

                // 清除变量
                pMovePolygonFeedback = null;
                pMoveSet.RemoveAll();
                pMoveSet = null;
                m_hookHelper.ActiveView.Refresh();
            }
        }
        #endregion
    }
}

程序运行结果如下图所示:
在这里插入图片描述

3、移动单个点、线要素

移动单个点、线要素的代码其实跟上面的代码差不多,只需要照葫芦画瓢即可:

3.1、移动单个点要素

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace EditApp.Tool
{
    /// <summary>
    /// Summary description for MovePointTool.
    /// </summary>
    [Guid("af4df943-8afa-4b8c-950e-54fc0b456258")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EditApp.Tool.MovePointTool")]
    public sealed class MovePointTool : 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);
            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);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;
        private IMovePointFeedback pMovePointFeedback;
        private ISet pMoveSet;

        public MovePointTool()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text 
            base.m_caption = "";  //localizable text 
            base.m_message = "";  //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)
        {
            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add MovePointTool.OnCreate implementation
        }

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

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            if (m_hookHelper.FocusMap.SelectionCount == 0)
            {
                MessageBox.Show("请选择需要移动的要素", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pMovePointFeedback == null)
            {
                IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
                pEnumFeature.Reset();
                IFeature pFeature = pEnumFeature.Next();
                pMoveSet = new Set();
                pMoveSet.Add(pFeature);

                // 开始移动
                IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMovePointFeedback = new MovePointFeedback();
                pMovePointFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                pMovePointFeedback.Start(pFeature.Shape as IPoint, pPoint);
            }
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            if (pMovePointFeedback != null)
            {
                IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMovePointFeedback.MoveTo(pPoint);
            }
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            if (pMovePointFeedback != null)
            {
                pMoveSet.Reset();
                IFeature pFeature = pMoveSet.Next() as IFeature;
                pFeature.Shape = pMovePointFeedback.Stop();
                pFeature.Store();

                // 清除变量
                pMovePointFeedback = null;
                pMoveSet.RemoveAll();
                pMoveSet = null;
                m_hookHelper.ActiveView.Refresh();
            }
        }
        #endregion
    }
}

运行结果如下图所示:

在这里插入图片描述
3.2、移动单个线要素

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace EditApp.Tool
{
    /// <summary>
    /// Summary description for MoveLineTool.
    /// </summary>
    [Guid("0d20dbc7-783c-4e68-93e7-4d83764388ea")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EditApp.Tool.MoveLineTool")]
    public sealed class MoveLineTool : 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);
            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);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;
        private IMoveLineFeedback pMoveLineFeedback;
        private ISet pMoveSet;

        public MoveLineTool()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text 
            base.m_caption = "";  //localizable text 
            base.m_message = "";  //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)
        {
            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add MoveLineTool.OnCreate implementation
        }

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

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            if (m_hookHelper.FocusMap.SelectionCount == 0)
            {
                MessageBox.Show("请选择需要移动的要素", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pMoveLineFeedback == null)
            {
                IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
                pEnumFeature.Reset();
                IFeature pFeature = pEnumFeature.Next();
                pMoveSet = new Set();
                pMoveSet.Add(pFeature);

                // 开始移动
                IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMoveLineFeedback = new MoveLineFeedback();
                pMoveLineFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                pMoveLineFeedback.Start(pFeature.Shape as IPolyline, pPoint);
            }
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            if (pMoveLineFeedback != null)
            {
                IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMoveLineFeedback.MoveTo(pPoint);
            }
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            if (pMoveLineFeedback != null)
            {
                pMoveSet.Reset();
                IFeature pFeature = pMoveSet.Next() as IFeature;
                pFeature.Shape = pMoveLineFeedback.Stop();
                pFeature.Store();

                // 清除变量
                pMoveLineFeedback = null;
                pMoveSet.RemoveAll();
                pMoveSet = null;
                m_hookHelper.ActiveView.Refresh();
            }
        }
        #endregion
    }
}

运行结果如下图所示:

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值