Revit二次开发之“改变文件打开时的预览图”

打开文件时,在打开对话框右侧会显示预览图。

默认是显示平面视图的缩略图,下面的代码把Revit文件的预览图修改为三维视图下的缩略图
来自:http://revit.5d6d.com/thread-1149-1-1.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;
    }
}

end

posted @ 2011-08-23 09:58  大气象  阅读(2235)  评论(0编辑  收藏  举报
http://www.tianqiweiqi.com