Author Topic: place Mtext to middle between two curves  (Read 658 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
place Mtext to middle between two curves
« on: January 25, 2013, 09:03:36 PM »
This code allow to add many lines in the command line one by another in loop
To start command type : mit
 
Code: [Select]
      [CommandMethod("PlaceMtextToMidpoint","mit", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public void PlaceMtextToMiddle()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect the first curve: ");
            peo.SetRejectMessage("Only a curve !");
            peo.AddAllowedClass(typeof(Curve), false);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            ObjectId id1 = per.ObjectId;
            Point3d p1 = per.PickedPoint;
            peo.Message = "\nSelect the second curve: ";
            per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            ObjectId id2 = per.ObjectId;

            PromptStringOptions pso = new PromptStringOptions("\nEnter first line of Mtext: ");
            pso.AllowSpaces = true;
            PromptResult res;
            res = ed.GetString(pso);
            if (res.Status != PromptStatus.OK)
                return;

            string txtStr = res.StringResult;
            do
            {
                pso.Message = "\nEnter next line of Mtext: ";
                res = ed.GetString(pso);
                if (res.Status != PromptStatus.OK)
                    return;
                txtStr = txtStr + "\\P" + res.StringResult;
            } while (res.Status != PromptStatus.OK);

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

                DBObject obj1 = (DBObject)tr.GetObject(id1, OpenMode.ForRead, false);
                Curve curve1 = obj1 as Curve;

                if (curve1 == null)
                {
                    ed.WriteMessage("\nNo curve selected\n");
                    return;
                }
                DBObject obj2 = (DBObject)tr.GetObject(id2, OpenMode.ForRead, false);
                Curve curve2 = obj2 as Curve;

                if (curve2 == null)
                {
                    ed.WriteMessage("\nNo curve selected\n");
                    return;
                }
                Point3d p2 = curve2.GetClosestPointTo(p1, false);

                Plane plan = curve1.GetPlane();

                Vector3d norm = plan.Normal;

                p1 = p1.TransformBy(Matrix3d.WorldToPlane(norm));

                p2 = p2.TransformBy(Matrix3d.WorldToPlane(norm));
                // calculate middle point one way
                Point3d mp = p1.Add(p2 - p1).ScaleBy(0.5, p1).TransformBy(Matrix3d.WorldToPlane(norm));

                double ang = (p2 - p1).AngleOnPlane(plan);
                // draw vectors for imagination
                ed.DrawVector(p1, mp, 1, true);

                ed.DrawVector(p2, mp, 5, true);
                // set angle to be more readable
                if ((ang > Math.PI / 2) && (ang < Math.PI * 1.5))
                    ang = ang + Math.PI;
                // create mtext
                MText mtxt = new MText();
                mtxt.SetDatabaseDefaults();
                mtxt.Attachment = AttachmentPoint.MiddleCenter;
                mtxt.Location = mp;
                mtxt.TextHeight = db.Textsize;
                mtxt.Width = 0.0;
                mtxt.Contents = txtStr;
                mtxt.Layer = "0";
                mtxt.ColorIndex = 256;
                Matrix3d mx = Matrix3d.Rotation(ang, norm, mp);
                mtxt.TransformBy(mx);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                // add mtext to database and transaction
                btr.AppendEntity(mtxt);
                tr.AddNewlyCreatedDBObject(mtxt, true);

                tr.Commit();
            }
        }