.NET

Reply
Mentor
amitk_189
Posts: 242
Registered: ‎12-15-2011
Message 1 of 4 (182 Views)
Accepted Solution

Ray Tracing Kind

182 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

 

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'~

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 4 (173 Views)

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
Mentor
amitk_189
Posts: 242
Registered: ‎12-15-2011
Message 3 of 4 (158 Views)

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

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 4 (152 Views)

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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!