This post is about how Revit views can export as images by using IronPython in Revit Dynamo Application.
Firstly, import the necessary modules:
# import modules import clr,sys,System clr.AddReference("RevitAPI") clr.AddReference("RevitServices") # import classes from System.Collections.Generic import List from Autodesk.Revit.DB import * from RevitServices.Persistence import DocumentManager # add ipy library path sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib") # os module import import os
Import clr to load .Net Assemblies as IronPython modules which are RevitAPI.dll and RevitServices.dll. And System for .Net List and sys to add IronPython library path(modules folder).
Second Part, get the current document (doc) from DocumentManager. And to save images, we need to provide a directory path (can be anywhere), the current Revit file location or user Desktop location. Image file name prefix is the current Revit file name, then we make the output directory (filePath).
# current document doc = DocumentManager.Instance.CurrentDBDocument # current working dir output_path = os.path.dirname(doc.PathName) # if cwd is none (file have not save yet) # get desktop path instead if not os.path.exists(output_path): output_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop') # doc name doc_name = os.path.basename(doc.PathName) # filenames with project name prefix filePath = os.path.join(output_path,doc_name) # input views views = IN[0] # create unwrap list if not isinstance(views,list): views = UnwrapElement([views]) else: views = UnwrapElement(views) # element's id list ids = [v.Id for v in views] # ids list to .net List viewIds = List[ElementId](ids)
Input views are the views that we want to export and these views(elements) keep Unwrapping to access API methods and Properties. Then, collect Id value and add to ElementId List.
The final part is to Instantiate ImageExportOptions class and set up its Properties values as follow:
- ExportRange – The export range defining which view(s) will be exported. (ExportRange Options : CurrentView, VisibleRegionOfCurrentView, SetOfViews)
- FilePath – The file name and path for the exported file.
- HLRandWFViewsFileType – File type for exported HLR and wireframe views.(ImageFile Type: BMP, JPEG, PNG, TARGA, TIFF)
- ImageResolution – The image resolution in dots per inch. (ImageResolution : DPI_72, DPI_150, DPI_300, DPI_600)
- PixelSize – The pixel size of an image in one direction. Used only if ZoomType is FitToPage.
- ShadowViewsFileType – The file type for exported shadow views. (ImageFile Type: BMP, JPEG, PNG, TARGA, TIFF)
- ZoomType – The zoom type, which defines how the image size is determined. (ZoomType: FitToPage, Zoom)
Finally, export the images. To return the image’s paths from the PythonScript output, use the GetFileName method from ieo object and concatenate with our filePath string.
# image export options constructor ieo = ImageExportOptions() # export range (exporting amount of view(s)) ieo.ExportRange = ExportRange.SetOfViews ieo.SetViewsAndSheets(viewIds) # filepath ieo.FilePath = filePath # File type for exported HLR and wireframe views ieo.HLRandWFViewsFileType = ImageFileType.PNG # image resolution ieo.ImageResolution = ImageResolution.DPI_300 # fit direction ieo.FitDirection = FitDirectionType.Horizontal # pixelsize ieo.PixelSize = 1200 # file type for exported shadow views ieo.ShadowViewsFileType = ImageFileType.PNG # zoom type ieo.ZoomType = ZoomFitType.FitToPage # export image doc.ExportImage(ieo) # get exported images paths images_fullpath = [filePath[:-4] + ieo.GetFileName(doc,id) + "."+ieo.HLRandWFViewsFileType.ToString() for id in ids] # output images fullpath OUT = images_fullpath
Sample image exporter usage and the complete code is below.

# dynamo version - 1.3.4 # import modules import clr,sys,System clr.AddReference("RevitAPI") clr.AddReference("RevitServices") # import classes from System.Collections.Generic import List from Autodesk.Revit.DB import * from RevitServices.Persistence import DocumentManager # add ipy library path sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib") # os module import import os # current document doc = DocumentManager.Instance.CurrentDBDocument # current working dir output_path = os.path.dirname(doc.PathName) # if cwd is none (file have not save yet) # get desktop path instead if not os.path.exists(output_path): output_path=os.path.join(os.path.join(os.environ['USERPROFILE']),'Desktop') # doc name doc_name = os.path.basename(doc.PathName) # filenames with project name prefix filePath = os.path.join(output_path,doc_name) # input views views = IN[0] # create unwrap list if not isinstance(views,list): views = UnwrapElement([views]) else: views = UnwrapElement(views) # element's id list ids = [v.Id for v in views] # ids list to .net List viewIds = List[ElementId](ids) # image export options constructor ieo = ImageExportOptions() # export range (exporting amount of view(s)) ieo.ExportRange = ExportRange.SetOfViews ieo.SetViewsAndSheets(viewIds) # filepath ieo.FilePath = filePath # File type for exported HLR and wireframe views ieo.HLRandWFViewsFileType = ImageFileType.PNG # image resolution ieo.ImageResolution = ImageResolution.DPI_300 # fit direction ieo.FitDirection = FitDirectionType.Horizontal # pixelsize ieo.PixelSize = 1200 # file type for exported shadow views ieo.ShadowViewsFileType = ImageFileType.PNG # zoom type ieo.ZoomType = ZoomFitType.FitToPage # export image doc.ExportImage(ieo) # get exported images paths images_fullpath = [filePath[:-4] + ieo.GetFileName(doc,id) + "."+ieo.HLRandWFViewsFileType.ToString() for id in ids] # output images fullpath OUT = images_fullpath