// tested on AutoCAD 2010
[CommandMethod("CopyTextToLeader", "ttl", CommandFlags.Redraw)]
public void TextToMleader()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
// save current DIMBLK variable
string dblk = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DIMBLK").ToString();
try
{
PromptEntityOptions peo =
new PromptEntityOptions("\nSelect Text / MText: ");
peo.SetRejectMessage("\nMust be select type of Text or MText.");
peo.AddAllowedClass(typeof(DBText), false);
peo.AddAllowedClass(typeof(MText), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
ObjectId txtId = per.ObjectId;
PromptPointOptions opt = new PromptPointOptions("\nSpecify leader arrowhead location: ");
PromptPointResult res = ed.GetPoint(opt);
if (res.Status != PromptStatus.OK) return;
Point3d pt = res.Value;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
ObjectId blkid = ObjectId.Null;
DBObject obj = (DBObject)tr.GetObject(txtId, OpenMode.ForWrite) as DBObject;
ed.WriteMessage("\nType\t{0}\n", obj.GetRXClass().DxfName);
DBText txtobj = null;
MText mtxtobj = null;
Point3d tp = new Point3d();
if (obj.GetRXClass().DxfName == "TEXT")
{
txtobj = obj as DBText;
if (txtobj == null) return;
tp = txtobj.Position;
}
if (obj.GetRXClass().DxfName == "MTEXT")
{
mtxtobj = obj as MText;
if (mtxtobj == null) return;
tp = mtxtobj.Location;
}
Leader leader = new Leader();
// Create the MText annotation
MText copymtx = new MText();
if (mtxtobj != null)
{
copymtx = mtxtobj.Clone() as MText;
}
if (txtobj != null)
{
copymtx = new MText();
copymtx.Location = txtobj.Position;
copymtx.Contents = txtobj.TextString;
}
// Add the new object to current space and the transaction
btr.AppendEntity(copymtx);
tr.AddNewlyCreatedDBObject(copymtx, true);
if (txtobj != null)
{
leader.TextStyleId = txtobj.TextStyleId;
}
if (mtxtobj != null)
{
leader.TextStyleId = mtxtobj.TextStyleId;
}
// set arrow head block to DOT
if (!bt.Has("_DOT"))
{
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("DIMBLK", "_DOT");
}
leader.HasArrowHead = true;
leader.Dimldrblk = bt["_DOT"];
leader.Dimasz = db.Dimtxt * 0.2;
leader.AppendVertex(pt);
leader.AppendVertex(tp);
btr.AppendEntity(leader);
tr.AddNewlyCreatedDBObject(leader, true);
// attach the annotation after the leader object is added
leader.Annotation = copymtx.ObjectId;
// evaluate leader
leader.EvaluateLeader();
// erase prototypes
if (mtxtobj != null) mtxtobj.Erase();
if (txtobj != null) txtobj.Erase();
tr.Commit();
}
}
catch (System.Exception ex)
{
ed.WriteMessage("\nError: {0}\nTrace: {1}", ex.Message, ex.StackTrace);
}
finally
{
// reset DIMBLK
if (dblk != "")
{
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("DIMBLK", dblk);
}
else
{
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("DIMBLK", ".");
}
}
}
~'J'~