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

Revit .NET API: DoorWindowOpeningCreation Class Used by Non-Leaning Pisa Tower

$
0
0

We have programmatically created various Revit elements using the Revit .NET Creation APIs, specifically those New or Create methods. We also created a non-leaning Pisa Tower using all those Revit .NET Creation APIs that have been demonstrated in detail separately.  

Here is how the resultant Pisa Tower looks beautifully in Revit after being rendered.
 PisaTower
The DoorWindowOpeningCreation class along with its useful methods have been fine tuned a bit when used to create the non-leaning Pisa Tower. Here is the latest code.

    public class DoorWindowOpeningCreation
    {
        public static IOrderedEnumerable<Level> FindAndSortLevels(RvtDocument doc)
        {
            return new FilteredElementCollector(doc)
                            .WherePasses(new ElementClassFilter(typeof(Level), false))
                            .Cast<Level>()
                            .OrderBy(e => e.Elevation);
        }

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

        public static FamilySymbol GetFirstSymbol(Family family)
        {
            return (from FamilySymbol fs in family.Symbols select fs).FirstOrDefault();
        }

        public static FamilyInstance InsertDoor(Document doc, XYZ location)
        {
            FamilySymbol symbol = GetFirstSymbol(FindDoorFamilies(doc).FirstOrDefault());
            FamilyInstance door = doc.Create.NewFamilyInstance(location, symbol, StructuralType.NonStructural);

            return door;
        }

        public static FamilyInstance InsertDoor(Document doc, XYZ location, Element host)
        {
            FamilySymbol symbol = GetFirstSymbol(FindDoorFamilies(doc).FirstOrDefault());
            //FamilyInstance door = doc.Create.NewFamilyInstance(location, symbol, host, StructuralType.NonStructural);
            FamilyInstance door = doc.Create.NewFamilyInstance(location, symbol, host, host.Level, StructuralType.NonStructural);

            return door;
        }


        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);
            door.get_Parameter("Sill Height").Set(distFromBottom);

            return door;
        }



        public 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 InsertWindow(Document doc, XYZ location, Element host)
        {
            FamilySymbol symbol = GetFirstSymbol(FindWindowFamilies(doc).FirstOrDefault());
            //FamilyInstance famInst = doc.Create.NewFamilyInstance(location, symbol, host, StructuralType.NonStructural);
            FamilyInstance famInst = doc.Create.NewFamilyInstance(location, symbol, host, host.Level, StructuralType.NonStructural);

            return famInst;
        }

        public static FamilyInstance InsertWindow(Document doc, Wall wall, double centerToLeft, double bottomToBottom)
        {
            FamilySymbol symbol = GetFirstSymbol(FindWindowFamilies(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() * centerToLeft;
            //FamilyInstance famInst = doc.Create.NewFamilyInstance(location, symbol, host, StructuralType.NonStructural);
            FamilyInstance famInst = doc.Create.NewFamilyInstance(location, symbol, wall, wall.Level, StructuralType.NonStructural);
            famInst.get_Parameter("Sill Height").Set(bottomToBottom);

            return famInst;
        }

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

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

        public static FamilyInstance InsertDoorAtBottomMiddle(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);
            famInst.get_Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM).Set(0);

            return famInst;
        }


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

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

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


        public static Opening InsertOpening(Wall wall, double height, double width, double centerToLeft = -1.0, double centerToBottom = -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;

            double wallHeight = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();
            double topOffset = wallHeight / 2 + height / 2;
            double baseOffset = wallHeight / 2 - height / 2;
            if (centerToBottom >= 0)
            {
                topOffset = centerToBottom + height / 2;
                baseOffset = centerToBottom - height / 2;
            }
            XYZ leftBottom = new XYZ(location.X - width / 2, location.Y, baseOffset);
            XYZ topRight = new XYZ(location.X + width / 2, location.Y, topOffset);

            Opening opening = wall.Document.Create.NewOpening(wall, leftBottom, topRight);

            return opening;
        }


        public static Opening InsertOpening2(Wall wall, double height, double width, double centerToLeft = -1.0, double centerToBottom = -1.0)
        {
            LocationCurve locCurve = wall.Location as LocationCurve;
            double curveLength = locCurve.Curve.Length;

            double locParam = 0.5;
            if (centerToLeft >= 0)
            {
                locParam = centerToLeft / curveLength;
            }

            double startParam = locParam - width / curveLength;
            double endParam = locParam + width / curveLength;
            XYZ startPt = locCurve.Curve.Evaluate(startParam, true);
            XYZ endPt = locCurve.Curve.Evaluate(endParam, true);

            double wallHeight = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();
            double topOffset = wallHeight / 2 + height / 2;
            double baseOffset = wallHeight / 2 - height / 2;
            if (centerToBottom >= 0)
            {
                topOffset = centerToBottom + height / 2;
                baseOffset = centerToBottom - height / 2;
            }
            XYZ leftBottom = new XYZ(startPt.X, startPt.Y, baseOffset);
            XYZ topRight = new XYZ(endPt.X, endPt.Y, topOffset);

            Opening opening = wall.Document.Create.NewOpening(wall, leftBottom, topRight);

            return opening;
        }

        public static Opening InsertCircularOpening(Wall wall, double radius, double centerToLeft = -1.0, double centerToBottom = -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;

            double wallHeight = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();
            double topOffset = wallHeight / 2 + radius / 2;
            double baseOffset = wallHeight / 2 - radius / 2;
            if (centerToBottom >= 0)
            {
                topOffset = centerToBottom + radius / 2;
                baseOffset = centerToBottom - radius / 2;
            }
            XYZ leftBottom = new XYZ(location.X - radius / 2, location.Y, baseOffset);
            XYZ topRight = new XYZ(location.X + radius / 2, location.Y, topOffset);

            Opening opening = wall.Document.Create.NewOpening(wall, leftBottom, topRight);

            return opening;
        }

    }


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