2

I need to batch import dgns. I can do this using the latest command options in the api using:

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

However, the issue is that I also need to control saving the drawing (and the saved file name). I don't see a way to do this without using my command in session context like this:

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

The Editor.Command method though is document context only.

Adding document locks doesn't work. Is there a way to switch to running code in document context while in a session command?

**Edit: Sample code with 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;
Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44

1 Answers1

2

You can execute in both application (session) or document context from your command, see these options:

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

EDIT

Based on your the original sample code, here is a suggestion of code:

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

Not fully tested, based on this blog post.

Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44