図面データベースのオブジェクトのオープン、追加、変更、削除などへの応答には、オブジェクト イベントが使用されます。オブジェクトに関連するイベントには、オブジェクト レベルとデータベース レベルの 2 種類があります。オブジェクト レベル イベントはデータベースの特定のオブジェクトに応答するために定義されており、データベース レベル イベントはデータベースのすべてのオブジェクトに応答します。
データベース オブジェクトのイベントにイベント ハンドラを登録することで、オブジェクト レベルのイベントを定義します。データベース レベルのオブジェクト イベントは、開いている Database オブジェクトのイベントの 1 つにイベント ハンドラを登録することにより定義します。
DBObjects では次のイベントを使用できます。
オブジェクトのオープンがキャンセルされると発生します。
オブジェクトがコピーされた後で発生します。
オブジェクトに削除予定のフラグが設定されるかオブジェクトが削除解除されると発生します。
関連付けられているデータベースが破棄されたためにオブジェクトがメモリから削除される直前に発生します。
オブジェクトが修正されると発生します。
オブジェクトに添付されている XData が修正されると発生します。
オブジェクトに対する前の変更が元に戻されると発生します。
オブジェクトが閉じられると発生します。
オブジェクトが修正される前に発生します。
オブジェクトが元に戻す操作の後でデータベースから削除され、その後、やり直し操作によって再び追加されると発生します。
オブジェクトのサブオブジェクトが修正されると発生します。
オブジェクトが元に戻す操作の後でデータベースから削除されると発生します。
次に示すのは、データベース レベルでオブジェクト変更への応答に使用されるイベントです。
オブジェクトがデータベースに追加されると発生します。
オブジェクトがデータベースから削除または削除解除されると発生します。
オブジェクトが修正されると発生します。
オブジェクトが修正される前に発生します。
オブジェクトが元に戻す操作の後でデータベースから削除され、その後、やり直し操作によって再び追加されると発生します。
オブジェクトが元に戻す操作の後でデータベースから削除されると発生します。
以下の例では、イベントを使用するライトウェイト ポリラインを作成します。ポリライン用のイベント ハンドラは、ポリラインが変更されるたびに新しい面積を表示します。イベントを発生させるには、AutoCAD でポリラインのサイズを変更するだけです。イベント ハンドラが起動する前に CreatePLineWithEvents サブルーチンを実行する必要があることを覚えておいてください。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
// Global variable for polyline object
Polyline acPoly = null;
[CommandMethod("AddPlObjEvent")]
public void AddPlObjEvent()
{
// Get the current document and database, and start a transaction
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table record for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create a closed polyline
acPoly = new Polyline();
acPoly.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
acPoly.AddVertexAt(3, new Point2d(3, 3), 0, 0, 0);
acPoly.AddVertexAt(4, new Point2d(3, 2), 0, 0, 0);
acPoly.Closed = true;
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
acPoly.Modified += new EventHandler(acPolyMod);
// Save the new object to the database
acTrans.Commit();
}
}
[CommandMethod("RemovePlObjEvent")]
public void RemovePlObjEvent()
{
if (acPoly != null)
{
// Get the current document and database, and start a transaction
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the polyline for read
acPoly = acTrans.GetObject(acPoly.ObjectId,
OpenMode.ForRead) as Polyline;
if (acPoly.IsWriteEnabled == false)
{
acTrans.GetObject(acPoly.ObjectId, OpenMode.ForWrite);
}
acPoly.Modified -= new EventHandler(acPolyMod);
acPoly = null;
}
}
}
public void acPolyMod(object senderObj,
EventArgs evtArgs)
{
Application.ShowAlertDialog("The area of " +
acPoly.ToString() + " is: " +
acPoly.Area);
}