二次开发之视图切换

    /// <summary>
        /// 将当前视图切换到三维视图
        /// </summary>
        /// <param name="uidoc"></param>
        public void SwitchTo3DView(UIDocument uidoc, Transaction transation)
        {
            Document document = uidoc.Document;

            //查看当前视图是否为3D视图
            var type = document.ActiveView.GetType();
            if (!typeof(View3D).Equals(type))
            {
                View3D view3D = null;

                //查看当前文件有没有3D视图
                FilteredElementCollector fristCollector = new FilteredElementCollector(document).OfClass(typeof(View3D));
                foreach (View3D view in fristCollector)
                {
                    if ("{三维}".Equals(view.Name))
                    {
                        view3D = view;
                        break;
                    }
                }

                //如果没有找到3D视图,创建一个
                if (view3D == null)
                {
                    //过滤出三维视图
                    ElementId viewFamilyTypeId = new ElementId(0);
                    FilteredElementCollector secondCollector = new                        FilteredElementCollector(document).OfClass(typeof(ViewFamilyType));
                    foreach (ViewFamilyType viewFamilyType in secondCollector.ToList())
                    {
                        if ("三维视图".Equals(viewFamilyType.Name))
                        {
                            transation.Start("新建三维视图");
                            view3D = View3D.CreateIsometric(document, viewFamilyType.Id);
                            transation.Commit();
                            break;
                        }
                    }
                }

                //切换到三维视图
                uidoc.ActiveView = view3D;
            }

        }

猜你喜欢

转载自blog.csdn.net/waiting233/article/details/117922442