トランザクションを使用すると複数のオブジェクトを開いて作業することが簡単になりますが、オブジェクトを開いて編集する方法はこれだけではありません。トランザクションを使用する以外にも、Open および Close メソッドを使ってオブジェクトを開き、閉じることができます。ただし、Open メソッドを使用するには、オブジェクト ID を取得する必要があります。トランザクションで使用される GetObject メソッドと同様にオープン モードを指定する必要があり、戻り値はオブジェクトになります。
Open メソッドを使って開いた後にオブジェクトに変更を加えた場合、Cancel メソッドを使用すると、オブジェクトを開いてから加えたすべての変更をロールバックすることができます。ロールバックする各オブジェクトで、Cancel を呼び出す必要があります。また、オブジェクトを閉じた後、Dispose メソッドを使用してオブジェクトを正しく破棄する必要があります。Using ステートメントを使用して、オブジェクトを閉じて破棄することもできます。
1 つのオブジェクトを操作する必要がある場合は、Open および Close メソッドを使用すると、トランザクション マネージャを使用する場合と比較して、記述する必要があるコードの行数を減らすことができます。ただし、オブジェクトを開いて閉じるには、トランザクションを使用する方法をお勧めします。
次の例では、トランザクションと GetObject メソッドを使用せずにオブジェクトを開き、閉じる方法について説明します。
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 = Nothing
Try
acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead)
' Open the Block table record Model space for read
Dim acBlkTblRec As BlockTableRecord = Nothing
Try
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
Catch es As Autodesk.AutoCAD.Runtime.Exception
MsgBox(es.Message)
Finally
' Close the Block table record
If Not acBlkTbl.ObjectId.IsNull Then
acBlkTblRec.Close()
acBlkTblRec.Dispose()
End If
End Try
Catch es As Autodesk.AutoCAD.Runtime.Exception
MsgBox(es.Message)
Finally
' Close the Block table
If Not acBlkTbl.ObjectId.IsNull Then
acBlkTbl.Close()
acBlkTbl.Dispose()
End If
End Try
End Sub
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 = null;
try
{
acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for read
BlockTableRecord acBlkTblRec = null;
try
{
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");
}
}
catch (Autodesk.AutoCAD.Runtime.Exception es)
{
System.Windows.Forms.MessageBox.Show(es.Message);
}
finally
{
// Close the Block table
if (!acBlkTblRec.ObjectId.IsNull)
{
// Close the Block table record
acBlkTblRec.Close();
acBlkTblRec.Dispose();
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception es)
{
System.Windows.Forms.MessageBox.Show(es.Message);
}
finally
{
// Close the Block table
if (!acBlkTbl.ObjectId.IsNull)
{
acBlkTbl.Close();
acBlkTbl.Dispose();
}
}
}
次の例では、手動でオブジェクトを閉じて破棄する代わりに、Using ステートメントを使用してオブジェクトを開いて閉じる方法を示します。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("OpenCloseObjectIdWithUsing")> _
Public Sub OpenCloseObjectIdWithUsing()
' 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
Using acBlkTbl As BlockTable = acCurDb.BlockTableId.Open(OpenMode.ForRead)
' Open the Block table record Model space for read
Using acBlkTblRec As BlockTableRecord = 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
End Using
' Close the Block table
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("OpenCloseObjectIdWithUsing")]
public static void OpenCloseObjectIdWithUsing()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Open the Block table for read
using (BlockTable acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
{
// Open the Block table record Model space for read
using (BlockTableRecord 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
}
// Close the Block table
}
}
この例では、新しいオブジェクトを作成し、トランザクション マネージャを使用せずにモデル空間に追加する方法を示します。
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
Using acBlkTbl As BlockTable = = acCurDb.BlockTableId.Open(OpenMode.ForRead)
' Open the Block table record Model space for write
Using acBlkTblRec As BlockTableRecord = acBlkTbl(BlockTableRecord.ModelSpace).Open(OpenMode.ForWrite)
' Create a circle with a radius of 3 at 5,5
Using 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
End Using
' Close the Block table record
End Using
' Close the Block table
End Using
End Sub
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
using (BlockTable acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
{
// Open the Block table record Model space for write
using (BlockTableRecord acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForWrite)
as BlockTableRecord)
{
// Create a circle with a radius of 3 at 5,5
using (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 and dispose the circle object
}
// Close the Block table record
}
// Close the Block table
}
}