其他分享
首页 > 其他分享> > ArcEngine编辑模块——移动多个要素的实现方法

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

作者:互联网

1、前言

前一篇博客介绍了在ArcEngine中移动单个要素的实现方法,这篇博客来介绍一下如何移动多个要素。移动多个要素需要使用IMoveGeometryFeedback接口,下面给出实现方法。

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 MoveGeometryTool();
            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 MoveGeometryTool.
    /// </summary>
    [Guid("a884fb9a-12be-4c25-8995-428c109e1a3a")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EditApp.Tool.MoveGeometryTool")]
    public sealed class MoveGeometryTool : 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 IMoveGeometryFeedback pMoveGeometryFeedback;
        private ISet pMoveSet;
        private IPoint pStartPoint;
        private IPoint pEndPoint;

        public MoveGeometryTool()
        {
            //
            // 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 MoveGeometryTool.OnCreate implementation
        }

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

        public override void onm ouseDown(int Button, int Shift, int X, int Y)
        {
            if (m_hookHelper.FocusMap.SelectionCount == 0)
            {
                MessageBox.Show("请选择需要移动的要素", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pMoveGeometryFeedback == null)
            {
                pStartPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMoveSet = new Set();
                pMoveGeometryFeedback = new MoveGeometryFeedback();
                pMoveGeometryFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                pMoveGeometryFeedback.Start(pStartPoint);

                // 遍历被选择要素
                IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
                pEnumFeature.Reset();
                IFeature pFeature = pEnumFeature.Next();
                while (pFeature != null)
                {
                    pMoveSet.Add(pFeature);
                    pMoveGeometryFeedback.AddGeometry(pFeature.Shape);
                    pFeature = pEnumFeature.Next();
                }
            }
        }

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

        public override void onm ouseUp(int Button, int Shift, int X, int Y)
        {
            if (pMoveGeometryFeedback != null)
            {
                pEndPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMoveSet.Reset();

                // 使用起点和终点构造一条线段
                ILine pLine = new Line();
                pLine.SpatialReference = m_hookHelper.FocusMap.SpatialReference;
                pLine.PutCoords(pStartPoint, pEndPoint);

                // 遍历被移动的要素
                IFeature pFeature = pMoveSet.Next() as IFeature;
                while (pFeature != null)
                {
                    IFeatureEdit pFeatureEdit = pFeature as IFeatureEdit;
                    pFeatureEdit.MoveSet(pMoveSet, pLine);
                    pFeature.Store();
                    pFeature = pMoveSet.Next() as IFeature;
                }

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

程序运行结果如下图所示:

在这里插入图片描述

3、添加包络框

上面虽然实现了多个要素的移动,但是看起来总觉得有些别扭,原因就是在移动的时候没有显示出这些要素的边框范围,下面就给它们加上包络框吧,代码如下:

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 MoveGeometryTool.
    /// </summary>
    [Guid("a884fb9a-12be-4c25-8995-428c109e1a3a")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EditApp.Tool.MoveGeometryTool")]
    public sealed class MoveGeometryTool : 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 IMoveGeometryFeedback pMoveGeometryFeedback;
        private IMoveEnvelopeFeedback pMoveEnvelopeFeedback;
        private ISet pMoveSet;
        private IPoint pStartPoint;
        private IPoint pEndPoint;

        public MoveGeometryTool()
        {
            //
            // 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 MoveGeometryTool.OnCreate implementation
        }

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

        public override void onm ouseDown(int Button, int Shift, int X, int Y)
        {
            if (m_hookHelper.FocusMap.SelectionCount == 0)
            {
                MessageBox.Show("请选择需要移动的要素", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pMoveGeometryFeedback == null)
            {
                pStartPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMoveSet = new Set();

                // 实例化MoveGeometryFeedback
                pMoveGeometryFeedback = new MoveGeometryFeedback();
                pMoveGeometryFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                pMoveGeometryFeedback.Start(pStartPoint);

                // 遍历被选择要素
                IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
                pEnumFeature.Reset();
                IFeature pFeature = pEnumFeature.Next();

                // 外界矩形坐标
                double xmin = pFeature.Shape.Envelope.XMin;
                double ymin = pFeature.Shape.Envelope.YMin;
                double xmax = pFeature.Shape.Envelope.XMax;
                double ymax = pFeature.Shape.Envelope.YMax;

                // 遍历被选择的要素
                while (pFeature != null)
                {
                    if (pFeature.Shape.Envelope.XMin < xmin)
                    {
                        xmin = pFeature.Shape.Envelope.XMin;
                    }
                    if (pFeature.Shape.Envelope.YMin < ymin)
                    {
                        ymin = pFeature.Shape.Envelope.YMin;
                    }
                    if (pFeature.Shape.Envelope.XMax > xmax)
                    {
                        xmax = pFeature.Shape.Envelope.XMax;
                    }
                    if (pFeature.Shape.Envelope.YMax > ymax)
                    {
                        ymax = pFeature.Shape.Envelope.YMax;
                    }

                    pMoveSet.Add(pFeature);
                    pMoveGeometryFeedback.AddGeometry(pFeature.Shape);
                    pFeature = pEnumFeature.Next();
                }

                // 创建包络框
                IEnvelope pEnvelope = new Envelope() as IEnvelope;
                pEnvelope.SpatialReference = m_hookHelper.FocusMap.SpatialReference;
                pEnvelope.PutCoords(xmin, ymin, xmax, ymax);

                // 实例化MoveEnvelopeFeedback
                pMoveEnvelopeFeedback = new MoveEnvelopeFeedback();
                pMoveEnvelopeFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                pMoveEnvelopeFeedback.Start(pEnvelope, pStartPoint);
            }
        }

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

        public override void onm ouseUp(int Button, int Shift, int X, int Y)
        {
            if (pMoveGeometryFeedback != null)
            {
                pEndPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMoveEnvelopeFeedback.Stop();
                pMoveGeometryFeedback.MoveTo(pEndPoint);
                pMoveSet.Reset();

                // 利用起点和终点构造一条线段,表示移动的方向
                ILine pLine = new Line();
                pLine.SpatialReference = m_hookHelper.FocusMap.SpatialReference;
                pLine.PutCoords(pStartPoint, pEndPoint);

                // 遍历被选择的要素
                IFeature pFeature = pMoveSet.Next() as IFeature;
                while (pFeature != null)
                {
                    IFeatureEdit pFeatureEdit = pFeature as IFeatureEdit;
                    pFeatureEdit.MoveSet(pMoveSet, pLine);
                    pFeature.Store();
                    pFeature = pMoveSet.Next() as IFeature;
                }

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

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

4、设置包络框样式

上面的代码使用IMoveEnvelopeFeedback显示包络框的移动效果,可以注意到在IMoveEnvelopeFeedback接口里有一个Symbol属性,看到这个属性后我们自然而然会联想到该接口可以给包络框设置样式,代码如下:

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 MoveGeometryTool.
    /// </summary>
    [Guid("a884fb9a-12be-4c25-8995-428c109e1a3a")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EditApp.Tool.MoveGeometryTool")]
    public sealed class MoveGeometryTool : 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 IMoveGeometryFeedback pMoveGeometryFeedback;
        private IMoveEnvelopeFeedback pMoveEnvelopeFeedback;
        private ISet pMoveSet;
        private IPoint pStartPoint;
        private IPoint pEndPoint;

        public MoveGeometryTool()
        {
            //
            // 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 MoveGeometryTool.OnCreate implementation
        }

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

        public override void onm ouseDown(int Button, int Shift, int X, int Y)
        {
            if (m_hookHelper.FocusMap.SelectionCount == 0)
            {
                MessageBox.Show("请选择需要移动的要素", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pMoveGeometryFeedback == null)
            {
                pStartPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMoveSet = new Set();

                // 实例化MoveGeometryFeedback
                pMoveGeometryFeedback = new MoveGeometryFeedback();
                pMoveGeometryFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                pMoveGeometryFeedback.Start(pStartPoint);

                // 遍历被选择要素
                IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
                pEnumFeature.Reset();
                IFeature pFeature = pEnumFeature.Next();

                // 外界矩形坐标
                double xmin = pFeature.Shape.Envelope.XMin;
                double ymin = pFeature.Shape.Envelope.YMin;
                double xmax = pFeature.Shape.Envelope.XMax;
                double ymax = pFeature.Shape.Envelope.YMax;

                // 遍历被选择的要素
                while (pFeature != null)
                {
                    if (pFeature.Shape.Envelope.XMin < xmin)
                    {
                        xmin = pFeature.Shape.Envelope.XMin;
                    }
                    if (pFeature.Shape.Envelope.YMin < ymin)
                    {
                        ymin = pFeature.Shape.Envelope.YMin;
                    }
                    if (pFeature.Shape.Envelope.XMax > xmax)
                    {
                        xmax = pFeature.Shape.Envelope.XMax;
                    }
                    if (pFeature.Shape.Envelope.YMax > ymax)
                    {
                        ymax = pFeature.Shape.Envelope.YMax;
                    }

                    pMoveSet.Add(pFeature);
                    pMoveGeometryFeedback.AddGeometry(pFeature.Shape);
                    pFeature = pEnumFeature.Next();
                }

                // 创建包络框
                IEnvelope pEnvelope = new Envelope() as IEnvelope;
                pEnvelope.SpatialReference = m_hookHelper.FocusMap.SpatialReference;
                pEnvelope.PutCoords(xmin, ymin, xmax, ymax);

                //实例化MoveEnvelopeFeedback
                pMoveEnvelopeFeedback = new MoveEnvelopeFeedback();
                pMoveEnvelopeFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                pMoveEnvelopeFeedback.Start(pEnvelope, pStartPoint);

                // 创建填充颜色
                IRgbColor pFillRgbColor = new RgbColor();
                pFillRgbColor.Red = 255;
                pFillRgbColor.Green = 0;
                pFillRgbColor.Blue = 0; 

                // 设置填充符号
                ISimpleFillSymbol pSimpleFillSymbol = pMoveEnvelopeFeedback.Symbol as ISimpleFillSymbol;
                pSimpleFillSymbol.Color = pFillRgbColor;
                pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;

                // 创建线颜色
                IRgbColor pLineRgbColor = new RgbColor();
                pLineRgbColor.Red = 0;
                pLineRgbColor.Green = 0;
                pLineRgbColor.Blue = 255;

                // 设置线符号
                ISimpleLineSymbol pSimpleLineSymbol = pSimpleFillSymbol.Outline as ISimpleLineSymbol;
                pSimpleLineSymbol.Color = pLineRgbColor;
                pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDashDotDot;
                pSimpleLineSymbol.Width = 5;
                pSimpleFillSymbol.Outline = pSimpleLineSymbol;
            }
        }

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

        public override void onm ouseUp(int Button, int Shift, int X, int Y)
        {
            if (pMoveGeometryFeedback != null)
            {
                pEndPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pMoveEnvelopeFeedback.Stop();
                pMoveGeometryFeedback.MoveTo(pEndPoint);
                pMoveSet.Reset();

                // 利用起点和终点构造一条线段,表示移动的方向
                ILine pLine = new Line();
                pLine.SpatialReference = m_hookHelper.FocusMap.SpatialReference;
                pLine.PutCoords(pStartPoint, pEndPoint);

                // 遍历被选择的要素
                IFeature pFeature = pMoveSet.Next() as IFeature;
                while (pFeature != null)
                {
                    IFeatureEdit pFeatureEdit = pFeature as IFeatureEdit;
                    pFeatureEdit.MoveSet(pMoveSet, pLine);
                    pFeature.Store();
                    pFeature = pMoveSet.Next() as IFeature;
                }

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

程序运行结果如下图所示:

在这里插入图片描述

标签:要素,registerType,void,ArcGIS,pFeature,hookHelper,模块,using,ArcEngine
来源: https://blog.csdn.net/HerryDong/article/details/121431511