1

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

  • What image file format is in fact generated? Who says that the file format must be one or the other? If the two views generate different file formats, and the extensions match the file format generated, what is the problem? Do the filename extensions match the generated file formats? – Jeremy Tammik May 11 '20 at 10:16
  • 1
    Jeremy, it seemed confusing that parameter HLRandWFViewsFileType = ImageFileType.PNG of ImageExportOptions is ignored. So, there is no way to know the result file name with extension before exporting. – Valerii Nozdrenkov May 12 '20 at 10:26
  • That does not answer any of my questions. Can you please respond to them? – Jeremy Tammik May 13 '20 at 11:31
  • 1
    Sorry Jeremy, let me answer you questions. I have checked file format and found that it is in one case jpeg in another png, which corresponds to file extension. The only problem I have described above, thanks. – Valerii Nozdrenkov May 14 '20 at 14:55
  • So the only problem is that you would like to know beforehand what file extension you will receive. Correct? – Jeremy Tammik May 15 '20 at 15:13
  • Exactly! It can be solved by checking file existence and then finding it. – Valerii Nozdrenkov May 16 '20 at 13:20

1 Answers1

2

This is because ImageExportOptions have two different file type properties:

  • ShadowViewsFileType (The file type for exported shadow views).
  • HLRandWFViewsFileType (File type for exported HLR and wireframe views).

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.

Håvard
  • 36
  • 4