I'm writing AutoCAD 2013 .NET plug-in with Windows Form, using C#. When I using modeless form in the command
Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog (new Form1 ());
instead of next code
System.Windows.Forms.Application.EnableVisualStyles ();
// System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); // here I get exception too
System.Windows.Forms.Application.Run (new Form1 ());
I have lock_exception in the line
BlockTableRecord btrRecord = new BlockTableRecord ();
btTable.UpgradeOpen (); // <--- here Exception
Thank you, in advance.
The right answer is
To make some actions with model of autocad, when modeless dialog is opened, it is nessesary to lock active document, that can't be modified other way, while actions is performed. For example:
public void CreateBlockReference (string strBlockName, Point3d Origin)
{
// First: lock document by next instruction
DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument ();
// Second: create separate transaction for each your action
Transaction t = db.TransactionManager.StartTransaction ();
try
{
// Third: try to make the action your wish
// here I'm getting the table of Blocks this drawing.dwg
BlockTable btTable = (BlockTable)t.GetObject (db.BlockTableId, OpenMode.ForRead);
// now get the space of AutoCAD model
BlockTableRecord btrModelSpace = (BlockTableRecord)t.GetObject (
btTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
if( !btTable.Has (strBlockName) )
{
ed.WriteMessage (string.Format (msgs.BlockNoExist, strBlockName));
throw new Exception (ErrorStatus.MissingBlockName,
string.Format (msgs.BlockNoExist, strBlockName));
}
// extract the Block definition, that I want to create a reference
ObjectId myBlockId = btTable[strBlockName];
// next I'm creating the new instance of my Block by definition
BlockReference brRefBlock = new BlockReference (Origin, myBlockId);
// insert created blockreference into space of model
btrModelSpace.AppendEntity (brRefBlock);
t.AddNewlyCreatedDBObject (brRefBlock, true);
// successfully close transaction
t.Commit ();
} // end try
finally // or not
{ t.Dispose ();
dl.Dispose (); // END OF LOCKING!!! ANYWAY
} // end finally
}