By Adam Nagy
Unlike in vanilla AutoCAD, in AutoCAD Mechanical each command is automatically wrapped in a Transaction. You can verify this by checking the value of Database.TransactionManager.TopTransaction inside your command - in case of AutoCAD Mechanical it will be an object with type AppTransaction:
When using the PlotEngine in your code to publish a drawing and creating your own Transaction to open the necessary objects, it seems to get entangled with AppTransaction. As a result you get an exception "Operation is not valid due to the current state of the object." when calling Transaction.Commit() at the end of your command:
There seems to be two workarounds: either just use the already available AppTransaction inside AutoCAD Mechanical to open the objects you need (not sure if that could have some side effects) or use an OpenCloseTransaction instead of a normal Transaction, by calling StartOpenCloseTransaction():