- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I use to utilize Xdata fairly extensively in our code base, until we started getting Audit errors
XData Handle Unknown
which motivated me to start using ExtensionDictionaries. I want to try and start bringing XData back as a tool I can use. But I had a question around setting XData which was motivated by my seeing this method
DBObject.GetXDataForApplication
If Xdata can be retrieved from an object based on the Application RegAppTable entry, then that means multiple Applications XData can exist on the same object. I've seen AutoCAD add xdata to objects before.
The methods that I use to use would always store things as a string. And I would append a unique application string to the beginning of that data so that I could differentiate my apps entries in the XData vs other applications. So anything that didnt have that unique application string I would copy into the new ResultBuffer, and then I would add the data I wanted to add to the end of the ResultBuffer.
I wonder if this was even neccessary or might have caused some of my problems by adding unneeded complexity. When I add XData by doing something like
DbObject.XData = new ResultBuffer(....);
am I removing any existing data that might have been there, or does the API keep track of different Appplications XData?
Here is some code I wrote just to show the simpliest way to add Xdata and Extension Dictionary data
[CommandMethod(nameof(XDataVsExtensionDictionary))]
public void XDataVsExtensionDictionary()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
string appName = "MyAppName";
var result = ed.GetEntity($"\nSelect polyline:");
if (result.Status != PromptStatus.OK) return;
ObjectId plineId = result.ObjectId;
using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{
try
{
var regAppTableId = db.RegAppTableId;
using (RegAppTable appTable =
tr.GetObject(regAppTableId, OpenMode.ForRead, false, true) as RegAppTable)
{
if (!appTable.Has(appName))
{
SymbolTableRecord newEntry = new RegAppTableRecord();
newEntry.Name = appName;
appTable.UpgradeOpen();
appTable.Add(newEntry);
tr.AddNewlyCreatedDBObject(newEntry, true);
}
}
using (Polyline line = tr.GetObject(plineId, OpenMode.ForWrite, false, true) as Polyline)
{
//xdata method
var data = new ResultBuffer(new[]
{
new TypedValue((short)DxfCode.ExtendedDataRegAppName, appName),
new TypedValue((short)DxfCode.ExtendedDataAsciiString, "Some Data")
});
line.XData = data;
//Extension Dictionary
if (line.ExtensionDictionary == ObjectId.Null)
{
line.CreateExtensionDictionary();
}
using (DBDictionary extensionDictionary =
tr.GetObject(line.ExtensionDictionary, OpenMode.ForWrite, false, true) as DBDictionary)
{
var extendData = new ResultBuffer(new[]
{
new TypedValue((short)DxfCode.Text, "Some Extended Data")
});
Xrecord record = new Xrecord();
record.Data = extendData;
ObjectId recordId = extensionDictionary.SetAt("SomeKey", record);
tr.AddNewlyCreatedDBObject(record, true);
}
}
tr.Commit();
}
catch
{
tr.Abort();
return;
}
}
}
Solved! Go to Solution.