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.
Solved! Go to Solution.
Solved by _gile. Go to Solution.
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); } }
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,
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); } } }
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.
Can't find what you're looking for? Ask the community or share your knowledge.