Since the picture you show is in a twisted angle, we need to clarify it first (that might be why people seem unwilling to reply to this seemd simple question.
I assume the entities (line, or block reference...) you as interested in getting their bounding box are drawn normally in World Coordinate. The picture you show is just a twist view. And also, by "Bounding Box" we mean it is an rectangle with X axis as 0 degree and Y axis as 90 degree, which just big enough to cover the said entities.
So, it is very simple: each entity has a property GeometricExtents property, which is Extents3d type. Therefore, as long as your code get to the entities in interest, you will know each entitiy's bounding box. if there are multiple entities in interest, you simply aggreagate each entity's bounding box. Following code will draw the bounding box of selected entities:
public class MyCommands
{
[CommandMethod("GetBound")]
public static void RunGetBound()
{
Document dwg = Application.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
ObjectId[] entIds = SelectEntities(ed);
if (entIds != null)
{
DrawBoundBox(dwg, entIds);
}
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
private static ObjectId[] SelectEntities(Editor ed)
{
PromptSelectionOptions opt = new PromptSelectionOptions();
opt.MessageForAdding = "Select:";
PromptSelectionResult res = ed.GetSelection(opt);
if (res.Status == PromptStatus.OK)
{
return res.Value.GetObjectIds();
}
else
{
return null;
}
}
private static void DrawBoundBox(Document dwg, ObjectId[] entIds)
{
using (Transaction tran = dwg.TransactionManager.StartTransaction())
{
Extents3d ext = GetBoundBox(entIds, tran);
Polyline poly = DrawPolygon(ext);
BlockTableRecord space = tran.GetObject(
dwg.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
space.AppendEntity(poly);
tran.AddNewlyCreatedDBObject(poly, true);
tran.Commit();
}
}
private static Extents3d GetBoundBox(ObjectId[] entIds, Transaction tran)
{
Extents3d ex = new Extents3d();
foreach (var id in entIds)
{
Entity ent = tran.GetObject(id, OpenMode.ForRead) as Entity;
ex.AddExtents(ent.GeometricExtents);
}
return ex;
}
private static Polyline DrawPolygon(Extents3d ext)
{
Polyline pl = new Polyline(4);
pl.AddVertexAt(0, new Point2d(ext.MinPoint.X, ext.MinPoint.Y), 0.0, 0.0, 0.0);
pl.AddVertexAt(1, new Point2d(ext.MinPoint.X, ext.MaxPoint.Y), 0.0, 0.0, 0.0);
pl.AddVertexAt(2, new Point2d(ext.MaxPoint.X, ext.MaxPoint.Y), 0.0, 0.0, 0.0);
pl.AddVertexAt(3, new Point2d(ext.MaxPoint.X, ext.MinPoint.Y), 0.0, 0.0, 0.0);
pl.Closed = true;
pl.SetDatabaseDefaults();
return pl;
}
}