处理对象事件
 
 

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:

Cancelled

Triggered when the opening of the object is cancelled text.

当对象的打开操作被取消时触发。

Copied

Triggered after the object is cloned.

对象被复制后触发。

Erased

Triggered when the object is flagged to be erased or is unerased.

当对象标记为删除或取消删除时触发。

Goodbye

Triggered when the object is about to be deleted from memory because its associated database is being destroyed.

当对象因为与它相关的数据库将被销毁而将要从内存中删除时触发。

Modified

Triggered when the object is modified.

当对象修改后触发。

ModifiedXData

Triggered when the XData attached to the object is modified.

当附加到对象的 XData 被修改后触发。

ModifyUndone

Triggered when previous changes to the object are being undone.

当对象先前的修改未完成时触发。

ObjectClosed

Triggered when the object is closed.

当对象关闭时触发。

OpenedForModify

Triggered before the object is modified.

对象修改前触发。

Reappended

Triggered when the object is removed from the database after an Undo operation and then re-appended with a Redo operation.

当一个撤消操作后对象从数据库中移除,然后使用重做操作重新追加时触发。

SubObjectModified

Triggered when a subobject of the object is modified.

当对象的子对象被修改时触发。

Unappended

Triggered when the object is removed from the database after an Undo operation.

当一个撤消操作后对象从数据库中移除时触发。

The following are some of the events used to respond to object changes at the Database level:

下面的一些事件用于响应数据库层的对象变化:

ObjectAppended

Triggered when an object is added to a database.

当对象添加到数据库时触发。

ObjectErased

Triggered when an object is erased or unerased from a database.

当对象从数据库中清除或撤消清除时触发。

ObjectModified

Triggered when an object has been modified.

当对象被修改时触发。

ObjectOpenedForModify

Triggered before an object is modified.

当对象被修改前触发。

ObjectReappended

Triggered when an object is removed from a database after an Undo operation and then re-appended with a Redo operation.

当一个撤消操作后对象从数据库中移除,然后使用重做操作重新追加时触发。

ObjectUnappended

Triggered when an object is removed from a database after an Undo operation.

当一个撤消操作后对象从数据库中移除时触发。

启用对象事件

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 子程序。

VB.NET

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

C#

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);
}
VBA/ActiveX 代码参考