Code Red > .NET

Using C# to Open and Close Drawing Files

(1/1)

HD:
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...
            //
            //
         }
      }
   }
}

Draftek:
Try this:

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

// do your stuff


oDoc.CloseAndDiscard();

Draftek:
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.

Navigation

[0] Message Index

Go to full version