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

Revit .NET Creations API: Create Window into Wall - Pt. 5 (Center Middle of Round Wall – cont.)

$
0
0

We created some windows and inserted them into some straight walls at right spots using Revit .NET API and C# before. We tried the same help methods as introduced before with round walls previously but found out things were not good.

In this post, let’s fix the issue and work out a new version of the InsertWindowAtCenterMiddle method.

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 InsertWindowAtCenterMiddle2(FamilySymbol windowSymbol, Wall wall)
{
    LocationCurve locCurve = wall.Location as LocationCurve;
    XYZ location = locCurve.Curve.Evaluate(0.5, true);

    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;
}

As can be seen, the only difference in the new version is that the curve parameter is used this time to retrieve the middle point of the wall location curve. The approach of using start and end points was apparently not good to calculate the middle point of the location curve as it can be line but can be arc or other irregular curve too.

Here is the round wall creation help method again:

    public static List<ElementId> CreateRoundWalls(Document doc, Level level, XYZ center, XYZ normal, double radius)
    {
        List<ElementId> walls = new List<ElementId>();

        Arc arc1 = doc.Application.Create.NewArc(new Plane(normal, center), radius, 0, Math.PI);
        Arc arc2 = doc.Application.Create.NewArc(new Plane(normal, center), radius, Math.PI, Math.PI * 2);

        Wall wall1 = Wall.Create(doc, arc1, level.Id, false);
        Wall wall2 = Wall.Create(doc, arc2, level.Id, false);

        walls.Add(wall1.Id);
        walls.Add(wall2.Id);

        return walls;
    }

Here is the sample caller code for these help methods on both straight and round walls.

Line line = CachedDoc.Application.Create.NewLineBound(new XYZ(50, 0, 0), new XYZ(80, 0, 0));
Wall wall = Wall.Create(CachedDoc, line, FindAndSortLevels(CachedDoc).Last().Id, false);
FamilySymbol windowSymbol = DoorWindowOpeningCreation.GetFirstSymbol(DoorWindowOpeningCreation.FindWindowFamilies(CachedDoc).FirstOrDefault());
DoorWindowOpeningCreation.InsertWindowAtCenterMiddle2(windowSymbol, wall);

List<ElementId> wallIDs = WallCreation.CreateRoundWalls(CachedDoc, FindAndSortLevels(CachedDoc).Last(), new XYZ(20, 0, 0), XYZ.BasisZ, 20);
FamilySymbol symbol = DoorWindowOpeningCreation.GetFirstSymbol(DoorWindowOpeningCreation.FindFamilies(CachedDoc, BuiltInCategory.OST_Windows).FirstOrDefault());
foreach (ElementId id in wallIDs)
{
    Wall roundwall = (Wall)CachedDoc.GetElement(id);
    try
    {
        DoorWindowOpeningCreation.InsertWindowAtCenterMiddle2(symbol, roundwall);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }
}

This time, the windows appear well in both straight walls and round walls.
 WindowInRoundWallGood

So, a good coding practice can be summarized, using curve parameter always please to location a particular point on the curve.

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