public static View3D CreatePhase3DView(Document doc, ItemInfo itemInfo, ViewFamilyType view3dFamilyType, bool overwrite)
        {
            View3D view3D = null;

            try
            {
                string viewName = "PH - 3D - " + itemInfo.ItemName;
                using (Transaction trans = new Transaction(doc))
                {
                    FilteredElementCollector collector = new FilteredElementCollector(doc);
                    List <View3D>            view3ds   = collector.OfClass(typeof(View3D)).ToElements().Cast <View3D>().ToList();
                    var views = from view in view3ds where view.Name == viewName select view;
                    if (views.Count() > 0)
                    {
                        if (overwrite)
                        {
                            view3D = views.First();
                        }
                        else
                        {
                            return(view3D);
                        }
                    }

                    if (null == view3D)
                    {
                        trans.Start("Create 3D View");
                        try
                        {
                            view3D      = View3D.CreateIsometric(doc, view3dFamilyType.Id);
                            view3D.Name = viewName;
                            if (view3D.CanModifyViewDiscipline())
                            {
                                view3D.Discipline = ViewDiscipline.Coordination;
                            }

                            Parameter param = view3D.get_Parameter(BuiltInParameter.VIEW_PHASE);
                            if (null != param)
                            {
                                if (!param.IsReadOnly)
                                {
                                    param.Set(itemInfo.ItemId);
                                }
                            }
                            trans.Commit();
                        }
                        catch (Exception ex)
                        {
                            trans.RollBack();
                            MessageBox.Show("Failed to create 3d view by phases.\n" + ex.Message, "Create 3D View", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create 3d views by phases.\n" + ex.Message, "Create 3D Views by Phases", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(view3D);
        }
        public static View3D Create3DView(Document doc)
        {
            View3D view = null;

            try
            {
                using (Transaction t = new Transaction(doc, "Create view"))
                {
                    t.Start();
                    //Find a 3D view type
                    var collector       = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType));
                    var viewFamilyTypes = from elem in collector
                                          let vftype = elem as ViewFamilyType
                                                       where vftype.ViewFamily == ViewFamily.ThreeDimensional
                                                       select vftype;
                    // Create a new View3D
                    if (viewFamilyTypes.Count() == 0)
                    {
                        return(null);
                    }
                    view = View3D.CreateIsometric(doc, viewFamilyTypes.First().Id);

                    if (view.CanModifyViewDiscipline())
                    {
                        view.Discipline = ViewDiscipline.Coordination;
                    }
                    if (view.CanModifyDetailLevel())
                    {
                        view.DetailLevel = ViewDetailLevel.Fine;
                    }
                    if (view.CanModifyDisplayStyle())
                    {
                        view.DisplayStyle = DisplayStyle.Realistic;
                    }

                    // show all categories
                    foreach (Category cat in doc.Settings.Categories)
                    {
                        if (view.GetCategoryHidden(cat.Id))
                        {
                            view.SetCategoryHidden(cat.Id, false);
                        }
                    }
                    // hide annotations
                    view.AreAnnotationCategoriesHidden = true;

                    // disable the crop box or section box
                    view.CropBoxActive      = false;
                    view.CropBoxVisible     = false;
                    view.IsSectionBoxActive = false;

                    t.Commit();
                }
                return(view);
            }
            catch (Exception)
            {
                return(null);
            }
        }
        public static View3D CreateDesignOption3DView(Document doc, ItemInfo itemInfo, ViewFamilyType view3dFamilyType, bool overwrite)
        {
            View3D view3D = null;

            try
            {
                string viewName = "OP - 3D - " + itemInfo.ItemName;
                using (Transaction trans = new Transaction(doc))
                {
                    trans.Start("Create 3D View");
                    try
                    {
                        FilteredElementCollector collector = new FilteredElementCollector(doc);
                        List <View3D>            view3ds   = collector.OfClass(typeof(View3D)).ToElements().Cast <View3D>().ToList();
                        var views = from view in view3ds where view.Name == viewName select view;
                        if (views.Count() > 0)
                        {
                            if (overwrite)
                            {
                                view3D = views.First();
                            }
                            else
                            {
                                return(view3D);
                            }
                        }

                        if (view3D == null)
                        {
                            view3D      = View3D.CreateIsometric(doc, view3dFamilyType.Id);
                            view3D.Name = viewName;
                            if (view3D.CanModifyViewDiscipline())
                            {
                                view3D.Discipline = ViewDiscipline.Coordination;
                            }

                            trans.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        trans.RollBack();
                        MessageBox.Show("Failed to create a 3d view for the design option, " + itemInfo.ItemName + "\n" + ex.Message, "Create 3D View", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create 3d views by design options.\n" + ex.Message, "Create 3D Views by Design Options", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(view3D);
        }
        public static View3D CreateWorkset3DView(Document doc, ItemInfo itemInfo, ViewFamilyType view3dFamilyType, bool overwrite)
        {
            View3D view3D = null;

            try
            {
                string viewName = "WS - 3D - " + itemInfo.ItemName;
                using (TransactionGroup tg = new TransactionGroup(doc))
                {
                    tg.Start("Create 3D View");
                    try
                    {
                        FilteredElementCollector collector = new FilteredElementCollector(doc);
                        List <View3D>            view3ds   = collector.OfClass(typeof(View3D)).ToElements().Cast <View3D>().ToList();
                        var views = from view in view3ds where view.Name == viewName select view;
                        if (views.Count() > 0)
                        {
                            if (overwrite)
                            {
                                view3D = views.First();
                            }
                            else
                            {
                                return(view3D);
                            }
                        }
                        if (null == view3D)
                        {
                            using (Transaction trans = new Transaction(doc))
                            {
                                trans.Start("Create Isometric");
                                try
                                {
                                    view3D      = View3D.CreateIsometric(doc, view3dFamilyType.Id);
                                    view3D.Name = viewName;
                                    if (view3D.CanModifyViewDiscipline())
                                    {
                                        view3D.Discipline = ViewDiscipline.Coordination;
                                    }
                                    trans.Commit();
                                }
                                catch (Exception ex)
                                {
                                    trans.RollBack();
                                    MessageBox.Show("Failed to create Isometric.\n" + ex.Message, "Create Isometric", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            }
                        }

                        using (Transaction trans = new Transaction(doc))
                        {
                            trans.Start("Set Visibility");
                            try
                            {
                                FilteredWorksetCollector worksetCollector = new FilteredWorksetCollector(doc);
                                IList <Workset>          worksetList      = worksetCollector.ToWorksets();
                                var worksets = from workset in worksetList where workset.Kind == WorksetKind.UserWorkset select workset;
                                foreach (Workset ws in worksets)
                                {
                                    if (ws.Kind == WorksetKind.UserWorkset)
                                    {
                                        if (ws.Id.IntegerValue == itemInfo.ItemId)
                                        {
                                            view3D.SetWorksetVisibility(ws.Id, WorksetVisibility.Visible);
                                        }
                                        else
                                        {
                                            view3D.SetWorksetVisibility(ws.Id, WorksetVisibility.Hidden);
                                        }
                                    }
                                }
                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                MessageBox.Show("Failed to set visibility.\n" + ex.Message, "Set Visibility", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }

                        using (Transaction trans = new Transaction(doc))
                        {
                            trans.Start("Set SectionBox");
                            try
                            {
                                collector = new FilteredElementCollector(doc, view3D.Id);
                                List <Element> elements = collector.ToElements().ToList();
                                if (elements.Count > 0)
                                {
                                    BoundingBoxXYZ boundingBox = GetBoundingBox(elements);
                                    if (null != boundingBox)
                                    {
                                        view3D.SetSectionBox(boundingBox);
                                    }
                                }
                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                MessageBox.Show("Failed to set sectionbox.\n" + ex.Message, "Set Sectionbox", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Failed to create 3d views by worksets.\n" + ex.Message, "Create 3D Views by Worksets", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        tg.RollBack();
                    }
                    tg.Assimilate();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create workset views.\n" + ex.Message, "Create Workset 3D View", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(view3D);
        }
        public static View3D CreateLink3DView(Document doc, ItemInfo itemInfo, ViewFamilyType view3dFamilyType, bool overwrite)
        {
            View3D view3D = null;

            try
            {
                string viewName = "LINKRVT - 3D - " + itemInfo.ItemName;
                using (TransactionGroup tg = new TransactionGroup(doc))
                {
                    tg.Start("Create 3D View");
                    try
                    {
                        FilteredElementCollector collector = new FilteredElementCollector(doc);
                        List <View3D>            view3ds   = collector.OfClass(typeof(View3D)).ToElements().Cast <View3D>().ToList();
                        var views = from view in view3ds where view.Name == viewName select view;
                        if (views.Count() > 0)
                        {
                            if (overwrite)
                            {
                                view3D = views.First();
                            }
                            else
                            {
                                return(view3D);
                            }
                        }
                        if (null == view3D)
                        {
                            using (Transaction trans = new Transaction(doc))
                            {
                                trans.Start("Create Isometric");
                                try
                                {
                                    view3D      = View3D.CreateIsometric(doc, view3dFamilyType.Id);
                                    view3D.Name = viewName;
                                    if (view3D.CanModifyViewDiscipline())
                                    {
                                        view3D.Discipline = ViewDiscipline.Coordination;
                                    }
                                    trans.Commit();
                                }
                                catch (Exception ex)
                                {
                                    trans.RollBack();
                                    MessageBox.Show("Failed to create Isometric.\n" + ex.Message, "Create Isometric", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            }
                        }


                        using (Transaction trans = new Transaction(doc))
                        {
                            trans.Start("Set SectionBox");
                            try
                            {
                                collector = new FilteredElementCollector(doc);
                                List <Element> linkInstances = collector.OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType().ToElements().ToList();
                                var            selectedLinks = from link in linkInstances where link.Name.Contains(itemInfo.ItemName) && null != link.Location select link;
                                if (selectedLinks.Count() > 0)
                                {
                                    BoundingBoxXYZ boundingBox = GetBoundingBox(selectedLinks.ToList());
                                    if (null != boundingBox)
                                    {
                                        view3D.SetSectionBox(boundingBox);
                                    }
                                }
                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                MessageBox.Show("Failed to set sectionbox.\n" + ex.Message, "Set Sectionbox", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Failed to create 3d views by worksets.\n" + ex.Message, "Create 3D Views by Worksets", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        tg.RollBack();
                    }
                    tg.Assimilate();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create workset views.\n" + ex.Message, "Create Workset 3D View", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(view3D);
        }