Author Topic: Create arc by 3 points  (Read 936 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
Create arc by 3 points
« on: January 08, 2013, 07:43:41 PM »
Code: [Select]
        [CommandMethod("CreateArcBy3Points", "cra3p", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public void CreateArcByThreePoints()
        {
            short osm = (short)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("osmode");
            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 0);
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            // get the points

            Point3d p1, p2, p3;
            PromptPointOptions ppo = new PromptPointOptions("\nFirst point: ");
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return;
            p1 = ppr.Value;

            ppo = new PromptPointOptions("\nSecond point: ");
            ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return;
            p2 = ppr.Value;

            ppo = new PromptPointOptions("\nThird point: ");
            ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return;
            p3 = ppr.Value;

            // create a CircularArc3d
           
            CircularArc3d carc = new CircularArc3d(p1, p2, p3);

            Arc arc = null;

            // now convert the CircularArc3d to an Arc
            Point3d cpt = carc.Center;
            Vector3d normal = carc.Normal;
            Vector3d refVec = carc.ReferenceVector;
            Plane plan = new Plane(cpt, normal);
            double ang = refVec.AngleOnPlane(plan);
            arc = new Arc(cpt, normal, carc.Radius, carc.StartAngle + ang, carc.EndAngle + ang);

            arc.SetDatabaseDefaults();
 
            // dispose CircularArc3d
            carc.Dispose();


            // get the current database

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                    // get the current space
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    // append arc to space
                    btr.AppendEntity(arc);
                    // add arc to transaction
                    tr.AddNewlyCreatedDBObject(arc, true);
                    tr.Commit();
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                }
                finally
                {
                    //do nothing (optional)
                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", osm);
                }
            }

        }