How to export an image from a specific view using Revit API C# ?

How to export an image from a specific view using Revit API C# ?

Anonymous
Not applicable
14,591 Views
18 Replies
Message 1 of 19

How to export an image from a specific view using Revit API C# ?

Anonymous
Not applicable

Hi

I have to export a specific view  to an Image using the API ( C# )

I have opened the models using "uiApp.Application.OpenDocumentFile..."

But I don't want to export the default view.

I have found the name of the view I want to export, I have set the Path, Pixels etc. but

how to get the right view exported ?

 

Thanks 

Anders Kaas

0 Likes
Accepted solutions (1)
14,592 Views
18 Replies
Replies (18)
Message 2 of 19

jeremytammik
Autodesk
Autodesk

Dear Anders,

 

Happy New Year to you!

 

Here is a description of a sample command that creates a new view to export an image from:

 

http://thebuildingcoder.typepad.com/blog/2013/12/setting-the-view-display-background.html

 

It is included in The Building Coder sample collection:

 

https://github.com/jeremytammik/the_building_coder_samples

 

That should provide a starting point, at least.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 19

Anonymous
Not applicable

Hi Jeremy

 

Thank you for Your answer, but I still can't get it to work !

I have this Imageexportoption but want only a view called e.g. "Level 1" in "OpenDoc" exportet as an image

I have opened a dafault template and are using the API to open others models but it is only the current view that are exportet

 

var BilledeExportOptions = new ImageExportOptions

{

ZoomType = ZoomFitType.FitToPage,

PixelSize = 2024,

FilePath = DistFolder + @"\Images\" + CSVTxt[ActRow][3].ToString(),

FitDirection = FitDirectionType.Horizontal,

HLRandWFViewsFileType = ImageFileType.JPEGLossless,

ImageResolution = ImageResolution.DPI_600,

};

 

OpenDoc.ExportImage(BilledeExportOptions);

 

Anders

 

0 Likes
Message 4 of 19

jeremytammik
Autodesk
Autodesk

Dear Anders,

 

It sounds to me as if you are answering your own question.

 

You say "the current view is exported".

 

Ok, in that case, how about making the view that you wish to export current before launching the process?

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 19

Anonymous
Not applicable

Hi

🙂  But it is the current view in the file I have opened before I run the code whitch opens an other model with the view I want to export, And I know I want to export several views in models in the future, if they have the right name, right values in specifics parameters etc.

Can't I specifie the view to be exportet ?

 

Anders

 

0 Likes
Message 6 of 19

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Anders,

 

Have you RTFM?

 

Have you tested the ImageExportOptions.ViewName property?

 

Oh no, I see that "This field is used only to save an image to a project as a new view."

 

Reading further, though, I immediately discover the ImageExportOptions.SetViewsAndSheets method: "Sets a list of views and sheets to be exported. Used only when ExportRange is SetOfViews."

 

You should try reading as well:

 

https://en.wikipedia.org/wiki/Reading_(process)

 

🙂

 

The Revit API help documentation is in RevitAPI.chm that is part of the Revit SDK.

 

Have you installed that and do you keep it handy as a desktop shortcut or something?

 

If not, please do!

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 7 of 19

Anonymous
Not applicable

Thanks a lot it worked

 

I have added the full code maybe it can be usefull to others 🙂

 

IList<ElementId> ImageExportList = new List<ElementId>();

ImageExportList.Add(View.Id);

var BilledeExportOptions = new ImageExportOptions
{
ZoomType = ZoomFitType.FitToPage,
PixelSize = 1024,
FilePath = DistFolder + @"\Images\" + ImageName.ToString(),
FitDirection = FitDirectionType.Horizontal,
HLRandWFViewsFileType = ImageFileType.JPEGLossless,
ImageResolution = ImageResolution.DPI_600,
ExportRange = ExportRange.SetOfViews,
};

 

BilledeExportOptions.SetViewsAndSheets(ImageExportList);

OpenDoc.ExportImage(BilledeExportOptions);

 

Anders

Message 8 of 19

Anonymous
Not applicable

I am trying to export image for an ExternalApplication and it does not work. Here is the error I get "a managed exception was thrown by Revit or by one of its external applications". What does that mean? Is it not allowed to call ExportImage from an external Application?

 

Here is my code 

 

                var uidoc = uiapp.ActiveUIDocument;
                var doc = uidoc.Document;
                var ieo = new ImageExportOptions
                {
                    ZoomType = ZoomFitType.Zoom,
                    PixelSize = 512,
                    FilePath = @"c:\temp\images\",
                    FitDirection = FitDirectionType.Horizontal,
                    HLRandWFViewsFileType = ImageFileType.PNG,
                    ImageResolution = ImageResolution.DPI_72,
                    ExportRange = ExportRange.SetOfViews,
                };

                ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Views);
                FilteredElementCollector views = new FilteredElementCollector(doc);
                views.WherePasses(filter).WhereElementIsNotElementType().ToElements();
                IList<ElementId> ImageExportList = null;

                foreach (View view in views)
                {
                    if (view.ViewType == ViewType.FloorPlan)
                    {
                        try
                        {
                            ImageExportList = new List<ElementId>();
                            ImageExportList.Add(view.Id);
                            ieo.SetViewsAndSheets(ImageExportList);
                            doc.ExportImage(ieo);
                        }
                        catch (Exception ex)
                        {
                            System.IO.File.WriteAllText(@"C:\Temp\Errors.txt", view.Name + " " + ex.Message);
                        }
                    }
                }

 

 

 

0 Likes
Message 9 of 19

jeremytammik
Autodesk
Autodesk

No, you are not per se allowed to make any Revit API calls whatsoever from an external application, or anywhere else either, for that matter, except from within a valid Revit API context.

 

It sounds to me as if you are not within a valid Revit API context when your code above is executed.

 

Please read the detailed description of what a valid Revit API context is and how to obtain it here:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 10 of 19

Revitalizer
Advisor
Advisor

Hi,

 

may it be that at least one view is not valid for export, e.g. view.IsTemplate ?

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 11 of 19

Revitalizer
Advisor
Advisor

Hi,

 

there is also a view.CanBePrinted property that may be checked before exporting or printing.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 12 of 19

Anonymous
Not applicable
Sorry I meant I am calling it from a class that implements IExternalApplication instead of a class that implements IExternalCommand. This is a revit addin so it should still have a valid revit api context

Sent from my iPhone
0 Likes
Message 13 of 19

jeremytammik
Autodesk
Autodesk

No, an external application does not automatically provide a valid Reivt API context.

 

Nothing in the universe provides a valid Reivt API context at all times.

 

A valid Reivt API context only exists temporarily within callback functions called from Revit, i.e. officially registered Revit event handlers, such as an external command Execute method or an external application ApplicationInitialized event handler.

 

I hop this clarifies.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 14 of 19

Anonymous
Not applicable

Hello!  Sorry if this is resurrecting an old thread, but I'm trying to generate a basic thumbnail of a view via the API using your code, and all I'm getting is a white rectangle.  It's of the correct size, so obviously some of my options are going through, but for some reason no data.  Is this a known thing?

 

Thanks.

 

--

-= Chris

0 Likes
Message 15 of 19

Anonymous
Not applicable
That happened to me if the Graphic display option had the Stype set to hidden lines. Change that to wireframe and then it will work like a charm

Sahana Pindikuri
Associate, Tech Solutions | Enstoa
Direct: +1 (212) 913 0870 x 133
Mobile: +1 (269) 599 5607
Enstoa launches Strategy & Consulting Business Unit
0 Likes
Message 16 of 19

Anonymous
Not applicable

Thanks for the quick reply, Sahana!

 

I tried as you suggested, and sadly, that did not resolve my issue.  I also tried setting the background color to green and it's still writing out just a blank white rectangle jpeg.  Also oddly, it's always writing out a jpg file, even if I tell it to write out a png file by setting HLRandWFViewsFileType to ImageFileType.PNG.

0 Likes
Message 17 of 19

FaustoMendezcom
Enthusiast
Enthusiast

Replace HLRandWFViewsFileType = ImageFileType.JPEGLossless,

 

With 

HLRandWFViewsFileType = ImageFileType.PNG;

 

for PNG

 

 

here is a code snippet that you can use on macros it will save the image as PNG on your Desktop.

 

 

	UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    Autodesk.Revit.DB.View view = doc.ActiveView; 
    
                Transaction tx3 = new Transaction(doc, "apply Filter");
                tx3.Start();

                ImageExportOptions img = new ImageExportOptions();



                img.ZoomType= ZoomFitType.FitToPage;
                img.PixelSize = 32;
                

                
                img.ImageResolution = ImageResolution.DPI_600;

                img.FitDirection = FitDirectionType.Horizontal;

               
                img.ExportRange = ExportRange.CurrentView;
                 img.HLRandWFViewsFileType = ImageFileType.PNG;
                 string DEsktoppath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                img.FilePath = DEsktoppath +@"\"+ view.Name;
                    img.ShadowViewsFileType = ImageFileType.PNG;
               
                doc.ExportImage(img);
             
                tx3.Commit();

 

Hope it helps 🙂
-------------------------------------------------------------------------------------
www.faustomendez.com
Message 18 of 19

jeremytammik
Autodesk
Autodesk

I confirm that this works perfectly well.

 

I implemented and tested it in The Building Coder samples release 2017.0.127.7:

 

https://github.com/jeremytammik/the_building_coder_samples

 

https://github.com/jeremytammik/the_building_coder_samples/releases/tag/2017.0.127.7

 

https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/C...

 

/// <summary>
/// New code as described in Revit API discussion 
/// forum thread on how to export an image from a 
/// specific view using Revit API C#,
/// http://forums.autodesk.com/t5/revit-api/how-to-export-an-image-from-a-specific-view-using-revit-api-c/m-p/6424418
/// </summary>
static Result ExportToImage3( Document doc )
{
  Result r = Result.Failed;
 
  usingTransaction tx = new Transaction( doc ) )
  {
    tx.Start( "Export Image" );
 
    string desktop_path = Environment.GetFolderPath( 
      Environment.SpecialFolder.Desktop );
 
    View view = doc.ActiveView;
 
    string filepath = Path.Combine( desktop_path, 
      view.Name );
 
    ImageExportOptions img = new ImageExportOptions();
 
    img.ZoomType = ZoomFitType.FitToPage;
    img.PixelSize = 32;
    img.ImageResolution = ImageResolution.DPI_600;
    img.FitDirection = FitDirectionType.Horizontal;
    img.ExportRange = ExportRange.CurrentView;
    img.HLRandWFViewsFileType = ImageFileType.PNG;
    img.FilePath = filepath;
    img.ShadowViewsFileType = ImageFileType.PNG;
 
    doc.ExportImage( img );
 
    tx.RollBack();
 
    filepath = Path.ChangeExtension( 
      filepath, "png" );
 
    Process.Start( filepath );
 
    r = Result.Succeeded;
  }
  return r;
}

 

Is the transaction really needed at all?

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 19 of 19

Anonymous
Not applicable

I got it working.  Not sure what the problem was, but I solved it by moving the method out of the larger (threaded) routine it was a part of up to the parent method.  Thanks for all the advice and code snippets!  I think I ended up using and modifying bits of all of them.

0 Likes