拡張データ(xdata)を、図面内のオブジェクトのリンク情報として利用できます。
次の例では、図面からオブジェクトを選択するよう指示するプロンプトを表示します。選択したオブジェクトが選択セットに入れられ、指定した拡張データが選択セット内のすべてのオブジェクトにアタッチされます。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[CommandMethod("AttachXDataToSelectionSetObjects")]
public void AttachXDataToSelectionSetObjects()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
Document acDoc = Application.DocumentManager.MdiActiveDocument;
string appName = "MY_APP";
string xdataStr = "This is some xdata";
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;
acTrans.GetObject(acCurDb.RegAppTableId, OpenMode.ForWrite);
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));
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
}
}
次の例では、前の例でアタッチした拡張データを表示します。文字列(タイプ 1000)以外の拡張データをアタッチする場合は、このコードを変更する必要があります。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[CommandMethod("ViewXData")]
public void ViewXData()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
Document acDoc = Application.DocumentManager.MdiActiveDocument;
string appName = "MY_APP";
string msgstr = "";
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)
{
SelectionSet acSSet = acSSPrompt.Value;
// Step through the objects in the selection set
foreach (SelectedObject acSSObj in acSSet)
{
// Open the selected object for read
Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
OpenMode.ForRead) as Entity;
// Get the extended data attached to each object for MY_APP
ResultBuffer rb = acEnt.GetXDataForApplication(appName);
// Make sure the Xdata is not empty
if (rb != null)
{
// Get the values in the xdata
foreach (TypedValue typeVal in rb)
{
msgstr = msgstr + "\n" + typeVal.TypeCode.ToString() + ":" + typeVal.Value;
}
}
else
{
msgstr = "NONE";
}
// Display the values returned
Application.ShowAlertDialog(appName + " xdata on " + acEnt.GetType().ToString() + ":\n" + msgstr);
msgstr = "";
}
}
// Ends the transaction and ensures any changes made are ignored
acTrans.Abort();
// Dispose of the transaction
}
}