ArcEngine编辑模块——批量删除要素

1、前言

ArcEngine中,删除要素的方法有很多,你可以使用IFeatureCursorITable查询出部分要素然后依次删除。但这两个接口只能针对单个图层的要素进行删除,而在编辑状态下,我们可能一次选中了多个图层下的多个要素,这时候就得使用IFeature接口的Delete方法进行操作,下面给出实现代码。

2、删除要素

2.1、主界面代码

using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using System;
using System.Windows.Forms;
using WindowsFormsApplication1.Command;

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

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

            btnStartEditing.Enabled = false;
            btnStopEditing.Enabled = true;
            btnSelect.Enabled = true;
            btnDelete.Enabled = true;
        }

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

            ICommand clearSelectionCommand = new ControlsClearSelectionCommand();
            clearSelectionCommand.OnCreate(axMapControl1.Object);
            clearSelectionCommand.OnClick();

            btnStartEditing.Enabled = true;
            btnStopEditing.Enabled = false;
            btnSelect.Enabled = false;
            btnDelete.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 btnDelete_Click(object sender, EventArgs e)
        {
            ICommand command = new DeleteFeaturesCommand();
            command.OnCreate(axMapControl1.Object);
            command.OnClick();
        }
    }
}

2.2、删除要素代码

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

namespace WindowsFormsApplication1.Command
{
    /// 
    /// Summary description for DeleteFeaturesCommand.
    /// 
    [Guid("c6662b93-3eaf-4615-acaa-ffe0de2a51f8")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("WindowsFormsApplication1.Command.DeleteFeaturesCommand")]
    public sealed class DeleteFeaturesCommand : 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
        /// 
        /// 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);
            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);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;

        public DeleteFeaturesCommand()
        {
            //
            // 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

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

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

            m_hookHelper.Hook = hook;
        }

        /// 
        /// Occurs when this command is clicked
        /// 
        public override void OnClick()
        {
            if (m_hookHelper.FocusMap.SelectionCount == 0)
            {
                MessageBox.Show("请选择需要旋转的要素", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 获取被选择的要素
            IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
            pEnumFeature.Reset();
            IFeature pFeature = pEnumFeature.Next();
            while (pFeature != null)
            {
                pFeature.Delete();
                pFeature = pEnumFeature.Next();
            }

            // 刷新地图
            m_hookHelper.ActiveView.Refresh();
        }
        #endregion
    }
}

运行结果如下图所示:
ArcEngine编辑模块——批量删除要素_第1张图片

你可能感兴趣的:(C#,ArcEngine,C#,ArcEngine)