转载以备后用:https://www.cnblogs.com/greatverve/archive/2011/08/23/revit-api-change-preview.html
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
//[Journaling(JournalingMode.NoCommandData)]
public class CmdHi : IExternalCommand
{
public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
{
UIDocument uiDoc = cmdData.Application.ActiveUIDocument;//base.m_uiDoc
Selection selection = uiDoc.Selection;
// Open Revit file
Document doc = cmdData.Application.Application.OpenDocumentFile(@"D:\Backup\桌面\局部显示.rvt");
// Create transaction and start
Transaction trans = new Transaction(doc, "T1");
trans.Start();
doc.Regenerate();//重新生成
// Create thumbnails for preview //设置预览
DocumentPreviewSettings previewSettings = doc.GetDocumentPreviewSettings();
//保存选项
SaveAsOptions saveOpts = new SaveAsOptions();
//
// Check for permanent永久 preview view
if (doc.GetDocumentPreviewSettings().PreviewViewId.Equals(ElementId.InvalidElementId))
{
// Access the intial view //文档打开时的预览图
StartingViewSettings startingViewSettings = StartingViewSettings.GetStartingViewSettings(doc);
if (!startingViewSettings.ViewId.Equals(ElementId.InvalidElementId))
{
// If valid, then set the viewId
//previewSettings.PreviewViewId
// = startingViewSettings.ViewId;
saveOpts.PreviewViewId = startingViewSettings.ViewId;
}
else
{
// Algorithmic approach - look for 3D views
FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(View));
IEnumerable<View> views
= from Autodesk.Revit.DB.View f in collector
where (f.ViewType == ViewType.ThreeD && !f.IsTemplate)
select f;
bool bNotFound = true;
foreach (View vw in views)
{
if (!vw.IsTemplate)
{
//If we were to set the preview view permanently,
// we would use the following two commented lines
//previewSettings.PreviewViewId = vw.Id;
//previewSettings.ForceViewUpdate( true );
// Set the temporary view
saveOpts.PreviewViewId = vw.Id;
bNotFound = false;
break;
}
}
// If no 3D view is obtained, look for other valid views
if (bNotFound)
{
IEnumerable<View> viewsNon3D
= from View fNon3D
in collector
where (fNon3D.ViewType == ViewType.FloorPlan
|| fNon3D.ViewType == ViewType.EngineeringPlan
|| fNon3D.ViewType == ViewType.Elevation
|| fNon3D.ViewType == ViewType.Section
&& !fNon3D.IsTemplate)
select fNon3D;
foreach (View vw in viewsNon3D)
{
if (!vw.IsTemplate)
{
// If we were to set the preview view permanently,
// we would use the following two commented lines
//previewSettings.PreviewViewId = vw.Id;
//previewSettings.ForceViewUpdate( true );
//设置为3D视图下的预览图
saveOpts.PreviewViewId = vw.Id;
break;
}
}
}
}
}
// Commit transaction
trans.Commit();
// Save Revit file to target destination
doc.SaveAs(@"D:\Backup\桌面\局部显示1.rvt", saveOpts);
// Close document
doc.Close();
return Result.Succeeded;
}
}