new Database, ReadDwgFile, crashing

new Database, ReadDwgFile, crashing

oransen
Collaborator Collaborator
803 Views
3 Replies
Message 1 of 4

new Database, ReadDwgFile, crashing

oransen
Collaborator
Collaborator

Is there anything wrong with this code? It is called in a loop with changing filenames. AT the very end AutoCAD crashes.

 

public Substituter (string sOldString, string sNewString, string sSourceFullFileName, string sDestFullPath)
        {
            // The name won't change but the directory will 
            string sNameOnly = Path.GetFileName(sSourceFullFileName);

            // Save as a new file 
            string sFullDestFileName = sDestFullPath + @"\" + sNameOnly;

            using (Database db = new Database(false, true))
            {
                db.ReadDwgFile(sSourceFullFileName, FileOpenMode.OpenForReadAndWriteNoShare, false, "");

                // closing the input makes sure the whole dwg is read from disk
                // it also closes the file so you can SaveAs the same name
                db.CloseInput(true);

                // store the current working database, to reset
                Database PreviousDb = HostApplicationServices.WorkingDatabase;

                // set working database
                HostApplicationServices.WorkingDatabase = db;

                // { Do Some Work}
                
// Take this out and there is no crash... db.SaveAs(sFullDestFileName, DwgVersion.Current); // reset the working database HostApplicationServices.WorkingDatabase = PreviousDb; } }

It creates the files and then crashes at the end. If I don't call SaveAs it does not crash. What am I missing? The SaveAs apparently works though...!

0 Likes
Accepted solutions (1)
804 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You do not show the { Do Some Work } part which might be relevant, according to what it contains setting the working database may be useless.

 

Assuming the arguments and the concatenation of the destination path are valid, you can try to reset the working database before calling db.SaveAs(). And a good practice would be to call it in the Finally block of a Try/Catch/Finally statement as shown here.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4

oransen
Collaborator
Collaborator

@_gile wrote:

Hi,

 

You do not show the Do Some Work part which might be relevant, according to what it contains setting the working database may be useless.


I don't show the Do Some Work because it is currently empty. Thanks for the other suggestions though, I'll try them tomorrow.

0 Likes
Message 4 of 4

oransen
Collaborator
Collaborator

@_gilethanks for pointing to that example, I followed the code there and all is working in my program now.

 

I'm clearly doing something wrong when I do the stuff with the PreviousDb...

 

 

 

0 Likes