.NET

.NET

Reply
Valued Contributor
HelloWorlddd
Posts: 87
Registered: ‎05-03-2013
Message 1 of 8 (347 Views)
Accepted Solution

How to delete any Xdata of a entity?

347 Views, 7 Replies
04-21-2014 05:53 AM

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.

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.

*Expert Elite*
_gile
Posts: 2,133
Registered: ‎04-29-2006
Message 2 of 8 (327 Views)

Re : How to delete any Xdata of a entity?

04-21-2014 07:42 AM 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
Valued Contributor
HelloWorlddd
Posts: 87
Registered: ‎05-03-2013
Message 3 of 8 (318 Views)

Re : How to delete any Xdata of a entity?

04-21-2014 09:37 AM 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,

*Expert Elite*
_gile
Posts: 2,133
Registered: ‎04-29-2006
Message 4 of 8 (307 Views)

Re : How to delete any Xdata of a entity?

04-21-2014 12:01 PM 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
Valued Contributor
cdinten
Posts: 61
Registered: ‎11-19-2010
Message 5 of 8 (296 Views)

Re : How to delete any Xdata of a entity?

04-21-2014 05:21 PM 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!
*Expert Elite*
_gile
Posts: 2,133
Registered: ‎04-29-2006
Message 6 of 8 (282 Views)

Re : How to delete any Xdata of a entity?

04-21-2014 11:57 PM 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
Valued Contributor
HelloWorlddd
Posts: 87
Registered: ‎05-03-2013
Message 7 of 8 (270 Views)

Re : How to delete any Xdata of a entity?

04-22-2014 01:49 AM in reply to: _gile

Thanks as alway.

Valued Contributor
cdinten
Posts: 61
Registered: ‎11-19-2010
Message 8 of 8 (192 Views)

Re : How to delete any Xdata of a entity?

05-18-2014 05:16 PM in reply to: _gile
ok, thank you _gile, now i know what to do. RegAppTable is, afterall, a symbol table.
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.