Community
How to get a point in the polyline with a set distance and obeying my current ucs. A projection of the point as an orthogonal snap.
I wrote a code creating a polyline between the lowest and the highest initial polyline and obtaining the intersection. But when it comes to many points the rendering is quite time consuming.
Try this code, this one is from my working project.
slightly edited per your needs
public void testDividePoly() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = HostApplicationServices.WorkingDatabase; Editor ed = doc.Editor; Matrix3d ucs = ed.CurrentUserCoordinateSystem; doc.TransactionManager.EnableGraphicsFlush(true); PromptEntityOptions peo = new PromptEntityOptions("\nSelect Polyline >>"); peo.SetRejectMessage("\nSelect LW Polyline only!\n"); peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline), false); PromptEntityResult res; res = ed.GetEntity(peo); if (res.Status != PromptStatus.OK) return; Point3d pickPt = res.PickedPoint; Autodesk.AutoCAD.DatabaseServices.Polyline lwpoly; Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead); if (ent == null) return; Curve curve = ent as Curve; if (curve == null) return; lwpoly = curve as Autodesk.AutoCAD.DatabaseServices.Polyline; Point3d sp = lwpoly.StartPoint; Point3d ep = lwpoly.EndPoint; Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("orthomode", 1);//optional PromptPointOptions pto = new PromptPointOptions("\nSpecify point below the polyle : "); pto.UseBasePoint = true; pto.BasePoint=pickPt; PromptPointResult ppr = ed.GetPoint(pto); if (ppr.Status != PromptStatus.OK) return; Point3d dp = ppr.Value; //double ofp = dp.Y-(db.Dimtxt * 5.0) ; int i = 0; BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); //iterate through polyline vertices for (i = 0; i < lwpoly.NumberOfVertices-1; i++) { Point3d p1 = lwpoly.GetPoint3dAt(i).TransformBy(ucs); Point3d p2 = lwpoly.GetPoint3dAt(i + 1).TransformBy(ucs); Point3d tp = new Point3d(p1.X, dp.Y, p1.Z).TransformBy(ucs); //Create Rotated dimension,set line points and dimension text position as default RotatedDimension adim = new RotatedDimension(0.0, p1, p2, tp, "", db.Textstyle);// may use to put measurement instead adim.Normal = lwpoly.Normal; adim.SetDatabaseDefaults(); // force to draw objects tr.TransactionManager.QueueForGraphicsFlush(); btr.AppendEntity(adim); tr.AddNewlyCreatedDBObject(adim, true); } doc.TransactionManager.FlushGraphics(); tr.Commit(); ed.UpdateScreen();//optional Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("orthomode", 0);//optional } }
Try this one, I haven't have a time for testing,
probably the last 2 options in IntersectWith metod may have other types
in your ACAD release...
public void testAddLineAtDistAlongPline() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = HostApplicationServices.WorkingDatabase; Editor ed = doc.Editor; Matrix3d ucs = ed.CurrentUserCoordinateSystem; short osm = (short)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("osmode"); PromptEntityOptions peo = new PromptEntityOptions("\nSelect Polyline >>"); peo.SetRejectMessage("\nSelect LW Polyline only!\n"); peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline), false); PromptEntityResult res; res = ed.GetEntity(peo); if (res.Status != PromptStatus.OK) return; Autodesk.AutoCAD.DatabaseServices.Polyline lwpoly; try { Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead); if (ent == null) return; Curve curve = ent as Curve; if (curve == null) return; lwpoly = curve as Autodesk.AutoCAD.DatabaseServices.Polyline; Point3d sp = lwpoly.StartPoint; Point3d ep = lwpoly.EndPoint; Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("orthomode", 1);//optional Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 1);//optional PromptPointOptions pto = new PromptPointOptions("\nSpecify start point of vertical line : "); PromptPointResult ppr = ed.GetPoint(pto); if (ppr.Status != PromptStatus.OK) return; Point3d dp1 = ppr.Value; PromptDistanceOptions dsr = new PromptDistanceOptions("\nSpecify dimension : "); dsr.UseBasePoint = true; dsr.BasePoint = dp1; dsr.UseDashedLine = true; PromptDoubleResult ddr = ed.GetDistance(dsr); if (ddr.Status != PromptStatus.OK) return; double dist = ddr.Value; Point3d dp2 = new Point3d(dp1.X + dist, dp1.Y, lwpoly.Elevation).TransformBy(ucs); Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 0); Point3dCollection pts = new Point3dCollection(); // use temp ray to get intersection Ray ray = new Ray(); using (ray) { ray.BasePoint = dp2; ray.UnitDir = new Vector3d(0, 1, lwpoly.Elevation).TransformBy(ucs); lwpoly.IntersectWith(ray, Intersect.OnBothOperands, pts, (int)IntPtr.Zero, (int)IntPtr.Zero); } if (pts == null) return; Line ln = new Line(dp2, pts[0].TransformBy(ucs)); ln.SetDatabaseDefaults(); ln.ColorIndex = 2; BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); btr.AppendEntity(ln); tr.AddNewlyCreatedDBObject(ln, true); // here you put dimension or text in the middle of points dp1 and dp2... tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message); } finally { Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("orthomode", 0);//optional Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", osm);//optional } }