Maybe this will turn into a series of common mistakes
.
My last post was about event handler exception handling, and Adam wrote a note about unknown commands. Now, here are two very common mistakes people make with Transactions when they first start using the AutoCAD .NET API that can waste hours of time debugging (and make you feel really stupid when you find them
).
Forgetting to commit a Transaction
I once spent hours as a young and innocent AutoCAD .NET programmer trying to work out why the entities I was trying to add to the database weren’t there after my code had run. The problem was simply that I’d forgotten to call Transaction.Commit() before I Disposed of the Transaction. An uncommitted Transaction is Aborted when it is Disposed, so every change you made to the Database in the Transaction is rolled back.
This is the first thing to check when something you expect to have been added to the database by your code isn’t there.
Forgetting to end a Transaction
A typical symptom of forgetting to end your Transaction before you return control to AutoCAD is that AutoCAD graphics will start to behave strangely. Often you’ll notice that the entity you just added to the drawing isn’t displaying at all. And when you close AutoCAD you’ll either see an error dialog, or the Visual Studio Output window will show a non-zero return value from acad.exe.
The best way to ensure you clean up your Transactions is to make use of the Using statement. Here is some very basic VB.NET to demonstrate:
_ Public Sub MyCommand() Using tr As Transaction = db.TransactionManager.StartTransaction 'Do stuff tr.Commit() End Using End Sub

Leave a Reply