Get document name BEFORE and AFTER saving

Get document name BEFORE and AFTER saving

denisyukJ
Advocate Advocate
669 Views
8 Replies
Message 1 of 9

Get document name BEFORE and AFTER saving

denisyukJ
Advocate
Advocate

Hi,

 

I would like to make a programm which write down information about current document. There are no problem when document open and close with same name, but problems occur when name is changing during session.

Could someone give me a hint how to catch a name of current document  BEFORE and AFTER saving (SAVE, QSAVE, SAVEAS)?

 

BR,

Evgeniy

0 Likes
Accepted solutions (1)
670 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant

Hi,

You can handle the Database.BeginSave and Database.SaveComplete events.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 9

denisyukJ
Advocate
Advocate

Hi, @_gile 

Non of those events catch information before saving?

 

0 Likes
Message 4 of 9

_gile
Consultant
Consultant

Databse.BeginSave does.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 9

denisyukJ
Advocate
Advocate

Please take a look at code below.

I created new file (Drawing 1) > Click SAVE> Gave a new name (qqq) > ACAD shown me the same messages.

I expected Drawing 1 and qqq.

denisyukJ_0-1731663002697.png

 

public class Class1 : IExtensionApplication
{
    public void Initialize()
    {
        var docMan = Application.DocumentManager;
        docMan.DocumentCreated += DocumentManager_DocumentCreated;

    }

    public void Terminate() { }

    private void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
    {
        e.Document.Database.BeginSave += Database_BeginSave;
        e.Document.Database.SaveComplete += Database_SaveComplete;

    }

    private void Database_BeginSave(object sender, DatabaseIOEventArgs e)
    {
        MessageBox.Show(e.FileName);
    }

    private void Database_SaveComplete(object sender, DatabaseIOEventArgs e)
    {
        MessageBox.Show(e.FileName);
    }
}

 

0 Likes
Message 6 of 9

_gile
Consultant
Consultant

e.FileName is the new document name.

Try this way:

private void Database_BeginSave(object sender, DatabaseIOEventArgs e)
{
    MessageBox.Show(Application.DocumentManager.MdiActiveDocument.Name);
}

private void Database_SaveComplete(object sender, DatabaseIOEventArgs e)
{
    MessageBox.Show(Application.DocumentManager.MdiActiveDocument.Name);
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 9

_gile
Consultant
Consultant

Or this way:

private void Database_BeginSave(object sender, DatabaseIOEventArgs e)
{
    MessageBox.Show(((Database)sender).Filename);
}

private void Database_SaveComplete(object sender, DatabaseIOEventArgs e)
{
    MessageBox.Show(((Database)sender).Filename);
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 9

denisyukJ
Advocate
Advocate
Accepted solution

This gives opposite result. Maybe this? 

        private void Database_BeginSave(object sender, DatabaseIOEventArgs e)
        {
            MessageBox.Show(Application.DocumentManager.MdiActiveDocument.Name);
        }

        private void Database_SaveComplete(object sender, DatabaseIOEventArgs e)
        {
            MessageBox.Show(e.FileName);
        }
0 Likes
Message 9 of 9

ActivistInvestor
Mentor
Mentor

Yes, that will give you the old and new filenames.

 

You can store the Document's current Name in a variable from the BeginSave handler and compare it to the e.FileName event args in SaveComplete handler.