1.功能简介
通过IEditEvents接口,开发者可以监听到Editor对象的相关的事件,并且做出反应。包括Editor中开始编辑、结束编辑等操作,下面对矢量数据的编辑事件的监听功能进行介绍。
2.功能实现说明
2.1实现思路及原理说明
第一步 |
前提处于编辑状态,在编辑的开始和结束的时候进行触发的操作事件 |
2.2 核心接口与方法
接口/类 |
方法/属性 |
说明 |
IEditEvents |
OnStartEditOperation; |
开始编辑委托事件 |
OnStopEditOperation |
结束编辑委托事件 |
|
IEditor |
GetEditState(); |
获取编辑状态 |
StartEditOperation(string strDescription); |
开始编辑操作 |
|
StopEditOperation() |
结束编辑操作 |
2.3示例代码
项目路径 |
百度云盘地址下/PIE示例程序/04数据操作/07矢量数据编辑事件的监听 |
数据路径 |
自定义即可 |
视频路径 |
百度云盘地址下/PIE视频教程/04数据操作/07矢量数据编辑事件的监听.avi |
示例代码 |
|
1 //要素编辑器 2 private IEditor m_Editor = Editor.Instance; 3 //鼠标按下起点 4 private IPoint m_StartPoint = null; 5 //要素 6 private IFeature m_Feature = null; 7 //标识符 8 private int m_Flag = 0; 9 //地图初始化绑定编辑监听事件 10 private void FormMain_Load(object sender, EventArgs e) 11 { 12 //初始化绑定编辑监听事件 13 IEditEvents editEvents = Editor.Instance; 14 editEvents.OnStartEditOperation += editEvents_OnStartEditOperation; 15 editEvents.OnStopEditOperation += editEvents_OnStopEditOperation; 16 } 17 //委托事件 18 public void editEvents_OnStartEditOperation() 19 { 20 MessageBox.Show("开始绘制"); 21 } 22 public void editEvents_OnStopEditOperation() 23 { 24 MessageBox.Show("完成绘制"); 25 } 26 27 /// <summary> 28 /// 开始编辑 29 /// </summary> 30 /// <param name="sender"></param> 31 /// <param name="e"></param> 32 private void ToolStripMenuItem_StartEdit_Click(object sender, EventArgs e) 33 { 34 if (m_Editor.GetEditState() == EditState.StateNotEditing) 35 { 36 MessageBox.Show("开始编辑"); 37 m_Editor.StartEditing(); 38 } 39 } 40 41 /// <summary> 42 /// 结束编辑 43 /// </summary> 44 /// <param name="sender"></param> 45 /// <param name="e"></param> 46 private void ToolStripMenuItem_StopEdit_Click(object sender, EventArgs e) 47 { 48 if (m_Editor.GetEditState() == EditState.StateEditing) 49 { 50 if (MessageBox.Show("结束编辑前是否保存编辑数据", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.OK) 51 { 52 m_Editor.SaveEditing();//保存编辑 53 } 54 m_Editor.StopEditing(true);//停止编辑 55 } 56 } 57 /// <summary> 58 /// 地图控件鼠标移动事件 59 /// </summary> 60 /// <param name="sender"></param> 61 /// <param name="e"></param> 62 63 private void mapControlMain_MouseDown(object sender, MouseEventArgs e) 64 { 65 if (e.Button != System.Windows.Forms.MouseButtons.Left) return; 66 67 if (m_Editor.GetEditState() == EditState.StateEditing) 68 { 69 m_StartPoint = mapControlMain.FocusMap.ToMapPoint(e.Location); 70 List<ILayer> layerList = new List<ILayer>(); 71 int iLayerCount = mapControlMain.FocusMap.LayerCount; 72 if (iLayerCount > 0) 73 { 74 ILayer layer = mapControlMain.GetLayer(iLayerCount - 1); 75 layerList.Add(layer); 76 if (layer.IsValid()) 77 { 78 if (layer.LayerType != LayerType.LayerTypeFeatureLayer) return; 79 80 IGeometry geometry = null; 81 switch (m_Flag) 82 { 83 case 2: 84 mapControlMain.ActiveView.FocusMap.ClearSelectionFeatures(); 85 m_Editor.StartEditOperation("开始编辑"); 86 geometry = mapControlMain.TrackPolygon() as IGeometry; 87 geometry.SpatialReference = mapControlMain.SpatialReference; 88 89 IFeatureClass featureClass = (layer as IFeatureLayer).FeatureClass; 90 m_Feature = featureClass.CreateNewFeature(); //创建Feature 91 m_Feature.Geometry = geometry; 92 featureClass.AddFeature(m_Feature); //添加Feature 93 // featureClass.Save(); //保存 94 m_Editor.StopEditOperation(); 95 mapControlMain.PartialRefresh(ViewDrawPhaseType.ViewAll);//不能少 96 m_Flag = 0; //一定要加 97 break; 98 default: 99 break; 100 } 101 } 102 } 103 } 104 } 105 106 /// <summary> 107 /// 绘制面状要素 108 /// </summary> 109 /// <param name="sender"></param> 110 /// <param name="e"></param> 111 private void toolStripButton_DrawPolygon_Click(object sender, EventArgs e) 112 { 113 m_Flag = 2; 114 } |
2.4示例截图