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.
In this article, let’s create a wall and insert it into a wall using some relative location instead of the default absolute one which case has been demonstrated previously . Here are the core help methods.
private static IEnumerable<Family> FindDoorFamilies(RvtDocument doc)
return new FilteredElementCollector(doc)
.Where(e => e.FamilyCategory != null
&& e.FamilyCategory.Id.IntegerValue == (int)BuiltInCategory.OST_Doors);
private static FamilySymbol GetFirstSymbol(Family family)
return (from FamilySymbol fs in family.Symbols select fs).FirstOrDefault();
public static FamilyInstance InsertDoor(Document doc, Wall wall, double distFromStart, double distFromBottom)
FamilySymbol symbol = GetFirstSymbol(FindDoorFamilies(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() * distFromStart;
//FamilyInstance door = doc.Create.NewFamilyInstance(location, symbol, host, StructuralType.NonStructural);
FamilyInstance door = doc.Create.NewFamilyInstance(location, symbol, wall, wall.Level, StructuralType.NonStructural);
As can be seen, this time, another signature of the Document.Create.NewFamilyInstance method is used so that we can set up the relationship between the door 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 parameter, were used, the following exception would come up no matter whatever adjustments were made to the wall or the door.
“Can't cut instance of 36" x 84" out of Wall.”
Here is some sample caller 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);
DoorWindowOpeningCreation.InsertDoor(CachedDoc, wall, 5, 5);
Here is what the door and the wall look like in Revit.
As can be seen, the door 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 door along with the wall honors the Level perfectly. In the default dimensions that Revit display, the vertical distance is displayed from the top of the door to the bottom of the wall, but we can do the simple calculation here, 5’ (the sill height) = 12’ (the head height) – 7’ (the door height).
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.