在Revit API中有一个方法Document.ExportImage(),可以将单个或多个视图导出为图片。
该方法需要传递一个ImageExportOptions类型的参数,在参数中你可以自定义需要导出的视图、图片地址、图片大小等。
- 单视图导出
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace ScreenShot
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
ImageExportOptions options = new ImageExportOptions();
options.ZoomType = ZoomFitType.FitToPage;
options.ExportRange = ExportRange.CurrentView;
options.FilePath = @"C:\Users\Administrator\Desktop\CurrentViewImg";
options.FitDirection = FitDirectionType.Horizontal;
options.HLRandWFViewsFileType = ImageFileType.JPEGMedium;
options.ShadowViewsFileType = ImageFileType.JPEGMedium;
options.PixelSize = 1920;
commandData.Application.ActiveUIDocument.Document.ExportImage(options);
return Result.Succeeded;
}
}
}
上面代码将当前视图以JPG图片的形式导出到桌面上;如果只想截取当前视窗中可见的部分(截图),可将options.ExportRange设置为VisibleRegionOfCurrentView。
- 多视图导出
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
namespace ScreenShot
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector views = new FilteredElementCollector(doc).OfClass(typeof(View));
views.OfCategory(BuiltInCategory.OST_Views);
IList<ElementId> ImageExportList = new List<ElementId>();
foreach (View view in views)
{
if (view.IsTemplate) continue;
ImageExportList.Add(view.Id);
}
var options = new ImageExportOptions
{
ZoomType = ZoomFitType.FitToPage,
PixelSize = 1920,
FilePath = @"C:\Users\Administrator\Desktop\Views\",
FitDirection = FitDirectionType.Horizontal,
HLRandWFViewsFileType = ImageFileType.JPEGMedium,
ShadowViewsFileType = ImageFileType.JPEGMedium,
ImageResolution = ImageResolution.DPI_300,
ExportRange = ExportRange.SetOfViews
};
options.SetViewsAndSheets(ImageExportList);
doc.ExportImage(options);
return Result.Succeeded;
}
}
}
上面代码将项目中所有视图以JPG图片形式导出到桌面Views文件夹中,如下图所示:
内容或有偏颇之处,还请指正,不胜感激!