The proper way to clear database after ReadDwgFile

The proper way to clear database after ReadDwgFile

soonhui
Advisor Advisor
449 Views
3 Replies
Message 1 of 4

The proper way to clear database after ReadDwgFile

soonhui
Advisor
Advisor

Thanks to @CADbloke 's  CADtest, I seem to be able to get Civil 3D automated testing to work! But now I have an issue though, in my test case, I need to load the existing drawing content to the database at the active MDI Document via the method ReadDwgFile.

 

        protected void RunTestAction(string drawingPath, Action<Database> action)
        {
            Assert.IsTrue(File.Exists(drawingPath));

            Document doc = Application.DocumentManager.MdiActiveDocument;
            using (doc.LockDocument())
            {
                using (var db = new Database(false, false))
                {
                    db.ReadDwgFile(drawingPath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
                    using (new WorkingDatabaseSwitcher(db))
                    {
                        action(db); //this is where the testing and Assert occurs
                    }

                }//database

            }//lock document

        }

 

My issue is that is there anyway to clear the database properly after I ReadDwgFile ( and do the necessary testing)? This is so that the Document's Database will be reset to the original state, so that it can be used for the next test case.

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Accepted solutions (1)
450 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor
Accepted solution

I know you are talking "Unit Test" of your code, so not sure how the test context in which the code runs is configured. But, if the code runs in AutoCAD, as it looks, it has nothing to do with the Document (MdiActiveDocument). Since you use "using (var db=...){...} to create a side database, it is disposed when the code goes out of "using{}...", I am not sure what you worry about, nor do I see the need to lock MdiActiveDocument, unless this line "action(db);" somehow deal with the MdiActiveDocument (if so, the good practice should pass the MdiActiveDocument as parameter to the action).

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4

soonhui
Advisor
Advisor

@norman.yuan, you mean, the moment I exit the using scope involving the database, the content in the database will be cleared immediately?

 

That's good to hear.

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Message 4 of 4

norman.yuan
Mentor
Mentor

Depending on what "cleared" means/concerns you, at the end of "using..." the database is disposed, the memory used to hold the database is flagged by .NET runtime as garbage-collectable, but when he memory is actually freed depends the .NET runtime. As far as AutoCAD .NET app is concerned, the database is cleared/gone.

 

Again, back to your original question, loading an existing drawing as side database in AutoCAD process has nothing to do with MdiActiveDocument. Also, in your code, you SHOULD use the constructor Database(false, true) to open drawing as side database, that is, you should pass "true" (e.g. no document) to second argument. In our custom .NET app code, we should never pass "false" to the "noDocument" argument (I guess that might only be used internally by Autodesk), because there is no way/API for us to build a database with document, or associated to a document. We can only build side database without document association 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes