Try this one
BoundBlock2d bb = arc.BoundBlock; Point2d minp = bb.GetMinimumPoint(); Point2d maxp = bb.GetMaximumPoint();
~'J'~
Why the attacched polyline has minimum point negative?
MinimumPoint:(-0.0784183035384186,-0.0333440291205946),
MaximumPoint: (0.339761703945585,0.452622696834632)
fro2001 is your resuts based on my disegno2.dwg?
Here is my code that produce the output:
MinimumPoint: (-0.0784183035384186,-0.0333440291205946),
MaximumPoint: (0.339761703945585,0.452622696834632)
Document doc Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; PromptEntityResult per = ed.GetEntity("Select a polyline\n"); if (per.Status == PromptStatus.OK) { Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { DBObject obj = tr.GetObject(per.ObjectId, OpenMode.ForRead); Polyline lwp = obj as Polyline; if (lwp != null) { int vn = lwp.NumberOfVertices; for (int i = 0; i < vn; i++) { SegmentType sg = lwp.GetSegmentType(i); if (sg.ToString().CompareTo("Arc") == 0) { CircularArc2d ar = lwp.GetArcSegment2dAt(i); ed.WriteMessage("ARC******************************************************\n"); ed.WriteMessage("Segment:" + i + "\n"); BoundBlock2d bb = ar.BoundBlock; Point2d minp = bb.GetMinimumPoint(); Point2d maxp = bb.GetMaximumPoint(); ed.WriteMessage("MinimumPoint:" + minp + ", MaximumPoint: " + maxp + "\n"); } if (sg.ToString().CompareTo("Line") == 0) { LineSegment2d li = lwp.GetLineSegment2dAt(i); ed.WriteMessage("LINE*****************************************************\n"); ed.WriteMessage("Segment:" + i + "\n"); BoundBlock2d bb = li.BoundBlock; Point2d minp = bb.GetMinimumPoint(); Point2d maxp = bb.GetMaximumPoint(); ed.WriteMessage("MinimumPoint:" + minp + ", MaximumPoint: " + maxp + "\n"); } } } } }
First look at the picture and see if that is what you want
This draws a rectangle around around the arc using the Entity.GeometricExtents
[CommandMethod("PolyLineExtents")] public void PolyLineExtents() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityResult per = ed.GetEntity("Select a polyline\n"); if (per.Status == PromptStatus.OK) { using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; BlockTableRecord msBtr = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead); Entity ent = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity; Point3d min = ent.GeometricExtents.MinPoint; Point3d max = ent.GeometricExtents.MaxPoint; DocumentCollection docs = Application.DocumentManager; AcadDocument acadDoc = (AcadDocument)doc.AcadDocument; acadDoc.SendCommand("rec " + min.X + "," + min.Y + " " + max.X + "," + max.Y + " "); tr.Commit(); } } }
Here is a different one run on revision cloud (which is nothing but a polyine with arcs) I am not using the GetBoundBlockOf because I can't figure out how to even put the parameter in the right way.
This does not explode the actual object
I changed the current layer before I ran the command to be able to see it better.
See Attached picture
[CommandMethod("PolyLineExtents2")] public void PolyLineExtents2() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityResult per = ed.GetEntity("Select a polyline\n"); if (per.Status == PromptStatus.OK) { using (Transaction tr = db.TransactionManager.StartTransaction()) { Entity ent = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity; DBObjectCollection dbObjColl = new DBObjectCollection(); ent.Explode(dbObjColl); foreach (Entity entExplode in dbObjColl) { Point3d min = entExplode.GeometricExtents.MinPoint; Point3d max = entExplode.GeometricExtents.MaxPoint; DocumentCollection docs = Application.DocumentManager; AcadDocument acadDoc = (AcadDocument)doc.AcadDocument; acadDoc.SendCommand("rec " + min.X + "," + min.Y + " " + max.X + "," + max.Y + " "); ed.WriteMessage("\n Min: " + min.ToString() + "\n Max: " + max.ToString()); } tr.Commit(); } } }
Where is included "AcadDocument"? I got the error: The type or namespace name 'AcadDocument' could not be found (are you missing a using directive or an assembly reference?)
Also I have the same problem to convert CircularArc2d to Entity not the selection with "per.ObjectId" that works fine.
Splines will not work right so here a little work around if the spline is small the precision might be set to high and it does not convert it to a polyline it just uses it for the extents.
To see the difference try the first method on a spline then this one or draw a spline and do a SplineEdit or EditSpline then choose purge and when you select it you will see the control points it uses for its extents
[CommandMethod("BoundBoxSpline")] public void BoundBoxSpline() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityResult per = ed.GetEntity("Select a Spline\n"); if (per.Status == PromptStatus.OK) { using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; BlockTableRecord btrMs = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite); Spline sp = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Spline; Application.SetSystemVariable("PLINECONVERTMODE", 1); sp.ToPolylineWithPrecision(99); Point3d min = sp.GeometricExtents.MinPoint; Point3d max = sp.GeometricExtents.MaxPoint; DocumentCollection docs = Application.DocumentManager; AcadDocument acadDoc = (AcadDocument)doc.AcadDocument; acadDoc.SendCommand("rec " + min.X + "," + min.Y + " " + max.X + "," + max.Y + " "); ed.WriteMessage("\n Min: " + min.ToString() + "\n Max: " + max.ToString()); tr.Commit(); } } }
I need to get StartPoint, EndPoint, Center for Arc and StartPoint, EndPoint for Line that compose a PolyLine.
Sorry for my inexperienced, but I don't know another way to decompose Polyline as Arc and Line:
CircularArc2d ar = lwp.GetArcSegment2dAt(i); LineSegment2d li = lwp.GetLineSegment2dAt(i);
Is there a better way to do this job and get StartPoint, EndPoint, Center, ent.GeometricExtents.MinPoint, ent.GeometricExtents.MaxPoint?
Do not if you cared are not but some how I changed the the code for spline and it is wrong it should
[CommandMethod("BoundBoxSpline")] public void BoundBoxSpline() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityResult per = ed.GetEntity("Select a Spline\n"); if (per.Status == PromptStatus.OK) { using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; BlockTableRecord btrMs = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite); Spline sp = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Spline; Application.SetSystemVariable("PLINECONVERTMODE", 1); Polyline pl = sp.ToPolylineWithPrecision(99) as Polyline; Point3d min = pl.GeometricExtents.MinPoint; Point3d max = pl.GeometricExtents.MaxPoint; DocumentCollection docs = Application.DocumentManager; AcadDocument acadDoc = (AcadDocument)doc.AcadDocument; acadDoc.SendCommand("rec " + min.X + "," + min.Y + " " + max.X + "," + max.Y + " "); ed.WriteMessage("\n Min: " + min.ToString() + "\n Max: " + max.ToString()); tr.Commit(); } } }
For getting boundingbox of a polyline I use:
Polyline lwp = obj as Polyline; object oAcadObj = lwp.AcadObject; object[] args = new object[2]; args[0] = new VariantWrapper(0); args[1] = new VariantWrapper(0); ParameterModifier pm = new ParameterModifier(2); pm[0] = true; pm[1] = true; ParameterModifier[] modifiers = new ParameterModifier[] { pm }; oAcadObj.GetType().InvokeMember("GetBoundingBox", BindingFlags.InvokeMethod, null, oAcadObj, args, modifiers, null, null); Point3d lowerLeftCorner = new Point3d((double[])args[0]); Point3d upperRightCorner = new Point3d((double[])args[1]);
but my problem remain to find boundingbox of
CircularArc2d ar = lwp.GetArcSegment2dAt(i); LineSegment2d li = lwp.GetLineSegment2dAt(i);
Right now I solved for CircularArc2d with Radius and using it in combination with Center and for LineSegment2d with startpoint and endpoint.
Many thanks fro2001
Are you trying to get bounding box for each segment like the attached picture if so that is BoundBoxExplode which does not actually explode the object but you could iterate through and get all the points.
The picture has a polyine with arcs,lines, arcs and lines