TheSwamp

Code Red => .NET => Topic started by: HD on October 18, 2005, 09:41:42 AM

Title: Using C# to Open and Close Drawing Files
Post by: HD on October 18, 2005, 09:41:42 AM
Hello All,

At the moment, I am simply trying to open and close drawings. The following C# program opens each drawing that was selected from a dialog box. I am not sure how to close each selected drawing (without saving changes) after it has been opened. I found the "Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.CloseAndDiscard()" method, but I am not sure how to make it work.


using System;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;


namespace TitleBlockConvert
{
   /// <summary>
   /// Summary description for Class1.
   /// </summary>
   public class Class1
   {
      public Class1()
      {
         //
         // TODO: Add constructor logic here
         //
      }

      // Define command.
      [CommandMethod("TBCONV")]
      static public void TBCONV()
      {
         // Get drawings to process
         Autodesk.AutoCAD.Windows.OpenFileDialog dwgsForm =
            new Autodesk.AutoCAD.Windows.OpenFileDialog("Drawings to Process","","dwg","",
            Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
         if (dwgsForm.ShowDialog() != DialogResult.OK)
            return;

         string[] dwgfiles = dwgsForm.GetFilenames();
         for (int i = 0; i < dwgfiles.GetLength(0); i++)
         {
            AcadApp.DocumentManager.Open(dwgfiles,false);
            //
            //
            // Stuff to do...
            //
            //
         }
      }
   }
}

Title: Re: Using C# to Open and Close Drawing Files
Post by: Draftek on October 18, 2005, 10:03:38 AM
Try this:

Document oDoc = AcadApp.DocumentManager.Open(dwgfiles,false);

// do your stuff


oDoc.CloseAndDiscard();
Title: Re: Using C# to Open and Close Drawing Files
Post by: Draftek on October 18, 2005, 10:06:03 AM
If your going to batch drawings, you might also make sure your command is defined in the application env, not the drawing by using the CommandFlags.Session attribute.