Quantcast
Channel: RevitNetAddinWizard & NavisworksNetAddinWizard
Viewing all articles
Browse latest Browse all 872

Revit .NET Creations API: Octagonal Tower (Composed of Walls Floors Roofs Doors Windows Openings and Levels)

$
0
0

Creations of various Revit elements such as Walls, Doors, Windows, Floors, Roofs, Openings, and Levels in many different aspects such as absolute vs. relative positions, regular vs. circular shapes, and ground vs. higher levels have been demonstrated in many early posts.

In this article, let’s create a beautiful Octagonal Tower using the simple and functional creation methods of Doors, Windows, Floors, Roofs, Openings, and Levels as already introduced there. Here is the core code along with some test code.

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace Rvt2013CsNet
{
    public class OctogonalTowerCreation
    {
        private double xBase;
        private double maxRadius;
        private double radiusDecrement;
        private double maxLevelHeight;
        private double groundElevation;
        private int levelCount;
        private int sideCount;

        private RvtDocument CachedDoc;
        private List<Level> Levels;

        public OctogonalTowerCreation(RvtDocument doc)
        {
            CachedDoc = doc;

            xBase = 0.0;
            maxRadius = 40.0;
            radiusDecrement = 4.0;
            maxLevelHeight = 20.0;
            groundElevation = 0.0;
            levelCount = 8;
            sideCount = 8;
        }

        public void Create()
        {
            Levels = CreateLevels();
            CreateWallsDoorsWindows();
            CreateFloorsWithOpenings();
            CreateRoof();
        }

        private double[] LevelElevations
        {
            get
            {
                List<double> list = new List<double>();
                double elevation = groundElevation;
                list.Add(elevation);

                for (int i = 0; i < levelCount; i++)
                {
                    elevation += maxLevelHeight - i;
                    list.Add(elevation);
                }

                return list.ToArray();
            }
        }

        private string[] LevelNames
        {
            get
            {
                return new string[]
                    {
                        "L0",
                        "L1",
                        "L2",
                        "L3",
                        "L4",
                        "L5",
                        "L6",
                        "L7",
                        "L8",
                    };
            }
        }

        private List<Level> CreateLevels()
        {
            CachedDoc.Delete(new FilteredElementCollector(CachedDoc)
                    .WherePasses(new ElementClassFilter(typeof(Level), false))
                    .Select(e => e.Id)
                    .ToList());
            List<Level> levels = LevelCreation.CreateLevels(
                    CachedDoc,
                    LevelElevations,
                    LevelNames
                );

            return levels;
        }

        private void CreateWallsDoorsWindows()
        {
            for (int i = 0; i < Levels.Count-1; i++)
            {
                Level level = Levels[i];

                List<ElementId> wallIds = WallCreation.CreateRegularPolygonalWall(
                                                CachedDoc,
                                                level,
                                                new XYZ(xBase, 0, 0),
                                                new XYZ(xBase, 0, 1),
                                                maxRadius - radiusDecrement * i, sideCount);

                int count = 0;
                foreach (ElementId id in wallIds)
                {
                    Wall wall = (Wall)CachedDoc.GetElement(id);
                    wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).Set(maxLevelHeight - i);

                    if (count++ % 2 == 0)
                    {
                        DoorWindowOpeningCreation.InsertDoorAtBottomMiddle(
                            DoorWindowOpeningCreation.GetFirstSymbol(
                                DoorWindowOpeningCreation.FindDoorFamilies(CachedDoc).First()),
                            wall);
                    }
                    else
                    {
                        DoorWindowOpeningCreation.InsertWindowAtCenterMiddle(
                            DoorWindowOpeningCreation.GetFirstSymbol(
                                DoorWindowOpeningCreation.FindWindowFamilies(CachedDoc).First()),
                                wall);
                    }
                }
            }
        }

        private void CreateFloorsWithOpenings()
        {
            for (int i = 0; i < Levels.Count - 1; i++)
            {
                Level level = Levels[i + 1];
                Floor floor = FloorCreation.CreateRegularPolygonalFloorWithRegularPolygonalOpening(
                                CachedDoc,
                                level,
                                new XYZ(xBase, 0, level.Elevation),
                                XYZ.BasisZ,
                                maxRadius - radiusDecrement * i, sideCount,
                                maxRadius - radiusDecrement * (i + 1), sideCount);
            }
        }

        private void CreateRoof()
        {
            RoofCreation.CreateRegularPolygonalRoof(
                        CachedDoc,
                        Levels.Last(),
                        RoofCreation.FindRoofTypes(CachedDoc).First(),
                        new XYZ(xBase, 0, Levels.Last().Elevation),
                        maxRadius - radiusDecrement * (levelCount-1),
                        sideCount,
                        Math.PI / 6,
                        0.0);
        }
    }
}

...
OctogonalTowerCreation tower = new OctogonalTowerCreation(CachedDoc);
tower.Create();
...

For brevity purpose, those help creation methods are not appended here since they have been demonstrated. They can be found from previous posts in the Revit .NET Creations API series. They are good and mature enough and work for the beautiful Octagonal Tower without any changes necessary.

Here is what the Octagonal Tower look like in Revit in a 3D view.
OctogonalTower3DView
 
Looking it in an elevation view:
 OctogonalTowerElevationView

Looking it from another perspective of a 3D view:
 OctogonalTower3DView2

Here is a rendered image of the octagonal tower:
 OctogonalTower3DRendered

It is so cool, isn’t it?

Revit Addin Wizard (RevitAddinWizard) provides various wizards, coders and widgets to help program Revit addins. It can be downloaded from the Download link at the bottom of the blog index page.


Viewing all articles
Browse latest Browse all 872

Trending Articles