明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1226|回复: 2

[几何] 仿Polyline功能

[复制链接]
发表于 2018-7-25 21:19 | 显示全部楼层 |阅读模式

Jig不好用,界面一直闪,自己写个玩玩,请前辈们指点一下,代码不够严谨

[CommandMethod("DrawPolyline")]
        public void DrawPolyline()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            DocumentLock dlock = doc.LockDocument();

            //为临时图形得到当前的颜色
            Autodesk.AutoCAD.Colors.Color col = doc.Database.Cecolor;

            //创建一个点集来存储顶点
            Point3dCollection pts = new Point3dCollection();

            //为所有的顶点设置选择选项
            PromptPointOptions opt = new PromptPointOptions("\n设置线段起点或者取消[Undo]", "Undo");

            //存储临时线段
            List<ObjectId> lines = new List<ObjectId>();

            opt.AllowNone = true;

            //得到的 polyline 第一点
            PromptPointResult res = ed.GetPoint(opt);

            while (res.Status == PromptStatus.OK || res.Status == PromptStatus.Keyword)//有点被选中时的操作
            {
                pts.Add(res.Value);

                if (pts.Count > 1)
                {
                    using (Transaction tran = db.TransactionManager.StartTransaction())
                    {

                        BlockTableRecord btrs = (BlockTableRecord)tran.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        Line line = new Line(pts[pts.Count - 2], pts[pts.Count - 1]);
                        lines.Add(btrs.AppendEntity(line));
                        tran.AddNewlyCreatedDBObject(line, true);
                        tran.Commit();
                    }
                }

                //撤销的时候从这里重新进入循环
            undo:
                opt.BasePoint = pts[pts.Count - 1];

                opt.UseBasePoint = true;

                opt.SetMessageAndKeywords("\n设置线段顶点或者取消[Undo]", "Undo");

                res = ed.GetPoint(opt);

                if (res.Status == PromptStatus.Keyword)
                {
                    if (res.StringResult.ToLower() == "undo")
                    {
                        if (lines.Count > 0)
                        {
                            using (Transaction tran = db.TransactionManager.StartTransaction())
                            {
                                lines[lines.Count - 1].GetObject(OpenMode.ForWrite).Erase(true);
                                lines.RemoveAt(lines.Count - 1);
                                tran.Commit();
                            }
                            pts.RemoveAt(pts.Count - 1);
                            goto undo;
                        }
                        if (pts.Count == 1)
                        {
                            goto undo;
                        }
                    }
                }
                else if (res.Status != PromptStatus.OK)
                {
                    break;
                }
            }

            if (pts.Count >= 1)
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    //获取当前的模型/图纸空间
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                    //得到当前的 UCS
                    Matrix3d ucs = ed.CurrentUserCoordinateSystem;

                    Point3d origin = new Point3d(0, 0, 0);

                    Vector3d normal = Vector3d.ZAxis;

                    //创建一个临时的 plane 来帮助计算
                    Plane plane = new Plane(origin, normal);

                    //创建 polyline,同时用已经选择的点的数量来指定顶点数
                    Polyline pline = new Polyline(pts.Count);

                    pline.Normal = normal;
                    foreach (Point3d pt in pts)//对点进行坐标转换,为什么要转换,UCS与 OCS的关系?
                    {
                        Point3d transformedPt = pt.TransformBy(ucs);
                        pline.AddVertexAt(pline.NumberOfVertices, plane.ParameterOf(transformedPt), 0, 0, 0);
                    }

                    ObjectId plineId = btr.AppendEntity(pline);
                    tr.AddNewlyCreatedDBObject(pline, true);

                    //将临时线段从数据库中删除
                    foreach (var item in lines)
                    {
                        item.GetObject(OpenMode.ForWrite).Erase(true);
                    }

                    tr.Commit();
                }
            }
            dlock.Dispose();
        }


发表于 2018-7-28 10:22 | 显示全部楼层
不懂VBA的路过
发表于 2018-8-4 09:24 | 显示全部楼层
下载试用,解决问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-29 15:19 , Processed in 0.195977 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表