• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Distinguished Contributor
    amitk_189
    Posts: 169
    Registered: ‎12-15-2011
    Accepted Solution

    Ray Tracing Kind

    110 Views, 3 Replies
    06-14-2012 10:08 PM

    I am doing a project where we should detect center of object and automatically draw lines in all directions

     

    example: Lets consider a rectangle, locate center of rectangle and draw lines on all directions

     

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Ray Tracing Kind

    06-15-2012 04:10 AM in reply to: amitk_189

    For rectangle it would be easy to doing:

            [CommandMethod("testDrawLines","tdl", CommandFlags.Modal)]
            public static void DrawLines()
            {
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    
                Database db = doc.Database;
    
                Editor ed = doc.Editor;
    
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        PromptEntityOptions peo = new PromptEntityOptions("\nSelect polyline: ");
    
                        peo.SetRejectMessage("Select polyline only!");
    
                        peo.AddAllowedClass(typeof(Polyline), true);
    
                        PromptEntityResult res = doc.Editor.GetEntity(peo);
    
                        if (res.Status != PromptStatus.OK) return;
    
                        Entity ent = tr.GetObject(res.ObjectId, OpenMode.ForRead, false) as Entity;
    
                        Polyline pline = ent as Polyline;
    
                        if (pline == null)
                        {
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("No luck");
    
                            return;
                        }
                        
                            Point3d lp = pline.GeometricExtents.MinPoint;
    
                            Point3d up = pline.GeometricExtents.MaxPoint;
    
                            Point3d cp = new Point3d((up.X+lp.X)/2,(up.Y+lp.Y)/2,(up.Z+lp.Z)/2);
    
                            double leng = Math.Abs(up.X - lp.X);
    
                            double wid = Math.Abs(up.Y - lp.Y);
    
                            if (leng > wid)
                                leng = wid;
    
                            int num = 16;
    
                            double ang = Math.PI * 2 / num;
    
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
    
                            for (int i = 0; i < num-1; i++)
                            {
                                Point3d ep = PolarPoint(cp, i * ang, leng);
    
                                Line ln = new Line(cp, ep);
    
                                Point3dCollection ips = new Point3dCollection();
    
                                ln.IntersectWith(pline, Intersect.ExtendArgument, ips,0, 0);
    
                                Point3d ip = ips[0];
    
                                ln.EndPoint = ip;
    
                                btr.AppendEntity(ln);
    
                                tr.AddNewlyCreatedDBObject(ln, true);
                            }
                            tr.Commit();
                      
    
                    }
                    catch (System.Exception ex)
                    {
                        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                    }
    
                }
    
            }

     

            public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
            {
                // credits to Tony Tanzillo
                return new Point3d(
                basepoint.X + (distance * Math.Cos(angle)),
                basepoint.Y + (distance * Math.Sin(angle)),
                basepoint.Z);
            }

     

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Distinguished Contributor
    amitk_189
    Posts: 169
    Registered: ‎12-15-2011

    Re: Ray Tracing Kind

    06-17-2012 11:10 PM in reply to: amitk_189

    Getting Error on 

     

    Point3d ip = ips[0];

     

    No data is present in here and we have initilised it not assigned data

     

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Ray Tracing Kind

    06-18-2012 02:43 AM in reply to: amitk_189

    Try again

            [CommandMethod("testDrawLines", "tdl", CommandFlags.Modal)]
            public static void DrawLines()
            {
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    
                Database db = doc.Database;
    
                Editor ed = doc.Editor;
    
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        PromptEntityOptions peo = new PromptEntityOptions("\nSelect polyline: ");
    
                        peo.SetRejectMessage("Select polyline only!");
    
                        peo.AddAllowedClass(typeof(Polyline), true);
    
                        PromptEntityResult res = doc.Editor.GetEntity(peo);
    
                        if (res.Status != PromptStatus.OK) return;
    
                        Entity ent = tr.GetObject(res.ObjectId, OpenMode.ForRead, false) as Entity;
    
                        Polyline pline = ent as Polyline;
    
                        if (pline == null)
                        {
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("No luck");
    
                            return;
                        }
    
                        Point3d lp = pline.GeometricExtents.MinPoint;
    
                        Point3d up = pline.GeometricExtents.MaxPoint;
    
                        Point3d cp = new Point3d((up.X + lp.X) / 2, (up.Y + lp.Y) / 2, (up.Z + lp.Z) / 2);
    
                        double leng = Math.Abs(up.X - lp.X);
    
                        double wid = Math.Abs(up.Y - lp.Y);
    
                        if (leng < wid)
                            leng = wid;
                        // number of lines
                        int num = 16;
    
                        double ang = Math.PI * 2 / num;
    
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
    
                        for (int i = 0; i < num - 1; i++)
                        {
                            Point3d ep = PolarPoint(cp, i * ang, leng);
    
                            Line ln = new Line(cp, ep);
    
                            ln.Normal = pline.Normal;
    
                            Point3dCollection ips = new Point3dCollection();
    
                            ln.IntersectWith(pline, Intersect.ExtendArgument, ips, (int)IntPtr.Zero, (int)IntPtr.Zero);
    
                            Point3d ip = ips[0];
    
                            ln.EndPoint = ip;
    
                            btr.AppendEntity(ln);
    
                            tr.AddNewlyCreatedDBObject(ln, true);
                        }
                        tr.Commit();
    
    
                    }
                    catch (System.Exception ex)
                    {
                        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                    }
    
                }
    
            }

     

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.