How to save and exit the current drawing?

How to save and exit the current drawing?

swaywood
Collaborator Collaborator
661 Views
5 Replies
Message 1 of 6

How to save and exit the current drawing?

swaywood
Collaborator
Collaborator

Hi, I manually opened a existing drawing, My question is: How to save and exit the current drawing? I have tried the following code, but failed //db.SaveAs(db.OriginalFileName, DwgVersion.Current); //db.GetDocument().Save(); //AcadApp.DocumentManager.MdiActiveDocument.Save(); Document acadDoc = db.GetDocument(); acadDoc.GetType().InvokeMember("Save",BindingFlags.InvokeMethod, null, acadDoc, new object[0]);

0 Likes
662 Views
5 Replies
Replies (5)
Message 2 of 6

583408432
Enthusiast
Enthusiast

Using the above code to save the drawing is no problem, but if you want to close the drawing then you need to add CommandFlags, the code is as follows:

[CommandMethod("XX", CommandFlags.Session)]

Message 3 of 6

swaywood
Collaborator
Collaborator

thanks for your reply.

i tried the following code , but failed again.

    [CommandMethod("Q6")]
    public void Q6()
    {
      Database db = HostApplicationServices.WorkingDatabase;
      db.PurgeAll(true);
      Document acadDoc = db.GetDocument();
      db.GetEditor().Command("qsave");
      acadDoc.CloseAndDiscard();
    }
    [CommandMethod("Q7", CommandFlags.Session)]
    public void Q7()
    {
      Database db = HostApplicationServices.WorkingDatabase;
      db.PurgeAll(true);
      Document acadDoc = db.GetDocument();
      db.GetEditor().Command("qsave");
      db.GetEditor().Command("QUIT");
    }
0 Likes
Message 4 of 6

norman.yuan
Mentor
Mentor

The first command (Q6) does not work, because the command has no CommandFlags.Session set, meaning the command runs in the current document context, so, you cannot close the document while a command is running.

 

The second command (Q7) also does not work, because you cannot call Editor.Command() in Application context (e.g. with CommandFlas.Session flag set).

 

Also, where does "Database.GetDocument()" come from? Is it your custom extension class/method? It does not make sense in many cases: a Document opened in AutoCAD contains Database, but a Database opened/created in AutoCAD may not associated to any document. 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 6

ActivistInvestor
Mentor
Mentor

I've used a GetDocument() extension method quite extensively. It is useful in cases where you have a database but don't have a document, and you need to know if the database is open in the editor or not.

 

I always try to design APIs so that they can be used both with or without AutoCAD (e.g, RealDWG) or with databases that are accessed via ReadDwgFile(), which is where a method like that becomes useful.

0 Likes
Message 6 of 6

swaywood
Collaborator
Collaborator

Hi, mr yuan

thanks for your reply.

could you revise my code?

best wishes

0 Likes