Adding field with entity data

By Virupaksha Aithal

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())
    {
       //%%
        string strId = acSSPrompt.ObjectId.OldIdPtr.ToString();
        string str1 = "%%).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();
    }
}

Comments

4 responses to “Adding field with entity data”

  1. The example uses the polyline area. The name of a variable shall correspond to contents, otherwise it confuses a developer. In this case the name “datetime” doesn’t correspond to contents of the field.

  2. Hi,
    Thanks for the comment. I have corrected the issue.
    regards
    Viru

  3. How can I find any doccument about Field expression? I want to know structure of string format.

  4. petcon Avatar
    petcon

    can you trans the code to c++ i use AcDbObjectId::asOldId to replace ObjectId.OldIdPtr in c++ code but i get wrong result

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading