If your code is executed in the so-called document context, you do not have to lock the document. Therefore, when you execute a command that has been registered via acedDefun(),or when it has been registered via acedRegCmds->addCommand() without the ACRX_CMD_SESSION flag ( CommandFlags.Session in .NET), your code executes in document context. In this context, AutoCAD locks the current document for you and unlocks it when your function ends. If you have to access other documents in the document context, you have to lock the other documents ‘manually’. You should register your command for the application context by using the ACRX_CMD_SESSION command flag ( CommandFlags.Session in .NET).
In any other context you have to lock the document you want to modify. This means you have to lock a document if your command is registered for the application context. This is by using the ACRX_CMD_SESSION flag on addCommand(), or in a message handler of a modeless dialog box where you have to modify database objects. If your application defines a COM interface and if you provide methods or properties where you have to change a document or objects, you have to lock a document, as well.
Following are small demos of ARX and .NET when you want to lock document in the command.
ObjectARX:
// define your command with ACRX_CMD_SESSION acedRegCmds->addCommand(L"ADSK",L"MyTest",L"MyTest",ACRX_CMD_MODAL | ACRX_CMD_SESSION , MyTestARX); // add one circle to an non-active document static void MyTestARX() { AcApDocument* pActiveDoc = acDocManager- >mdiActiveDocument(); AcApDocumentIterator *pIter = acDocManager->newAcApDocumentIterator(); // just select one non-active document AcApDocument *pDocToDo = NULL; for (;!pIter->done() ; pIter->step() ) { if(pIter->document() != pActiveDoc) { pDocToDo = pIter->document() ; break; } } delete pIter; // lock the document // in default, the DocLockMode // is write enable. Acad::ErrorStatus es = acDocManager->lockDocument( pDocToDo /* AcAp::DocLockMode = AcAp::kWrite */ ); if (es != Acad::eOk) { // fail to lock return; }
// return status: // 1. Acad::eOk if the lock change // was successful. // 2. Acad::eLockChangeInProgress // if an attempt to lock a document // is made from the document lock // change reactor callback. // You cannot "nest" locking requests. // 3. Acad::eVetoed // if the lock change is vetoed by // another application // 4. Acad::eNoDocument // if pDoc is NULL. AcDbDatabase* pDb = pDocToDo->database(); // get the block table AcDbBlockTable *pBlockTable; es = pDb->getBlockTable(pBlockTable, AcDb::kForRead); if (es != Acad::eOk) { return; } // get model space AcDbBlockTableRecord *pBlockTableRec; es = pBlockTable-> getAt(ACDB_MODEL_SPACE, pBlockTableRec, AcDb::kForWrite); if (es != Acad::eOk) { pBlockTable->close(); return; } pBlockTable->close(); // create a new entity AcDbCircle *pCircle = new AcDbCircle(AcGePoint3d(0,0,0), AcGeVector3d(0,0,1),100); // add the new entity to the model space AcDbObjectId objId; pBlockTableRec->appendAcDbEntity(objId, pCircle); // close the entity pCircle->close(); // close the model space block pBlockTableRec->close(); // unlock the document es = acDocManager->unlockDocument(pDocToDo); if(es == Acad::eOk) { // handle one document // successfully } }
<div style="font-family: courier new;background: white;color: black;font-size: 8pt"> <p style="margin: 0px"><span style="line-height: 140%"></span></p> </div> </div> .<strong>NET</strong> <p></p> <pre class="line-numbers"><code>using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;</code></pre> <p style="margin: 0px"><span style="line-height: 140%;color: green">// modify value of 'TILEMODE ' of one document</span></p> <p style="margin: 0px"><span style="line-height: 140%">[</span><span style="line-height: 140%;color: #2b91af">CommandMethod</span><span style="line-height: 140%">(</span><span style="line-height: 140%;color: #a31515">"MyTest"</span><span style="line-height: 140%">, </span><span style="line-height: 140%;color: #2b91af">CommandFlags</span><span style="line-height: 140%">.Session)]</span></p> <p style="margin: 0px"><span style="line-height: 140%;color: blue">public</span><span style="line-height: 140%"> </span><span style="line-height: 140%;color: blue">void</span><span style="line-height: 140%"> MyTestNet()</span></p> <p style="margin: 0px"><span style="line-height: 140%">{</span></p> <p style="margin: 0px"><span style="line-height: 140%">    </span><span style="line-height: 140%;color: blue">foreach</span><span style="line-height: 140%"> (</span><span style="line-height: 140%;color: #2b91af">Document</span><span style="line-height: 140%"> doc </span><span style="line-height: 140%;color: blue">in</span></p> <p style="margin: 0px"><span style="line-height: 140%">        </span><span style="line-height: 140%;color: #2b91af">AcadApp</span><span style="line-height: 140%">.DocumentManager)</span></p> <p style="margin: 0px"><span style="line-height: 140%">    {</span></p> <p style="margin: 0px"><span style="line-height: 140%">        </span><span style="line-height: 140%;color: blue">if</span><span style="line-height: 140%"> (doc != </span><span style="line-height: 140%;color: #2b91af">AcadApp</span><span style="line-height: 140%">.DocumentManager.MdiActiveDocument)</span></p> <p style="margin: 0px"><span style="line-height: 140%">        {</span></p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: green">// switch the active document </span></p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: #2b91af">AcadApp</span><span style="line-height: 140%">.DocumentManager.MdiActiveDocument = doc;</span></p> <p style="margin: 0px"> </p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: green">// If you do not lock the document </span></p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: green">// then the below code just simply has no effect </span></p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: green">// whereas "doc.Database.TileMode = </span></p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: green">// !doc.Database.TileMode;"  </span></p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: green">// would even crash AutoCAD       </span></p> <p style="margin: 0px"> </p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: green">// If you lock the document then both should <br />                work fine </span></p> <p style="margin: 0px"><span style="line-height: 140%">            </span><span style="line-height: 140%;color: blue">using</span><span style="line-height: 140%"> (doc.LockDocument())</span></p> <p style="margin: 0px"><span style="line-height: 140%">            {</span></p> <p style="margin: 0px"><span style="line-height: 140%">                </span><span style="line-height: 140%;color: blue">short</span><span style="line-height: 140%"> tm = <br />                (</span><span style="line-height: 140%;color: blue">short</span><span style="line-height: 140%">)</span><span style="line-height: 140%;color: #2b91af">AcadApp</span><span style="line-height: 140%">.GetSystemVariable(</span><span style="line-height: 140%;color: #a31515">"TILEMODE"</span><span style="line-height: 140%">);</span></p> <p style="margin: 0px"><span style="line-height: 140%">    &#1
60; AcadApp.SetSystemVariable("TILEMODE",
(tm + 1) % 2);
}
break;
}
}
}

Leave a Reply to john keaysCancel reply