revit-apirevit

Document ExportImage command creates image file with different extensions


There is a strange behavior when I try create an image from view in Revit via API. For some reason the target file sometimes is "png", sometimes "jpg" (for different View3D). As a workaround I check file existence and replace the extension, but I think it's not a good solution. The idea was taken from

https://thebuildingcoder.typepad.com/blog/2013/08/setting-a-default-3d-view-orientation.html

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    UIDocument uidoc = commandData.Application.ActiveUIDocument;
    Document doc = uidoc.Document;

    var tempFileName = Path.ChangeExtension(Path.GetRandomFileName(), "png");

    string tempImageFile;

    try
    {
        tempImageFile = Path.Combine(Path.GetTempPath(), tempFileName);
    }
    catch (IOException)
    {
        return Result.Failed;
    }

    var opt = new ImageExportOptions
    {
        ZoomType = ZoomFitType.Zoom,
        FilePath = tempImageFile,
        FitDirection = FitDirectionType.Horizontal,
        HLRandWFViewsFileType = ImageFileType.PNG,
        ImageResolution = ImageResolution.DPI_300,
    };

    doc.ExportImage(opt);
Debug.WriteLine(File.Exists(tempImageFile) ? "File exists." : "File does not exist.");

    return Result.Succeeded;

}

}

Steps to reproduce:

  1. Create external command (implement IExternalCommand interface)
  2. Use provided above implementation of Execute method
  3. Open Revit 2020 sample model (rac_basic_sample_project.rvt)
  4. Select 3D Views->Selection Perspective (or Kitchen)
  5. Execute external command
  6. Check the result of doc.ExportImage(opt) command

AR: Result file has "jpg" extension instead of "png"

ER: File should be "png".

PS. If you select 3D Views->{3D} file has extension "png"

Look at screens


Solution

  • This is because ImageExportOptions have two different file type properties:

    If some of your 3D views are Shaded (jpeg), and some are Hidden Line (png), you would have to set both file type properties to PNG (both properties defaults to JPEGMedium) to ensure export to PNG.