Below code shows the procedure to add a field object which shows an object specific data. Code actually shows the area of a polyline. Below code when run in AutoCAD, prompts the user to select a ployline and then a location. An Mtext showing area of the selected polyline is created at the specified location.
[CommandMethod("AddAreaField")]
public void AddAreaField()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions options =
new PromptEntityOptions("\nSelect a polyline");
options.SetRejectMessage("\nSelect polyline");
options.AddAllowedClass(
typeof(Autodesk.AutoCAD.DatabaseServices.Polyline), false);
PromptEntityResult acSSPrompt = ed.GetEntity(options);
if (acSSPrompt.Status != PromptStatus.OK)
return;
PromptPointOptions ppo = new
PromptPointOptions("\nSpecify insertion point: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK)
return;
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
//%<\AcObjProp Object(%<\_ObjId 2128995688>%).Area \f "%lu2">%
string strId = acSSPrompt.ObjectId.OldIdPtr.ToString();
string str1 = "%<\\AcObjProp Object(%<\\_ObjId ";
string str2 = ">%).Area \\f \"%lu2\">%";
string format = str1 + strId + str2;
MText text = new MText();
text.Location = ppr.Value;
ObjectId ModelSpaceId =
SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord record = Tx.GetObject(ModelSpaceId,
OpenMode.ForWrite) as BlockTableRecord;
record.AppendEntity(text);
Tx.AddNewlyCreatedDBObject(text, true);
Field entField =
new Field(format);
entField.Evaluate();
text.SetField(entField);
Tx.AddNewlyCreatedDBObject(entField, true);
Tx.Commit();
}
}