In Revit .NET API 2013, though the NewWall method has been moved to the Wall class itself, the Opening generation method has not. It is still in the Document.Create instance. Anyway, we figured where it is and were able to create some Openings.
In this article, let’s create an Opening using a consistent coordinate system and insert it into a wall. Here is the code.
Line line = CachedDoc.Application.Create.NewLineBound(new XYZ(50, 0, 0), new XYZ(70, 0, 0));
Wall wall = Wall.Create(CachedDoc, line, FindAndSortLevels(CachedDoc).Last().Id, false); //Level 2
Opening opening1 = DoorWindowOpeningCreation.InsertOpening(wall, 6, 4);
Opening opening2 = DoorWindowOpeningCreation.InsertOpening(wall, 6, 4, 5, 5);
Opening opening3 = DoorWindowOpeningCreation.InsertOpening(wall, 6, 4, 15, 15);
...
public static Opening InsertOpening(Wall wall, double height, double width, double centerToLeft = -1.0, double centerToBottom = -1.0)
{
LocationCurve locCurve = wall.Location as LocationCurve;
XYZ start = locCurve.Curve.get_EndPoint(0);
XYZ end = locCurve.Curve.get_EndPoint(1);
XYZ location = (start + end) / 2;
if (centerToLeft >= 0)
location = start + (end - start).Normalize() * centerToLeft;
double wallHeight = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();
double topOffset = wallHeight / 2 + height / 2;
double baseOffset = wallHeight / 2 - height / 2;
if (centerToBottom >= 0)
{
topOffset = centerToBottom + height / 2;
baseOffset = centerToBottom - height / 2;
}
XYZ leftBottom = new XYZ(location.X - width / 2, location.Y, baseOffset);
XYZ topRight = new XYZ(location.X + width / 2, location.Y, topOffset);
Opening opening = wall.Document.Create.NewOpening(wall, leftBottom, topRight);
return opening;
}
...
By the way, the last level obtained by the FindAndSortLevels() method in our test cases is the ‘Level 2’ which has an elevation of 10’ from the ground. The help method accepts the height and width of the rectangle opening and two relative distances besides the host wall argument. And the two distance arguments are optional. If they are not provided, the opening will be at the center of the wall. Things are reasonable, consistent, and convenient now.
Here is what the opening and the wall look like in Revit.
Things are all fine. Enjoy 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.