liutingxiang的个人博客分享 http://blog.sciencenet.cn/u/liutingxiang

博文

ArcGIS Engine二次开发学习(15)自定义编辑工具栏

已有 675 次阅读 2018-6-14 11:06 |个人分类:地理信息系统二次开发|系统分类:教学心得

 设计编辑工具栏,如下图所示:



代码如下:


1、form1的load事件里添加:

currentTasks.Items.Add("创建要素");
currentTasks.Items.Add("编辑要素");


2、定义变量

//编辑工具栏变量
        private IEngineEditor m_engineEditor = new EngineEditorClass();
        private IEngineEditLayers m_EngineEditLayers;
        private IEngineEditTask pEngineEditCurentTask;
        private IWorkspace editWorkspace;
        private ArrayList workSpaceArrayList;


3、各个按钮的代码,调用的方法如下:

private void BeginEditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int featLayerCount = 0;
            ILayer pLayer;
            workSpaceArrayList = new ArrayList();
            for (int i = 0; i <= axMapControl1.Map.LayerCount - 1; i++)
            {
                pLayer = axMapControl1.Map.get_Layer(i);
                if (pLayer is IFeatureLayer)
                {
                    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                    pFeatureLayer.Selectable = false;
                    IDataset dataset = pFeatureLayer.FeatureClass as IDataset;
                    IWorkspace pWorkspace = dataset.Workspace;
                    if (!workSpaceArrayList.Contains(pWorkspace))
                    {
                        workSpaceArrayList.Add(pWorkspace);
                    }
                    featLayerCount++;
                }

            }
            if (featLayerCount == 0)
            {
                MessageBox.Show("请添加矢量图层后再启动编辑!", "信息提示");
                return;
            }
            IMap map;
            map = axMapControl1.Map;
            //此处可用窗体的进行选择一个工作控件,本例子默认找到的第一个工作空间
            editWorkspace = workSpaceArrayList[0] as IWorkspace;
            m_engineEditor.StartEditing(editWorkspace, map);
            m_EngineEditLayers = m_engineEditor as IEngineEditLayers;
            LoadLayers();
            currentTasks.Text = "创建要素";
            EditingStatesControls(true);
            
        }
        //改函数用于将当前地图中处于编辑工作空间内的所有要素图层添加到currentEditLayer控件中
        private void LoadLayers()
        {
            currentEditLayers.Items.Clear();
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                ILayer pLayer = axMapControl1.get_Layer(i);
                if (pLayer is IFeatureLayer)
                {
                    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                    IDataset dataset = pFeatureLayer.FeatureClass as IDataset;
                    if (dataset.Workspace == editWorkspace)
                    {
                        currentEditLayers.Items.Add(pLayer.Name);
                    }
                }
            }
            if (currentEditLayers.Items.Count > 0)
            {
                currentEditLayers.SelectedIndex = 0;
            }
        }

       private void SaveEditToolStripMenuItem_Click(object sender, EventArgs e)
        {
           ICommand pCommand = new ControlsEditingSaveCommandClass();
           pCommand.OnCreate(axMapControl1.Object);
           pCommand.OnClick();

        }

       private void StopEditToolStripMenuItem_Click(object sender, EventArgs e)
       {
           //监测是否发生编辑操作,没有就不保存直接停止
           if (m_engineEditor.HasEdits() == false)
               m_engineEditor.StopEditing(false);
           else
           {
               if (MessageBox.Show("是否需要对所做的编辑进行保存?", "消息提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                   m_engineEditor.StopEditing(true);
               else
                   m_engineEditor.StopEditing(false);
               
           }
           EditingStatesControls(false);
       }

       private void currentTasks_SelectedIndexChanged(object sender, EventArgs e)
       {
           pEngineEditCurentTask = m_engineEditor.CurrentTask;
           ICommand pCommand;
           switch (currentTasks.Text)
           {
               case "创建要素":
                   pCommand = new ControlsClearSelectionCommandClass();
                   pCommand.OnCreate(axMapControl1.Object);
                   pCommand.OnClick();
                   pCommand = new ESRI.ArcGIS.Controls.ControlsEditingSketchToolClass();
                   pCommand.OnCreate(axMapControl1.Object);
                   axMapControl1.CurrentTool = pCommand as ITool;
                   editToolButton.CheckState = CheckState.Unchecked;
                   break;
               case "编辑要素":
                   pCommand = new ESRI.ArcGIS.Controls.ControlsEditingEditToolClass();
                   pCommand.OnCreate(axMapControl1.Object);
                   axMapControl1.CurrentTool = pCommand as ITool;
                   editToolButton.CheckState = CheckState.Checked;
                   break;
               default :
                   pEngineEditCurentTask = null;
                   axMapControl1.CurrentTool = null;
                   break;

           }
           m_engineEditor.CurrentTask = pEngineEditCurentTask;
           axMapControl1.Focus();
       }

       private void tlBtnCopy_Click(object sender, EventArgs e)
       {
           ICommand pCommand = new ControlsEditingCopyCommandClass();
           pCommand.OnCreate(axMapControl1.Object);
           pCommand.OnClick();
       }

       private void tlBtnPaste_Click(object sender, EventArgs e)
       {
           ICommand pCommand = new ControlsEditingPasteCommandClass();
           pCommand.OnCreate(axMapControl1.Object);
           pCommand.OnClick();
       }

       private void tlBtnDelete_Click(object sender, EventArgs e)
       {
           ICommand pCommand = new ControlsEditingClearCommandClass();
           pCommand.OnCreate(axMapControl1.Object);
           pCommand.OnClick();
       }

       private void currentEditLayers_SelectedIndexChanged(object sender, EventArgs e)
       {
           for (int i = 0; i <= axMapControl1.Map.LayerCount - 1; i++)
           {
               ILayer pLayer = axMapControl1.Map.get_Layer(i);
               if (pLayer is IFeatureLayer)
               {
                   IFeatureLayer m_pCurrentLayer = pLayer as IFeatureLayer;
                   m_pCurrentLayer.Selectable = false;
                   IFeatureClass pFeatureClass = m_pCurrentLayer.FeatureClass;
                   if (currentEditLayers.SelectedItem.ToString() == pLayer.Name && pFeatureClass != null)
                   {
                       m_pCurrentLayer.Selectable = true;
                       if (m_engineEditor.EditWorkspace != null)
                       {
                           m_EngineEditLayers.SetTargetLayer(m_pCurrentLayer, 0);
                       }
                   }
               }
           }
           axMapControl1.Focus();

       }

       private void editToolButton_Click(object sender, EventArgs e)
       {
           currentTasks.Text = "编辑要素";
           editToolButton.CheckState = CheckState.Checked;

       }

       private void currentToolSketch_Click(object sender, EventArgs e)
       {
           currentTasks.Text = "创建要素";
           editToolButton.CheckState = CheckState.Unchecked;
       }
       private void EditingStatesControls(bool editTag)
       {
           StopEditToolStripMenuItem.Enabled = editTag;
           SaveEditToolStripMenuItem.Enabled = editTag;
           BeginEditToolStripMenuItem.Enabled = !editTag;
           editToolButton.Enabled = editTag;
           currentToolSketch.Enabled = editTag;
           tlBtnCopy.Enabled = editTag;
           tlBtnPaste.Enabled = editTag;
           tlBtnDelete.Enabled = editTag;
           currentTasks.Enabled = editTag;
           currentEditLayers.Enabled = editTag;
       }



问题:

(1)private IEngineEditor m_engineEditor = new EngineEditorClass();这句话如果报错,需要在program.cs的main第一行里添加

ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);

(2)添加system.collections的命名空间。



http://blog.sciencenet.cn/blog-3373120-1118908.html

上一篇:ArcGIS Engine二次开发学习(14)图层符号选择器的实现
下一篇:ArcGIS Engine二次开发学习(16)生成程序安装文件

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备14006957 )

GMT+8, 2018-8-20 07:38

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部