Revit二次开发——改变文件打开时的预览图

转载以备后用: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;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值