title: Run AutoCAD Commands in session Context
body: <p>I need to batch import dgns. I can do this using the latest command options in the api using: </p>
  
  <pre><code>Application.DocumentManager.MdiActiveDocument.Editor.Command("-dgnimport", currentdgnname, "", "Master", "Standard");
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>const string DGNIMPORTBATCHname = "DGNIMPORTBATCH";
  [CommandMethod(DGNIMPORTBATCHname, CommandFlags.Session)]
  </code></pre>
  
  <p>The Editor.Command method though is document context only.</p>
  
  <p>Adding document locks doesn't work. Is there a way to switch to running code in document context while in a session command?</p>
  
  <p>**Edit: Sample code with Activationcontext:</p>
  
  <pre><code>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&lt;string&gt; names = new List&lt;string&gt;() { 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;
  </code></pre>