.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Ray Tracing Kind

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
amitnkukanur
422 Views, 3 Replies

Ray Tracing Kind

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

 

Senior Software Engineer
3 REPLIES 3
Message 2 of 4
Hallex
in reply to: amitnkukanur

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
Message 3 of 4
amitnkukanur
in reply to: amitnkukanur

Getting Error on 

 

Point3d ip = ips[0];

 

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

 

Senior Software Engineer
Message 4 of 4
Hallex
in reply to: amitnkukanur

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost