オブジェクトの変更または AutoCAD へのアクセスの要求はどのようなコンテキストでも発生する可能性があり、複数のアプリケーションから要求される場合があります。他の要求との競合を回避するため、変更する前にドキュメントをロックする必要があります。特定のコンテキストでドキュメントをロックしないと、データベースの変更中にロック違反が発生します。アプリケーションで次のことを行うときは、ドキュメントをロックします。
たとえば、現在のドキュメント以外のドキュメントのモデル空間またはペーパー空間に図形を追加するときは、そのドキュメントをロックする必要があります。ロックする Database オブジェクトの LockDocument メソッドを使用します。LockDocument メソッドを呼び出すと、DocumentLock オブジェクトが返されます。
ロックされているデータベースの変更が終了したら、データベースのロックを解除する必要があります。データベースのロックを解除するには、DocumentLock オブジェクトの Dispose メソッドを呼び出します。DocumentLock オブジェクトで Using ステートメントを使用することもできます。Using ステートメントが終了すると、データベースがロック解除されます。
この例では、新しいドキュメントを作成し、次に円を描きます。ドキュメントを作成された後、新しいドキュメントのデータベースがロックされ、その後で円がドキュメントに追加されます。円が追加された後、データベースのロックが解除され、関連付けられたドキュメント ウィンドウが現在のものとして設定されます。
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("LockDoc", CommandFlags.Session)> _
Public Sub LockDoc()
'' Create a new drawing
Dim acDocMgr As DocumentCollection = Application.DocumentManager
Dim acNewDoc As Document = DocumentCollectionExtension.Add(acDocMgr, "acad.dwt")
Dim acDbNewDoc As Database = acNewDoc.Database
'' Lock the new document
Using acLckDoc As DocumentLock = acNewDoc.LockDocument()
'' Start a transaction in the new database
Using acTrans As Transaction = acDbNewDoc.TransactionManager.StartTransaction()
'' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acDbNewDoc.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 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)
acTrans.AddNewlyCreatedDBObject(acCirc, True)
End Using
'' Save the new object to the database
acTrans.Commit()
End Using
'' Unlock the document
End Using
'' Set the new document current
acDocMgr.MdiActiveDocument = acNewDoc
End Sub
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("LockDoc", CommandFlags.Session)]
public static void LockDoc()
{
// Create a new drawing
DocumentCollection acDocMgr = Application.DocumentManager;
Document acNewDoc = acDocMgr.Add("acad.dwt");
Database acDbNewDoc = acNewDoc.Database;
// Lock the new document
using (DocumentLock acLckDoc = acNewDoc.LockDocument())
{
// Start a transaction in the new database
using (Transaction acTrans = acDbNewDoc.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acDbNewDoc.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 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);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
}
// Save the new object to the database
acTrans.Commit();
}
// Unlock the document
}
// Set the new document current
acDocMgr.MdiActiveDocument = acNewDoc;
}