自定义arcengine编辑条 (能够撤销和重做)
http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=75611&extra=&page=1
http://www.gisall.com/?uid-99501-mode-theme
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region IEditStack
interface IEditStack
{
bool IsEditing();
bool HasEdited();
void StartEditing();
void SaveEditing(bool save);
void StopEditing(bool save);
bool HasRedo();
bool HasUndo();
void RodoEditing();
void UndoEditing();
}
#endregion
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Edit : IEditStack
{
protected bool isEditing;
protected bool hasEditing;
protected IFeatureLayer currentLayer; //当前编辑图层
protected IWorkspaceEdit workspaceEdit;
protected IMap map;
protected List<IFeature> selectFeatures; //选择要素集,用于删除要素
public Edit(IFeatureLayer editLayer, IMap map)
{
currentLayer = editLayer;
this.map = map;
}
public Edit()
{
}
#region 添加、选择、删除要素方法
/// <summary>
/// 在当前图层添加要素
/// </summary>
/// <param name="geometry">几何要素</param>
public void AddFeature(IGeometry geometry)
{
if (currentLayer == null) return;
IFeatureClass featureClass = currentLayer.FeatureClass;
if (featureClass.ShapeType != geometry.GeometryType || geometry == null)
return;
if (!isEditing)
{
MessageBox.Show("请先开启编辑");
return;
}
try
{
workspaceEdit.StartEditOperation();
IFeature feature;
feature = featureClass.CreateFeature();
feature.Shape = geometry;
feature.Store();
workspaceEdit.StopEditOperation();
//高亮显示添加要素
map.ClearSelection();
map.SelectFeature(currentLayer as ILayer, feature);
((IActiveView)map).PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
hasEditing = true;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
/// <summary>
/// 在当前图层上进行要素的选择,如果需要对所有FeatureLayer进行选择的话,可以使用IMap.SelectByShape()
/// </summary>
/// <param name="geometry"></param>
public void SelectFeatures(IGeometry geometry)
{
if (geometry == null)
return;
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = geometry;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
IFeatureCursor featureCursor = currentLayer.Search(spatialFilter, false);
selectFeatures = new List<IFeature>();
IFeature feature = featureCursor.NextFeature();
map.ClearSelection();
while (feature != null)
{
map.SelectFeature(currentLayer as ILayer, feature);
selectFeatures.Add(feature);
feature = featureCursor.NextFeature();
}
//System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
((IActiveView)map).PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
/// <summary>
/// 删除选择的要素
/// </summary>
public void DeleteSelectFeatures()
{
if (selectFeatures == null || !isEditing)
return;
//先清除选择集
ClearSelection();
//删除要素
workspaceEdit.StartEditOperation();
foreach (IFeature feature in selectFeatures)
{
feature.Delete();
}
workspaceEdit.StopEditOperation();
hasEditing = true;
selectFeatures = null;
}
/// <summary>
/// 清除地图上显示的选择要素集
/// </summary>
public void ClearSelection()
{
((IActiveView)map).PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
map.ClearSelection();
((IActiveView)map).PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
#endregion
#region IEditStack 成员
public bool IsEditing()
{
return isEditing;
}
public bool HasEdited()
{
return hasEditing;
}
public void StartEditing()
{
IFeatureClass featureClass = currentLayer.FeatureClass;
IWorkspace workspace = (featureClass as IDataset).Workspace;
workspaceEdit = workspace as IWorkspaceEdit;
if (workspaceEdit == null)
return;
if (!workspaceEdit.IsBeingEdited())
{
workspaceEdit.StartEditing(true);
workspaceEdit.EnableUndoRedo();
isEditing = true;
}
}
public void SaveEditing(bool save)
{
if (!save)
{
workspaceEdit.StopEditing(false);
}
else if (save && hasEditing && isEditing)
{
workspaceEdit.StopEditing(true);
}
hasEditing = false;
}
public void StopEditing(bool save)
{
this.SaveEditing(save);
isEditing = false;
}
public bool HasRedo()
{
bool hasRedo = false;
if (isEditing && hasEditing)
{
workspaceEdit.HasRedos(ref hasRedo);
}
return hasRedo;
}
public bool HasUndo()
{
bool hasUndo = false;
if (isEditing && hasEditing)
{
workspaceEdit.HasUndos(ref hasUndo);
}
return hasUndo;
}
public void RodoEditing()
{
if (!isEditing || !hasEditing)
return;
workspaceEdit.RedoEditOperation();
}
public void UndoEditing()
{
if (!isEditing || !hasEditing)
return;
//先清除选择集
ClearSelection();
workspaceEdit.UndoEditOperation();
}
#endregion
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Edit2 : Edit
{
private IFeatureDataset featureDataset; //当前编辑featuredataset
//对featureDataset操作构造函数
public Edit2(IFeatureDataset featureDataset, IMap map)
{
this.featureDataset = featureDataset;
this.map = map;
}
//对单个图层操作构造函数
public Edit2(IFeatureLayer featLyr, IMap map)
: base(featLyr, map)
{
}
public Edit2()
{
}
public new void StartEditing()
{
if (featureDataset != null)
{
IFeatureWorkspace featureWorksapce = featureDataset.Workspace as IFeatureWorkspace;
workspaceEdit = featureWorksapce as IWorkspaceEdit;
if (!workspaceEdit.IsBeingEdited())
{
workspaceEdit.StartEditing(true);
workspaceEdit.EnableUndoRedo();
isEditing = true;
}
}
else //单一图层的话调用基类开始编辑
{
base.StartEditing();
}
}
//保存、停止、撤销、重做等编辑可直接使用基类函数
//对featuredataset中单个featureclass进行添加和选择要素
public void AddFeature(IFeatureLayer featurelayer, IGeometry geometry)
{
currentLayer = featurelayer;
base.AddFeature(geometry);
}
public void SelectFeatures(IFeatureLayer featureLayer, IGeometry geometry)
{
currentLayer = featureLayer;
base.SelectFeatures(geometry);
}
}