Exemple #1
0
        private View GetViewByName(string name)
        {
            FilteredElementCollector docFilter = new FilteredElementCollector(uidoc.Document);

            if (docFilter != null)
            {
                FilteredElementIterator viewsIterator = docFilter.OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();

                while (viewsIterator.MoveNext())
                {
                    Autodesk.Revit.DB.View curView = viewsIterator.Current as Autodesk.Revit.DB.View;
                    string curViewTypeName         = curView.GetType().Name;

                    if ((curViewTypeName == "ViewDrafting") || (curViewTypeName == "ViewPlan"))
                    {
                        Autodesk.Revit.DB.ElementId   curElementId   = curView.GetTypeId();
                        Autodesk.Revit.DB.ElementType curElementType = uidoc.Document.GetElement(curElementId) as ElementType;

                        if (curElementType != null)
                        {
                            if (curElementType.GetType().Name == "ViewFamilyType")
                            {
                                Autodesk.Revit.DB.ViewFamilyType curViewFamilyType = (ViewFamilyType)curElementType;

                                if (curViewFamilyType != null)
                                {
                                    string curViewName = curViewFamilyType.Name + ": " + curView.Name;

                                    if (curViewName == name)
                                    {
                                        return(curView);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemple #2
0
        private List <string> GetDraftAndPlanViewsNames(Document vdoc)
        {
            List <string> names = new List <string>();

            FilteredElementCollector viewFilter = new FilteredElementCollector(vdoc);

            if (viewFilter != null)
            {
                FilteredElementIterator viewsIterator = viewFilter.OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();

                while (viewsIterator.MoveNext())
                {
                    Autodesk.Revit.DB.View curView = viewsIterator.Current as Autodesk.Revit.DB.View;
                    string curViewTypeName         = curView.GetType().Name;

                    if ((curViewTypeName == "ViewDrafting") || (curViewTypeName == "ViewPlan") || (curViewTypeName == "ViewSchedule"))
                    {
                        Autodesk.Revit.DB.ElementId   curElementId   = curView.GetTypeId();
                        Autodesk.Revit.DB.ElementType curElementType = vdoc.GetElement(curElementId) as ElementType;

                        if (curElementType != null)
                        {
                            if (curElementType.GetType().Name == "ViewFamilyType")
                            {
                                Autodesk.Revit.DB.ViewFamilyType curViewFamilyType = (ViewFamilyType)curElementType;

                                if (curViewFamilyType != null)
                                {
                                    string curName = curViewFamilyType.Name + ": " + curView.Name;
                                    names.Add(curName);
                                }
                            }
                        }
                    }
                }
            }

            names.Sort();

            return(names);
        }
 public RevitUI.Result Execute(RevitUI.ExternalCommandData commandData, ref string message, RevitDB.ElementSet elements)
 {
     RevitUI.UIDocument uidoc = commandData.Application.ActiveUIDocument;
     RevitDB.Document   doc   = uidoc.Document;
     if (doc.IsFamilyDocument)//感觉族文档没必要
     {
         RevitUI.TaskDialog.Show("Revit", "该操作仅适用项目文档,不适用族文档!");
         return(RevitUI.Result.Succeeded);
     }
     try
     {
         RevitDB.View view = doc.ActiveView;                                    //当前视图
         ICollection <RevitDB.ElementId> ids = uidoc.Selection.GetElementIds(); //选择对象
         if (0 == ids.Count)                                                    //如果没有选择
         {
             if (view is RevitDB.ViewSection)                                   //当前视图为立面视图且没有选择对象时退出
             {
                 return(RevitUI.Result.Succeeded);
             }
             else if (view is RevitDB.View3D)//当前视图为三维视图且没有选择对象时切换至原楼层平面视图
             {
                 RevitDB.FilteredElementCollector collectL    = new RevitDB.FilteredElementCollector(doc);
                 ICollection <RevitDB.Element>    collectionL = collectL.OfClass(typeof(RevitDB.View)).ToElements();
                 foreach (RevitDB.Element element in collectionL)
                 {
                     RevitDB.View v = element as RevitDB.View;
                     if (view.ViewName == "三维视图_" + v.ViewType.ToString() + "_" + v.ViewName)
                     {
                         if (!v.IsTemplate)
                         {
                             uidoc.ActiveView = v;
                             break;
                         }
                     }
                 }
                 return(RevitUI.Result.Succeeded);
             }//未选择对象时列出当前视图所有可见对象
             RevitDB.FilteredElementCollector collector = new RevitDB.FilteredElementCollector(doc, view.Id).WhereElementIsNotElementType();
             ids = collector.ToElementIds();
         }
         RevitDB.Transaction ts = new RevitDB.Transaction(doc, "Create_View3D");
         ts.Start();
         bool fg = true;
         RevitDB.FilteredElementCollector collect    = new RevitDB.FilteredElementCollector(doc);
         ICollection <RevitDB.Element>    collection = collect.OfClass(typeof(RevitDB.View3D)).ToElements();
         RevitDB.View3D view3d = null;
         foreach (RevitDB.Element element in collection)
         {
             RevitDB.View3D v = element as RevitDB.View3D;
             if (v.ViewName == "三维视图_" + view.ViewType.ToString() + "_" + view.ViewName)
             {
                 if (!v.IsTemplate)
                 {
                     view3d = v;
                     fg     = false;//已生成过当前视图的三维视图
                     break;
                 }
             }
         }
         if (fg)
         {
             RevitDB.ViewFamilyType viewFamilyType = null;
             collect = new RevitDB.FilteredElementCollector(doc);
             var viewFamilyTypes = collect.OfClass(typeof(RevitDB.ViewFamilyType)).ToElements();
             foreach (RevitDB.Element e in viewFamilyTypes)
             {
                 RevitDB.ViewFamilyType v = e as RevitDB.ViewFamilyType;
                 if (v.ViewFamily == RevitDB.ViewFamily.ThreeDimensional)
                 {
                     viewFamilyType = v;
                     break;
                 }
             }
             view3d          = RevitDB.View3D.CreateIsometric(doc, viewFamilyType.Id);
             view3d.ViewName = "三维视图_" + view.ViewType.ToString() + "_" + view.ViewName;
         }
         ts.Commit();
         uidoc.ActiveView = view3d;//设置生成的三维视图为当前视图
         RevitDB.Transaction tss = new RevitDB.Transaction(doc, "View3D_HideElements");
         tss.Start();
         if (fg)
         {
             view    = doc.ActiveView;
             collect = new RevitDB.FilteredElementCollector(doc, view.Id).WhereElementIsNotElementType();
             ICollection <RevitDB.ElementId> idss = collect.ToElementIds();
             ICollection <RevitDB.ElementId> ds   = idss;
             ds = idss.Except(ids).ToList();
             ids.Clear();
             foreach (RevitDB.ElementId id in ds)
             {
                 RevitDB.Element element = doc.GetElement(id);
                 if (element.CanBeHidden(view) == true)
                 {
                     ids.Add(id);
                 }
             }
             view.HideElementsTemporary(ids);//临时隐藏其他图元
         }
         tss.Commit();
     }
     catch (Exception)
     {
         return(RevitUI.Result.Cancelled);
     }
     return(RevitUI.Result.Succeeded);
 }
        private List <string> getOpenPlanViewsTitles()
        {
            List <string> planViewsTitles = new List <string>();

            if (this.uidoc != null)
            {
                Document doc = this.uidoc.Document;
                if (doc != null)
                {
                    IList <UIView> uiviews   = this.uidoc.GetOpenUIViews();
                    IList <UIView> cluiviews = new List <UIView>();

                    for (int i = 0; i < uiviews.Count; i++)
                    {
                        UIView    curUIView = uiviews[i];
                        ElementId curViewId = curUIView.ViewId;
                        if (curViewId != null)
                        {
                            Element curElement = doc.GetElement(curViewId);
                            if (curElement != null)
                            {
                                string curElementTypeName = curElement.GetType().Name;
                                if (curElementTypeName == "ViewPlan")
                                {
                                    ViewPlan curViewPlan = curElement as ViewPlan;
                                    if (curViewPlan != null)
                                    {
                                        Autodesk.Revit.DB.ElementId   curElementId   = curViewPlan.GetTypeId();
                                        Autodesk.Revit.DB.ElementType curElementType = doc.GetElement(curElementId) as ElementType;

                                        if (curElementType != null)
                                        {
                                            if (curElementType.GetType().Name == "ViewFamilyType")
                                            {
                                                Autodesk.Revit.DB.ViewFamilyType curViewFamilyType = (ViewFamilyType)curElementType;

                                                if (curViewFamilyType != null)
                                                {
                                                    string curTitle = curViewFamilyType.Name + ": " + curViewPlan.Name;
                                                    planViewsTitles.Add(curTitle);
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    cluiviews.Add(curUIView);
                                }
                            }
                        }
                    }

                    if (cluiviews.Count > 1)
                    {
                        while (cluiviews.Count > 0)
                        {
                            try
                            {
                                UIView curUIView = cluiviews.ElementAt(0);
                                cluiviews.RemoveAt(0);
                                if (curUIView != null)
                                {
                                    try
                                    {
                                        if (cluiviews.Count > 1)
                                        {
                                            curUIView.Close();
                                            curUIView.Dispose();
                                        }
                                    }
                                    catch (Exception exA)
                                    {
                                    }
                                }
                            }
                            catch (Exception exB)
                            {
                            }
                        }
                    }
                }
            }

            return(planViewsTitles);
        }