ArcEngine编辑功能(四)

前面实现的编辑功能存在以下问题:

没有与属性表编辑联动,对于分组图层无法编辑,没有考虑不同工作空间的情况,没有考虑版本的问题,没有设置右键菜单……

今天解决没有与属性表编辑联动,对于分组图层无法编辑两个问题:

1. 实现与界面分离的编辑事件类,只需传入编辑工具条按钮进行构造,执行OnCreate()方法。

View Code
  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using ESRI.ArcGIS.Controls;
6 using ESRI.ArcGIS.Carto;
7 using ESRI.ArcGIS.Geodatabase;
8 using ESRI.ArcGIS.SystemUI;
9 using System.Windows.Forms;
10 using DevComponents.DotNetBar;
11 namespace HouseMange
12 {
13 class GEditEvents
14 {
15 ButtonItem tspStartEdit;
16 ButtonItem tspSaveEdit;
17 ButtonItem tspStopEdit;
18 ButtonItem tspEdit;
19 ButtonItem tspSketch;
20 ComboBoxItem tspCurrentLayer;
21 ComboBoxItem tspCurrentTask;
22 ButtonItem tspEditAttribute;
23 ButtonItem tspEditXYZ;
24 ButtonItem tspRedo;
25 ButtonItem tspUndo;
26 ButtonItem tspCopy;
27 ButtonItem tspPaste;
28 ButtonItem tspDelete;
29 ButtonItem OpenAttributeTable;
30 ButtonItem tspSnap;
31 private IEngineEditor m_EngineEditor = new EngineEditorClass();
32 private IEngineEditEvents_Event m_EngineEditEvent_Event;
33 AxMapControl axMapControl1;
34 IMap m_Map = null;
35 /// <summary>
36 /// 构造函数
37 /// </summary>
38 /// <param name="axMapControl1"></param>
39 /// <param name="tsp_StartEdit"></param>
40 /// <param name="tsp_SaveEdit"></param>
41 /// <param name="tsp_StopEdit"></param>
42 /// <param name="tsp_Edit"></param>
43 /// <param name="tsp_Sketch"></param>
44 /// <param name="tsp_Copy"></param>
45 /// <param name="tsp_Paste"></param>
46 /// <param name="tsp_Delete"></param>
47 /// <param name="tsp_Redo"></param>
48 /// <param name="tsp_Undo"></param>
49 /// <param name="tsp_CurrentLayer"></param>
50 /// <param name="tsp_CurrentTask"></param>
51 /// <param name="tsp_EditAttribute"></param>
52 /// <param name="tsp_EditXYZ"></param>
53 /// <param name="_OpenAttributeTable"></param>
54 /// <param name="tspSnap"></param>
55 public GEditEvents(AxMapControl _MapControl,
56 ButtonItem tsp_StartEdit,
57 ButtonItem tsp_SaveEdit,
58 ButtonItem tsp_StopEdit,
59 ButtonItem tsp_Snap,
60 ButtonItem tsp_Edit,
61 ButtonItem tsp_Sketch,
62
63 ButtonItem tsp_Copy,
64 ButtonItem tsp_Paste,
65 ButtonItem tsp_Delete,
66 ButtonItem tsp_Redo,
67 ButtonItem tsp_Undo,
68
69 ComboBoxItem tsp_CurrentLayer,
70 ComboBoxItem tsp_CurrentTask,
71 ButtonItem tsp_EditAttribute,
72 ButtonItem tsp_EditXYZ,
73 ButtonItem _OpenAttributeTable)
74 {
75 axMapControl1 = _MapControl;
76 m_Map = axMapControl1.Map as IMap;
77 tspStartEdit = tsp_StartEdit;
78 tspSaveEdit = tsp_SaveEdit;
79 tspStopEdit=tsp_StopEdit;
80 tspSnap = tsp_Snap;
81 tspEdit = tsp_Edit;
82 tspSketch = tsp_Sketch;
83
84 tspCopy = tsp_Copy;
85 tspPaste = tsp_Paste;
86 tspDelete=tsp_Delete;
87 tspRedo=tsp_Redo;
88 tspUndo = tsp_Undo;
89
90 tspCurrentLayer = tsp_CurrentLayer;
91 tspCurrentTask = tsp_CurrentTask;
92 tspEditAttribute = tsp_EditAttribute;
93 tspEditXYZ = tsp_EditXYZ;
94 OpenAttributeTable = _OpenAttributeTable;
95
96 }
97 public void OnCreate()
98 {
99
100 axMapControl1.OnMapReplaced += new IMapControlEvents2_Ax_OnMapReplacedEventHandler(axMapControl1_OnMapReplaced);
101 tspStartEdit.Click += new EventHandler(tspStartEdit_Click);
102 tspSaveEdit.Click += new EventHandler(tspSaveEdit_Click);
103 tspStopEdit.Click += new EventHandler(tspStopEdit_Click);
104 tspSnap.Click += new EventHandler(tspSnap_Click);
105 tspCopy.Click += new EventHandler(tspCopy_Click);
106 tspPaste.Click += new EventHandler(tspPaste_Click);
107 tspDelete.Click += new EventHandler(tspDelete_Click);
108 tspEdit.Click += new EventHandler(tspEdit_Click);
109 tspSketch.Click += new EventHandler(tspSketch_Click);
110 tspCurrentLayer.SelectedIndexChanged += new EventHandler(tspCurrentLayer_SelectedIndexChanged);
111 tspCurrentTask.SelectedIndexChanged += new EventHandler(tspCurrentTask_SelectedIndexChanged);
112 tspEditAttribute.Click += new EventHandler(tspEditAttribute_Click);
113 InitializeEditorState();
114 }
115
116 void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
117 {
118 m_Map = axMapControl1.Map as IMap;
119 }
120 private void InitializeEditorState()
121 {
122 tspCurrentTask.Items.Add("新建要素");
123 tspCurrentTask.Items.Add("修改要素");
124
125 tspCopy.Enabled = false;
126 tspDelete.Enabled = false;
127 tspPaste.Enabled = false;
128 tspRedo.Enabled = false;
129 tspUndo.Enabled = false;
130 tspSketch.Enabled = false;
131 tspEdit.Enabled = false;
132 tspSaveEdit.Enabled = false;
133 tspStopEdit.Enabled = false;
134 tspSnap.Enabled = false;
135 tspSketch.Enabled = false;
136 tspEdit.Enabled = false;
137 tspCurrentLayer.Enabled = false;
138 tspCurrentTask.Enabled = false;
139
140 }
141
142 private void tspStartEdit_Click(object sender, EventArgs e)
143 {
144 #region 开始编辑状态
145 if (m_Map.LayerCount == 0)
146 {
147 MessageBox.Show("没有图层!");
148 return;
149 }
150 for (int i = 0; i < m_Map.LayerCount; i++)
151 {
152 ILayer pLayer = m_Map.get_Layer(i);
153 if (pLayer is IFeatureLayer)
154 {
155 tspCurrentLayer.Items.Add(pLayer.Name);
156 }
157 else if (pLayer is IGroupLayer)
158 {
159 ICompositeLayer pComposLyr = pLayer as ICompositeLayer;
160 for (int j = 0; j < pComposLyr.Count;j++ )
161 {
162 ILayer pCompospLayer = pComposLyr.get_Layer(j);
163 if (pCompospLayer is IFeatureLayer)
164 {
165 tspCurrentLayer.Items.Add(pCompospLayer.Name);
166 }
167 }
168 }
169 }
170
171 tspStartEdit.Enabled = false;
172 tspSaveEdit.Enabled = true;
173 tspStopEdit.Enabled = true;
174 tspSnap.Enabled = true;
175 tspSketch.Enabled = false;
176 tspEdit.Enabled = false;
177 tspCurrentLayer.Enabled = true;
178 tspCurrentTask.Enabled = true;
179 tspCurrentLayer.SelectedIndex = 0;
180 tspCurrentTask.SelectedIndex = 0;
181 if (m_EngineEditor.EditState == esriEngineEditState.esriEngineStateNotEditing)
182 {
183 //Start editing the workspace of the first feature layer found.
184 //设置编辑的WorkSpace和编辑对象图层
185 ILayer currentLayer = m_Map.get_Layer(0);
186 if (currentLayer is IFeatureLayer)
187 {
188 IFeatureLayer featureLayer = currentLayer as IFeatureLayer;
189 IDataset dataset = featureLayer.FeatureClass as IDataset;
190 IWorkspace workspace = dataset.Workspace;
191 m_EngineEditor.StartEditing(workspace, m_Map);
192 m_EngineEditEvent_Event = m_EngineEditor as IEngineEditEvents_Event;
193 m_EngineEditEvent_Event.OnCurrentTaskChanged += new IEngineEditEvents_OnCurrentTaskChangedEventHandler(m_EngineEditEvent_Event_OnCurrentTaskChanged);
194 m_EngineEditEvent_Event.OnSelectionChanged += new IEngineEditEvents_OnSelectionChangedEventHandler(m_EngineEditEvent_Event_OnSelectionChanged);
195 m_EngineEditEvent_Event.OnSketchModified += new IEngineEditEvents_OnSketchModifiedEventHandler(m_EngineEditEvent_Event_OnSketchModified);
196 }
197 else if (currentLayer is ICompositeLayer)
198 {
199 ICompositeLayer pCompos = currentLayer as ICompositeLayer;
200 currentLayer = pCompos.get_Layer(0);
201 IFeatureLayer featureLayer = currentLayer as IFeatureLayer;
202 IDataset dataset = featureLayer.FeatureClass as IDataset;
203 IWorkspace workspace = dataset.Workspace;
204 m_EngineEditor.StartEditing(workspace, m_Map);
205 m_EngineEditEvent_Event = m_EngineEditor as IEngineEditEvents_Event;
206 m_EngineEditEvent_Event.OnCurrentTaskChanged += new IEngineEditEvents_OnCurrentTaskChangedEventHandler(m_EngineEditEvent_Event_OnCurrentTaskChanged);
207 m_EngineEditEvent_Event.OnSelectionChanged += new IEngineEditEvents_OnSelectionChangedEventHandler(m_EngineEditEvent_Event_OnSelectionChanged);
208 m_EngineEditEvent_Event.OnSketchModified += new IEngineEditEvents_OnSketchModifiedEventHandler(m_EngineEditEvent_Event_OnSketchModified);
209 }
210
211 }
212
213 #endregion
214 }
215 private void tspSaveEdit_Click(object sender, EventArgs e)
216 {
217 pCmd = new ESRI.ArcGIS.Controls.ControlsEditingSaveCommandClass();
218 pCmd.OnCreate(axMapControl1.Object);
219 axMapControl1.CurrentTool = pCmd as ITool;
220 pCmd.OnClick();
221 }
222 private void tspStopEdit_Click(object sender, EventArgs e)
223 {
224 if (m_EngineEditor.HasEdits() == false)
225 m_EngineEditor.StopEditing(false);
226 else
227 {
228 DialogResult dr=MessageBox.Show("是否保存编辑?", "保存选项", MessageBoxButtons.YesNoCancel);
229 if (dr == DialogResult.Yes)
230 {
231 m_EngineEditor.StopEditing(true);
232
233 }
234 else if (dr == DialogResult.No)
235 {
236 m_EngineEditor.StopEditing(false);
237
238 }
239 else
240 return;
241 }
242 tspStartEdit.Enabled = true;
243 tspCopy.Enabled = false;
244 tspDelete.Enabled = false;
245 tspPaste.Enabled = false;
246 tspRedo.Enabled = false;
247 tspUndo.Enabled = false;
248 tspSketch.Enabled = false;
249 tspEdit.Enabled = false;
250 tspSaveEdit.Enabled = false;
251 tspStopEdit.Enabled = false;
252 tspSnap.Enabled = false;
253 tspSketch.Enabled = false;
254 tspEdit.Enabled = false;
255 tspCurrentLayer.SelectedIndex = -1;
256 tspCurrentTask.SelectedIndex = -1;
257 tspCurrentLayer.Enabled = false;
258 tspCurrentTask.Enabled = false;
259
260 }
261 private void tspEdit_Click(object sender, EventArgs e)
262 {
263 pCmd = new ControlsEditingEditToolClass();
264 pCmd.OnCreate(axMapControl1.Object);
265 axMapControl1.CurrentTool = pCmd as ITool;
266 pCmd.OnClick();
267
268
269 }
270
271 void m_EngineEditEvent_Event_OnSketchModified()
272 {
273
274 IEngineEditProperties ep = new EngineEditorClass();
275 ILayer m_pCurrentLayer = ep.TargetLayer;
276 if (m_pCurrentLayer == null) return;
277 IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pCurrentLayer;
278 IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
279 if (pDataset == null) return;
280 pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
281 bool bHasUndos = false;
282 pWorkspaceEdit.HasUndos(ref bHasUndos);
283 if (bHasUndos)
284 tspUndo.Enabled = true;
285 else
286 tspUndo.Enabled = false;
287
288 bool bHasRedos = false;
289 pWorkspaceEdit.HasRedos(ref bHasRedos);
290 if (bHasRedos)
291 tspRedo.Enabled = true;
292 else
293 tspRedo.Enabled = false;
294 }
295
296 void m_EngineEditEvent_Event_OnSelectionChanged()
297 {
298 if (m_EngineEditor.SelectionCount == 0)
299 {
300 tspCopy.Enabled = false;
301 tspDelete.Enabled = false;
302 tspPaste.Enabled = false;
303 if (m_EngineEditor.CurrentTask.Name == "Modify Feature")
304 tspSketch.Enabled = false;
305 }
306 else
307 {
308 tspCopy.Enabled = true;
309 tspDelete.Enabled = true;
310 tspPaste.Enabled = true;
311 if (m_EngineEditor.CurrentTask.Name == "Modify Feature")
312 tspSketch.Enabled = true;
313 else if (m_EngineEditor.CurrentTask.Name == "Create New Feature")
314 tspSketch.Enabled = true;
315 }
316
317 }
318
319 void m_EngineEditEvent_Event_OnCurrentTaskChanged()
320 {
321 if (m_EngineEditor.CurrentTask.Name == "Create New Feature")
322 {
323 tspCurrentTask.SelectedIndex = 0;
324 }
325 else if (m_EngineEditor.CurrentTask.Name == "Modify Feature")
326 {
327 tspCurrentTask.SelectedIndex = 1;
328 }
329 //MessageBox.Show(m_EngineEditor.CurrentTask.Name + m_EngineEditor.TaskCount.ToString());
330 }
331
332
333 private void tspSketch_Click(object sender, EventArgs e)
334 {
335 pCmd = new ControlsEditingSketchToolClass();
336 pCmd.OnCreate(axMapControl1.Object);
337 axMapControl1.CurrentTool = pCmd as ITool;
338 pCmd.OnClick();
339 }
340 private void tspRedo_Click(object sender, EventArgs e)
341 {
342 MapRedoEdit(m_Map);
343 bool bHasUndos = false;
344 pWorkspaceEdit.HasUndos(ref bHasUndos);
345 if (bHasUndos)
346 tspUndo.Enabled = true;
347 else
348 tspUndo.Enabled = false;
349
350 }
351 private void tspUndo_Click(object sender, EventArgs e)
352 {
353 MapUndoEdit(m_Map);
354 bool bHasRedos = false;
355 pWorkspaceEdit.HasRedos(ref bHasRedos);
356 if (bHasRedos)
357 tspRedo.Enabled = true;
358 else
359 tspRedo.Enabled = false;
360
361 }
362 private void tspCurrentTask_SelectedIndexChanged(object sender, EventArgs e)
363 {
364 switch (tspCurrentTask.Text)
365 {
366 case "新建要素":
367 SetCurrentTask(ref m_EngineEditor, "新建要素");
368 tspSketch.Enabled = true;
369 tspEdit.Enabled = true;
370
371 break;
372 case "修改要素":
373 SetCurrentTask(ref m_EngineEditor, "修改要素");
374 if (m_EngineEditor.SelectionCount == 0)
375 {
376 tspSketch.Enabled = false;
377 tspEdit.Enabled = true;
378 }
379 else
380 {
381 tspSketch.Enabled = true;
382 tspEdit.Enabled = true;
383 }
384
385 break;
386
387 }
388 }
389 private void tspCurrentLayer_SelectedIndexChanged(object sender, EventArgs e)
390 {
391 if (tspCurrentLayer.SelectedIndex != -1)
392 {
393 IFeatureLayer pCurrentLyr = m_Map.get_Layer(tspCurrentLayer.SelectedIndex) as IFeatureLayer;
394 ((IEngineEditLayers)m_EngineEditor).SetTargetLayer(pCurrentLyr, 0);
395 }
396
397 }
398 /// <summary>
399 /// 设置编辑的任务类型
400 /// </summary>
401 /// <param name="pEngineEditor"></param>
402 /// <param name="taskName"></param>自己命名的任务名称
403 public void SetCurrentTask(ref IEngineEditor pEngineEditor, string taskName)
404 {
405
406 IEngineEditTask edittask;
407 if (pEngineEditor.EditState == esriEngineEditState.esriEngineStateEditing)
408 {
409 switch (taskName)
410 {
411 case "新建要素":
412 edittask = pEngineEditor.GetTaskByUniqueName("ControlToolsEditing_CreateNewFeatureTask");
413 if (edittask != null)
414 {
415 pEngineEditor.CurrentTask = edittask;
416 }
417 break;
418 case "修改图形":
419 edittask = pEngineEditor.GetTaskByUniqueName("ReshapePolylineEditTask_Reshape Polyline_CSharp");
420 if (edittask != null)
421 {
422 pEngineEditor.CurrentTask = edittask;
423 }
424 break;
425 case "修改要素":
426 edittask = pEngineEditor.GetTaskByUniqueName("ControlToolsEditing_ModifyFeatureTask");
427 if (edittask != null)
428 {
429 pEngineEditor.CurrentTask = edittask;
430 }
431 break;
432
433 }
434 }
435 }
436 /// <summary>
437 /// 撒消编辑
438 /// </summary>
439 /// <param name="m_pMap">IMap 地图对象</param>
440 void MapUndoEdit(IMap m_pMap)
441 {
442 bool bHasUndos = false;
443 pWorkspaceEdit.HasUndos(ref bHasUndos);
444 if (bHasUndos)
445 pWorkspaceEdit.UndoEditOperation();
446 IActiveView pActiveView = (IActiveView)m_pMap;
447 pActiveView.Refresh();
448
449 }
450 IWorkspaceEdit pWorkspaceEdit = null;
451 /// <summary>
452 /// 重做
453 /// </summary>
454 /// <param name="m_pMap">IMap 地图对象</param>
455 void MapRedoEdit(IMap m_pMap)
456 {
457 bool bHasRedos = false;
458 pWorkspaceEdit.HasRedos(ref bHasRedos);
459 if (bHasRedos)
460 pWorkspaceEdit.RedoEditOperation();
461 IActiveView pActiveView = (IActiveView)m_pMap;
462 pActiveView.Refresh();
463 }
464 ESRI.ArcGIS.SystemUI.ICommand pCmd = null;
465 private void tspDelete_Click(object sender, EventArgs e)
466 {
467 pCmd = new ESRI.ArcGIS.Controls.ControlsEditingClearCommandClass();
468 pCmd.OnCreate(axMapControl1.Object);
469 axMapControl1.CurrentTool = pCmd as ITool;
470 pCmd.OnClick();
471
472 }
473
474 private void tspCopy_Click(object sender, EventArgs e)
475 {
476 pCmd = new ControlsEditingCopyCommandClass();
477 pCmd.OnCreate(axMapControl1.Object);
478 axMapControl1.CurrentTool = pCmd as ITool;
479 pCmd.OnClick();
480
481 }
482
483 private void tspPaste_Click(object sender, EventArgs e)
484 {
485 pCmd = new ControlsEditingPasteCommandClass();
486 pCmd.OnCreate(axMapControl1.Object);
487 axMapControl1.CurrentTool = pCmd as ITool;
488 pCmd.OnClick();
489 }
490 private void tspSnap_Click(object sender, EventArgs e)
491 {
492 pCmd = new ControlsEditingSnappingCommandClass();
493 pCmd.OnCreate(axMapControl1.Object);
494 axMapControl1.CurrentTool = pCmd as ITool;
495 pCmd.OnClick();
496 }
497 private void tspEditAttribute_Click(object sender, EventArgs e)
498 {
499 pCmd = new ControlsEditingAttributeCommandClass();
500 pCmd.OnCreate(axMapControl1.Object);
501 axMapControl1.CurrentTool = pCmd as ITool;
502 pCmd.OnClick();
503 }
504 ILayer CurrentLayer = null;
505
506 //打开属性表菜单
507
508
509 }
510 }

2.修改属性窗体,这里双击dataGridView1的RowHeader,将刷新要素,同时放大的要素的范围。要素选择改变时会改变属性表中记录的颜色。开始编辑dataGridView1的ColumnHeader的字体会倾斜。这都要归功于为事件订阅新的方法。

View Code
  1 // Copyright 2008 ESRI
2 //
3 // All rights reserved under the copyright laws of the United States
4 // and applicable international laws, treaties, and conventions.
5 //
6 // You may freely redistribute and use this sample code, with or
7 // without modification, provided you include the original copyright
8 // notice and use restrictions.
9 //
10 // See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
11 //
12
13 using System;
14 using System.Collections.Generic;
15 using System.ComponentModel;
16 using System.Drawing;
17 using System.Text;
18 using System.Windows.Forms;
19 using ESRI.ArcGIS.Geodatabase;
20 //using ESRI.ArcGIS.DataSourcesGDB;
21 using ESRI.ArcGIS.Carto;
22 using ESRI.ArcGIS.Controls;
23 using ESRI.ArcGIS.Geometry;
24 using ESRI.ArcGIS.Display;
25
26 namespace GEditor
27 {
28 /// <summary>
29 /// This class demonstrates how to bind an ITable to Windows Forms controls.
30 /// </summary>
31 /// <remarks>
32 /// The form this class defines contains a DataGridView, a TextBox and a
33 /// BindingNavigator. The class will load the test data and bind it to both
34 /// the TextBox and the DataGridView by using a BindingSource. The use of a
35 /// BindingSource allows us to bind the test data to a single object that will
36 /// act as a datasource for the grid, the text box and the navigation control.
37 /// This allows changes made in one control to be reflected in the other controls
38 /// with minimal coding effort. This is most clearly demonstrated by using the
39 /// navigation control to step backwards and forwards through the grid rows
40 /// while also showing the value of a single field in the current row in the
41 /// text box.
42 /// </remarks>
43 public partial class AttributeToControl : Form
44 {
45 #region Private Memebers
46 /// <summary>
47 /// This is used to bind our ITable to the binding source. We need to keep
48 /// a reference to it as we will need to re-attach it to the binding source
49 /// to force a refresh whenever we change from displaying coded value domain
50 /// values to displaying their text equivalents and vice versa.
51 /// </summary>
52 private ArcDataBinding.TableWrapper tableWrapper;
53
54 /// <summary>
55 /// This binding object sets the data member within the data source for the
56 /// text box. We need to keep a reference to this as it needs to be reset
57 /// whenever viewing of coded value domains is changed.
58 /// </summary>
59 private Binding txtBoxBinding;
60 #endregion Private Memebers
61
62 #region Construction/Destruction
63 public AttributeToControl()
64 {
65 InitializeComponent();
66 }
67 #endregion Construction/Destruction
68
69 #region 私有变量
70 IFeatureLayer m_FeatureLayer = null;
71 IEngineEditor m_pEngineEditor = null;
72 IMapControl4 m_MapCtrl = null;
73
74 public IMapControl4 MapCtrl
75 {
76 get { return m_MapCtrl; }
77 set { m_MapCtrl = value; }
78 }
79 public IFeatureLayer CurrentFeatureLayer
80 {
81 get { return m_FeatureLayer; }
82 set { m_FeatureLayer = value; }
83 }
84 IFeatureClass m_FeatureClass = null;
85 #endregion
86 #region Private Event Handlers
87 private void MainWnd_Load(object sender, EventArgs e)
88 {
89 // Get workspace and open mdb file
90 //IWorkspaceFactory2 wkspcFactory = (IWorkspaceFactory2)new FileGDBWorkspaceFactoryClass();
91 //IFeatureWorkspace wkspc = wkspcFactory.OpenFromFile(
92 //@"..\..\..\..\..\..\data\CreateNetworkDataset\Paris.gdb",
93 //Handle.ToInt32()) as IFeatureWorkspace;
94
95 ////open the Geodatabase table
96 //ITable foundITable = wkspc.OpenFeatureClass("Streets") as ITable;
97 if ( m_FeatureLayer==null)
98 {
99 return;
100 }
101 m_FeatureClass = m_FeatureLayer.FeatureClass;
102 m_pEngineEditor=new EngineEditorClass();
103 IEngineEditLayers engineEditLayers = m_pEngineEditor as IEngineEditLayers;
104 IFeatureLayer featLyr = engineEditLayers.TargetLayer;
105
106
107 ITable foundITable = m_FeatureClass as ITable;
108 if (null != foundITable)
109 {
110 // Bind dataset to the binding source
111 tableWrapper = new ArcDataBinding.TableWrapper(foundITable);
112 bindingSource1.DataSource = tableWrapper;
113
114 // Bind binding source to grid. Alternatively it is possible to bind TableWrapper
115 // directly to the grid to this offers less flexibility
116 dataGridView1.DataSource = bindingSource1;
117
118 // Bind binding source to text box, we are binding the FULL_NAME
119 // field.
120 txtBoxBinding = new Binding("Text", bindingSource1, "FID");
121 textBox1.DataBindings.Add(txtBoxBinding);
122 }
123 if (m_pEngineEditor.EditState == esriEngineEditState.esriEngineStateNotEditing)
124 {
125 dataGridView1.ReadOnly = true;
126 }
127 else
128 {
129 dataGridView1.ReadOnly = false;
130 }
131 IEngineEditEvents_Event m_EngineEditEvent_Event = m_pEngineEditor as IEngineEditEvents_Event;
132 m_EngineEditEvent_Event.OnStartEditing += new IEngineEditEvents_OnStartEditingEventHandler(m_EngineEditEvent_Event_OnStartEditing);
133 m_EngineEditEvent_Event.OnStopEditing += new IEngineEditEvents_OnStopEditingEventHandler(m_EngineEditEvent_Event_OnStopEditing);
134 dataGridView1.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseDoubleClick);
135 IMapControlEvents2_Event m_MapcontrolEvent = m_MapCtrl as IMapControlEvents2_Event;
136 m_MapcontrolEvent.OnSelectionChanged += new IMapControlEvents2_OnSelectionChangedEventHandler(m_MapcontrolEvent_OnSelectionChanged);
137
138 }
139
140 void m_MapcontrolEvent_OnSelectionChanged()
141 {
142 IFeatureSelection featureSelection = m_FeatureLayer as IFeatureSelection;
143 ISelectionSet selectionSet = featureSelection.SelectionSet;
144 IEnumIDs selectIDs = selectionSet.IDs;
145
146 System.Collections.Generic.List<int> constructIDList = new System.Collections.Generic.List<int>();
147 int iD = selectIDs.Next();
148 while (iD != -1) //-1 is reutned after the last valid ID has been reached
149 {
150 constructIDList.Add(iD);
151 iD = selectIDs.Next();
152 }
153 foreach (DataGridViewRow perRow in dataGridView1.Rows)
154 {
155 int tempInt = Convert.ToInt32(perRow.Cells[0].Value);
156 if (constructIDList.Contains(tempInt))
157 {
158 perRow.DefaultCellStyle.BackColor = System.Drawing.Color.DeepSkyBlue;
159 }
160 else if (perRow.DefaultCellStyle.BackColor != dataGridView1.DefaultCellStyle.BackColor)
161 {
162 perRow.DefaultCellStyle.BackColor = dataGridView1.DefaultCellStyle.BackColor;
163 }
164 }
165 }
166 public IRgbColor SetColor(int r, int g, int b)
167 {
168 IRgbColor pColor;
169 pColor = new RgbColorClass();
170 pColor.Red = r;
171 pColor.Green = g;
172 pColor.Blue = b;
173 return pColor;
174 }
175
176 public void FlashFeature(IFeature pFeature, IMapControl4 pMapControl, bool isShowLine)
177 {
178 ICartographicLineSymbol pCartographicLineSymbol; //线性符号
179 ISimpleFillSymbol pSimpleFillSymbol; //填充
180 ISimpleMarkerSymbol pSimpleMarkersymbol;
181 ISymbol pSymbol = null;
182 int Size = 5;
183 IGeometry pGeometry;
184
185 IGeometryCollection pGeometryCollection = new PolygonClass();
186 pGeometry = pFeature.Shape;
187 if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
188 {
189 pGeometryCollection = new PolylineClass();
190 pGeometry = pFeature.Shape;
191 pGeometryCollection.AddGeometryCollection((IGeometryCollection)pGeometry);
192 pCartographicLineSymbol = new CartographicLineSymbol();
193 pSymbol = (ISymbol)pCartographicLineSymbol;
194 pSymbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen; //给符号实例设置颜色的方式
195 pCartographicLineSymbol.Width = Size; //线性符号宽度
196 pCartographicLineSymbol.Color = SetColor(255, 255, 0); ////线性符号颜色
197 }
198 else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
199 {
200
201 pGeometryCollection = new PolygonClass();
202 pGeometry = pFeature.Shape;
203 pGeometryCollection.AddGeometryCollection((IGeometryCollection)pGeometry);
204 pSimpleFillSymbol = new SimpleFillSymbol();
205 if (isShowLine)
206 {
207 pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSCross;
208 }
209 else
210 {
211 pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
212 }
213 pSymbol = (ISymbol)pSimpleFillSymbol;
214
215 pSymbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen; //给符号实例设置颜色的方式
216
217 pSimpleFillSymbol.Color =SetColor(255, 255, 0);
218 }
219 else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint || pFeature.Shape.GeometryType == esriGeometryType.esriGeometryMultipoint)
220 {
221 pGeometryCollection = new MultipointClass();
222 pGeometry = pFeature.Shape;
223 object obj = System.Type.Missing;
224 pGeometryCollection.AddGeometry(pGeometry, ref obj, ref obj);
225 pSimpleMarkersymbol = new SimpleMarkerSymbol();
226 pSymbol = (ISymbol)pSimpleMarkersymbol;
227 pSymbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
228 pSimpleMarkersymbol.Color = SetColor(255, 255, 0);
229 pSimpleMarkersymbol.Size = 8;
230 }
231 pMapControl.FlashShape((IGeometry)pGeometryCollection, 3, 300, pSymbol);
232 }
233 void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
234 {
235 int getFeatID = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
236
237 if (m_FeatureClass != null)
238 {
239 if (m_FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
240 {
241 IPoint m_Pt= m_FeatureClass.GetFeature(getFeatID).Shape as IPoint;
242 m_MapCtrl.CenterAt(m_Pt);
243 FlashFeature(m_FeatureClass.GetFeature(getFeatID) as IFeature, (IMapControl4)m_MapCtrl.Object,false);
244 }
245 else
246 {
247 m_MapCtrl.Extent = m_FeatureClass.GetFeature(getFeatID).Extent;
248 FlashFeature(m_FeatureClass.GetFeature(getFeatID) as IFeature, (IMapControl4)m_MapCtrl.Object, false);
249 //m_axMapCtrl.FlashShape (m_FeatureClass.GetFeature(getFeatID).Shape);
250 }
251 }
252 }
253
254 void m_EngineEditEvent_Event_OnStopEditing(bool saveChanges)
255 {
256
257 dataGridView1.ReadOnly = true;
258 DataGridViewCellStyle style = dataGridView1.ColumnHeadersDefaultCellStyle;
259 style.Font = new Font(dataGridView1.Font,FontStyle.Regular);
260
261 }
262
263 void m_EngineEditEvent_Event_OnStartEditing()
264 {
265 if (m_pEngineEditor.EditState == esriEngineEditState.esriEngineStateNotEditing)
266 {
267 dataGridView1.ReadOnly = true;
268
269 }
270 else
271 {
272 dataGridView1.ReadOnly = false;
273 DataGridViewCellStyle style = dataGridView1.ColumnHeadersDefaultCellStyle;
274 style.Font = new Font(dataGridView1.Font, FontStyle.Italic);
275 //dataGridView1.ColumnHeadersDefaultCellStyle = style;
276 }
277 }
278
279 private void chkUseCVD_CheckedChanged(object sender, EventArgs e)
280 {
281 // Set useage of CV domain on or off
282 tableWrapper.UseCVDomains = chkUseCVD.Checked;
283
284 // Refresh the binding source by setting it to null and then rebinding
285 // to the data. This will refresh all the field types and ensures that all
286 // the fields are using the correct type converters (we will need different
287 // type converters depending on whether we are showing cv domain values or
288 // their text equivalents). Note that as we will be setting the binding source
289 // to null, the text box binding will fail as the "FULL_NAME" field will no
290 // longer be present. To prevent any problems here, we need to remove and
291 // reset the text box's binding.
292 textBox1.DataBindings.Clear();
293 bindingSource1.DataSource = null;
294 bindingSource1.DataSource = tableWrapper;
295 textBox1.DataBindings.Add(txtBoxBinding);
296 dataGridView1.Refresh();
297 }
298 #endregion Private Event Handlers
299 }
300 }

  上面的代码还是存在问题,解决IgroupLayer的方法并不正确,现在是采用UID过滤出所有的IGeoFeatureLayer得到IEnumLayer,遍历添加到ComBoxCurrentLayer中,在SelectIndex_changed事件中通过再次遍历,找到与索引值相同的图层作为当前的编辑图层。(2012-03-23-09:59:45)

文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家留言交流,转载请注明出处。
原文地址:https://www.cnblogs.com/yhlx125/p/2388601.html