auto CAD 二次开发 c#.net 之JIG画多段线(polyline)

在CAD 命令中画多段线的命令为:pline。下面将介绍一种JIG画多段线的方法。

首先,来介绍一下JIG这个东西。

EntityJig(实体动态预览),按照指定步骤模仿特定类型的单个实体对象。首先,定义一个类,该类继承于: Autodesk.AutoCAD.EditorInput.EntityJig。继承该类后,需要重写两个方法。 protected override SamplerStatus Sampler(JigPrompts prompts)和  protected override bool Update()这两个方法。

与正常的Prompt一样需要处理JIG的Prompt。

在Sampler方法中编写如下代码:处理提示消息。并获得用户输入信息

 JigPromptPointOptions jigOpts = new JigPromptPointOptions();
            jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates |
                UserInputControls.NullResponseAccepted |
                UserInputControls.NoNegativeResponseAccepted);

在Update方法中更新每一次的拖拽。

 

完整代码如下:

 

 public class ClsDrawJigLine : Autodesk.AutoCAD.EditorInput.EntityJig
    {
        #region 成员变量
        public static int color = 0;
        public static Point3dCollection m_pts;
        Point3d m_tempPoint;
        Plane m_plane;
        #endregion
        #region 构造方法

        public ClsDrawJigLine(Matrix3d ucs)
            : base(new Polyline())
        {
            m_pts = new Point3dCollection();
            Point3d origin = new Point3d(0, 0, 0);
            Vector3d normal = new Vector3d(0, 0, 1);
            normal = normal.TransformBy(ucs);
            m_plane = new Plane(origin, normal);
            Polyline pline = Entity as Polyline;
            pline.SetDatabaseDefaults();
            pline.Normal = normal;
            pline.ColorIndex = color;
            pline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
        }

        #endregion

        ///


        /// 画线
        ///

        ///
        ///
        public Point3dCollection DragLine(int colorIndex)
        {
            return ClsDrawJigLine.PolyJig(colorIndex);
        }

        #region 画线方法
        ///


        /// 画线
        ///

        /// 颜色
        ///
        public static Point3dCollection PolyJig(int colorIndex)
        {
            color = colorIndex;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Matrix3d ucs = ed.CurrentUserCoordinateSystem;
            ClsDrawJigLine jig = new ClsDrawJigLine(ucs);
            bool bSuccess = true, bComplete = false;
            do
            {
                PromptResult res = ed.Drag(jig);
                bSuccess = (res.Status == PromptStatus.OK);
                if (bSuccess)
                    jig.AddLatestVertex();
                if (res.Status != PromptStatus.OK)
                {
                    Confirm frm = new Confirm();
                    frm.ShowDialog();
                    Confirm.Operate operateType = frm._operrateType;
                    switch (operateType)
                    {
                        case Confirm.Operate.Continue:
                            bSuccess = true;
                            bComplete = false;
                            break;
                        case Confirm.Operate.Back:
                            if (m_pts.Count > 0)
                            {
                                m_pts.RemoveAt(m_pts.Count - 1);
                                bSuccess = true;
                                bComplete = false;
                                jig.RemoveLastVertex();
                            }
                            break;
                        case Confirm.Operate.Accept:
                            break;
                    }
                }
            } while (bSuccess && !bComplete);
            return m_pts;
        }

        #endregion
        #region 用于在完成Drag后,移除最后个虚构的点
        ///


        /// 用于在完成Drag后,移除最后个虚构的点
        ///

        public void RemoveLastVertex()
        {
            Polyline pline = Entity as Polyline;
            if (pline.NumberOfVertices > 1)
            {
                pline.RemoveVertexAt(m_pts.Count);
            }
        }
        #endregion

        #region 在添加一个点时激发事件
        ///


        /// 事件委托
        ///

        /// 事件参数
        public delegate void AddHandle(EventArgs e);

        public class EventArgs
        {
            public EventArgs(Point3dCollection currentPoints, int index)
            {
                this.currentPoints = currentPoints;
                this.index = index;
            }
            ///


            /// 当前点集合
            ///

            private Point3dCollection currentPoints;
            ///
            /// 当前点集合
            ///

            public Point3dCollection CurrentPoints
            {
                get { return currentPoints; }
            }
            ///
            /// 当前点索引
            ///

            private int index;
            ///
            /// 当前点索引
            ///

            public int Index
            {
                get { return index; }
            }
            ///
            /// 是否取消点绘制
            ///

            private bool isCancel = false;
            ///
            /// 是否取消点绘制
            ///

            public bool IsCancel
            {
                get { return isCancel; }
                set { isCancel = value; }
            }
        }

        ///


        /// 添加一个点时激发事件
        ///

        public static event AddHandle AddPoint;
        #endregion
        #region 总是设置polyline为一个虚构的点,在完成Drag后,此点会被移除
        ///
        /// 总是设置polyline为一个虚构的点,在完成Drag后,此点会被移除
        ///

        public void AddLatestVertex()
        {
            m_pts.Add(m_tempPoint);
            Polyline pline = Entity as Polyline;
            pline.AddVertexAt(pline.NumberOfVertices, new Point2d(m_tempPoint.X, m_tempPoint.Y), 0, 0, 0);
            if (AddPoint != null)
            {
                EventArgs e = new EventArgs(m_pts, m_pts.Count - 1);
                e.IsCancel = false;
                AddPoint(e);
                if (e.IsCancel)
                {
                    RemoveLastVertex();
                }
            }
        }
        #endregion

        #region 取样
        ///


        /// 取样
        ///

        ///
        ///
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigOpts = new JigPromptPointOptions();
            jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates |
                UserInputControls.NullResponseAccepted |
                UserInputControls.NoNegativeResponseAccepted);
            if (m_pts.Count == 0)
            {
                jigOpts.Message = "\n请输入起点坐标 ";
            }
            else if (m_pts.Count > 0)
            {
                jigOpts.BasePoint = m_pts[m_pts.Count - 1];
                jigOpts.UseBasePoint = true;
                jigOpts.Message = "\n请输入下一个点[或按ESC退出] ";
            }
            else
                return SamplerStatus.Cancel;
            PromptPointResult res = prompts.AcquirePoint(jigOpts);
            if (m_tempPoint == res.Value)
            {
                return SamplerStatus.NoChange;
            }
            else if (res.Status == PromptStatus.OK)
            {
                m_tempPoint = res.Value;
                return SamplerStatus.OK;
            }
            return SamplerStatus.Cancel;
        }
        #endregion


        #region 更新
        ///


        /// 更新
        ///

        ///
        protected override bool Update()
        {
            Polyline pline = Entity as Polyline;
            pline.SetPointAt(pline.NumberOfVertices - 1, m_tempPoint.Convert2d(m_plane));
            Matrix3d m = Matrix3d.Displacement(new Vector3d(0, 0, 1000));
            pline.TransformBy(m);
            return true;
        }
        #endregion
    }

其中confirm是一个窗体。用来和用户交互信息,实现交互操作

 

auto CAD 二次开发 c#.net 之JIG画多段线(polyline)_第1张图片

 

后台代码为:

 ///


    ///

    public partial class Confirm : Form
    {
        ///
        ///

        // ARXClass _aRXClass = null;

        #region 构造方法
        ///


        /// 构造方法
        ///

        public Confirm()
        {
            //    _aRXClass = new ARXClass();
            InitializeComponent();
        }
        #endregion

        #region 操作类型
        ///


        /// 操作类型
        ///

        public enum Operate
        {
            ///
            /// 继续
            ///

            Continue = 0,
            ///
            /// 后退
            ///

            Back = 1,
            ///
            /// 确定
            ///

            Accept = 2
        }
        #endregion

        #region 类成员
        ///


        /// 类成员
        ///

        public Operate _operrateType = Operate.Continue;
        #endregion

        #region 点击继续按钮激发事件
        ///


        /// 点击继续按钮激发事件
        ///

        private void btnContinue_Click(object sender, EventArgs e)
        {
            this._operrateType = Operate.Continue;
            this.Close();
        }
        #endregion

        #region 点击回退按钮激发事件
        ///


        /// 点击回退按钮激发事件
        ///

        private void btnBack_Click(object sender, EventArgs e)
        {
            this._operrateType = Operate.Back;
            this.Close();
        }
        #endregion

        #region 点击确定按钮激发事件
        ///


        /// 点击确定按钮激发事件
        ///

        private void btnAccept_Click(object sender, EventArgs e)
        {
            // 确定
            this._operrateType = Operate.Accept;
            // 关闭窗体
            this.Close();
        }
        #endregion

    }

 

以上方法仅供参考。

若各位大侠手里有更好的方法,请顺便贴一下,谢谢。

如有问题:请加qq1419226548或QQmail给我。 若转载,请注明出处。谢谢。

 

 

你可能感兴趣的:(auto CAD 二次开发 c#.net 之JIG画多段线(polyline))