CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

处理对象事件 (.NET)

2023-1-1 09:03| 发布者: admin| 查看: 483| 评论: 0|来自: AutoCAD

对象事件用于响应从图形数据库中打开、添加、修改和拭除对象的过程。有两种类型的对象相关事件:对象级别和数据库级别。对象级事件定义为响应数据库中的特定对象,而数据库级事件响应数据库中的所有对象。

通过向数据库对象的事件注册事件处理程序来定义对象级事件。数据库级对象事件是通过向 openobject 的一个事件注册事件处理程序来定义的。Database

以下事件可用于:DBObjects

取消

当对象的打开被取消文本时触发。

复制

克隆对象后触发。

删除

在对象被标记为擦除或未擦除时触发。

再见

当对象即将从内存中删除时触发,因为其关联的数据库正在被销毁。

改 性

修改对象时触发。

修改后的XData

修改附加到对象的 XData 时触发。

修改撤消

在撤消以前对对象的更改时触发。

对象已关闭

关闭对象时触发。

打开修改

在修改对象之前触发。

重新追加

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

子对象修改

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

未追加

在撤消操作后从数据库中删除对象时触发。

以下是用于在数据库级别响应对象更改的一些事件:

对象追加

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

对象已擦除

从数据库中擦除或取消擦除对象时触发。

对象修改

修改对象时触发。

对象打开修改

在修改对象之前触发。

对象重新追加

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

对象未追加

在撤消操作后从数据库中删除对象时触发。

启用对象事件

本示例创建一条包含事件的轻量级折线。然后,每当更改折线时,折线的事件处理程序都会显示新区域。要触发事件,只需在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.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
              acTrans.GetObject(acPoly.ObjectId, OpenMode.ForWrite)
          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.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);
}

VBA/ActiveX Code Reference

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

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 15:30

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部