When opening a dwg with the given code I get the following fatal error and autocad crashes.
.
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 } } }
Solved! Go to Solution.
Solved by Virupaksha_aithal. Go to Solution.
@Anonymous wrote:When opening a dwg with the given code I get the following fatal error and autocad crashes.
.
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.
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.
@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); }
Hi,
As you are trying to open an drawing, please try with using (Database AcDB = new Database(false, false)).
@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; } } }
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.
Can't find what you're looking for? Ask the community or share your knowledge.