0

我正在使用 C# 编写带有 Windows 窗体的 AutoCAD 2013 .NET 插件。当我在命令中使用无模式形式时

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

先感谢您。

4

1 回答 1

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
  }
于 2013-12-07T11:59:32.460 回答