在会话上下文中运行AutoCAD命令

时间:2016-12-06 13:50:21

标签: autocad autocad-plugin

我需要批量导入dgns。我可以使用api中的最新命令选项执行此操作:

Application.DocumentManager.MdiActiveDocument.Editor.Command("-dgnimport", currentdgnname, "", "Master", "Standard");

然而,问题是我还需要控制保存图形(和保存的文件名)。如果不在会话环境中使用我的命令,我就看不到这样做的方法:

const string DGNIMPORTBATCHname = "DGNIMPORTBATCH";
[CommandMethod(DGNIMPORTBATCHname, CommandFlags.Session)]

Editor.Command方法虽然只是文档上下文。

添加文档锁定不起作用。有没有办法在会话命令中切换到在文档上下文中运行代码?

**编辑:使用Activationcontext的示例代码:

const string DGNIMPORTBATCHname = "DGNIMPORTBATCH";
[CommandMethod(DGNIMPORTBATCHname)]
public static void RunDGNIMPORTBATCH()
{
  Document doc = Application.DocumentManager.MdiActiveDocument;
  Editor ed = doc.Editor;

  OpenFileDialog ofd = new OpenFileDialog("Convert DGNs", "", "dgn", "", OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
  System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
  if (dr != System.Windows.Forms.DialogResult.OK)
    return;
  foreach (string f in ofd.GetFilenames())
  {
    string dwgfilename = Path.Combine(Path.GetDirectoryName(f), Path.GetFileNameWithoutExtension(f) + ".dwg");
    if (File.Exists(dwgfilename))
      File.Delete(dwgfilename);
    currentdgnname = f;
    currentdwgname = dwgfilename;
    List<string> names = new List<string>() { currentdgnname, currentdwgname };
    //creates our document and sets it current                  Application.DocumentManager.ExecuteInApplicationContext(CreateDGNDocHelper, names);
    currentdoc.Editor.Command("-dgnimport", currentdgnname, "", "Master", "Standard");
    currentdoc.Editor.Command("._ZOOM", "Extents");
  }
}
static Document currentdoc;
static void CreateDGNDocHelper(object data)
{
  currentdoc = Application.DocumentManager.Add("acad.dwt");
  Application.DocumentManager.MdiActiveDocument = currentdoc;
}
static string currentdgnname;
static string currentdwgname;

1 个答案:

答案 0 :(得分:2)

您可以在命令中执行应用程序(会话)或文档上下文,请参阅以下选项:

Application.DocumentManager.ExecuteInApplicationContext();
Application.DocumentManager.ExecuteInCommandContextAsync();

修改

根据您的原始示例代码,以下是代码建议:

const string DGNIMPORTBATCHname = "DGNIMPORTBATCH";
[CommandMethod(DGNIMPORTBATCHname)]
public async static void RunDGNIMPORTBATCH()
{
  OpenFileDialog ofd = new OpenFileDialog("Convert DGNs", "", "dgn", "", OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
  System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
  if (dr != System.Windows.Forms.DialogResult.OK) return;

  foreach (string f in ofd.GetFilenames())
  {
    string dwgfilename = Path.Combine(Path.GetDirectoryName(f), Path.GetFileNameWithoutExtension(f) + ".dwg");
    if (File.Exists(dwgfilename)) File.Delete(dwgfilename);

    currentdgnname = f;
    currentdwgname = dwgfilename;

    await Application.DocumentManager.ExecuteInCommandContextAsync(
    async (obj) =>
    {
      Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
      await ed.CommandAsync(new object[] { "-dgnimport", currentdgnname, "", "Master", "Standard" });
      await ed.CommandAsync(new object[] { "._ZOOM", "Extents" });
    },
    null);
  }
}

未完全测试,基于this blog post