找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 705|回复: 0

[分享] Draw grid by picking corners

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-7 01:14:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
        public static void TestGraph()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Matrix3d ucs = ed.CurrentUserCoordinateSystem;
            PromptPointOptions ppo = new PromptPointOptions("\nJust a Lower Left corner: ");
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK)
            {
                return;
            }
            PromptCornerOptions pco = new PromptCornerOptions("\nUpper Right corner: ", ppr.Value);
            PromptPointResult pcr = ed.GetCorner(pco);
            if (pcr.Status != PromptStatus.OK)
            {
                return;
            }
            Point3d p1 = ppr.Value;
            Point3d p2 = pcr.Value;
            if (((p1.X == p2.X || p1.Y == p2.Y) & (p2.X > p1.X || p2.Y > p1.Y)))
            {
                ed.WriteMessage("\nWrong coordinate specification");
                return;
            }
            // Calculate the four corner points of the box
            // and add them to a Point3dCollection:
            Point3dCollection points = new Point3dCollection();
            points.Add(p1);
            points.Add(new Point3d(p2.X, p1.Y, 0.0));
            points.Add(p2);
            points.Add(new Point3d(p1.X, p2.Y, 0.0));
            double leng = 0;
            leng = p2.X - p1.X;
            double wid = 0;
            wid = p2.Y - p1.Y;

            //'-------------------------------------------------------''
            PromptIntegerOptions pio = new PromptIntegerOptions("");
            pio.Message = "\nEnter the number of rows: ";

            // Restrict input to positive and non-negative values
            pio.AllowZero = false;
            pio.AllowNegative = false;
            // Add default value
            pio.DefaultValue = 4;
            pio.AllowNone = true;

            // Get the value entered by the user
            PromptIntegerResult ires = ed.GetInteger(pio);
            if (ires.Status != PromptStatus.OK)
            {
                return;
            }

            int rownum = ires.Value;

            pio.Message = "\nnEnter the number of columns: ";

            // Restrict input to positive and non-negative values
            pio.AllowZero = false;
            pio.AllowNegative = false;
            // Add default value
            pio.DefaultValue = 8;
            pio.AllowNone = true;

            // Get the value entered by the user
            PromptIntegerResult ires2 = ed.GetInteger(pio);
            if (ires2.Status != PromptStatus.OK)
            {
                return;
            }

            int colnum = ires2.Value;

            double rowstep = 0;
            rowstep = wid / rownum;

            double colstep = 0;
            colstep = leng / colnum;
            
            Database db = ed.Document.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                Plane plan = new Plane(Point3d.Origin, Vector3d.ZAxis);
                Polyline poly = new Polyline(4);
                int i = 0;
                foreach (Point3d pt in points)
                {
                    poly.AddVertexAt(i, pt.Convert2d(plan), 0, 0, 0);
                    i += 1;
                }
                poly.Closed = true;
                btr.AppendEntity(poly);
                tr.AddNewlyCreatedDBObject(poly, true);

                Point3dCollection vps = new Point3dCollection();
                Point3dCollection hps = new Point3dCollection();
                for (i = 1; i <= rownum - 1; i++)
                {
                    vps.Add(new Point3d(p1.X, p1.Y + (i * rowstep), p1.Z));
                }
                for (i = 1; i <= colnum - 1; i++)
                {
                    hps.Add(new Point3d(p1.X + (i * colstep), p1.Y, p1.Z));
                }
                Line ln = new Line();

                foreach (Point3d pt in vps)
                {
                    ln = new Line(pt, new Point3d(pt.X + leng, pt.Y, 0));
                    btr.AppendEntity(ln);
                    tr.AddNewlyCreatedDBObject(ln, true);
                }
                foreach (Point3d pt in hps)
                {
                    ln = new Line(pt, new Point3d(pt.X, pt.Y + wid, 0));
                    btr.AppendEntity(ln);
                    tr.AddNewlyCreatedDBObject(ln, true);
                }
                tr.Commit();
            }
        }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-22 13:14 , Processed in 0.432303 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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