2013-10-02 115 views
0

我正在用Windows Form,使用C#編寫AutoCAD 2013 .NET插件。 當我在命令中使用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 
    }