Message 1 of 2
Issues generating image file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I've been tasked with the developing a python add-in that will save an open Revit document to a location and generate an image file/PDF of that file. I've been able to generate the save document but, I'm having issues generating a PDF or PNG. The below snippet is my attempt to generate a local save file and the PNG file. I'm not getting any errors in Revit when I run the Plugin. I get my save file and a string output declaring the file locations but I don't get the PNG. Through my research, apparently you can only export specific views to PDFs in Revit, so this may be the cause of my PDF issue, but I can't figure out why the image isn't generating.
import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
from Autodesk.Revit.DB import ImageExportOptions, FitDirectionType, FilteredElementCollector, View
from Autodesk.Revit.DB import Document
import System
from System.IO import Path
# Get the active document
doc = __revit__.ActiveUIDocument.Document
# Define the image export options
image_export_options = ImageExportOptions()
image_export_options.FitDirection = FitDirectionType.Horizontal
# Specify the target folder paths for images and documents
image_target_folder_path = "C:\\DocStorage\\" # desired image path
document_target_folder_path = "C:\\DocStorage\\" # desired path
# Get all views in the document
view_collector = FilteredElementCollector(doc).OfClass(View)
# Loop through views and export images
for view in view_collector:
if view.CanBePrinted:
try:
# Get the print manager for the current view
print_manager = view.PrintManager
# Specify the image output file path
image_output_path = image_target_folder_path + view.Name + ".png"
# Export the current view to PNG
print_manager.PrintToFileName(image_output_path, image_export_options)
except:
pass
print("Images exported to:", image_target_folder_path)
# Get the document title without extension
document_title = doc.Title
filename_without_extension = System.IO.Path.GetFileNameWithoutExtension(document_title)
# Get the current timestamp
timestamp = System.DateTime.Now.ToString("yyyyMMdd_hhmmss")
# Construct the new file path for saving the document
new_file_path = Path.Combine(document_target_folder_path, filename_without_extension + "_" + timestamp + ".rvt")
# Save the document with the new file path
doc.SaveAs(new_file_path)
print("Document saved to:", new_file_path)