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 and insert it into a wall. Here is the code.
Line line = CachedDoc.Application.Create.NewLineBound(new XYZ(50, 0, 0), new XYZ(65, 0, 0));
Wall wall = Wall.Create(CachedDoc, line, FindAndSortLevels(CachedDoc).Last().Id, false); //Level 2 or higher
Opening opening = CachedDoc.Create.NewOpening(wall, new XYZ(55, 0, 0), new XYZ(60, 0, 10));
...
public static IOrderedEnumerable<Level> FindAndSortLevels(RvtDocument doc)
{
return new FilteredElementCollector(doc)
.WherePasses(new ElementClassFilter(typeof(Level), false))
.Cast<Level>()
.OrderBy(e => e.Elevation);
}
...
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. As we figured previously, the X coordinates have to be absolute and the Z ones relative. We exactly follow the rule here regardless of it sounding good or bad. Anyway, the opening stays at the right spot this time. Here is what the opening and the wall look like in Revit.
To recap a bit, the Document.Create.NewOpening method behaves totally different from the Document.NewFamilyInstance method that we demonstrated many times. The Document.NewFamilyInstance uses absolute coordinates to locate doors or windows inside walls. The Document.Create.NewOpening method however, thinks the X coordinates in the two corners as absolute but the Z coordinates as relative which are the Base Offset and the Top Offset from the bottom of the wall. If people get confused when using the Document.NewFamilyInstance method to create doors and windows, the Document.Create.NewOpening method is more confusing. The Document.Create.NewOpening method treats the same guys (X, Y and Z coordinates) differently in the same group (XYZ corner), the X coordinates being in absolute positions, the Z relative, and the Y not clear.
By the way, let’s make an educational guess here, if the opening location does not fall into the wall (e.g. by specifying the two X coordinates as 5’ and 10’ respectively), an exception as follows would be thrown out.
“Can't cut instance of 60" x 120" out of Wall.”
In the coming posts, we will be addressing this issue by inserting opening into wall using a consistent coordinate system.
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.