Using .NET API to Add and Remove XData

By Virupaksha Aithal

The read/write property XData of the Autodesk.AutoCAD.DatabaseServices.DBObject class is used to get and set  XData of any database resident object. This example demonstrates the same for an entity.

[CommandMethod("ADDXDATA")]
static public void AddXdata() 
{
    Document doc = 
        Application.DocumentManager.MdiActiveDocument;
    Database db = 
        doc.Database;
    Transaction tr = 
        db.TransactionManager.StartTransaction();
    using(tr)
    {
        Editor ed = 
            Application.DocumentManager.MdiActiveDocument.Editor;
        // Prompt the user to select an entity
        PromptEntityResult ers = 
           ed.GetEntity("Pick entity ");
        // Open the entity
        Entity ent = 
          (Entity)tr.GetObject(ers.ObjectId, 
                                        OpenMode.ForWrite);
        // Get the registered application names table
        RegAppTable regTable = 
           (RegAppTable)tr.GetObject(db.RegAppTableId, 
                                        OpenMode.ForRead);
 
        if(!regTable.Has("ADS"))
        {
            regTable.UpgradeOpen();
 
   // Add the application names that would be used to add Xdata
            RegAppTableRecord app = 
                    new RegAppTableRecord();
            app.Name = "ADS";
            regTable.Add(app);
            tr.AddNewlyCreatedDBObject(app, true);
        }
        // Append the Xdata to the entity - two different 
        // applications added.
        ent.XData = new ResultBuffer(new TypedValue(1001, "ADS"),
                                new TypedValue(1070, 100));
        tr.Commit();
    }
}
 
[CommandMethod("REMXDATA")]
static public void RemoveXdata() // This method can have any name
{
    Document doc =
        Application.DocumentManager.MdiActiveDocument;
    Database db =
        doc.Database;
    Transaction tr =
        db.TransactionManager.StartTransaction();
    using (tr)
    {
        Editor ed =
            Application.DocumentManager.MdiActiveDocument.Editor;
        try
        {
            // Prompt the user to select an entity
            PromptEntityResult ers =
                ed.GetEntity("Pick entity ");
            // Open the selected entity
            Entity ent =
                (Entity)tr.GetObject(ers.ObjectId,
                            OpenMode.ForRead);
 
            ResultBuffer buffer =
                ent.GetXDataForApplication("ADS");
            // This call would ensure that the
            //Xdata of the entity associated with ADSK application 
            //name only would be removed
            if (buffer != null)
            {
                ent.UpgradeOpen();
                ent.XData =
                    new ResultBuffer(new TypedValue(1001, "ADS"));
                buffer.Dispose();
            }
            tr.Commit();
        }
        catch
        {
            tr.Abort();
        }
    }
}

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading