PDA

View Full Version : Transactions Problem


trichardson.155519
2007-12-11, 04:52 PM
I'm getting an eLockViolation that I'm just banging my head against. I'm a newbie developer, and I'm trying to insert a block into a drawing by looking it up in a form. (The end result will be a BlockTableRecord with Block Reference and an DBText, and a BlockReference in model space.) I look it up in a form that then fires this event. My problem is that when I try to do any sort of upgradeOpen or OpenMode.ForWrite from a form, I get an elockviolation. My next step is to make sure this works if I invoke purely from commandline. If something looks fishy, its because I'm translating and interpreting this from the DevTV stuff thats written in VB. The code appears as I'm debugging it, that is it can be run and it can cause the violation when it gets to the bt.UpgradeOpen(); line.

public Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

public void ann_block(Object sender, EventArgs e)
{

string blkName = myFrm.listBox1.SelectedItem.ToString();
string annotation = myFrm.textBox1.Text;

if ((blkName == null) || (annotation == null) || (annotation == ""))
{
return;
}

Database db = ed.Document.Database;

using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord v_block = new BlockTableRecord();
string v_name = string.Concat(blkName, "_", annotation);
v_block.Name = v_name;
if (!bt.Has(v_name))
{
ed.WriteMessage("\n\nCreating Block-\n");
bt.UpgradeOpen(); //HERES THE ERROR
}
}
catch (System.Exception ex)
{
string error_msg = string.Concat("\n\n****Error: ", ex.Message.ToString(), "******\n\n");
ed.WriteMessage(error_msg);
ed.WriteMessage(ex.StackTrace);

}
finally
{
trans.Dispose();
}
}

}

trichardson.155519
2007-12-11, 06:22 PM
Ok... after banging my head against the wall and getting some more coffee... the solution was in a news group and on a blog....

Basically had to add a lock and execute the write code in that using block


using (DocumentLock dl = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
//blah blah blah
bt.UpgradeOpen();
Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen();
}


Thanks for any that were getting ready to post a reply.

PS. address for blog entry that helped http://bobbycjones.spaces.live.com/blog/cns!2A36783BF92E8178!160.entry
Thanks again!