Open and Close Objects without the Transaction Manager
 
 
 

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.

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.

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.

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.

Query objects

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.

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();
}

Add a new object to the database

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)
 
  '' Create a circle with a radius of 3 at 5,5
  Dim acCirc As Circle = New Circle()
  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;
 
  // Create a circle with a radius of 3 at 5,5
  Circle acCirc = new Circle();
  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();
}