.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to delete any Xdata of a entity?

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
HelloWorlddd
3454 Views, 7 Replies

How to delete any Xdata of a entity?

I just know how to delete a specified Xdata that assign by  specified Registered Applications, just like follow

   [CommandMethod("ESX", CommandFlags.UsePickSet)]
        public void EraseSpecifiedXdata()
        {
            // Get the current database and start a transaction
            Database acCurDb= Application.DocumentManager.MdiActiveDocument.Database;
            Document acDoc = Application.DocumentManager.MdiActiveDocument;

            string appName = "MY_APP";
         
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Request objects to be selected in the drawing area
                PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();

                // If the prompt status is OK, objects were selected
                if (acSSPrompt.Status == PromptStatus.OK)
                {
                    // Open the Registered Applications table for read
                    RegAppTable acRegAppTbl;
                    acRegAppTbl = acTrans.GetObject(acCurDb.RegAppTableId, OpenMode.ForRead) as RegAppTable;

                    // Check to see if the Registered Applications table record for the custom app exists
                    if (acRegAppTbl.Has(appName) == false)
                    {
                        using (RegAppTableRecord acRegAppTblRec = new RegAppTableRecord())
                        {
                            acRegAppTblRec.Name = appName;

                            acRegAppTbl.UpgradeOpen();
                            acRegAppTbl.Add(acRegAppTblRec);
                            acTrans.AddNewlyCreatedDBObject(acRegAppTblRec, true);
                        }
                    }

                    // Define the Xdata to add to each selected object
                    using (ResultBuffer rb = new ResultBuffer())
                    {
                        rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
                        //Because we leave this blank except for the regappid,it erases any Xdata we previously added.
                        //rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, xdataStr));

                        SelectionSet acSSet = acSSPrompt.Value;

                        // Step through the objects in the selection set
                        foreach (SelectedObject acSSObj in acSSet)
                        {
                            // Open the selected object for write
                            Entity acEnt = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite) as Entity;

                            // Append the extended data to each object
                            acEnt.XData = rb;
                        }
                    }
                }

                // Save the new object to the database
                acTrans.Commit();

                // Dispose of the transaction
            }
        }

 

I want to get a clean entity, but In my case the entity has uncertain Registered Applications, how to delete any Xdata of a entity?

Thanks for any help.

7 REPLIES 7
Message 2 of 8
_gile
in reply to: HelloWorlddd

Hi,

 

You can try this extension method, it can be called as an instance method on any object deriving from DBObject which have to be opened for write with a transaction.

 

    static class Extension
    {
        public static void RemoveAllXdata(this DBObject dbObj)
        {
            if (dbObj == null)
                throw new ArgumentNullException("dbObj");

            Transaction tr = dbObj.Database.TransactionManager.TopTransaction;
            if (tr == null)
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NoActiveTransactions);

            if (!dbObj.IsWriteEnabled)
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NotOpenForWrite);

            ResultBuffer data = dbObj.XData;
            if (data != null)
                foreach (TypedValue tv in data.AsArray().Where(tv => tv.TypeCode == 1001))
                    dbObj.XData = new ResultBuffer(tv);
        }
    }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 8
HelloWorlddd
in reply to: _gile

Hi _gile,

Lambda expression, well, I must learn it, but now it appears a compile error: System.Array do not contain the defintion of Where, do you know where is the problem?

Very thanks,

Message 4 of 8
_gile
in reply to: HelloWorlddd

Where is a Linq extension method, so you have to reference System.Linq to get it work.

 

Anyway, if you prefer a more classical imperative way:

 

        public void RemoveAllXdata(DBObject dbObj)
        {
            if (dbObj == null)
                throw new ArgumentNullException("dbObj");

            Transaction tr = dbObj.Database.TransactionManager.TopTransaction;
            if (tr == null)
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NoActiveTransactions);

            if (!dbObj.IsWriteEnabled)
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NotOpenForWrite);

            ResultBuffer data = dbObj.XData;
            if (data != null)
            {
                foreach (TypedValue tv in data)
                {
                    if (tv.TypeCode == 1001)
                        dbObj.XData = new ResultBuffer(tv);
                }
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 8
cdinten
in reply to: _gile

hi, _gile, i overheared that just setting XData to null could not delete an entity's XData, could you explain how your code works? it seems like it assigned a RegAppName to the original Entity with no other Xdata items, and does it means that in AutoCAD, if an Entity's XData has nothing but just the RegAppname, this equals No XData? And How could we get all the RegAppname In a drawing? thanks!
Message 6 of 8
_gile
in reply to: cdinten

Hi,

 

Yes, to remove an object extended data for a specific registered application, you have to set the XData property of this object with a resultBuffer wich only contains the registered application.

 

The code I posted upper gets the xdata of an object and, if any (i.e. the ResultBuffer is not null), iterates through the ResulBuffer searching for registered applications (i.e. TypedValue which TypeCode is 1001) and, foreach registered application, removes the xdata as explained upper.

 

To get all registered applications in a drawing, you have to iterate the drawing RegAppTable.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 8
HelloWorlddd
in reply to: _gile

Thanks as alway.

Message 8 of 8
cdinten
in reply to: _gile

ok, thank you _gile, now i know what to do. RegAppTable is, afterall, a symbol table.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost