Object events are used to respond to the opening, adding, modifying, and erasing of objects from a drawing database. There are two types of object related events: object and database level. Object level events are defined to respond to a specific object in a database, whereas Database level events respond to all objects in a database.
对象事件用于响应从图形数据库中打开、添加、修改和删除对象。有两种与对象类型相关的事件:对象和数据库层。对象层事件被定义成响应数据库中指定的对象,而数据库层事件用于响应数据库中所有对象的事件。
You define an object level event by registering an event handler with the event of a database object. Database level object events are defined by registering an event handler with one of the events of an open Database object.
定义一个对象层事件是通过注册一个事件处理程序到一个数据库对象的事件中来完成的。数据库层对象事件的定义是通过注册一个事件处理程序到任一个打开数据库对象的事件来完成的。
The following events are available for DBObjects:
下列事件可用于 DBObjects:
The following are some of the events used to respond to object changes at the Database level:
下面的一些事件用于响应数据库层的对象变化:
This example creates a lightweight polyline with events. The event handler for the polyline then displays the new area whenever the polyline is changed. To trigger the event, simply change the size of the polyline in AutoCAD. Remember that you must run the CreatePLineWithEvents subroutine before the event handler is activated.
本例创建一个用于轻量多段线的事件。只要多段线被修改,多段线的事件处理程序将显示新的面积。若要触发事件,只要简单的在 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.SetDatabaseDefaults()
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.SetDatabaseDefaults();
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);
}