When you are interested in the frame around the Dimension Text, add this to the code:
[CommandMethod("MtFromDim")]
public static void MtextFromDimension()
{
Document doc = null;
Database db = null;
Editor ed = null;
try
{
doc = AcadApp.DocumentManager.MdiActiveDocument;
if (doc == null)
throw new System.Exception("No MdiActiveDocument");
db = doc.Database;
ed = doc.Editor;
var peo = new PromptEntityOptions("Select Dimension");
var per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var dim = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Dimension;
if (dim == null)
throw new System.Exception("Selected object is not a dimension");
var dimBlk = tr.GetObject(dim.DimBlockId, OpenMode.ForRead) as BlockTableRecord;
foreach(var id in dimBlk)
{
if (id.ObjectClass.DxfName=="MTEXT")
{
var mt = tr.GetObject(id, OpenMode.ForRead) as MText;
ed.WriteMessage("\n Dim Text:");
ed.WriteMessage("\n\t Block: {0}", dimBlk.Name);
ed.WriteMessage("\n\t Text: {0}", mt.Contents);
ed.WriteMessage("\n\t Position: {0}, {1}", mt.Location.X, mt.Location.Y);
ed.WriteMessage("\n\t AttachmentPoint: {0}",Enum.GetName(typeof(AttachmentPoint), mt.Attachment));
ed.WriteMessage("\n\t Rotation: {0}", mt.Rotation * 180 / Math.PI);
ed.WriteMessage("\n\t Width, Height: {0}, {1}", mt.ActualHeight, mt.ActualWidth);
var mtAngle = mt.Rotation;
var mtPos = mt.Location;
mt.UpgradeOpen();
mt.TransformBy(Matrix3d.Rotation(-mtAngle, Vector3d.ZAxis, mtPos));
var mtBounds = mt.GetBoundingPoints();
mt.TransformBy(Matrix3d.Rotation(mtAngle, Vector3d.ZAxis, mtPos));
for (int i = 0; i < mtBounds.Count; i++)
mtBounds[i]=mtBounds[i].TransformBy(Matrix3d.Rotation(mtAngle, Vector3d.ZAxis, mtPos));
ed.WriteMessage("\n\t Lower Left: ({0}, {1})", mtBounds[2].X, mtBounds[2].Y);
ed.WriteMessage("\n\t Lower Right: ({0}, {1})", mtBounds[3].X, mtBounds[3].Y);
ed.WriteMessage("\n\t Upper Right: ({0}, {1})", mtBounds[1].X, mtBounds[1].Y);
ed.WriteMessage("\n\t Upper Left: ({0}, {1})", mtBounds[0].X, mtBounds[0].Y);
var pl = new Polyline();
pl.AddVertexAt(0, new Point2d(mtBounds[2].X, mtBounds[2].Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(mtBounds[3].X, mtBounds[3].Y), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(mtBounds[1].X, mtBounds[1].Y), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(mtBounds[0].X, mtBounds[0].Y), 0, 0, 0);
pl.Closed = true;
pl.LayerId = mt.LayerId;
var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
ms.AppendEntity(pl);
tr.AddNewlyCreatedDBObject(pl, true);
}
}
tr.Commit();
}
}
catch (System.Exception ex)
{
if (ed != null)
ed.WriteMessage("\n Error in MtextFromDimension: {0}", ex.Message);
}
}
