We programmatically created a non-leaning Pisa Tower and an Octagonal Tower before using the various Revit .NET Creation APIs and C#, and provided all these nice help classes and methods in some early post.
In this post, let’s reuse all those helpful creation classes and methods to create another kind of and still very nice tower, Round Tower. Here is how the resultant Round Tower that we are going to create programmatically looks like when rendered in a Revit 3D view.
Here is how it looks like in an elevation view.
Here we go for the source code for the Round Tower creation.
#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 RoundTowerCreation
{
private double mXBase;
private double mMaxRadius;
private double mRadiusDecrement;
private double mMaxLevelHeight;
private double mGroundElevation;
private int mLevelCount;
private XYZ mNormal;
private RvtDocument CachedDoc;
private List<Level> Levels;
public RoundTowerCreation(RvtDocument doc)
{
CachedDoc = doc;
mXBase = 0.0;
mMaxRadius = 40.0;
mRadiusDecrement = 4.0;
mMaxLevelHeight = 20.0;
mGroundElevation = 0.0;
mLevelCount = 8;
mNormal = XYZ.BasisZ;
}
public void Create()
{
Levels = CreateLevels();
CreateWallsDoorsWindows();
CreateFloorsWithOpenings();
CreateRoof();
}
private double[] LevelElevations
{
get
{
List<double> list = new List<double>();
double elevation = mGroundElevation;
list.Add(elevation);
for (int i = 0; i < mLevelCount; i++)
{
elevation += mMaxLevelHeight - 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.CreateRoundWalls(
CachedDoc,
level,
new XYZ(mXBase, 0, 0),
new XYZ(mXBase, 0, 1),
mMaxRadius - mRadiusDecrement * i);
int count = 0;
foreach (ElementId id in wallIds)
{
Wall wall = (Wall)CachedDoc.GetElement(id);
wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).Set(mMaxLevelHeight - i);
if (count++ % 2 == 0)
{
DoorWindowOpeningCreation.InsertDoorAtBottomMiddle(
DoorWindowOpeningCreation.GetFirstSymbol(
DoorWindowOpeningCreation.FindDoorFamilies(CachedDoc).First()),
wall);
}
else
{
DoorWindowOpeningCreation.InsertWindowAtCenterMiddle2(
DoorWindowOpeningCreation.GetFirstSymbol(
DoorWindowOpeningCreation.FindWindowFamilies(CachedDoc).First()),
wall);
}
}
}
}
private void CreateFloorsWithOpenings()
{
for (int i = 0; i < Levels.Count - 1; i++)
{
Level level = Levels[i];
Floor floor = FloorCreation.CreateRoundFloorWithRoundOpening(
CachedDoc,
level,
new XYZ(mXBase, 0, level.Elevation),
new XYZ(mXBase, 0, 1),
mMaxRadius - mRadiusDecrement * (i - 1),
mMaxRadius - mRadiusDecrement * i);
}
}
private void CreateRoof()
{
RoofCreation.CreateRoundRoof(
CachedDoc,
Levels.Last(),
RoofCreation.FindRoofTypes(CachedDoc).First(),
new XYZ(mXBase, 0, Levels.Last().Elevation),
mNormal,
mMaxRadius - mRadiusDecrement * (mLevelCount-1),
Math.PI / 6,
0.0);
}
}
}
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.