.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fatal Error when opening a drawing.

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
dpayton
930 Views, 7 Replies

Fatal Error when opening a drawing.

When opening a dwg with the given code I get the following fatal error and autocad crashes. 

.2017-07-24_9-34-21.png

 

The dwg I am opening is corrupt, I can fix the dwg. But given the condition of client drawings this is likely going to be an issue I am going to run into again. I would like to be able to run my programs without it crashing when I run into a dwg I cannot open. I have tryed enclosing it in a try-catch but it still crashes aoutcad. 

 

How do I deal with these types of drawings without the program crashing? Is there a way to check a drawing to see if it can be opened beforehand? 

 

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (doc.LockDocument())
            {
                using (Database AcDB = new Database(true, false))
                {
                    AcDB.ReadDwgFile(file, false, string.Empty);
                    AcDB.CloseInput(true);
                    using (Transaction AcTrans = AcDB.TransactionManager.StartTransaction())
                    {
                       //do stuff
                    }
                }
            }

 

7 REPLIES 7
Message 2 of 8
ActivistInvestor
in reply to: dpayton


@Anonymous wrote:

When opening a dwg with the given code I get the following fatal error and autocad crashes. 

.2017-07-24_9-34-21.png

 

The dwg I am opening is corrupt, I can fix the dwg. But given the condition of client drawings this is likely going to be an issue I am going to run into again. I would like to be able to run my programs without it crashing when I run into a dwg I cannot open. I have tryed enclosing it in a try-catch but it still crashes aoutcad. 

 

How do I deal with these types of drawings without the program crashing? Is there a way to check a drawing to see if it can be opened beforehand? 

 

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (doc.LockDocument())
            {
                using (Database AcDB = new Database(true, false))
                {
                    AcDB.ReadDwgFile(file, false, string.Empty);
                    AcDB.CloseInput(true);
                    using (Transaction AcTrans = AcDB.TransactionManager.StartTransaction())
                    {
                       //do stuff
                    }
                }
            }

 


I don't know of a way to detect a corrupt DWG file without trying to open it. If you can't catch the exception using catch(System.Exception) {...}, then try using catch{...} without an exception variable, and see if that catches it.

Message 3 of 8
dpayton
in reply to: ActivistInvestor

The program never gets to the catch. As soon as it hits AcDB.ReadDwgFile(file, false, string.Empty); AcDB.CloseInput(true);  in the try autocad crashes.

Message 4 of 8
ActivistInvestor
in reply to: dpayton


@Anonymous wrote:

The program never gets to the catch. As soon as it hits AcDB.ReadDwgFile(file, false, string.Empty); AcDB.CloseInput(true);  in the try autocad crashes.


Did you try what I suggested, and outside of the debugger?

 

If you did try it, and get the same result, here is one more thing you can try:

 

// using System.Runtime.CompilerServices;
try
{
   // Call ReadDwgFile() here
}
catch(System.Exception ex)
{
   RuntimeWrappedException rwe = ex as RuntimeWrappedException;
   if(rwe != null)
      Console.WriteLine(rwe.Message);
}

 

 

Message 5 of 8
Virupaksha_aithal
in reply to: dpayton

Hi,

 

As you are trying to open an drawing, please try with using (Database AcDB = new Database(false, false)).



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

Message 6 of 8
ActivistInvestor
in reply to: dpayton


@Anonymous wrote:

The program never gets to the catch. As soon as it hits AcDB.ReadDwgFile(file, false, string.Empty); AcDB.CloseInput(true);  in the try autocad crashes.


I confess to having missed your use of true as the first argument to the Database constructor, which as @Virupaksha_aithal points out, is incorrect.

 

That's probably because since the beginning of time, I've always relied on this helper for opening DWG files:

 

 

 

public static class DatabaseHelper
{
   public static Database Open(string filename, FileOpenMode mode = FileOpenMode.OpenForReadAndWriteNoShare)
   {
      if(string.IsNullOrWhiteSpace(filename))
         throw new ArgumentException("filename");
      if(!System.IO.File.Exists(filename))
         throw new System.IO.FileNotFoundException(filename);
      Database db = new Database(false, false);
      try
      {
         db.ReadDwgFile(filename, mode, false, string.Empty);
         return db;
      }
      catch
      {
         db.Dispose();
         throw;
      }
   }
}
Message 7 of 8
dpayton
in reply to: ActivistInvestor

Thank you Virupaksha.aithal and Activist_Investor for your help.

Message 8 of 8

Hello @Virupaksha_aithal

I tried to use 

 

using (Database db = new Database(false, false))
{
        try
       {
              db.ReadDwgFile(docname, FileOpenMode.OpenForReadAndWriteNoShare, false, null);
             db.CloseInput(true);
      }
      catch (System.Exception ex)
     {


      }

}

 

But above code also crashing and show fatal error while running using accoreconsole.exe. Fatal error screenshot attached. Please help how console program can catch this issue.

 

 

Tags (2)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report