在没有事务管理器时打开和关闭对象
 
 

Transactions make it easier to open and work with multiple objects, but they are not the only way to open and edit objects. Other than using a transaction, you can open and close objects using the Open and Close methods. You still need to obtain an object id to use the Open method. Like the GetObject method used with transactions, you need to specify an open mode and the return value is an object. If you make changes to an object after you opened it with the Open method, you can use the Cancel method to rollback all the changes made since it was opened. Cancel must be called on each object in which you want to rollback.

事务使打开和使用多个对象变得很容易,但它们并不是打开和编辑对象的唯一方法。除了使用事务,也可以使用 OpenClose 方法打开和关闭对象。若要使用 Open 方法仍然需要获得对象 ID。和使用事务的 GetObject 方法类似,需要指定打开的模式和返回值。如果你修改了使用 Open 方法打开的对象后,可以使用 Cancel 方法回滚自对象被打开以后所做的所有修改。Cancel 必须被每一个你想回滚的对象调用。

NoteObjects must be paired with an open and close operation. If you use the Open method on an object, you must close it using either the Close or Cancel method. Failure to close the object will lead to read access violations and cause AutoCAD to become unstable.

注意对象必须成对的使用打开和关闭操作。如果在一个对象上使用了 Open 方法,就必须利用 CloseCancel 方法中的一个来关闭它。若关闭对象失败,将会导致一个读访问违规并会引起 AutoCAD 的不稳定。

If you need to work with a single object, using the Open and Close methods can reduce the number of lines of code that you might otherwise have to write compared to working with the Transaction Manager. However, using transactions is the recommended way of opening and closing objects.

如果需要使用单个对象,用 OpenClose 方法可以减少代码行,它是与使用事务管理器相比较不得不写的代码行。可是,推荐使用事务来打开和关闭对象。

WarningYou should not use the Open and Close methods when using transactions, as objects might not get opened or closed properly by the Transaction Manager which could cause AutoCAD to crash.

警告当使用事务的时候,不应该使用 OpenClose 方法,当这么做时,通过事务管理器可能无法完全的打开或关闭对象,而且可能引起 AutoCAD 崩溃。

查询对象

The following example demonstrates how to open and close objects without using a transaction and the GetObject method. To see the same example using the Transaction Manager, see Start a New Transaction and Open an Object.

下面示例演示如何在没有使用事务和 GetObject 方法时打开和关闭对象。若要查看使用事务管理器完成相同功能的代码,请参见 开始新事务并打开对象 部分。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("OpenCloseObjectId")> _
Public Sub OpenCloseObjectId()
  '' 获得当前文档和数据库   Get the current document and database
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  '' 以只读方式打开块表   Open the Block table for read
  Dim acBlkTbl As BlockTable
  acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead)
 
  '' 以只读方式打开模型空间的块表记录    Open the Block table record Model space for read
  Dim acBlkTblRec As BlockTableRecord
  acBlkTblRec = acBlkTbl(BlockTableRecord.ModelSpace).Open(OpenMode.ForRead)
 
  '' 遍历块表记录    Step through the Block table record
  For Each acObjId As ObjectId In acBlkTblRec
      acDoc.Editor.WriteMessage(vbLf & "DXF name: " & acObjId.ObjectClass().DxfName)
      acDoc.Editor.WriteMessage(vbLf & "ObjectID: " & acObjId.ToString())
      acDoc.Editor.WriteMessage(vbLf & "Handle: " & acObjId.Handle.ToString())
      acDoc.Editor.WriteMessage(vbLf)
  Next
 
  '' 关闭块表记录  Close the Block table record
  acBlkTblRec.Close()
  acBlkTblRec.Dispose()
 
  '' 关闭块表     Close the Block table
  acBlkTbl.Close()
  acBlkTbl.Dispose()
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("OpenCloseObjectId")]
public static void OpenCloseObjectId()
{
  // 获得当前文档和数据库   Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // 以只读方式打开块表   Open the Block table for read
  BlockTable acBlkTbl;
  acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable;
 
  // 以只读方式打开模型空间的块表记录    Open the Block table record Model space for read
  BlockTableRecord acBlkTblRec;
  acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForRead) as BlockTableRecord;
 
  // 遍历块表记录    Step through the Block table record
  foreach (ObjectId acObjId in acBlkTblRec)
  {
      acDoc.Editor.WriteMessage("\nDXF name: " + acObjId.ObjectClass.DxfName);
      acDoc.Editor.WriteMessage("\nObjectID: " + acObjId.ToString());
      acDoc.Editor.WriteMessage("\nHandle: " + acObjId.Handle.ToString());
      acDoc.Editor.WriteMessage("\n");
  }
 
  // 关闭块表记录  Close the Block table record
  acBlkTblRec.Close();
  acBlkTblRec.Dispose();
 
  // 关闭块表     Close the Block table
  acBlkTbl.Close();
  acBlkTbl.Dispose();
}

添加新对象到数据库中

This example demonstrates how to create a new object and append it to Model space without using the Transaction manager. To see the same example using the Transaction manager, see Start a New Transaction and Open an Object.

本例演示如何在没有使用事务管理器时创建新对象并将其追加到模型空间中。若要查看使用事务管理器完成相同功能的代码,请参见 开始新事务并打开对象

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("AddNewCircleOpenClose")> _
Public Sub AddNewCircleOpenClose()
  '' 获得当前文档和数据库   Get the current document and database
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  '' 以只读方式打开块表   Open the Block table for read
  Dim acBlkTbl As BlockTable
  acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead)
 
  '' 以写方式打开模型空间块表记录   Open the Block table record Model space for write
  Dim acBlkTblRec As BlockTableRecord
  acBlkTblRec = acBlkTbl(BlockTableRecord.ModelSpace).Open(OpenMode.ForWrite)
 
  '' 创建一个半径为3圆心在5,5的圆    Create a circle with a radius of 3 at 5,5
  Dim acCirc As Circle = New Circle()
  acCirc.SetDatabaseDefaults()
  acCirc.Center = New Point3d(5, 5, 0)
  acCirc.Radius = 3
 
  '' 添加新对象到模型空间和事务中   Add the new object to Model space and the transaction
  acBlkTblRec.AppendEntity(acCirc)
 
  ''关闭圆对象   Close the circle object
  acCirc.Close()
  acCirc.Dispose()
 
  '' 关闭块表记录  Close the Block table record
  acBlkTblRec.Close()
  acBlkTblRec.Dispose()
 
  '' 关闭块表     Close the Block table
  acBlkTbl.Close()
  acBlkTbl.Dispose()
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("AddNewCircleOpenClose")]
public static void AddNewCircleOpenClose()
{
  // 获得当前文档和数据库   Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // 以只读方式打开块表   Open the Block table for read
  BlockTable acBlkTbl;
  acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable;
 
  // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
  BlockTableRecord acBlkTblRec;
  acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForWrite) 
                  as BlockTableRecord;
 
  // 创建一个半径为3圆心在5,5的圆    Create a circle with a radius of 3 at 5,5
  Circle acCirc = new Circle();
  acCirc.SetDatabaseDefaults();
  acCirc.Center = new Point3d(5, 5, 0);
  acCirc.Radius = 3;
 
  // 添加新对象到模型空间和事务中   Add the new object to Model space and the transaction
  acBlkTblRec.AppendEntity(acCirc);
 
  // 关闭圆对象    Close the circle object
  acCirc.Close();
  acCirc.Dispose();
 
  // 关闭块表记录  Close the Block table record
  acBlkTblRec.Close();
  acBlkTblRec.Dispose();
 
  // 关闭块表     Close the Block table
  acBlkTbl.Close();
  acBlkTbl.Dispose();
}