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

Revit .NET Creations API: Wrap Document.NewFamilyInstance for Door/Window Insertion into Wall

$
0
0

We have demonstrated creating Revit doors and windows using the Document.NewFamilyInstance Revit .NET API at various positions with both absolute coordinates and relative ones. When doing so, we noticed that they share the same patterns, dimensions, and parameters, so decided to merge those door and window creation methods into one.

public static IEnumerable<Family> FindFamilies(RvtDocument doc, BuiltInCategory biCategory)
{
    return new FilteredElementCollector(doc)
        .OfClass(typeof(Family))
        .Cast<Family>()
        .Where(e => e.FamilyCategory != null
                && e.FamilyCategory.Id.IntegerValue == (int)biCategory);
}

public static List<FamilySymbol> GetSymbols(Family family)
{
    return (from FamilySymbol fs in family.Symbols select fs).ToList();
}

public static FamilyInstance InsertDoorOrWindow(FamilySymbol symbol, Wall wall, double centerToLeft = -1.0, double bottomToBottom = -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;

    FamilyInstance famInst = wall.Document.Create.NewFamilyInstance(location, symbol, 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();
    double newSillHeight = wallHeight / 2 - (headHeight - sillHeight) / 2;
    if (bottomToBottom >= 0)
        newSillHeight = bottomToBottom;

    famInst.get_Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM).Set(newSillHeight);

    return famInst;
}

Here is the test code.

public static void TestInsertDoorOrWindow(RvtDocument doc)
{
    Line line1 = doc.Application.Create.NewLineBound(new XYZ(10, 0, 0), new XYZ(30, 0, 0));
    Wall wall1 = Wall.Create(doc, line1, FindAndSortLevels(doc).Last().Id, false);
    FamilySymbol symbol1 = GetSymbols(FindFamilies(doc, BuiltInCategory.OST_Windows).First())[0];
    DoorWindowOpeningCreation.InsertDoorOrWindow(symbol1, wall1, 5, 5);
    DoorWindowOpeningCreation.InsertDoorOrWindow(symbol1, wall1);
    DoorWindowOpeningCreation.InsertDoorOrWindow(symbol1, wall1, 15, 11);

    Line line2 = doc.Application.Create.NewLineBound(new XYZ(40, 0, 0), new XYZ(60, 0, 0));
    Wall wall2 = Wall.Create(doc, line2, FindAndSortLevels(doc).Last().Id, false);
    FamilySymbol symbol2 = GetSymbols(FindFamilies(doc, BuiltInCategory.OST_Doors).First())[2];
    DoorWindowOpeningCreation.InsertDoorOrWindow(symbol2, wall2, 5, 2);
    DoorWindowOpeningCreation.InsertDoorOrWindow(symbol2, wall2);
    DoorWindowOpeningCreation.InsertDoorOrWindow(symbol2, wall2, 15, 12);
}

All these windows, doors and walls look good in Revit.


 
Enjoy it, guys!

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