ArcEngine编辑功能的实现(二)

这里给出采用混合模式开发编辑工具条的完整代码,但是没有给出界面,相信熟悉ArcGIS操作的朋友一定可以设计出类似的界面。功能还有不少不完善的地方,希望感兴趣的朋友可以帮忙改进!

View Code
  1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using ESRI.ArcGIS.Controls;
10 using ESRI.ArcGIS.Carto;
11 using ESRI.ArcGIS.esriSystem;
12 using ESRI.ArcGIS.Geodatabase;
13 using System.Collections;
14 using ESRI.ArcGIS.SystemUI;
15 using MapDataViewAndControlLib;
16
17 namespace GEditor
18 {
19 public partial class EditFrm : Form
20 {
21 public EditFrm()
22 {
23 InitializeComponent();
24 }
25 private IEngineEditor m_EngineEditor = new EngineEditorClass();
26 private IEngineEditEvents_Event m_EngineEditEvent_Event;
27 private void Form1_Load(object sender, EventArgs e)
28 {
29 axTOCControl1.SetBuddyControl(axMapControl1);
30 axToolbarControl1.SetBuddyControl(axMapControl1);
31   
32 //添加与数据相关的工具条,包括打开文档、保存文档、添加数据、放大、缩小、漫游、全显、上一视图
33 axToolbarControl1.AddItem("esriControls.ControlsOpenDocCommand", 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
34 axToolbarControl1.AddItem("esriControls.ControlsSaveAsDocCommand", 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
35 axToolbarControl1.AddItem("esriControls.ControlsAddDataCommand", 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
36 axToolbarControl1.AddItem("esriControls.ControlsMapZoomInTool", 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly);
37 axToolbarControl1.AddItem("esriControls.ControlsMapZoomOutTool", 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
38 axToolbarControl1.AddItem("esriControls.ControlsMapPanTool", 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
39 axToolbarControl1.AddItem("esriControls.ControlsMapFullExtentCommand", 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
40 axToolbarControl1.AddItem("esriControls.ControlsMapZoomToLastExtentBackCommand", 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
41 axToolbarControl1.AddItem("esriControls.ControlsMapZoomToLastExtentForwardCommand", 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
42
43 m_Map = axMapControl1.Map;
44 InitializeEditorState();
45 }
46 private void InitializeEditorState()
47 {
48 tspCurrentTask.Items.Add("新建要素");
49 tspCurrentTask.Items.Add("修改要素");
50
51 tspCopy.Enabled = false;
52 tspDelete.Enabled = false;
53 tspPaste.Enabled = false;
54 tspRedo.Enabled = false;
55 tspUndo.Enabled = false;
56 tspSketch.Enabled = false;
57 tspEdit.Enabled = false;
58 tspSaveEdit.Enabled = false;
59 tspStopEdit.Enabled = false;
60 tspSketch.Enabled = false;
61 tspEdit.Enabled = false;
62 tspCurrentLayer.Enabled = false;
63 tspCurrentTask.Enabled = false;
64
65 }
66 IMap m_Map = null;
67 private void tspStartEdit_Click(object sender, EventArgs e)
68 {
69 #region 开始编辑状态
70 if (m_Map.LayerCount==0)
71 {
72 MessageBox.Show("没有图层!");
73 return;
74 }
75 for (int i = 0; i < m_Map.LayerCount ; i++)
76 {
77 ILayer pLayer = m_Map.get_Layer(i);
78 if (pLayer is IFeatureLayer)
79 {
80 tspCurrentLayer.Items.Add(pLayer.Name);
81
82 }
83 }
84
85 tspStartEdit.Enabled = false;
86 tspSaveEdit.Enabled = true;
87 tspStopEdit.Enabled = true;
88 tspSketch.Enabled = false;
89 tspEdit.Enabled = false;
90 tspCurrentLayer.Enabled = true;
91 tspCurrentTask.Enabled = true;
92 tspCurrentLayer.SelectedIndex = 0;
93 tspCurrentTask.SelectedIndex = 0;
94 if (m_EngineEditor.EditState == esriEngineEditState.esriEngineStateNotEditing)
95 {
96 //Start editing the workspace of the first feature layer found.
97 //设置编辑的WorkSpace和编辑对象图层
98 ILayer currentLayer = m_Map.get_Layer(0);
99 if (currentLayer is IFeatureLayer)
100 {
101 IFeatureLayer featureLayer = currentLayer as IFeatureLayer;
102 IDataset dataset = featureLayer.FeatureClass as IDataset;
103 IWorkspace workspace = dataset.Workspace;
104 m_EngineEditor.StartEditing(workspace, m_Map);
105 m_EngineEditEvent_Event = m_EngineEditor as IEngineEditEvents_Event;
106 m_EngineEditEvent_Event.OnCurrentTaskChanged += new IEngineEditEvents_OnCurrentTaskChangedEventHandler(m_EngineEditEvent_Event_OnCurrentTaskChanged);
107 m_EngineEditEvent_Event.OnSelectionChanged += new IEngineEditEvents_OnSelectionChangedEventHandler(m_EngineEditEvent_Event_OnSelectionChanged);
108 m_EngineEditEvent_Event.OnSketchModified += new IEngineEditEvents_OnSketchModifiedEventHandler(m_EngineEditEvent_Event_OnSketchModified);
109 }
110
111 }
112
113 #endregion
114 }
115 private void tspSaveEdit_Click(object sender, EventArgs e)
116 {
117 pCmd = new ESRI.ArcGIS.Controls.ControlsEditingSaveCommandClass();
118 pCmd.OnCreate(axMapControl1.Object);
119 axMapControl1.CurrentTool = pCmd as ITool;
120 pCmd.OnClick();
121 }
122 private void tspStopEdit_Click(object sender, EventArgs e)
123 {
124 if (m_EngineEditor.HasEdits() == false)
125 m_EngineEditor.StopEditing(false);
126 else
127 {
128 if (MessageBox.Show("保存编辑?", "保存选项", MessageBoxButtons.YesNo)
129 == DialogResult.Yes)
130 {
131 m_EngineEditor.StopEditing(true);
132
133 }
134 else
135 {
136 m_EngineEditor.StopEditing(false);
137
138 }
139 }
140 tspStartEdit.Enabled = true;
141 tspCopy.Enabled = false;
142 tspDelete.Enabled = false;
143 tspPaste.Enabled = false;
144 tspRedo.Enabled = false;
145 tspUndo.Enabled = false;
146 tspSketch.Enabled = false;
147 tspEdit.Enabled = false;
148 tspSaveEdit.Enabled = false;
149 tspStopEdit.Enabled = false;
150 tspSketch.Enabled = false;
151 tspEdit.Enabled = false;
152 tspCurrentLayer.SelectedIndex = -1;
153 tspCurrentTask.SelectedIndex = -1;
154 tspCurrentLayer.Enabled = false;
155 tspCurrentTask.Enabled = false;
156
157 }
158 private void tspEdit_Click(object sender, EventArgs e)
159 {
160 pCmd = new ControlsEditingEditToolClass();
161 pCmd.OnCreate(axMapControl1.Object);
162 axMapControl1.CurrentTool = pCmd as ITool;
163 pCmd.OnClick();
164
165
166 }
167
168 void m_EngineEditEvent_Event_OnSketchModified()
169 {
170
171 IEngineEditProperties ep = new EngineEditorClass();
172 ILayer m_pCurrentLayer = ep.TargetLayer;
173 if (m_pCurrentLayer == null) return;
174 IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pCurrentLayer;
175 IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
176 if (pDataset == null) return;
177 pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
178 bool bHasUndos = false;
179 pWorkspaceEdit.HasUndos(ref bHasUndos);
180 if (bHasUndos)
181 tspUndo.Enabled = true;
182 else
183 tspUndo.Enabled = false;
184
185 bool bHasRedos = false;
186 pWorkspaceEdit.HasRedos(ref bHasRedos);
187 if (bHasRedos)
188 tspRedo.Enabled = true;
189 else
190 tspRedo.Enabled = false;
191 }
192
193 void m_EngineEditEvent_Event_OnSelectionChanged()
194 {
195 if (m_EngineEditor.SelectionCount==0)
196 {
197 tspCopy.Enabled = false;
198 tspDelete.Enabled = false;
199 tspPaste.Enabled = false;
200 if (m_EngineEditor.CurrentTask.Name == "Modify Feature")
201 tspSketch.Enabled = false;
202 }
203 else
204 {
205 tspCopy.Enabled = true;
206 tspDelete.Enabled = true;
207 tspPaste.Enabled = true;
208 if (m_EngineEditor.CurrentTask.Name == "Modify Feature")
209 tspSketch.Enabled = true;
210 else if (m_EngineEditor.CurrentTask.Name == "Create New Feature")
211 tspSketch.Enabled = true;
212 }
213
214 }
215
216 void m_EngineEditEvent_Event_OnCurrentTaskChanged()
217 {
218 if (m_EngineEditor.CurrentTask.Name == "Create New Feature")
219 {
220 tspCurrentTask.SelectedIndex = 0;
221 }
222 else if (m_EngineEditor.CurrentTask.Name == "Modify Feature")
223 {
224 tspCurrentTask.SelectedIndex = 1;
225 }
226
227 MessageBox.Show(m_EngineEditor.CurrentTask.Name+m_EngineEditor.TaskCount.ToString());
228 }
229
230
231 private void tspSketch_Click(object sender, EventArgs e)
232 {
233 pCmd = new ControlsEditingSketchToolClass();
234 pCmd.OnCreate(axMapControl1.Object);
235 axMapControl1.CurrentTool = pCmd as ITool;
236 pCmd.OnClick();
237 }
238 private void tspRedo_Click(object sender, EventArgs e)
239 {
240 MapRedoEdit(m_Map);
241 bool bHasUndos = false;
242 pWorkspaceEdit.HasUndos(ref bHasUndos);
243 if (bHasUndos)
244 tspUndo.Enabled = true;
245 else
246 tspUndo.Enabled = false;
247
248 }
249 private void tspUndo_Click(object sender, EventArgs e)
250 {
251 MapUndoEdit(m_Map);
252 bool bHasRedos = false;
253 pWorkspaceEdit.HasRedos(ref bHasRedos);
254 if (bHasRedos)
255 tspRedo.Enabled = true;
256 else
257 tspRedo.Enabled = false;
258
259 }
260 private void tspCurrentTask_SelectedIndexChanged(object sender, EventArgs e)
261 {
262 switch (tspCurrentTask.Text)
263 {
264 case "新建要素":
265 SetCurrentTask(ref m_EngineEditor, "新建要素");
266 tspSketch.Enabled = true;
267 tspEdit.Enabled = true;
268
269 break;
270 case "修改要素":
271 SetCurrentTask(ref m_EngineEditor, "修改要素");
272 if (m_EngineEditor.SelectionCount==0)
273 {
274 tspSketch.Enabled = false;
275 tspEdit.Enabled = true;
276 }
277 else
278 {
279 tspSketch.Enabled = true;
280 tspEdit.Enabled = true;
281 }
282
283 break;
284
285 }
286 }
287 private void tspCurrentLayer_SelectedIndexChanged(object sender, EventArgs e)
288 {
289 if (tspCurrentLayer.SelectedIndex!=-1)
290 {
291 IFeatureLayer pCurrentLyr = m_Map.get_Layer(tspCurrentLayer.SelectedIndex) as IFeatureLayer;
292 ((IEngineEditLayers)m_EngineEditor).SetTargetLayer(pCurrentLyr, 0);
293 }
294
295 }
296 /// <summary>
297 /// 设置编辑的任务类型
298 /// </summary>
299 /// <param name="pEngineEditor"></param>
300 /// <param name="taskName"></param>自己命名的任务名称
301 public static void SetCurrentTask(ref IEngineEditor pEngineEditor, string taskName)
302 {
303
304 IEngineEditTask edittask;
305 if (pEngineEditor.EditState == esriEngineEditState.esriEngineStateEditing)
306 {
307 switch (taskName)
308 {
309 case "新建要素":
310 edittask = pEngineEditor.GetTaskByUniqueName("ControlToolsEditing_CreateNewFeatureTask");
311 if (edittask != null)
312 {
313 pEngineEditor.CurrentTask = edittask;
314 }
315 break;
316 case "修改图形":
317 edittask = pEngineEditor.GetTaskByUniqueName("ReshapePolylineEditTask_Reshape Polyline_CSharp");
318 if (edittask != null)
319 {
320 pEngineEditor.CurrentTask = edittask;
321 }
322 break;
323 case "修改要素":
324 edittask = pEngineEditor.GetTaskByUniqueName("ControlToolsEditing_ModifyFeatureTask");
325 if (edittask != null)
326 {
327 pEngineEditor.CurrentTask = edittask;
328 }
329 break;
330
331 }
332 }
333 }
334 /// <summary>
335 /// 撒消编辑
336 /// </summary>
337 /// <param name="m_pMap">IMap 地图对象</param>
338 public void MapUndoEdit(IMap m_pMap)
339 {
340 bool bHasUndos = false;
341 pWorkspaceEdit.HasUndos(ref bHasUndos);
342 if (bHasUndos)
343 pWorkspaceEdit.UndoEditOperation();
344 IActiveView pActiveView = (IActiveView)m_pMap;
345 pActiveView.Refresh();
346
347 }
348 IWorkspaceEdit pWorkspaceEdit = null;
349 /// <summary>
350 /// 重做
351 /// </summary>
352 /// <param name="m_pMap">IMap 地图对象</param>
353 public void MapRedoEdit(IMap m_pMap)
354 {
355 bool bHasRedos = false;
356 pWorkspaceEdit.HasRedos(ref bHasRedos);
357 if (bHasRedos)
358 pWorkspaceEdit.RedoEditOperation();
359 IActiveView pActiveView = (IActiveView)m_pMap;
360 pActiveView.Refresh();
361 }
362 ICommand pCmd = null;
363 private void tspDelete_Click(object sender, EventArgs e)
364 {
365 pCmd = new ESRI.ArcGIS.Controls.ControlsEditingClearCommandClass();
366 pCmd.OnCreate(axMapControl1.Object);
367 axMapControl1.CurrentTool = pCmd as ITool;
368 pCmd.OnClick();
369
370 }
371
372 private void tspCopy_Click(object sender, EventArgs e)
373 {
374 pCmd = new ControlsEditingCopyCommandClass();
375 pCmd.OnCreate(axMapControl1.Object);
376 axMapControl1.CurrentTool = pCmd as ITool;
377 pCmd.OnClick();
378
379 }
380
381 private void tspPaste_Click(object sender, EventArgs e)
382 {
383 pCmd = new ControlsEditingPasteCommandClass();
384 pCmd.OnCreate(axMapControl1.Object);
385 axMapControl1.CurrentTool = pCmd as ITool;
386 pCmd.OnClick();
387 }
388
389 private void tspEditAttribute_Click(object sender, EventArgs e)
390 {
391 pCmd = new ControlsEditingAttributeCommandClass();
392 pCmd.OnCreate(axMapControl1.Object);
393 axMapControl1.CurrentTool = pCmd as ITool;
394 pCmd.OnClick();
395 }
396
397 }
398 }

声明:该程序是本人利用周末时间开发的程序,大家可以用作学习之用。

感谢http://www.gisall.com/html/52/25952-2752.html lifengling353博主,文中很多代码都借鉴了你的博文!

posted @ 2012-02-27 11:39  太一吾鱼水  阅读(6148)  评论(11编辑  收藏  举报