Example #1
0
        public Result insert_windows(XYZ[] windows_locations, Level level)
        {
            FamilySymbol wfs = new FilteredElementCollector(doc)
                               .OfClass(typeof(FamilySymbol))
                               .Cast <FamilySymbol>().FirstOrDefault(q
                                                                     => q.Name == "48\" x 60\"");


            using (Transaction t = new Transaction(doc))
            {
                t.Start("Activate window");

                if (!wfs.IsActive)
                {
                    wfs.Activate(); doc.Regenerate();
                }
                t.Commit();
            }

            foreach (XYZ location in windows_locations)
            {
                DoorOperations orientation     = (DoorOperations)location.Z;
                XYZ            window_location = new XYZ(location.X, location.Y, level.Elevation + 2.5); // Windows should be at 30" from the ground

                #region Find the hosting Wall (nearest wall to the insertion point)

                FilteredElementCollector collector = new FilteredElementCollector(doc);
                collector.OfClass(typeof(Wall));

                List <Wall> walls = collector.Cast <Wall>().Where(wl => wl.LevelId == level.Id).ToList();

                Wall w_ = null;

                double distance = double.MaxValue;

                foreach (Wall w in walls)
                {
                    double proximity = (w.Location as LocationCurve).Curve.Distance(window_location);

                    if (proximity < distance)
                    {
                        distance = proximity;
                        w_       = w;
                    }
                }

                #endregion

                using (Transaction t = new Transaction(doc))
                {
                    t.Start("Create window");

                    FamilyInstance window = doc.Create.NewFamilyInstance(window_location, wfs, w_, level, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);

                    if ((orientation & DoorOperations.Should_flip) != 0)
                    {
                        window.flipFacing();
                    }
                    if ((orientation & DoorOperations.Should_rotate) != 0)
                    {
                        window.flipHand();
                    }
                    t.Commit();
                }
            }

            return(Result.Succeeded);
        }
Example #2
0
        public Result insert_doors(XYZ[] doors_locations, Level level)
        {
            FamilySymbol fs = new FilteredElementCollector(doc)
                              .OfClass(typeof(FamilySymbol))
                              .Cast <FamilySymbol>().FirstOrDefault(q
                                                                    => q.Name == "36\" x 84\"");

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Activate door");

                if (!fs.IsActive)
                {
                    fs.Activate(); doc.Regenerate();
                }
                t.Commit();
            }


            foreach (XYZ location in doors_locations)
            {
                DoorOperations orientation   = (DoorOperations)location.Z;
                XYZ            door_location = new XYZ(location.X, location.Y, level.Elevation);

                #region Find the hosting Wall (nearest wall to the insertion point)

                FilteredElementCollector collector = new FilteredElementCollector(doc);
                collector.OfClass(typeof(Wall));

                List <Wall> walls = collector.Cast <Wall>().Where(wl => wl.LevelId == level.Id).ToList();

                Wall w_ = null;

                double distance = double.MaxValue;

                foreach (Wall w in walls)
                {
                    double proximity = (w.Location as LocationCurve).Curve.Distance(door_location);

                    if (proximity < distance)
                    {
                        distance = proximity;
                        w_       = w;
                    }
                }

                #endregion

                using (Transaction t = new Transaction(doc))
                {
                    t.Start("Create door");

                    FamilyInstance door = doc.Create.NewFamilyInstance(door_location, fs, w_, level, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                    // Doors probably face up by default. Or maybe it depends on the wall direction?
                    // E-W wall = door N by default

                    if ((orientation & DoorOperations.Should_flip) != 0)
                    {
                        door.flipFacing();
                    }
                    if ((orientation & DoorOperations.Should_rotate) != 0)
                    {
                        door.flipHand();
                    }
                    t.Commit();
                }
            }
            return(Result.Succeeded);
        }