public static void AttachXDataToSelectionSetObjects(ref ObjectIdCollection idsCollection, Database acCurDb)
{
// Get the current database and start a transaction
Document acDoc = Application.DocumentManager.MdiActiveDocument;
string appName = "GreenSnake";
string xdataStr = acCurDb.GetBuildingAreaNumber();
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 ( idsCollection.Count > 0 )
{
// Open the Registered Applications table for read
RegAppTable 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));
rb.Add(new TypedValue((int) DxfCode.ExtendedDataAsciiString, xdataStr));
// Step through the objects in the selection set
foreach ( object acSSObj in idsCollection )
{
// Open the selected object for write
Entity acEnt = acTrans.GetObject((ObjectId) acSSObj, OpenMode.ForWrite, false, true) 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
}
}