Author Topic: Extend leader to selected curve  (Read 490 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
Extend leader to selected curve
« on: February 05, 2013, 11:50:26 AM »
 
Code: [Select]
       [CommandMethod("Lex")]
        public void testLeaderExtend()
        {
            // Get active document and database
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            // Prompt the user to select a leader.
            PromptEntityOptions pso = new PromptEntityOptions("\nPlease select a leader ");
            pso.SetRejectMessage("\nSelect leader only ");
            pso.AddAllowedClass(typeof(Leader), false);
            PromptEntityResult res = ed.GetEntity(pso);
            if (res.Status != PromptStatus.OK) return;
            // Prompt the user to select a curve.
            PromptEntityOptions pno = new PromptEntityOptions("\nPlease select a single curve to extend leader to: ");
            pso.SetRejectMessage("\nSelect curve only ");
            pso.AddAllowedClass(typeof(Curve), false);
            PromptEntityResult nres = ed.GetEntity(pno);
            if (nres.Status != PromptStatus.OK) return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // open current space for write to modify leader
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                ObjectId ldid;
                ldid = res.ObjectId;
                ObjectId crid;
                crid = nres.ObjectId;
                Leader leader = tr.GetObject(ldid, OpenMode.ForRead) as Leader;
                if (leader == null) return;
                Point3d arrPt = leader.VertexAt(0);
                ed.WriteMessage("\nPoint on arrow:\t({0:f3}\n", arrPt);
                Curve curv = tr.GetObject(crid, OpenMode.ForRead) as Curve;
                if (curv == null) return;
                // Get point on curve
                Point3d nearPt = curv.GetClosestPointTo(nres.PickedPoint, false).TransformBy(Matrix3d.Identity);
                ed.WriteMessage("\nPoint on line:\t({0:f3}\n", nearPt);
                leader.UpgradeOpen();
                leader.SetVertexAt(0, nearPt);
                leader.RecordGraphicsModified(true);
                // Commit transaction
                tr.Commit();
            }
        }