Author Topic: Circle by 3 points  (Read 537 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
Circle by 3 points
« on: May 06, 2012, 01:14:55 PM »
Code: [Select]
        [CommandMethod("circ3p")]// This method can have any name
        public static void TestFor3PointCircle()
        {
            // Get the document and database
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            // If we have a valid point selection, get all 3 points
           PromptPointOptions ppo =   new PromptPointOptions("\nPick a point : ");
            ppo.AllowNone = true;
            PromptPointResult ppr;
                ppr = ed.GetPoint(ppo);
                if (ppr.Status != PromptStatus.OK) return;
            Point3d p1= ppr.Value;
            ppo =   new PromptPointOptions("\nPick a second point : ");
                 ppr = ed.GetPoint(ppo);
                if (ppr.Status != PromptStatus.OK) return;
            Point3d p2= ppr.Value;
                        ppo =   new PromptPointOptions("\nPick a third point : ");
                 ppr = ed.GetPoint(ppo);
                if (ppr.Status != PromptStatus.OK) return;
            Point3d p3= ppr.Value;
            // To pass center point by reference
            Point3d pc = new Point3d();
            // Get result
            bool result = CircleCenter(p1, p2, p3, out pc);
            if (result)
                ed.WriteMessage("\nCenter point calculated:\t{0:f3},{1:f3},{2:f3}", pc.X, pc.Y, pc.Z);
            else return;
            // Calculate radius
            double rad = pc.DistanceTo(p1);
            // Open transaction and add circle to the current space
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Circle circ = new Circle(pc, Vector3d.ZAxis, rad);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                btr.AppendEntity(circ);
                tr.AddNewlyCreatedDBObject(circ, true);
                // Commit transaction
                tr.Commit();
               
            }
         
        }

        public static bool CircleCenter(Point3d p0, Point3d pm, Point3d p1, out Point3d pc)
        {
            Vector3d v1 = p0 - pm;

            Vector3d v2 = p1 - pm;

            if (v1.IsParallelTo(v2))
            {
                pc = new Point3d();

                return false;
            }

            CircularArc3d crc = new CircularArc3d(p0, pm, p1);

            pc = crc.Center;

            return true;
        }

    }

Regards,

~'J'~