[SOLVED] Open "Save as Revit" with chosen path

bo60vdx07
Participant
Participant

[SOLVED] Open "Save as Revit" with chosen path

bo60vdx07
Participant
Participant

Hi !

I'm looking for a way to open Revit's "Save as" interface with an already chosen path.

This will allow users to know in which folder to save files and if necessary be able to change the save path, but for most cases it will always be in the same place.

 

Of course, I've found topics that allow you to save documents, but not via the Revit interface.

Do you think this is possible ? If so, can you provide me with a code extract ?

 

Thanks ๐Ÿ˜‰

0 Likes
Reply
Accepted solutions (1)
310 Views
2 Replies
Replies (2)

sragan
Collaborator
Collaborator
Accepted solution

This is the standard windows dialog box.  As a macro, this is the code you need in C#;

 

SaveFileDialog savefiledialog = new SaveFileDialog();
savefiledialog.InitialDirectory = "C:\\";

savefiledialog.Filter = "Revit files (*.rvt)|*.rvt|All files (*.*)|*.*";
 savefiledialog.ShowDialog();
            

 

You also need to add a reference and using statements for System.Windows.Forms.

 

FileDialog Class (System.Windows.Forms) | Microsoft Learn

 

SaveFileDialog Class (System.Windows.Forms) | Microsoft Learn

 

 

 

 

bo60vdx07
Participant
Participant

Thanks a lot @sragan !

 

It worked perfectly ! ๐Ÿ˜

 

[Transaction(TransactionMode.Manual)]
public class CommandUpload : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        try
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Revit family file (*.rfa)|*.rfa";
            saveFileDialog.InitialDirectory = "...";
            DialogResult result = saveFileDialog.ShowDialog();

                if (result == DialogResult.OK)
                {
                    string filePath = saveFileDialog.FileName;
                    Document doc = commandData.Application.ActiveUIDocument.Document;
                    doc.SaveAs(filePath);
                }
            }
        catch (Exception ex)
        {
            MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return Result.Succeeded;
    }
}