Quantcast
Channel: RevitNetAddinWizard & NavisworksNetAddinWizard
Viewing all articles
Browse latest Browse all 872

Revit .NET Creations API: Create Window into Wall - Pt. 3 (At Center Middle of the Wall)

$
0
0

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 at the center and middle of a wall using some relative position. 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 InsertWindowAtCenterMiddle(FamilySymbol windowSymbol, Wall wall)
{
    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;

    FamilyInstance famInst = wall.Document.Create.NewFamilyInstance(location, windowSymbol, wall, wall.Level, StructuralType.NonStructural);
    double headHeight = famInst.get_Parameter(BuiltInParameter.INSTANCE_HEAD_HEIGHT_PARAM).AsDouble();
    double sillHeight = famInst.get_Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM).AsDouble();
    double wallHeight = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();
    famInst.get_Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM).Set(wallHeight / 2 - (headHeight - sillHeight) / 2);

    return famInst;
}

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);
FamilySymbol windowSymbol = DoorWindowOpeningCreation.GetFirstSymbol(DoorWindowOpeningCreation.FindWindowFamilies(CachedDoc).FirstOrDefault());
DoorWindowOpeningCreation.InsertWindowAtCenterMiddle(windowSymbol, wall);

Here is what the window and the wall look like in Revit.
 WindowInWall3
As can be seen, the window is inserted into the wall at the right spot, center along the vertical direction and middle along the horizontal direction, 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.


Viewing all articles
Browse latest Browse all 872

Trending Articles