ArcEngine实现撤销和重置功能

我们首先要自定义了一个接口和两个类,接口IEditStack是仿照IOperationStack定义的,Edit类是争对单个图层进行编辑,Edit2类在Edit上面进行了适当的改进,分别支持对整个featuredataset和featurelayer进行编辑,目前功能就只有添加、选择、删除要素,撤销、重做,要运用到自己项目的话稍作修改应该就可以了吧,代码仍有待提高和改进的地方。
IeditStack

   #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

 

Edit类

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

    }

Edit2类:

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);

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值