I don't know what kind of data you want to store.
But please look at my example. With this function i create a new drawing and add an xrecord with my specified data.
[CommandMethod("NewDoc")]
public void NewDoc()
{
Database acDbNewDoc = new Database();
// get the editor object
Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = acDbNewDoc.TransactionManager.StartTransaction())
{
try
{
// open the NOD for read
DBDictionary nod = tr.GetObject(acDbNewDoc.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;
try
{
// check to see if our entry is in there, excpetion will be thrown if not so process that
// condition in the catch
ObjectId entryId = nod.GetAt(ACMEDATA);
// if we are here, then all is ok
// print the data
//ed.WriteMessage("This entity already has data...");
// ok extract the xrecord
Xrecord myXrecord = default(Xrecord);
// read it from the NOD dictionary
myXrecord = tr.GetObject(entryId, OpenMode.ForRead) as Xrecord;
// now print out the values
// Get data from Xrecord
ResultBuffer resBuf = myXrecord.Data;
TypedValue[] resbufvalue = resBuf.AsArray();
acadApp.ShowAlertDialog(string.Format("{0}, {1}, {2}", resbufvalue[0].Value, resbufvalue[1].Value, resbufvalue[2].Value));
}
catch
{
// upgrade to write status
nod.UpgradeOpen();
// create a new XRecord
Xrecord myXrecord = new Xrecord();
// create the resbuf list
ResultBuffer data = new ResultBuffer(new TypedValue((int)DxfCode.Text, "ACMEDATA1"),
new TypedValue((int)DxfCode.Text, "ACMEDATA2"),
new TypedValue((int)DxfCode.Text, "ACMEDATA3"));
// now add it to the xrecord
myXrecord.Data = data;
// create the entry
nod.SetAt(_dictionaryName, myXrecord);
// tell the transaction about the newly created xrecord
tr.AddNewlyCreatedDBObject(myXrecord, true);
}
// all ok, commit it
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
// a problem occurred, lets print it
ed.WriteMessage("a problem occurred because " + ex.Message);
}
finally
{
// whatever happens we must dispose the transaction
}
// Save the document
acDbNewDoc.SaveAs(@"c:\temp\XrecDWG.dwg", DwgVersion.Current);
}
}
public void ReadDocData(object sender, DocumentCollectionEventArgs e)
{
Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = ed.Document.Database.TransactionManager.StartTransaction())
{
try
{
// open the NOD for read
DBDictionary nod = tr.GetObject(ed.Document.Database.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;
// check to see if our entry is in there, excpetion will be thrown if not so process that
// condition in the catch
ObjectId entryId = nod.GetAt("ACMEDATA");
// if we are here, then all is ok
// print the data
// ok extract the xrecord
Xrecord myXrecord = default(Xrecord);
// read it from the NOD dictionary
myXrecord = tr.GetObject(entryId, OpenMode.ForRead) as Xrecord;
// now print out the values
// Get data from Xrecord
ResultBuffer resBuf = myXrecord.Data;
TypedValue[] resbufvalue = resBuf.AsArray();
ed.WriteMessage(string.Format("\n{0}, {1}, {2}", resbufvalue[0].Value, resbufvalue[1].Value, resbufvalue[2].Value));
}
catch
{
ed.WriteMessage("\nThis is drawing doesn't have ACME data");
}
}
}