In Revit .NET API 2013, though the NewWall method has been moved to the Wall class itself, the FamilyInstance generation method has not. It is still in the Document.Create instance. Anyway, we figured where it is and were able to create some FamilyInstance objects such as Doors and Windows.
In this article, let’s create a window and insert it into a wall using some relative location instead of absolute one that has been demonstrated previously. Here are the core help methods.
private static IEnumerable<Family> FindWindowFamilies(RvtDocument doc)
{
return new FilteredElementCollector(doc)
.OfClass(typeof(Family))
.Cast<Family>()
.Where(e => e.FamilyCategory != null
&& e.FamilyCategory.Id.IntegerValue == (int)BuiltInCategory.OST_Windows);
}
public static FamilyInstance InsertWindow(Document doc, Wall wall, double centerToLeft, double bottomToBottom)
{
FamilySymbol symbol = GetFirstSymbol(FindWindowFamilies(doc).FirstOrDefault());
LocationCurve locCurve = wall.Location as LocationCurve;
XYZ start = locCurve.Curve.get_EndPoint(0);
XYZ direction = locCurve.Curve.get_EndPoint(1) - start;
XYZ location = start + direction.Normalize() * centerToLeft;
//FamilyInstance famInst = doc.Create.NewFamilyInstance(location, symbol, host, StructuralType.NonStructural);
FamilyInstance famInst = doc.Create.NewFamilyInstance(location, symbol, wall, wall.Level, StructuralType.NonStructural);
famInst.get_Parameter("Sill Height").Set(bottomToBottom);
return famInst;
}
As can be seen, the signature having a level argument of the Document.Create.NewFamilyInstance method is used so that we can set up the relationship between the window and the wall. However, one odd thing is that if another simpler signature, as commented out in the code, which does not have the Level argument, were used, the following exception would come up no matter whatever adjustments were made to the wall or the window.
“Can't cut instance of 36" x 48" out of Wall.
Here is some sample caller code for the good Document.Create.NewFamilyInstance usage that we figured for inserting a window into a wall.
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);
DoorWindowOpeningCreation.InsertWindow(CachedDoc, wall, 5, 5);
Here is what the window and the wall look like in Revit.
As can be seen, the window is inserted into the wall at the right relative spot (which has a X distance with 5 feet from the left side of the wall and a Z distance with 5 feet from the bottom of the wall) and the window along with the wall honors the Level perfectly.
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.