找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 957|回复: 0

[分享] Autonumbering with prefix

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-7 03:08:13 | 显示全部楼层 |阅读模式

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

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

×
        /// <summary>
          /// function to return increment string
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="start"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        private static string TextInc(string prefix, string start, int step)
        {
            string result = "";
            string head = "";
            bool szero = false;
            int leg = (prefix + start).Length;
            if (start.StartsWith("0"))
            {
                szero = true;
                int pos = start.LastIndexOf("0");

                for (int n = 0; n <= pos; n++)
                    head = head + "0";
            }
            int num = Convert.ToInt32(start) + step;

            if (szero)
            {
                result = String.Concat(prefix, head, num.ToString());
                if (result.Length > leg)
                {
                    if (head.Length > 1)
                        result = String.Concat(prefix, head.Remove(1, result.Length - leg), num.ToString());
                    else
                        result = String.Concat(prefix, num.ToString());
                }
            }
            else
            {
                result = String.Concat(prefix, num.ToString());
            }
            return result;

        }

        /// <summary>
        ///  Command for autonumbering with text with prefix
        /// </summary>
        [CommandMethod("inm")]
        public static void IncrementWithFrame()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Matrix3d ucs = ed.CurrentUserCoordinateSystem;
            // Get or discard prefix as string entered by the user
            PromptStringOptions pso =
                 new PromptStringOptions("\n\tEnter prefix or press Enter w/o it: ");
            pso.AllowSpaces = true;
            pso.DefaultValue = "";
            PromptResult res;
            res = ed.GetString(pso);
            if (res.Status != PromptStatus.OK)  return;

            string pfx = res.StringResult;
            int pos = pfx.Length;
            ed.WriteMessage("\nPrefix Entered:\t{0}", pfx);
            // Get initial number as string entered by the user
            pso = new PromptStringOptions("\nEnter initial number  : ");
            pso.UseDefaultValue = true;
            pso.DefaultValue = "0001";

            res = ed.GetString(pso);
            if (res.Status != PromptStatus.OK)return;

            string strnum = res.StringResult;
            ed.WriteMessage("\ninitial number:\t{0}", strnum);

            PromptIntegerOptions pio = new PromptIntegerOptions("");
            pio.Message = "\nEnter an increment step: ";

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

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

            int step = ires.Value;

            ed.WriteMessage("\nStep entered\t{0}", step);
            // Get gap value entered by the user
            PromptDoubleOptions pdo =
                           new PromptDoubleOptions("\nEnter the gap size between text and frame  : ");
            pdo.AllowNone = true;
            pdo.UseDefaultValue = true;
            pdo.DefaultValue = db.Textsize / 2;

            PromptDoubleResult dbres;
            dbres = ed.GetDouble(pdo);
            if (res.Status != PromptStatus.OK)  return;

            double gap = dbres.Value;

            ed.WriteMessage("\nGap Entered\t{0}", gap);
            // using a current textsize
            double txtheight = db.Textsize;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                doc.TransactionManager.EnableGraphicsFlush(true);

                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                PromptPointOptions ppo = new PromptPointOptions("\n\tSpecify text insertion point (Enter to stop): ");
                ppo.AllowNone = true;
                PromptPointResult ptres;
                int n = 0;

                string txt = "";
                // get text points multiple
                do
                {
                    Point3d pt = new Point3d();
                    ptres = ed.GetPoint(ppo);

                    if (ptres.Status == PromptStatus.OK)
                    {
                        pt = ptres.Value;
                        DBText otext = new DBText();
                        otext.Position = pt;
                        otext.VerticalMode = TextVerticalMode.TextVerticalMid;
                        otext.HorizontalMode = TextHorizontalMode.TextMid;
                        otext.AlignmentPoint = otext.Position;
                        otext.AdjustAlignment(db);
                        if (n == 0)
                        {
                            txt = TextInc(pfx, strnum, 0);

                        }
                        else
                            txt = TextInc(pfx, strnum, step);

                        otext.TextString = txt;
                        
                        n += 1;
                        strnum = txt.TrimStart(pfx.ToCharArray());
                        
                        btr.AppendEntity(otext);
                        tr.AddNewlyCreatedDBObject(otext, true);
                        tr.TransactionManager.QueueForGraphicsFlush();
                        Plane plan = otext.GetPlane();
                        Extents3d ext = otext.GeometricExtents;
                        Point3d bl = ext.MinPoint;
                        Point3d ur = ext.MaxPoint;
                        Point3d mp = new Point3d((bl.X + ur.X) / 2, (bl.Y + ur.Y) / 2, (bl.Z + ur.Z) / 2).TransformBy(ucs);
                        otext.Position = mp;
                        otext.AlignmentPoint = otext.Position;
                        otext.AdjustAlignment(db);

                        Point3d br = new Point3d(ur.X, bl.Y, bl.Z);
                        Point3d ul = new Point3d(bl.X, ur.Y, bl.Z);
                        bl = bl.Subtract(new Vector3d(gap, gap, 0)).TransformBy(ucs);
                        ur = ur.Subtract(new Vector3d(-gap, -gap, 0)).TransformBy(ucs);
                        br = br.Subtract(new Vector3d(-gap, gap, 0)).TransformBy(ucs);
                        ul = ul.Subtract(new Vector3d(gap, -gap, 0)).TransformBy(ucs);
                        Polyline poly = new Polyline();
                        poly.AddVertexAt(0, bl.Convert2d(plan), 0, 0, 0);
                        poly.AddVertexAt(0, br.Convert2d(plan), 0, 0, 0);
                        poly.AddVertexAt(0, ur.Convert2d(plan), 0, 0, 0);
                        poly.AddVertexAt(0, ul.Convert2d(plan), 0, 0, 0);
                        poly.Closed = true;
                        poly.ColorIndex = 1;
                        btr.AppendEntity(poly);
                        tr.AddNewlyCreatedDBObject(poly, true);
                        tr.TransactionManager.QueueForGraphicsFlush();

                    }

                } while (ptres.Status == PromptStatus.OK);
                doc.TransactionManager.FlushGraphics();
                tr.Commit();
            }

        }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-22 15:28 , Processed in 0.347384 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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