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

Saving and Closing a Drawing

10 REPLIES 10
Reply
Message 1 of 11
drauckman
2039 Views, 10 Replies

Saving and Closing a Drawing

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?

10 REPLIES 10
Message 2 of 11
Jeffrey_H
in reply to: drauckman

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);
            }
        }

 

You can also find your answers @ TheSwamp
Message 3 of 11
drauckman
in reply to: drauckman

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?

Message 4 of 11
Jeffrey_H
in reply to: drauckman

It is tough to say or too many things to say without seeing any code

 

You can also find your answers @ TheSwamp
Message 5 of 11
Paulio
in reply to: drauckman

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.

 

Message 6 of 11
Jeffrey_H
in reply to: Paulio

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

You can also find your answers @ TheSwamp
Message 7 of 11
elMarko
in reply to: Jeffrey_H

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

Message 8 of 11
Jeffrey_H
in reply to: elMarko

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

You can also find your answers @ TheSwamp
Message 9 of 11
elMarko
in reply to: elMarko

duh, nevermind, it was capitalization difference

😞

Message 10 of 11
Jeffrey_H
in reply to: drauckman

There might be a problem between locking your documents then trying to save them since you are using the same file name to open and save your drawings.

Also opening the same drawing will cause it to open read only

You can also find your answers @ TheSwamp
Message 11 of 11
Jeffrey_H
in reply to: Jeffrey_H

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

 

You can also find your answers @ TheSwamp

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost