Create FileSaveDialog Box

Create FileSaveDialog Box

Iev60047
Advocate Advocate
612 Views
6 Replies
Message 1 of 7

Create FileSaveDialog Box

Iev60047
Advocate
Advocate

Seemingly simple question, but I had no luck searching around for the answer. I am trying to set the file path for an image I am exporting using C# in VisualStudio to create an .addin to use in Revit.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Architecture;

//We will not be committing any changes to the model, so read only for the transaction mode.
[Transaction(TransactionMode.ReadOnly)]
public class Main : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        //Get application and document objects
        UIApplication uiapp = commandData.Application;
        Document doc = uiapp.ActiveUIDocument.Document;
     

        //
        ImageExportOptions imgExportOpts = new ImageExportOptions();
        {
            imgExportOpts.ZoomType = ZoomFitType.FitToPage;
            imgExportOpts.PixelSize = 500;
            imgExportOpts.FilePath = "C:/Users/username/Documents/Test.TIFF";
            imgExportOpts.FitDirection = FitDirectionType.Vertical;
            imgExportOpts.HLRandWFViewsFileType = ImageFileType.TIFF;
            imgExportOpts.ShadowViewsFileType = ImageFileType.TIFF;
            imgExportOpts.ImageResolution = ImageResolution.DPI_72;
            imgExportOpts.ShouldCreateWebSite = false;
        };

        doc.ExportImage(imgExportOpts);

        //Returns success if programs runs well
        return Result.Succeeded;
    }
     
}

 

I am thinking of adding: 

 

        //Setting export path        
        string filePath;
        public Return FileSaveDialog()
        {
            filePath = ...   
            return filePath
        }

 somewhere after I define doc as a type of class Document. As you can tell I am not really sure what I am doing here. I tried looking at other examples and the revitapi help document, to no avail. I would be really grateful for any assistance. I am not sure if I should be doing something more like:

 

FileSaveDialog fileSaveDialog = new FileSaveDialog(string);
{
Stuff inside here;
}
0 Likes
613 Views
6 Replies
Replies (6)
Message 2 of 7

jeremy_tammik
Alumni
Alumni
0 Likes
Message 3 of 7

Iev60047
Advocate
Advocate

@jeremy_tammik I am trying my best here. Went through the internet search you provided, but it seems more aimed at actually saving a file. I am just trying to specify a file path. I do not need file output with the dialog.

 

        public string textBox1 = null;
    private void SaveButton_Click(object sender, EventArgs e)
        {
            FileSaveDialog saveFileDialog1 = new FileSaveDialog();
            saveFileDialog1.InitialDirectory = @ "C:\";      
            saveFileDialog1.Title = "Select path for image export";
            saveFileDialog1.CheckFileExists = true;
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.DefaultExt = "TIFF";
            saveFileDialog1.Filter = "TIFF files (*.TIFF)|*.TIFF|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = saveFileDialog1.FilePath;
            }
        }

 

I tried modifying a snippet I found, but to no avail.

0 Likes
Message 4 of 7

nice3point
Advocate
Advocate

Just specify this path as a variable and you can use it anywhere

 

var imagePath = @"C:\Users\username\Desktop\Test.tif";
var options = new ImageExportOptions
{
    ExportRange = ExportRange.CurrentView,
    ZoomType = ZoomFitType.FitToPage,
    PixelSize = 500,
    FilePath = imagePath,
    FitDirection = FitDirectionType.Vertical,
    HLRandWFViewsFileType = ImageFileType.TIFF,
    ShadowViewsFileType = ImageFileType.TIFF,
    ImageResolution = ImageResolution.DPI_72,
    ShouldCreateWebSite = false,
};

doc.ExportImage(options);

 

 
 

 

 

0 Likes
Message 5 of 7

jeremy_tammik
Alumni
Alumni

Sorry, I do not understand what your problem is. I thought that you wanted to specify the full file path when initialising the form and before displaying it to the user. Apparently, you have some other need. What is it? Please describe it as a black box. What input do you have? What is the desired output? What is the desired side effect?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 6 of 7

Iev60047
Advocate
Advocate

Again thank you for  your help! Input is selection of folder through what I suppose is the FileSaveDialog. This selection of the folder will complete the call and return a value (the name of the folder, i.e. the desired file path) to a variable I have previously defined. I will then use this variable in the export method. 

0 Likes
Message 7 of 7

mhannonQ65N2
Collaborator
Collaborator

I'd recommend using the .NET SaveFileDialog instead of Revit's FileSaveDialog. The minimum you need to do is call the dialog's ShowFile method then put the value of its FileName property into your filePath variable.

 

This example, although it uses the OpenFileDialog, shows how to get the selected file name.

0 Likes