When using the following code to close a drawing:
Document doc = Application.DocumentManager.MdiActiveDocument;
doc.CloseAndSave(Path);
Where path is the current location of the drawing. This same path was used to open the drawing and the file does exist previous to this code running.
What happens is I always get a "Error saving the document" exception.
Has anyone run into this trouble before?
I would not use Path as a variable because of the System.IO.Path class.
Here is a very simple example to try out there are alot more options and things to consider but see if this helps out to get you started.
string fileName = @"C:\Test\Test.dwg"; [CommandMethod("OpenDrawingTest", CommandFlags.Session)] public void OpenDrawingTest() { DocumentCollection docMgr = Application.DocumentManager; Document newDoc = docMgr.Open(fileName, false); docMgr.MdiActiveDocument = newDoc; } [CommandMethod("SaveDrawingTest", CommandFlags.Session)] public void SaveDrawingTest() { Document doc = Application.DocumentManager.MdiActiveDocument; if (doc.Name != fileName) { System.Windows.Forms.MessageBox.Show(doc.Name + " and" + fileName + " Do not Match"); } else { doc.CloseAndSave(fileName); } }
I modified it to avoid using Path. I have a project that parses an Excel file and creates drawings based on that input. I know that there are examples like the one you have given that do work but for some reason it will not work in my application.
Currently I can open a drawing, place blocks in it and update the attributes. When I go to close it it throws a generic error saying the drawing cannot be saved, but there is no apparant reason. I check if there is a command active, there is not. I check if the drawing is read only, it is not. After the exception is thrown and I am returned to the drawing in AutoCAD I can click the save button and it saves.
After seeing this I thought fine at least I can open and create drawings, I will just have to manually save or try and make a separate command to loop through and save.
If I remove all the logic to save the drawing, the first drawing will open normally and I can work on it, but all subsequent drawings are opened in Read Only mode even though I set the Read Only mode flag to false.
I have checked all my code that modifies drawings and they all use document locks (since I am in session mode I believe this is necessary). I put all my document locks and transactions in using statements so they should be taken care of automatically.
Other than this what else can cause a session context command to not want to save and only have one write mode document open at a time?
I had exactly the same problem and I couldn't see why it was failing either.
I resorted to using the saveas method of the document's database which seems to work fine.
doc.Database.SaveAs(srcFile, DwgVersion.Current)
then close it
doc.CloseAndDiscard()
It would be nice to know why the CloseAndSave method doesn't work though.
Check this out and there is link in one of the post
http://forums.autodesk.com/t5/NET/The-Perils-of-Database-Filename/td-p/2741743
curious, did you try that and it worked?
it didn't work for me.
i got the message box saying C:\0\0DWGS\TESTDWGS\test.dwg andC:\0\0DWGS\TESTDWGS\Test.dwg Do not Match
This is from the docs
If bBakAndRename is false, then no .bak file is created, a full save is always done, and if the Database being saved is the main database for a document in AutoCAD, then the document's filename will NOT change to the saved name.
test.dwg & Test.dwg do not match is why you got the message box
In the code posted in message 2 if I change the second command to
[CommandMethod("SaveDrawingTest", CommandFlags.Session)] public void SaveDrawingTest() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (DocumentLock docLock = doc.LockDocument()) { if (doc.Name != fileName) { System.Windows.Forms.MessageBox.Show(doc.Name + " and" + fileName + " Do not Match"); } else { doc.CloseAndSave(fileName); } } }
I get a error