ICommandSubType的使用

绘制工具定制
本文介绍了一个定制的绘制工具,该工具能够根据用户需求绘制线或面,并通过子类型区分不同的绘制功能。文章详细描述了如何利用ArcGIS API进行开发,包括初始化、绘制过程中的交互反馈、绘制结果的显示等。

想用一个ToolDraw,既能绘制线,又能绘制面。DeveloperHelp里面关于ICommandSubType的介绍是:

由于BaseTool继承了BaseCommand类,而BaseCommand类实例化了ICommand接口。

考虑如下定义:

结果是:可以实现不同的绘制功能,但由于我们的工具实在最开始初始化并放在工具池中,所以无法将其按照SubType的区分生成各自的Tool。

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;
using EngineLib.BaseUtil;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using MapMgr.BaseUtil;
using CommonLib.Pub;
using DevExpress.XtraBars;
using LUGISMapMgrMain.BaseFrm;
using ESRI.ArcGIS.Geodatabase;
using EngineLib.EngineCommon;
using DevExpress.XtraEditors;
using ESRI.ArcGIS.esriSystem;
using System.IO;

namespace LUGISMapMgrMain.BGTool
{
    /// <summary>
    /// Summary description for ToolDraw.
    /// </summary>
    [Guid("52b4cb8b-88b3-4e45-bce0-8224f85a8607")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("LUGISMapMgrMain.BGTool.ToolDraw")]
    public sealed class ToolDraw : ReBaseDrawTool, ICommandSubType
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;
        private IPointCollection _pointColl = null;
        private IGeometry _geometry = null;
        private IDisplayFeedback _geometryFeedback = null;
        private long _subType;
        private IPoint _startPoint = null;
        private IPoint _endPoint = null;

        //private bool _drawStart = false;
        private static PopupMenu pmRight = null;
        private static BarButtonItem biComplete = null;
        private static BarButtonItem biDrawNext = null;
        private static BarButtonItem biDelete = null;
        private static BarButtonItem biBack = null;
        private static BarButtonItem biImportToGraphic = null;
        private static BarButtonItem biGenerate = null;
        //public event AfterDrawGeometry eventAfterDrawGeometry;

        public ToolDraw()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text 
            base.m_caption = "绘制草图";  //localizable text 
            base.m_message = "";  //localizable text
            base.m_toolTip = "用鼠标在地图容器上绘制";  //localizable text
            base.m_name = "宗地草图绘制工具";   //unique id, non-localizable (e.g. "MyCategory_MyTool")
            try
            {
                //
                // TODO: change resource name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
                base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        public ToolDraw(BGType bgType)
        {
            _alterType = bgType;
        }
        #region Overriden Class Methods

        /// <summary>
        /// Occurs when this tool is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            base.OnCreate(hook);      
        }

        public override bool Enabled
        {
            get
            {
                if (myHook.FocusMap != null && !String.IsNullOrEmpty(TheReferenceInstances.TheActiveXMH))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// Occurs when this tool is clicked
        /// </summary>
        public override void OnClick()
        {
            try
            {
                //是否为AEditor的编辑状态
                if (TheReferenceInstances.TheAEditer.PrepareEditor() == false)
                    return;
                             
                //初始化IDisplayFeedback和pointColl
                //if (_geometryFeedback == null)
                //{
                    if (_subType == 1)
                    {
                        _geometryFeedback = new NewLineFeedbackClass();
                        _pointColl = new PolylineClass();
                    }
                    else if(_subType == 2)
                    {
                        _geometryFeedback = new NewPolygonFeedbackClass();
                        _pointColl = new PolygonClass();
                    }                    
                //}   
             
                //初始化对应关联值
                TheReferenceInstances.TheAEditer.SketchEdit.DrawTool = this;
                TheReferenceInstances.TheAEditer.SketchEdit.PntColl = _pointColl;
                TheReferenceInstances.TheAEditer.StructSnapOption.IsHaveSnap = true;
                TheReferenceInstances.TheAEditer.StructSnapOption.Tolerance = 20;
                TheReferenceInstances.CurrentTool = this;
                TheReferenceInstances.TheAEditer.CurTask = _alterType;

                _startPoint = null;
                _endPoint = null;

                //设置当前工具为this            
                if (myHook.Hook is IMapControl3)
                {
                    (myHook.Hook as IMapControl3).CurrentTool = this;
                    if (_subType == 1)
                        (_geometryFeedback as INewLineFeedback).Display = myHook.ActiveView.ScreenDisplay;
                    else if(_subType == 2)
                        (_geometryFeedback as INewPolygonFeedback).Display = myHook.ActiveView.ScreenDisplay;
                }
                else if (myHook.Hook is IPageLayoutControl2)
                {
                    (myHook.Hook as IPageLayoutControl2).CurrentTool = this;
                    if (_subType == 1)
                        (_geometryFeedback as INewLineFeedback).Display = (myHook.FocusMap as IActiveView).ScreenDisplay;
                    else if(_subType == 2)
                        (_geometryFeedback as INewPolygonFeedback).Display = (myHook.FocusMap as IActiveView).ScreenDisplay;
                }
              
                //右键菜单及Cursor
                this.m_cursor = Cursors.Cross;
                InitialRightMenu();
            }
            catch (System.Exception e)
            {
                XtraMessageBox.Show(String.Format("初始化变更工具失败!\n{0}", e.Message), "警告", MessageBoxButtons.OK);
            }
        }

        private void InitialRightMenu()
        {
            pmRight = new PopupMenu(TheReferenceInstances.TheBarManager);
            biComplete = new BarButtonItem(TheReferenceInstances.TheBarManager, "完成草图");
            biComplete.ItemClick += new ItemClickEventHandler(biComplete_ItemClick);
            pmRight.AddItem(biComplete);

            biDelete = new BarButtonItem(TheReferenceInstances.TheBarManager, "删除草图");
            biDelete.ItemClick += new ItemClickEventHandler(biDelete_ItemClick);
            pmRight.AddItem(biDelete);

            biDrawNext = new BarButtonItem(TheReferenceInstances.TheBarManager, "根据角度和距离绘制下一个点");
            biDrawNext.ItemClick += new ItemClickEventHandler(biDrawNextPntByDis_ItemClick);
            pmRight.AddItem(biDrawNext);
            pmRight.ItemLinks[pmRight.ItemLinks.Count - 1].BeginGroup = true;

            biBack = new BarButtonItem(TheReferenceInstances.TheBarManager, "回退到前一个草图结点");
            biBack.ItemClick += new ItemClickEventHandler(biBack_ItemClick);
            pmRight.AddItem(biBack);

            biImportToGraphic = new BarButtonItem(TheReferenceInstances.TheBarManager, "导入草图坐标串");
            biImportToGraphic.ItemClick += new ItemClickEventHandler(biImportToGraphic_ItemClick);
            pmRight.AddItem(biImportToGraphic);
            pmRight.ItemLinks[pmRight.ItemLinks.Count - 1].BeginGroup = true;

            biGenerate = new BarButtonItem(TheReferenceInstances.TheBarManager, "从选择要素中生成草图");
            biGenerate.ItemClick += new ItemClickEventHandler(biGenerate_ItemClick);
            pmRight.AddItem(biGenerate);
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            if (Button == 1)
            {
                object obj = Type.Missing;
                if (_startPoint == null)
                {
                    _startPoint = (myHook.FocusMap as IActiveView).ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                    TheReferenceInstances.TheAEditer.OSnap(true, ref _startPoint);
                    _pointColl.AddPoint(_startPoint, ref obj, ref obj);

                    //用于同步反应在解析编辑对话框
                    if (TheReferenceInstances.TheAEditer.FrmSketch != null && TheReferenceInstances.TheAEditer.FrmSketch.IsOpen)
                        TheReferenceInstances.TheAEditer.FrmSketch.RefreshCorrds();
                    
                    if (_subType == 1)
                    {
                        (_geometryFeedback as INewLineFeedback).Start(_startPoint);
                    }
                    else if (_subType == 2)
                    {
                        (_geometryFeedback as INewPolygonFeedback).Start(_startPoint);
                    }
                }
                else
                {
                    _endPoint = (myHook.FocusMap as IActiveView).ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                    TheReferenceInstances.TheAEditer.OSnap(true, ref _endPoint);
                    _pointColl.AddPoint(_endPoint, ref obj, ref obj);

                    //用于同步反应在解析编辑对话框
                    if (TheReferenceInstances.TheAEditer.FrmSketch != null && TheReferenceInstances.TheAEditer.FrmSketch.IsOpen)
                        TheReferenceInstances.TheAEditer.FrmSketch.RefreshCorrds();

                    if (_subType == 1)
                    {
                        (_geometryFeedback as INewLineFeedback).AddPoint(_endPoint);
                    }
                    else if (_subType == 2)
                    {
                        (_geometryFeedback as INewPolygonFeedback).AddPoint(_endPoint);
                    }
                }
            }
            else if (Button == 2)
            {
                if (pmRight != null)
                    pmRight.ShowPopup(Control.MousePosition);
            }
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            IPoint movePoint = (myHook.FocusMap as IActiveView).ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            TheReferenceInstances.TheAEditer.OSnap(false, ref movePoint);
            TheReferenceInstances.TheAEditer.PrePoint = movePoint;

            if (_subType == 1)
            {
                (_geometryFeedback as INewLineFeedback).MoveTo(movePoint);
            }
            else if (_subType == 2)
            {
                (_geometryFeedback as INewPolygonFeedback).MoveTo(movePoint);
            }

            //屏幕上给出角度和面积的提示
            if (_geometryFeedback != null)
            {
                double dtX = movePoint.X - _startPoint.X;
                double dtY = movePoint.Y - _startPoint.Y;
                double distance = Math.Sqrt((Math.Pow(dtX, 2) + Math.Pow(dtY, 2)));

                double arc = Math.Asin(dtX / distance);
                double angle = 180 * (arc / Math.PI);

                if (dtX >= 0 && dtY > 0)  // 0=<angle < 90 
                {
                    ;//什么都不用做
                }
                else if (dtX > 0 && dtY <= 0) // 90 =<angle < 180 
                {
                    angle = 180 - angle;
                }
                else if (dtX <= 0 && dtY < 0) // 180=<angle < 270 
                {
                    angle = 180 - angle;
                }
                else if (dtX < 0 && dtY >= 0) // 270=<angle < 360 
                {
                    angle += 360;
                }

                // InfoUserStatusbar.InfoTip = "当前鼠标方位角:" + Convert.ToString(angle) + "°";
            }

        }

        public override void OnDblClick()
        {
            if(_geometryFeedback is INewPolygonFeedback)
            {
               _geometry =  (_geometryFeedback as INewPolygonFeedback).Stop();
            }
            else if(_geometryFeedback is INewLineFeedback)
            {
                _geometry = (_geometryFeedback as INewLineFeedback).Stop();
            }
          
            TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry = _geometry;           
            RefreshFeedBack();
        }

        public override void Refresh(int hDC)
        {
            base.Refresh(hDC);
            if(_geometryFeedback != null)
            {
                _geometryFeedback.Refresh(hDC);
            }
        }

        public override void RefreshFeedBack()
        {
            try
            {
                if (TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry == null)
                    return;

                //屏幕绘制graphic表达此刻的Geometry
                IElement ipElement = null;
                if(_subType == 1)
                {
                    ipElement = new LineElementClass();
                    ipElement.Geometry = TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry;                    
                }
                else if(_subType == 2)
                {
                    ipElement = new PolygonElementClass();
                    ipElement.Geometry = TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry;
                    ipElement.Geometry.SpatialReference = TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry.SpatialReference;
                                     
                    //绘制Geometry时使用空心红框,以方便查看叠盖关系
                    ISimpleFillSymbol iSymbol = new SimpleFillSymbolClass();
                    ISimpleLineSymbol iOutlineSymbol = new SimpleLineSymbolClass();
                    IColor irgbColor = new RgbColorClass();
                    (irgbColor as IRgbColor).Red = 255; (irgbColor as IRgbColor).Blue = 0; (irgbColor as IRgbColor).Green = 0;
                    iOutlineSymbol.Color = irgbColor;
                    iSymbol.Style = esriSimpleFillStyle.esriSFSHollow;
                    iSymbol.Outline = iOutlineSymbol;
                    (ipElement as IFillShapeElement).Symbol = iSymbol;
                    
                }

                (myHook.FocusMap as IGraphicsContainer).AddElement(ipElement, 0);
                (myHook.FocusMap as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGraphics, ipElement, TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry.Envelope);
                InfoUserStatusbar.InfoTip = "草图绘制完成!";             
            }
            catch (System.Exception e)
            {
                throw new ApplicationException(String.Format("把草图更新到Map出错!\n{0}", e.Message));
                InfoUserStatusbar.InfoTip = "草图绘制失败!";
            }
            finally
            {
                if (myHook.Hook is IMapControl3)
                {
                    TheReferenceInstances.TheAxMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
                    (myHook.Hook as IMapControl3).CurrentTool = null;
                }
                else if (myHook.Hook is IPageLayoutControl2)
                {
                    (myHook.Hook as IPageLayoutControl2).CurrentTool = null;
                }

                m_cursor = Cursors.Default;
            }
        }
        
#endregion

        #region ICommandSubType 成员

        public int GetCount()
        {
            return 2;
        }

        public void SetSubType(int SubType)
        {
            _subType = SubType;
        }

        #endregion

        #region 自己编写的方法
        /// <summary>
        /// 删除草图操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void biDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            // 2011.2.3 by hz 完成草图时,删除存储的点集
            if (_pointColl.PointCount != 0)
            {
                _pointColl.RemovePoints(0, _pointColl.PointCount);
            }

            _startPoint = null;
            if (_subType == 1)
                (_geometryFeedback as INewLineFeedback).Stop();
            else if (_subType == 2)
               (_geometryFeedback as INewPolygonFeedback).Stop();
        }

        /// <summary>
        /// 从导入的坐标串中生成草图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void biImportToGraphic_ItemClick(object sender, ItemClickEventArgs e)
        {
            string filePath = "";
            object pbefore = Type.Missing;
            object pafter = Type.Missing;
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "Text files(*.txt)|*.txt";

            IPointCollection ipPoitCol = null;
            IGeometry iGeo = null;
            try
            {
                if (_subType == 1)
                {
                    ipPoitCol = new PolylineClass();
                }
                else if(_subType == 2)
                {
                    ipPoitCol = new PolygonClass();
                }

                if (openDlg.ShowDialog() == DialogResult.OK)
                {
                    filePath = openDlg.FileName;
                    StreamReader strRead = new StreamReader(filePath);
                    string strLine;
                    int RowCou = 0;
                    string strX = string.Empty;
                    string strY = string.Empty;
                    string[] strCoor = new string[2];
                    while ((strLine = strRead.ReadLine()) != null)
                    {
                        if (strLine != "")
                        {
                            RowCou++;
                            strCoor = strLine.Split(',');
                            double x = double.Parse(strCoor[0]);
                            double y = double.Parse(strCoor[1]);
                            if (RowCou == 1)
                            {
                                strX = strCoor[0];
                                strY = strCoor[1];
                            }
                            IPoint ipNewPoint = new PointClass();
                            ipNewPoint.PutCoords(x, y);
                            ipPoitCol.AddPoint(ipNewPoint, ref pbefore, ref pafter);
                        }
                    }
                    //如果第一个点和最后一个点不等,则再加一遍一个点的坐标
                    if (strX != strCoor[0] && strY != strCoor[1])
                    {
                        IPoint ipPoint = new PointClass();
                        ipPoint.PutCoords(double.Parse(strCoor[0]), double.Parse(strCoor[1]));
                        ipPoitCol.AddPoint(ipPoint, ref pbefore, ref pafter);
                    }
                    strRead.Close();
                }

                iGeo = ipPoitCol as IGeometry;

                if (iGeo != null)
                {
                    if (_subType == 1)         
                        (_geometryFeedback as INewLineFeedback).Stop();
                    else if(_subType ==2)
                        (_geometryFeedback as INewPolygonFeedback).Stop();

                    TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry = iGeo;
                    _startPoint = null;

                    RefreshFeedBack();
                }
                else
                {
                    XtraMessageBox.Show("导入的点坐标不能构成面!", "提示");
                    return;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 完成草图的操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void biComplete_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {               
                if (_pointColl.PointCount != 0)
                {
                    _pointColl.RemovePoints(0, _pointColl.PointCount);
                }

                if (_subType == 1)
                {
                    _geometry = (_geometryFeedback as INewLineFeedback).Stop();
                }
                else if (_subType == 2)
                {
                    _geometry = (_geometryFeedback as INewPolygonFeedback).Stop();
                }
                TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry = _geometry;
              

                RefreshFeedBack();
            }
            catch (System.Exception ex)
            {
                XtraMessageBox.Show(String.Format("绘制草图失败!\n{0}", ex.Message), "提示");
            }

        }

        /// <summary>
        /// 根据角度和距离绘制下一个点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void biDrawNextPntByDis_ItemClick(object sender, ItemClickEventArgs e)
        {
            IPoint iLastPnt = null, iLast2Pnt = null;
            if (_pointColl.PointCount < 1)
            {
                MessageBox.Show("请至少画一个点!");
                return;
            }
            else if (_pointColl.PointCount == 1)
            {
                iLastPnt = _pointColl.get_Point(0);
            }
            else
            {
                iLastPnt = _pointColl.get_Point(_pointColl.PointCount - 1);
                iLast2Pnt = _pointColl.get_Point(_pointColl.PointCount - 2);
            }

            IPoint iMousePnt = TheReferenceInstances.TheAEditer.PrePoint;
            FrmGetNextPnt dlg = new FrmGetNextPnt(iLastPnt, iLast2Pnt, iMousePnt);

            if ((dlg.ShowDialog()) == DialogResult.OK)
            {
                _endPoint = dlg.nextPnt;
                TheReferenceInstances.TheAEditer.OSnap(true, ref _endPoint);
                object obj = Type.Missing;
                _pointColl.AddPoint(_endPoint, ref obj, ref obj);

                //用于同步反应在解析编辑对话框
                if (TheReferenceInstances.TheAEditer.FrmSketch != null && TheReferenceInstances.TheAEditer.FrmSketch.IsOpen)
                    TheReferenceInstances.TheAEditer.FrmSketch.RefreshCorrds();
              
                if (_subType == 1)
                {
                    (_geometryFeedback as INewLineFeedback).AddPoint(_endPoint);
                }
                else if (_subType == 2)
                {
                    (_geometryFeedback as INewPolygonFeedback).AddPoint(_endPoint);
                }
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 从选择要素中生成草图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void biGenerate_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (_startPoint != null)
                {
                    XtraMessageBox.Show(String.Format("当前处于编辑草图状态无法生成草图,请先删除草图!"), "提示");
                }

                ISet iSet = new SetClass();
                ISelection iSelection = null;

                iSelection = TheReferenceInstances.TheProjectAgent.Map.FeatureSelection;
                IEnumFeatureSetup iSetUp = iSelection as IEnumFeatureSetup;
                iSetUp.AllFields = true;
                iSetUp.Recycling = false;

                IEnumFeature iEnumFeat = iSelection as IEnumFeature;
                iEnumFeat.Reset();
                IFeature iFeat = null;
                IGeometry iGeo = null;
                while ((iFeat = iEnumFeat.Next()) != null)
                {
                    iGeo = iFeat.Shape;
                    iSet.Add(iGeo);
                }

                if (iSet.Count < 1)
                {
                    XtraMessageBox.Show(String.Format("当前无被选中的地物!"), "提示");
                    return;
                }

                int n = TheReferenceInstances.TheAEditer.SketchEdit.PntColl.PointCount;
                if (n > 0)
                {
                    TheReferenceInstances.TheAEditer.SketchEdit.PntColl.RemovePoints(0, n);
                }

                SpatialTool spatialTool = new SpatialTool();

                IGeometry iGeometry = null;
                if (_subType == 1)
                {
                    if (iSet.Count > 1 && !spatialTool.CanMergeAsSinglePath(iSet))
                    {
                        XtraMessageBox.Show(String.Format("要素不相邻或者自相交, 不能进行合并!"), "提示");
                    }

                    iGeometry = spatialTool.UnionPolylines(iSet) as IGeometry;

                    if (iGeometry == null)
                    {
                        XtraMessageBox.Show(String.Format("合并要素失败!"), "提示");
                        return;
                    }
                    (_geometryFeedback as INewLineFeedback).Stop();
                }
                else if (_subType == 2)
                {
                    iGeometry = spatialTool.UnionPolygons(iSet) as IGeometry;
                    if (iGeometry == null)
                    {
                        XtraMessageBox.Show(String.Format("合并要素失败!"), "提示");
                        return;
                    }

                    if ((iGeometry as IPolygon).ExteriorRingCount > 1)
                    {
                        XtraMessageBox.Show(String.Format("要素不相邻,不能合并!"), "提示");
                        return;
                    }
                    (_geometryFeedback as INewPolygonFeedback).Stop();
                }                

                //从CAD转化来的图形可能含有Z值,造成后续iNewFeat.Shape = iGeometry时出错。故进行转化。
                IZAware iZAware = iGeometry as IZAware;
                if (iZAware != null && iZAware.ZAware == true)
                {
                    iZAware.ZAware = false;
                }

                TheReferenceInstances.TheAEditer.SketchEdit.DrawGeometry = iGeometry;
                TheReferenceInstances.TheProjectAgent.Map.ClearSelection();

                _startPoint = null;
                RefreshFeedBack();
                (myHook.FocusMap as IActiveView).Refresh();
            }
            catch (System.Exception ex)
            {
                XtraMessageBox.Show(String.Format("从选择要素中生成草图失败!\n{0}", ex.Message), "提示");
            }
        }

        /// <summary>
        /// 回退到前一个草图结点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void biBack_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                VertexBack();
            }
            catch (System.Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "提示");
            }
        }

        /// <summary>
        /// 回退到前一个草图节点
        /// </summary>
        public override void VertexBack()
        {
            try
            {
                if (_pointColl == null || _pointColl.PointCount == 0)
                {
                    return;
                }

                if(_subType == 1)
                {
                    _geometryFeedback = new NewLineFeedbackClass();
                    (_geometryFeedback as INewLineFeedback).Display = myHook.ActiveView.ScreenDisplay;
                }
                else if(_subType == 2)
                {
                    _geometryFeedback = new NewPolygonFeedbackClass();
                    (_geometryFeedback as INewPolygonFeedback).Display = myHook.ActiveView.ScreenDisplay;
                }
           
                _startPoint = null;
                _endPoint = null;
                _geometry = null;//每次重设矩形

                if (_pointColl.PointCount == 1)
                {
                    _pointColl.RemovePoints(0, 1);
                    myHook.ActiveView.Refresh();
                }
                else if (_pointColl.PointCount > 1)
                {
                    int iCount = _pointColl.PointCount;
                    _pointColl.RemovePoints(iCount - 1, 1);
                    bool bIsFirst = true;

                    for (int i = 0; i < _pointColl.PointCount; i++)
                    {
                        if (bIsFirst)
                        {
                            _startPoint = _pointColl.get_Point(i);
                            if (_subType == 1)
                                (_geometryFeedback as INewLineFeedback).Start(_startPoint);
                            else if (_subType == 2)
                                (_geometryFeedback as INewPolygonFeedback).Start(_startPoint);

                            bIsFirst = false;
                        }
                        _endPoint = _pointColl.get_Point(i);
                        if (_endPoint != null)
                        {
                            if (_subType == 1)
                                (_geometryFeedback as INewLineFeedback).AddPoint(_endPoint);
                            else if (_subType == 2)
                                (_geometryFeedback as INewPolygonFeedback).AddPoint(_endPoint);
                        }
                    }
                }

                myHook.ActiveView.Refresh();
            }
            catch (System.Exception e)
            {
                throw new Exception(String.Format("回退到前一个草图节点失败!\n{0}", e.Message));
            }
        }
        #endregion
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值