Community
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
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
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'~
Getting Error on
Point3d ip = ips[0];
No data is present in here and we have initilised it not assigned data
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'~