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

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

$
0
0

We created some doors 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 InsertDoorAtBottomMiddle method.

private static IEnumerable<Family> FindDoorFamilies(RvtDocument doc)
{
    return new FilteredElementCollector(doc)
        .OfClass(typeof(Family))
        .Cast<Family>()
        .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 InsertDoorAtBottomMiddle2(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);
    famInst.get_Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM).Set(0);

    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.

Level level = FindAndSortLevels(CachedDoc).Last();
Line line = CachedDoc.Application.Create.NewLineBound(new XYZ(50, 0, 0), new XYZ(80, 0, 0));
Wall wall = Wall.Create(CachedDoc, line, level.Id, false);
FamilySymbol symbol = DoorWindowOpeningCreation.GetFirstSymbol(DoorWindowOpeningCreation.FindFamilies(CachedDoc, BuiltInCategory.OST_Doors).FirstOrDefault());
DoorWindowOpeningCreation.InsertDoorAtBottomMiddle2(symbol, wall);

List<ElementId> wallIDs = WallCreation.CreateRoundWalls(CachedDoc, level, new XYZ(20, 0, 0), XYZ.BasisZ, 20);
foreach (ElementId id in wallIDs)
{
    DoorWindowOpeningCreation.InsertDoorAtBottomMiddle2(symbol, (Wall)CachedDoc.GetElement(id));
}

This time, the doors appear well in both straight walls and round walls.
 DoorInRoundWallGood

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