2013-10-02 3 views
0

Я пишу плагин AutoCAD 2013 .NET с Windows Form, используя C#. Когда я с помощью немодальной формы в командеC# DataGridView и форма Windows

Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog (new Form1()); 

вместо следующего кода

System.Windows.Forms.Application.EnableVisualStyles(); 
// System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); // here I get exception too 
System.Windows.Forms.Application.Run (new Form1()); 

У меня есть lock_exception в линии

BlockTableRecord btrRecord = new BlockTableRecord(); 
btTable.UpgradeOpen(); // <--- here Exception 

Спасибо, заранее.

ответ

0

Правильный ответ

Чтобы сделать какие-либо действия с моделью AUTOCAD, когда открывается диалоговое окно модальным, то Обязательное для блокировки активного документа, который не может быть изменен другим способом, в то время как действия выполняется. Например:

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 
    }