Example #1
0
        private void ChangeViewTemplateScaleIfFound(int scale)
        {
            View viewTemplate =
                ViewTemplate.FindViewTemplateOrDefault(
                    _doc,
                    ViewType.FloorPlan,
                    Properties.Settings.Default.TEMPLATE_NAME_THREED_VIEW,
                    out bool templateIsFound);

            View foundationViewTemplate =
                ViewTemplate.FindViewTemplateOrDefault(
                    _doc,
                    ViewType.FloorPlan,
                    Properties.Settings.Default.TEMPLATE_NAME_FOUNDATION,
                    out bool foundationTemplateIsFound);

            if (templateIsFound)
            {
                viewTemplate.Scale = scale;
            }

            if (foundationTemplateIsFound)
            {
                foundationViewTemplate.Scale = scale;
            }
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            _uiapp = commandData.Application;
            _uidoc = _uiapp.ActiveUIDocument;
            _doc   = _uidoc.Document;

            // Get list of all levels for structural elements
            IList <Level> strLevels = GetAllLevels(_doc, true, true);

            if (strLevels.Count == 0)
            {
                return(Result.Cancelled);
            }

            // Get a ViewFamilyType for a 3D view
            ViewFamilyType viewFamilyType =
                (from v in new FilteredElementCollector(_doc)
                 .OfClass(typeof(ViewFamilyType))
                 .Cast <ViewFamilyType>()
                 where v.ViewFamily == ViewFamily.ThreeDimensional
                 select v).First();

            Autodesk.Revit.DB.View viewTemplate = ViewTemplate.FindViewTemplateOrDefault(_doc,
                                                                                         ViewType.ThreeD,
                                                                                         THREED_VIEW_TEMPLATE_NAME,
                                                                                         out bool templateIsFound);

            if (viewTemplate != null && !templateIsFound)     // Dont find the view template named as "DCE Structure 3D"
            {
                viewTemplate = CreateTemplateFor3DView(viewTemplate);
            }

            // Get all the 3D view created by the plugin
            IList <View3D> threeDViewList =
                (from v in new FilteredElementCollector(_doc)
                 .OfClass(typeof(View3D))
                 .Cast <View3D>()
                 where v.GetEntitySchemaGuids().Count != 0
                 select v)
                .ToList();

            Transaction t = new Transaction(_doc);

            // loop through all levels, except the first one
            for (int highFloorLevelInd = 1; highFloorLevelInd < strLevels.Count; highFloorLevelInd++)
            {
                Level highFloorLevel = strLevels[highFloorLevelInd];

                t.Start("Create or update 3D view");

                View3D view;
                if (threeDViewList.Any(v => GetAssociateLevelOf3DView(v).Equals(highFloorLevel.Id)))
                {
                    view = (from v in threeDViewList
                            where GetAssociateLevelOf3DView(v).Equals(highFloorLevel.Id)
                            select v)
                           .First();
                }
                else
                {
                    // Create the 3d view
                    view = View3D.CreateIsometric(_doc, viewFamilyType.Id);
                    // Asociate the high-floor level to the 3d view
                    AssociateLevelTo3DView(highFloorLevel, view);
                }

                if (viewTemplate != null)
                {
                    // Apply the "DCE Structure 3D" view template to the created view
                    view.ViewTemplateId = viewTemplate.Id;
                }

                // Set the name of the view
                if (view.Name != highFloorLevel.Name)
                {
                    view.Name = highFloorLevel.Name;
                }

                // Get the bounding box of space between the high-floor and low-floor
                BoundingBoxXYZ boundingBoxXYZ = GetBoundingBoxOfLevel(strLevels, highFloorLevelInd, view);

                // Apply this bounding box to the view's section box
                view.SetSectionBox(boundingBoxXYZ);

                // Save orientation and lock the view
                view.SaveOrientationAndLock();

                t.Commit();

                // Open the just-created view
                // There cannot be an open transaction when the active view is set
                _uidoc.ActiveView = view;
            }

            return(Result.Succeeded);
        }
        private List <ViewPlan> CreateNewViewPlan(Document doc, List <Level> newLevels)
        {
            List <ViewPlan> viewPlans = new List <ViewPlan>();

            // Prepare the ViewFamilyType for the ViewPlan creation
            ViewFamilyType viewFamilyType =
                (from elem in new FilteredElementCollector(doc)
                 .OfClass(typeof(ViewFamilyType))
                 .Cast <ViewFamilyType>()
                 where elem.ViewFamily == ViewFamily.StructuralPlan
                 select elem)
                .First();

            View standardTemplate =
                ViewTemplate.FindViewTemplateOrDefault(
                    _doc,
                    ViewType.CeilingPlan,
                    Default.TEMPLATE_NAME_STANDARD_FLOOR,
                    out bool standardTemplateIsFound);

            View foundationTemplate =
                ViewTemplate.FindViewTemplateOrDefault(
                    _doc,
                    ViewType.CeilingPlan,
                    Default.TEMPLATE_NAME_FOUNDATION,
                    out bool foundationTemplateIsFound);

            using (Transaction t = new Transaction(doc, "Create View Plans"))
            {
                try
                {
                    t.Start();
                    for (int i = 0; i < newLevels.Count; i++)
                    {
                        Level    level    = newLevels[i];
                        ViewPlan viewPlan = ViewPlan.Create(doc, viewFamilyType.Id, level.Id);
                        viewPlan.Name = $"0{i + 1} {level.Name}";
                        viewPlans.Add(viewPlan);
                        // TODO: Deal with the case when template is not found
                        if (!viewPlan.Name.ToLower().Contains(Default.KEYWORD_FOUNDATION) && standardTemplateIsFound)
                        {
                            viewPlan.ViewTemplateId = standardTemplate.Id;
                        }

                        if (viewPlan.Name.ToLower().Contains(Default.KEYWORD_FOUNDATION) && foundationTemplateIsFound)
                        {
                            viewPlan.ViewTemplateId = foundationTemplate.Id;
                        }

                        // Associate level to the new viewplan
                        AssociateLevelToNewViewPlan(level, viewPlan);
                    }
                    t.Commit();
                }
                catch (Exception e)
                {
                    TaskDialog.Show("Revit", $"Exception when creating view plans : {e.Message}");
                    t.RollBack();
                }
            }
            return(viewPlans);
        }