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

Run AutoCAD Commands in session Context

我需要批量导入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;

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

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