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;
                }
            }
        }

 

猜你喜欢

转载自blog.csdn.net/weixin_40626630/article/details/84074742