autocad - 在 session 上下文中运行 AutoCAD 命令

标签 autocad autocad-plugin

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

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

但是,问题是我还需要控制保存绘图(以及保存的文件名)。如果不在 session 上下文中使用我的命令,我看不出有什么方法可以做到这一点,如下所示:

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

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

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

**编辑:带有 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;

最佳答案

您可以通过命令在应用程序( 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);
  }
}

未完全测试,基于 this blog post .

关于autocad - 在 session 上下文中运行 AutoCAD 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40996954/

相关文章:

python - 如何为 COM 构造对象列表?

c# - AutoCAD 未定义形状;样式对话框崩溃

lisp - 在 autocad 的连接点上打破多段线 - 任何 lisp 函数?

c# - 设置 Visual Studio 2013 来开发 AutoCAD 2015 插件

autocad - ObjectARX、RealDWG 还是 Teigha?

autocad - Java 库以编程方式将 AutoCAD .dwg 文件转换为 PDF 或图像?

c# - 绘制带网格的实心框

c# - 使用 Lisp 函数将 .NET 插件加载到 AutoCAD 2014

c# - 向 WPF 窗口实例添加资源