C#二次开发小实现-元素编辑响应事件
- 设置全局局部变量
- 编辑事件的操作
- 编辑绘制事件
设置全局局部变量
#region 成员变量
//要素编辑器
private IEditor m_Editor = Editor.Instance;
//鼠标按下起点
private IPoint m_StartPoint = null;
//要素
private IFeature m_Feature = null;
//标识符
private int m_Flag = 0;
#endregion
编辑事件的操作
/// <summary>
/// 撤销
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton_Undo_Click(object sender, EventArgs e)
{
ICommand cmd = new UndoCommand();
cmd.OnCreate(mapControlMain);
cmd.OnClick();
}
/// <summary>
/// 回退功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton_Redo_Click(object sender, EventArgs e)
{
ICommand cmd = new RedoCommand();
cmd.OnCreate(mapControlMain);
cmd.OnClick();
}
/// <summary>
/// 开始编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToolStripMenuItem_StartEdit_Click(object sender, EventArgs e)
{
if (m_Editor.GetEditState() == EditState.StateNotEditing)
{
MessageBox.Show("开始编辑");
m_Editor.StartEditing();
}
}
/// <summary>
/// 结束编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToolStripMenuItem_StopEdit_Click(object sender, EventArgs e)
{
if (m_Editor.GetEditState() == EditState.StateEditing)
{
if (MessageBox.Show("结束编辑前是否保存编辑数据", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.OK)
{
m_Editor.SaveEditing();//保存编辑
}
m_Editor.StopEditing(true);//停止编辑
}
}
编辑绘制事件
if (e.Button != System.Windows.Forms.MouseButtons.Left) return;
if (m_Editor.GetEditState() == EditState.StateEditing)
{
m_StartPoint = mapControlMain.FocusMap.ToMapPoint(e.Location);
List<ILayer> layerList = new List<ILayer>();
int iLayerCount = mapControlMain.FocusMap.LayerCount;
if (iLayerCount > 0)
{
ILayer layer = mapControlMain.GetLayer(iLayerCount - 1);
layerList.Add(layer);
if (layer.IsValid())
{
if (layer.LayerType != LayerType.LayerTypeFeatureLayer) return;
IGeometry geometry = null;
switch (m_Flag)
{
case 1:
//清空选择的所有要素
mapControlMain.ActiveView.FocusMap.ClearSelectionFeatures();
m_Editor.StartEditOperation("开始编辑");
IFeatureClass featureClass1 = (layer as IFeatureLayer).FeatureClass;
m_Feature = featureClass1.CreateNewFeature();
m_Feature.Geometry = geometry;
featureClass1.Save();
m_Editor.StopEditOperation();
mapControlMain.DrawShape(geometry, SystemSymbolSetting.Instance.DefaultFillSymbol);
mapControlMain.PartialRefresh(ViewDrawPhaseType.ViewAll);
m_Flag = 0; //一定要加
break;
case 2://画多边形
mapControlMain.ActiveView.FocusMap.ClearSelectionFeatures();
m_Editor.StartEditOperation("开始编辑");
geometry = mapControlMain.TrackPolygon() as IGeometry;
geometry.SpatialReference = mapControlMain.SpatialReference;
IFeatureClass featureClass = (layer as IFeatureLayer).FeatureClass;
m_Feature = featureClass.CreateNewFeature(); //创建Feature
m_Feature.Geometry = geometry;
featureClass.AddFeature(m_Feature); //添加Feature
// featureClass.Save(); //保存
m_Editor.StopEditOperation();
mapControlMain.PartialRefresh(ViewDrawPhaseType.ViewAll);//不能少
m_Flag = 0; //一定要加
break;
case 3:
mapControlMain.ActiveView.FocusMap.ClearSelectionFeatures();
m_Editor.StartEditOperation("开始编辑");
geometry = mapControlMain.TrackRectangle() as IGeometry;
geometry.SpatialReference = mapControlMain.SpatialReference;
IFeatureClass featureClass2 = (layer as IFeatureLayer).FeatureClass;
m_Feature = featureClass2.CreateNewFeature(); //创建Feature
m_Feature.Geometry = geometry;
featureClass2.AddFeature(m_Feature); //添加Feature
//featureClass2.Save(); //保存
m_Editor.StopEditOperation();
mapControlMain.PartialRefresh(ViewDrawPhaseType.ViewAll);//不能少
m_Flag = 0; //一定要加
break;
case 4://TrackLine
mapControlMain.ActiveView.FocusMap.ClearSelectionFeatures();
m_Editor.StartEditOperation("开始编辑");
geometry = mapControlMain.TrackLine() as IGeometry;
geometry.SpatialReference = mapControlMain.SpatialReference;
IFeatureClass featureClass3 = (layer as IFeatureLayer).FeatureClass;
m_Feature = featureClass3.CreateNewFeature(); //创建Feature
m_Feature.Geometry = geometry;
featureClass3.AddFeature(m_Feature); //添加Feature
// featureClass.Save(); //保存
m_Editor.StopEditOperation();
mapControlMain.PartialRefresh(ViewDrawPhaseType.ViewAll);//不能少
m_Flag = 0; //一定要加
break;
default:
break;
}
}
}
}
坚持开发学习,目前是C#语言
链接: link.
图片:
- 计划任务
- 完成任务