We have programmatically created various Revit elements using the Revit .NET Creation APIs, specifically those New or Create methods. We also created a non-leaning Pisa Tower using all those Revit .NET Creation APIs that have been demonstrated in detail separately.
Here is how the resultant Pisa Tower looks beautifully in Revit after being rendered.
The
FloorCreation class along with its useful methods have been fine tuned a
bit when used to create the non-leaning Pisa Tower. Here is the latest
code.
public class FloorCreation
{
public static IOrderedEnumerable<Level> FindAndSortLevels(RvtDocument doc)
{
return new FilteredElementCollector(doc)
.WherePasses(new ElementClassFilter(typeof(Level), false))
.Cast<Level>()
.OrderBy(e => e.Elevation);
}
public static Floor CreateRegularPolygonalFloor(Document doc, Level level, XYZ location, XYZ normal, double radius, int sides)
{
CurveArray profile = new CurveArray();
for (int i = 0; i < sides; i++)
{
double curAngle = i * Math.PI / sides * 2;
double nextAngle = (i < sides - 1 ? i + 1 : 0) * Math.PI / sides * 2;
XYZ curVertex = new XYZ(location.X + radius * Math.Cos(curAngle), location.Y + radius * Math.Sin(curAngle), location.Z);
XYZ nextVertex = new XYZ(location.X + radius * Math.Cos(nextAngle), location.Y + radius * Math.Sin(nextAngle), location.Z);
Line line = doc.Application.Create.NewLineBound(curVertex, nextVertex);
profile.Append(line);
}
return doc.Create.NewFloor(profile, false);
}
public static Floor CreateRegularPolygonalFloorWithRegularPolygonalOpening(Document doc, Level level, XYZ location, XYZ normal, double floorradius, int floorsides, double openingradius, int openingsides)
{
CurveArray profile = new CurveArray();
for (int i = 0; i < floorsides; i++)
{
double curAngle = i * Math.PI / floorsides * 2;
double nextAngle = (i < floorsides - 1 ? i + 1 : 0) * Math.PI / floorsides * 2;
XYZ curVertex = new XYZ(location.X + floorradius * Math.Cos(curAngle), location.Y + floorradius * Math.Sin(curAngle), location.Z);
XYZ nextVertex = new XYZ(location.X + floorradius * Math.Cos(nextAngle), location.Y + floorradius * Math.Sin(nextAngle), location.Z);
Line line = doc.Application.Create.NewLineBound(curVertex, nextVertex);
profile.Append(line);
}
Floor floor = doc.Create.NewFloor(profile, false);
doc.Regenerate();
CurveArray profile1 = new CurveArray();
for (int i = 0; i < openingsides; i++)
{
double curAngle = i * Math.PI / openingsides * 2;
double nextAngle = (i < openingsides - 1 ? i + 1 : 0) * Math.PI / openingsides * 2;
XYZ curVertex = new XYZ(location.X + openingradius * Math.Cos(curAngle), location.Y + openingradius * Math.Sin(curAngle), location.Z);
XYZ nextVertex = new XYZ(location.X + openingradius * Math.Cos(nextAngle), location.Y + openingradius * Math.Sin(nextAngle), location.Z);
Line line = doc.Application.Create.NewLineBound(curVertex, nextVertex);
profile1.Append(line);
}
doc.Create.NewOpening(floor, profile1, false);
return floor;
}
public static Floor CreateRegularPolygonalFloorWithRegularPolygonalOpening2(Document doc, Level level, XYZ location, XYZ normal, double floorradius, int floorsides, double openingradius, int openingsides)
{
CurveArray profile = new CurveArray();
for (int i = 0; i < floorsides; i++)
{
double curAngle = i * Math.PI / floorsides * 2;
double nextAngle = (i < floorsides - 1 ? i + 1 : 0) * Math.PI / floorsides * 2;
XYZ curVertex = new XYZ(location.X + floorradius * Math.Cos(curAngle), location.Y + floorradius * Math.Sin(curAngle), location.Z);
XYZ nextVertex = new XYZ(location.X + floorradius * Math.Cos(nextAngle), location.Y + floorradius * Math.Sin(nextAngle), location.Z);
Line line = doc.Application.Create.NewLineBound(curVertex, nextVertex);
profile.Append(line);
}
Floor floor = doc.Create.NewFloor(profile, null, level, false);
doc.Regenerate();
CurveArray profile1 = new CurveArray();
for (int i = 0; i < openingsides; i++)
{
double curAngle = i * Math.PI / openingsides * 2;
double nextAngle = (i < openingsides - 1 ? i + 1 : 0) * Math.PI / openingsides * 2;
XYZ curVertex = new XYZ(location.X + openingradius * Math.Cos(curAngle), location.Y + openingradius * Math.Sin(curAngle), location.Z);
XYZ nextVertex = new XYZ(location.X + openingradius * Math.Cos(nextAngle), location.Y + openingradius * Math.Sin(nextAngle), location.Z);
Line line = doc.Application.Create.NewLineBound(curVertex, nextVertex);
profile1.Append(line);
}
doc.Create.NewOpening(floor, profile1, false);
try
{
Line axis = doc.Application.Create.NewLine(location, XYZ.BasisZ.CrossProduct(normal), false);
double angle = normal.AngleOnPlaneTo(XYZ.BasisZ, normal);
ElementTransformUtils.RotateElement(doc, floor.Id, axis, angle);
}
catch { }
return floor;
}
public static Floor CreateRoundFloorWithRoundOpening(RvtDocument doc, Level level, XYZ center, XYZ normal, double floorRadius, double openingRadius)
{
Arc arc1 = doc.Application.Create.NewArc(new Plane(normal, center), floorRadius, 0, Math.PI);
Arc arc2 = doc.Application.Create.NewArc(new Plane(normal, center), floorRadius, Math.PI, Math.PI * 2);
Arc arc3 = doc.Application.Create.NewArc(new Plane(normal, center), openingRadius, 0, -Math.PI);
Arc arc4 = doc.Application.Create.NewArc(new Plane(normal, center), openingRadius, -Math.PI, -Math.PI * 2);
CurveArray profile = new CurveArray();
profile.Append(arc1);
profile.Append(arc2);
CurveArray profile1 = new CurveArray();
profile1.Append(arc3);
profile1.Append(arc4);
Floor floor = doc.Create.NewFloor(profile, null, level, false, normal);
doc.Regenerate();
doc.Create.NewOpening(floor, profile1, false);
return floor;
}
}
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.