1. 程式人生 > >Revit二次開發—更改啟用檢視(activeview)失敗原因

Revit二次開發—更改啟用檢視(activeview)失敗原因

錯誤提示:

Cannot change the active view of a modifiable document (with a transaction curently open)

原因在於:檢視不能在事務進行時更改,因為事務的執行牽扯到檢視,檢視正在被利用,所以應該在事務提交之後再更改啟用檢視!

public static void ActiveViewByName(UIApplication app, string viewname)
        {
            Document doc = app.ActiveUIDocument.Document;
            UIDocument uidoc = app.ActiveUIDocument;

            FilteredElementCollector collector
              = new FilteredElementCollector(doc)
                .OfClass(typeof(View));

            foreach (View v in collector)
            {
                Debug.Assert(null != v,
                  "never expected a null view to be returned"
                  + " from filtered element collector");

                // Skip view template here because view 
                // templates are invisible in project 
                // browser

                if (v.Name == viewname && !v.IsTemplate)
                {
                    uidoc.ActiveView = v;
                }
            }
        }