这个星期在做自定义的要素编辑工具栏,不过遇到不少的困难。
现在初步了解到大概用到的下面的接口和类:
IEngineEditor
IEngineEditTask
IEngineEditLayers
IEngineEditEvents
IEngineEditProperties
ControlsEditingSketchToo
ControlsEditingEditToolC
ControlsEditingCopyComma
ControlsEditingPasteComm
ControlsEditingClearComm
现在使用 IEngineEditor、IEngineEditTask、IEngineEditLayers这三个接口,再加上Command命令和Tool可以是实现新建要素、复制、剪切、黏贴、删除、移动要素。
下面说明一下我实现新建图形功能的思路:
1、根据workspace和map开启编辑。
2、设置目标图层。
3、设置编辑操作的任务类型。
4、激活SletchTool,ControlsEditingSketchToo
5、这时就可以在AxMapControl中画图形了。图形的点线面类型,SletchTool工具会根据目标图层的类型自动识别。
使用了全局变量IEngineEditor,保证在其他的模块该变量的值
private IEngineEditor m_EngineEditor = new EngineEditorClass();
private void btnNewFeature_Click(object sender, EventArgs e)
{
if(m_engineEditor.EditState==esriEngineEditState.esriEngineStateNotEditin
{
//Start editing the workspace of the first feature layer found.
//设置编辑的WorkSpace和编辑对象图层
ILayer currentLayer = mMap.get_Layer(0);
if (currentLayer is IFeatureLayer)
{
IFeatureLayer featureLayer = currentLayer as IFeatureLayer;
IDataset dataset = featureLayer.FeatureClass as IDataset;
IWorkspace workspace = dataset.Workspace;
m_engineEditor.StartEditing(workspace, mMap.Map);
((IEngineEditLayers)m_engineEditor).SetTargetLayer(featureLayer, 0);
SetCurrentTask(ref m_engineEditor, "新建要素");
}
}
}
/// <summary>
/// 设置编辑的任务类型
/// </summary>
/// <param name="pEngineEditor"></param>
/// <param name="taskName"></param>自己命名的任务名称
public static void SetCurrentTask(ref IEngineEditor pEngineEditor, string taskName)
{
IEngineEditTask edittask;
if (pEngineEditor.EditState == esriEngineEditState.esriEngineStateEditing)
{
switch (taskName)
{
case "新建要素":
edittask = pEngineEditor.GetTaskByUniqueName("ControlToolsEditing_CreateNewFeatureTask");
if (edittask != null)
{
pEngineEditor.CurrentTask = edittask;
}
break;
case "修改图形":
edittask=pEngineEditor.GetTaskByUniqueName("ReshapePolylineEditTask_Reshape Polyline_CSharp");
if (edittask != null)
{
pEngineEditor.CurrentTask = edittask;
}
break;
case "修改要素":
edittask= pEngineEditor.GetTaskByUniqueName("ControlToolsEditing_ModifyFeatureTask");
if (edittask != null)
{
pEngineEditor.CurrentTask = edittask;
}
break;
}
}
}
删除、复制、黏贴的功能,将m_EngineEditor 置于开启编辑的状态后,直接调用下面的命令就好了
ControlsEditingEditToolC
ControlsEditingCopyComma
ControlsEditingPasteComm
ControlsEditingClearComm
撤销和重做的功能
/// <summary>
/// 撒消编辑
/// </summary>
/// <param name="m_pMap">IMap 地图对象</param>
public void MapUndoEdit(IMap m_pMap)
{
IEngineEditProperties ep = new EngineEditorClass();
ILayer m_pCurrentLayer = ep.TargetLayer;
if (m_pCurrentLayer == null) return;
IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pCurrentLayer;
IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
if (pDataset == null) return;
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
bool bHasUndos = false;
pWorkspaceEdit.HasUndos(ref bHasUndos);
if (bHasUndos)
pWorkspaceEdit.UndoEditOperation();
IActiveView pActiveView = (IActiveView)m_pMap;
pActiveView.Refresh();
}
/// <summary>
/// 重做
/// </summary>
/// <param name="m_pMap">IMap 地图对象</param>
public void MapRedoEdit(IMap m_pMap)
{
IEngineEditProperties ep = new EngineEditorClass();
ILayer m_pCurrentLayer = ep.TargetLayer;
if (m_pCurrentLayer == null) return;
IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pCurrentLayer;
IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
if (pDataset == null) return;
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
bool bHasUndos = false;
pWorkspaceEdit.HasUndos(ref bHasUndos);
if (bHasUndos)
pWorkspaceEdit.RedoEditOperation();
IActiveView pActiveView = (IActiveView)m_pMap;
pActiveView.Refresh();
}