図面データベースのオブジェクトのオープン、追加、変更、削除などへの応答には、オブジェクト イベントが使用されます。オブジェクトに関連するイベントには、オブジェクト レベルとデータベース レベルの 2 種類があります。オブジェクト レベル イベントはデータベースの特定のオブジェクトに応答するために定義されており、データベース レベル イベントはデータベースのすべてのオブジェクトに応答します。
データベース オブジェクトのイベントにイベント ハンドラを登録することで、オブジェクト レベルのイベントを定義します。データベース レベルのオブジェクト イベントは、開いている Database オブジェクトのイベントの 1 つにイベント ハンドラを登録することにより定義します。
DBObjects では次のイベントを使用できます。
オブジェクトのオープンがキャンセルされると発生します。
オブジェクトがコピーされた後で発生します。
オブジェクトに削除予定のフラグが設定されるかオブジェクトが削除解除されると発生します。
関連付けられているデータベースが破棄されたためにオブジェクトがメモリから削除される直前に発生します。
オブジェクトが修正されると発生します。
オブジェクトに添付されている XData が修正されると発生します。
オブジェクトに対する前の変更が元に戻されると発生します。
オブジェクトが閉じられると発生します。
オブジェクトが修正される前に発生します。
オブジェクトが元に戻す操作の後でデータベースから削除され、その後、やり直し操作によって再び追加されると発生します。
オブジェクトのサブオブジェクトが修正されると発生します。
オブジェクトが元に戻す操作の後でデータベースから削除されると発生します。
次に示すのは、データベース レベルでオブジェクト変更への応答に使用されるイベントです。
オブジェクトがデータベースに追加されると発生します。
オブジェクトがデータベースから削除または削除解除されると発生します。
オブジェクトが修正されると発生します。
オブジェクトが修正される前に発生します。
オブジェクトが元に戻す操作の後でデータベースから削除され、その後、やり直し操作によって再び追加されると発生します。
オブジェクトが元に戻す操作の後でデータベースから削除されると発生します。
以下の例では、イベントを使用するライトウェイト ポリラインを作成します。ポリライン用のイベント ハンドラは、ポリラインが変更されるたびに新しい面積を表示します。イベントを発生させるには、AutoCAD でポリラインのサイズを変更するだけです。イベント ハンドラが起動する前に CreatePLineWithEvents サブルーチンを実行する必要があることを覚えておいてください。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
'' Global variable for polyline object
Dim acPoly As Polyline = Nothing
<CommandMethod("AddPlObjEvent")> _
Public Sub AddPlObjEvent()
'' Get the current document and database, and start a transaction
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Block table record for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
OpenMode.ForRead)
'' Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' 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)
AddHandler acPoly.Modified, AddressOf acPolyMod
'' Save the new object to the database
acTrans.Commit()
End Using
End Sub
<CommandMethod("RemovePlObjEvent")> _
Public Sub RemovePlObjEvent()
If acPoly <> Nothing Then
'' Get the current document and database, and start a transaction
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the polyline for read
acPoly = acTrans.GetObject(acPoly.ObjectId, _
OpenMode.ForRead)
If acPoly.IsWriteEnabled = False Then
acPoly.UpgradeOpen()
End If
RemoveHandler acPoly.Modified, AddressOf acPolyMod
acPoly = Nothing
End Using
End If
End Sub
Public Sub acPolyMod(ByVal senderObj As Object, _
ByVal evtArgs As EventArgs)
Application.ShowAlertDialog("The area of " & _
acPoly.ToString() & " is: " & _
acPoly.Area)
End Sub
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)
{
acPoly.UpgradeOpen();
}
acPoly.Modified -= new EventHandler(acPolyMod);
acPoly = null;
}
}
}
public void acPolyMod(object senderObj,
EventArgs evtArgs)
{
Application.ShowAlertDialog("The area of " +
acPoly.ToString() + " is: " +
acPoly.Area);
}
Public WithEvents PLine As AcadLWPolyline
Sub CreatePLineWithEvents()
' This example creates a light weight polyline
Dim points(0 To 9) As Double
points(0) = 1: points(1) = 1
points(2) = 1: points(3) = 2
points(4) = 2: points(5) = 2
points(6) = 3: points(7) = 3
points(8) = 3: points(9) = 2
Set PLine = ThisDrawing.ModelSpace. _
AddLightWeightPolyline(points)
PLine.Closed = True
ThisDrawing.Application.ZoomAll
End Sub
Private Sub PLine_Modified _
(ByVal pObject As AutoCAD.IAcadObject)
' This event is triggered when the polyline is resized.
' If the polyline is deleted the modified event is still
' triggered, so we use the error handler to avoid
' reading data from a deleted object.
On Error GoTo ERRORHANDLER
MsgBox "The area of " & pObject.ObjectName & " is: " _
& pObject.Area
Exit Sub
ERRORHANDLER:
MsgBox Err.Description
End Sub