处理对象事件 (.NET)
对象事件用于响应图形数据库中对象的打开、添加、修改和擦除。有两种类型的对象相关事件:对象级别和数据库级别。对象级事件定义为响应数据库中的特定对象,而数据库级事件响应数据库中的所有对象。 通过向数据库对象的事件注册事件处理程序来定义对象级事件。数据库级对象事件是通过向打开对象的事件之一注册事件处理程序来定义的。Database 以下事件可用于:DBObjects
以下是用于在数据库级别响应对象更改的一些事件:
启用 Object 事件此示例创建包含事件的轻量级折线。然后,每当折线发生更改时,折线的事件处理程序都会显示新区域。要触发事件,只需在AutoCAD中更改折线的大小即可。请记住,在激活事件处理程序之前,必须运行 CreatePLineWithEvents 子例程。 VB.NETImports 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 代码参考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
相关概念父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-30 23:21
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.