Document Locking

Automation requests can occur in any AutoCAD context, coming from any number of client applications. To prevent conflicts with other requests, you are responsible for locking a document before modifying it. Failure to lock the document in certain contexts will cause a lock violation during modification of the database.

At times you will want to make a document current temporarily. For example, when adding an entity to *MODELSPACE or *PAPERSPACE, you need to both lock the document and make it current. Failure to make the document current will cause your entity to be invisible in the graphics display, even after a display regeneration.

In the ObjectARX API, the document manager class provides locking and context-switching functionality. Because these are common tasks, the Automation API encapsulates this behavior in an exported class named  AcAxDocLock. Instantiating an AcAxDocLock object, with appropriate arguments, both locks the document and makes it current.

For example:

STDMETHODIMP CMyEntity::Modify()
{
    AcAxDocLock docLock(m_objId, AcAxDocLock::kNormal);
    if(docLock.lockStatus() != Acad::eOk)
    {
        return E_FAIL;
    }
    // It is now safe to modify the database
    //
    return S_OK;
}