So I just ran into the problem of needing to lock the document before I can open the database for write. This may be obvious to some, but I’d love to get some confirmation or correction about my understanding of the issue.
I’ve got functions that get called from a [CommandMethod] and run great. If I call the same functions from a button on my palette, I get an eLockViolation. In order to get this code to run, I need to call the LockDocument() function, which I found via the interwebs in Lab6 of the Autodesk .NET tutorials from back in the day.
My guess is that CAD locks the document everytime a command is called, which is why I don’t need to call it at all if my functions have been called by commands. Is this correct?
If so, I only need to lock the document when calling a database write from a non command (such as a button click) or an updated field in a properties grid.
My current approach is to lock the document in the button click event as shown. This looks like a bad practice to me, but I’m not sure I need to imbed it deeper in my code. Are the interface events a good place to lock the document from? Is there some sort of document unlock/release I should call when I'm done?
private void btnValveInsert_Click(object sender, EventArgs e)
{
Active.Document.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true);
ValveFactory.InsertDynamicValve(30);
}
// VS
[CommandMethod("IR_InsValve")]
public void InsertValve()
{
ValveFactory.InsertDynamicValve(30);
}
On a slightly different note, when reviewing the LAB6 from the tutorial, I noticed that the following comment: //Whatever happens we must dispose the transaction. (This is in the Finally block).
Whenever I use a transaction, I commit() it when I’m done with it and have not been calling dispose(), and things have been running fine. What's the purpose of disposing of a transaction?