CAD secondary development StartTransaction, StartOpenCloseTransaction

In the CAD secondary development of C#, both StartTransaction and StartOpenCloseTransaction are used to start a new transaction. Their specific functions and usage are as follows:

  • StartTransaction
    StartTransaction is the method used to start a new transaction, it is called in the graph database context. Use this method to ensure proper locking and avoid data conflicts when reading and writing to the database.

For example, the following code demonstrates how to use the StartTransaction method in C# to create a new transaction and modify entities in the database in it:

using (Transaction tr = db.TransactionManager.StartTransaction())
{
    
    
    // 获取当前文档的块表
    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
    
    // 获取块表记录
    BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
    
    // 创建一个新的直线对象
    Line line = new Line(new Point3d(0, 0, 0), new Point3d(10, 10, 0));
    
    // 将直线对象添加到块表记录中
    btr.AppendEntity(line);
    
    // 在事务完成之前将新的实体添加到数据库中
    tr.AddNewlyCreatedDBObject(line, true);
    
    // 提交事务
    tr.Commit();
}

In this code example, a new transaction is started in the Transaction block and the entity in the database is modified. Before the transaction completes, new entities are added to the database by calling the AddNewlyCreatedDBObject method. Finally, commit the transaction by calling the Commit method.

  • StartOpenCloseTransaction
    StartOpenCloseTransaction is also a method for starting a new transaction, but its usage scenario is different from StartTransaction. This method is mainly used to handle the opening and closing operations of the document, for example, when some operations need to be performed before the document is opened or closed.

For example, the following code demonstrates how to use the StartOpenCloseTransaction method in C# to create a new transaction and open and close documents in it:

using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{
    
    
    // 在打开文档之前执行某些操作
    tr.StartOpenCloseSession();
    
    // 打开文档
    Document doc = Application.DocumentManager.Open(docName, true);
    
    // 在关闭文档之前执行某些操作
    tr.Commit();
    tr.EndOpenCloseSession();
    
    // 关闭文档
    doc.CloseAndDiscard();
}

In this code example, a new transaction is started within the Transaction block, and documents are opened and closed within it. Before opening the document, perform certain operations by calling the StartOpenCloseSession method. Before closing the document, commit the transaction by calling the Commit method, and end the open-close session by calling the EndOpenCloseSession method.

The function of the EndOpenCloseSession method is to notify AutoCAD that the opening and closing session has ended and release related resources. In the StartOpenCloseSession method, we call the Document.LockDocument method to lock the document to prevent other threads from operating on the document at the same time. Therefore, after the transaction is committed or rolled back, we must release the lock by calling the EndOpenCloseSession method so that other threads can Operate on the document.


In summary, when using the StartOpenCloseTransaction method, the following steps must be followed:

  1. Call the StartOpenCloseSession method within the transaction to perform any necessary operations.
  2. Call the Commit method in the transaction to commit the transaction.
  3. Call the EndOpenCloseSession method in the transaction to end the opening and closing session and release related resources.

ps: When using the StartOpenCloseTransaction method, you can call the StartOpenCloseSession method in the transaction to perform any necessary operations. The "necessary operations" here refer to some operations that need to be performed before opening and closing the document, such as setting some properties of the document, loading a specific plug-in or application, and so on. These operations are usually not related to the content of the document itself, but to the environment and settings in which the document is operated.


Any operations performed in the StartOpenCloseSession method will be done before the transaction is committed or rolled back to ensure that the document is in the correct state when it is opened or closed. For example, in the StartOpenCloseSession method, you can use the Document.LockDocument method to lock the document to ensure that no other threads will make changes to the document during the transaction.

It should be noted that after calling the StartOpenCloseSession method, the Commit method must be called in the transaction to commit the transaction so that all necessary operations can be completed before closing the document. Closing the document before committing the transaction may result in the document being permanently locked so that it cannot be opened again.

In addition, the StartOpenCloseTransaction method will lock the document when the transaction is started, and it will be unlocked only after the transaction is committed or rolled back. This means that when using the StartOpenCloseTransaction method, you must ensure that the document is not closed during the operation, otherwise the document may be permanently locked and cannot be opened again.

Therefore, you should use the StartOpenCloseTransaction method when you need to perform some operations before opening and closing the document, and use the StartTransaction method in other cases.

Guess you like

Origin blog.csdn.net/ultramand/article/details/130275085