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



我需要批量导入DGN。我可以使用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(session)或从命令中执行文档上下文,请参阅以下选项:

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);
  }
}

未完全测试,基于此博客文章。

相关内容

  • 没有找到相关文章

最新更新