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

Revit .NET Creations API: Create 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. In this post, let’s create a non-leaning Pisa Tower using all those Revit .NET Creation APIs that have been demonstrated in detail separately and C#.  

Here is how the resultant Pisa Tower that we are going to create programmatically looks like first when rendered in Revit.
 PisaTower

It is so cool, isn’t it?

Here we go for the whole nice and succint source code.

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace Rvt2013CsNet
{
    public class PisaTowerCreation
    {
        private double xBase;
        private double maxRadius;
        private double radiusDecrement;
        private double maxLevelHeight;
        private double groundElevation;
        private int levelCount;

        private RvtDocument CachedDoc;
        private List<Level> Levels;

        public PisaTowerCreation(RvtDocument doc)
        {
            CachedDoc = doc;

            xBase = 0.0;
            maxRadius = 45.0;
            radiusDecrement = 8.0;
            maxLevelHeight = 40.0;
            groundElevation = 0.0;
            levelCount = 8;
        }

        public void Create()
        {
            View3D view3d = null;
            using (Transaction tran = new Transaction(CachedDoc, "NewView3D"))
            {
                tran.Start();

                view3d = ViewCreation.NewView3D(CachedDoc, new XYZ(1, 2, 3));

                tran.Commit();
            }

            ExtCmd.CachedUiApp.ActiveUIDocument.ActiveView = view3d;

            using (Transaction tran = new Transaction(CachedDoc, "PisaTower"))
            {
                tran.Start();

                SettingAdjuster.TurnOnAllGraphicsInActiveView(CachedDoc);

                List<Element> elementsToTransform = new List<Element>();

                Levels = CreateLevels();
                CreateFloorCeilingViews();

                elementsToTransform.AddRange(CreateWallsAndOpenings());

                List<Element> floors = CreateFloorsWithOpenings();
                floors.RemoveAt(0);
                //elementsToTransform.AddRange(floors);

                elementsToTransform.AddRange(CreateArchGates());
                //CreateRoof();

                Line axis = CachedDoc.Application.Create.NewLine(new XYZ(xBase, 0, 0), XYZ.BasisY, false);
                List<ElementId> ids = (from Element e in elementsToTransform select e.Id).ToList();
                //ElementTransformUtils.RotateElements(CachedDoc, ids, axis, Math.PI / 6);

                tran.Commit();
            }

        }

        private double[] LevelElevations
        {
            get
            {
                List<double> list = new List<double>();
                double elevation = groundElevation;
                list.Add(elevation);

                for (int i = 0; i < levelCount; i++)
                {
                    if (i == 0)
                        elevation += 1.5 * maxLevelHeight;
                    else
                        elevation += maxLevelHeight;

                    list.Add(elevation);
                }

                return list.ToArray();
            }
        }

        private string[] LevelNames
        {
            get
            {
                return new string[]
                    {
                        "L0",
                        "L1",
                        "L2",
                        "L3",
                        "L4",
                        "L5",
                        "L6",
                        "L7",
                        "L8",
                    };
            }
        }

        private List<Level> CreateLevels()
        {
            CachedDoc.Delete(new FilteredElementCollector(CachedDoc)
                    .WherePasses(new ElementClassFilter(typeof(Level), false))
                    .Select(e => e.Id)
                    .ToList());
            List<Level> levels = LevelCreation.CreateLevels(
                    CachedDoc,
                    LevelElevations,
                    LevelNames
                );

            return levels;
        }

        private List<Autodesk.Revit.DB.View> CreateFloorCeilingViews()
        {
            List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();

            foreach (Level l in Levels)
            {
                list.Add( ViewCreation.NewViewPlan("Floor Plan - " + l.Name, l, ViewType.FloorPlan) );
                list.Add( ViewCreation.NewViewPlan("Ceiling Plan - " + l.Name, l, ViewType.CeilingPlan) );
            }

            return list;
        }

        private List<Element> CreateWallsAndOpenings()
        {
            List<Element> list = new List<Element>();

            for (int i = 0; i < Levels.Count - 1; i++)
            {
                Level level = Levels[i];

                double radius = maxRadius - radiusDecrement;
                double centerToLeft = (int)(radius * Math.PI / 8) * (i + 0.5);
                if (i == 0)
                {
                    radius = maxRadius;
                    centerToLeft = 7.0 * 1.8;
                }
                else if (i == Levels.Count - 2)
                {
                    radius = maxRadius - 2 * radiusDecrement;
                    centerToLeft = 7.0 * 1.4;
                }

                List<ElementId> wallIds = WallCreation.CreateRoundWalls(
                                                CachedDoc,
                                                level,
                                                new XYZ(xBase, 0, 0),
                                                new XYZ(xBase, 0, 1),
                                                radius);

                foreach (ElementId id in wallIds)
                {
                    Wall wall = (Wall)CachedDoc.GetElement(id);
                    double wallHeight = Levels[i + 1].Elevation - level.Elevation;
                    wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).Set(wallHeight);

                    Opening opening = DoorWindowOpeningCreation.InsertOpening2(
                        wall,
                        wallHeight/2,
                        wallHeight/10,
                        centerToLeft,
                        wallHeight/4);

                    list.Add(wall);
                    list.Add(opening);
                }
            }

            return list;
        }

        private List<Element> CreateFloorsWithOpenings()
        {
            List<Element> list = new List<Element>();

            for (int i = 0; i < Levels.Count; i++)
            {
                double radius1 = maxRadius;
                double radius2 = maxRadius - 2 * radiusDecrement;
                if (i == 0)
                {
                    radius1 = maxRadius + 8 * radiusDecrement;
                    radius2 = maxRadius - 2 * radiusDecrement;
                }
                else if (i == Levels.Count - 2)
                {
                    radius1 = maxRadius;
                    radius2 = maxRadius - 4 * radiusDecrement;
                }
                else if (i == Levels.Count - 1)
                {
                    radius1 = maxRadius - 2 * radiusDecrement;
                    radius2 = 0.01;
                }

                Level level = Levels[i];
                Floor floor = FloorCreation.CreateRoundFloorWithRoundOpening(
                                CachedDoc,
                                level,
                                new XYZ(xBase, 0, level.Elevation),
                                XYZ.BasisZ,
                                radius1,
                                radius2 );
                list.Add(floor);
            }

            return list;
        }


        private List<Element> CreateArchGates()
        {
            List<Element> list = new List<Element>();

            for (int i = 0; i < Levels.Count - 1; i++)
            {
                double radius = maxRadius;
                double factor = 1.0;
                double startAngle = 0.0;
                if (i == 0 )
                {
                    radius = maxRadius;
                    factor = 1.8;
                    //startAngle = Math.PI / 8;
                }
                else if (i == Levels.Count - 2)
                {
                    radius = maxRadius - 2 * radiusDecrement;
                    factor = 1.4;
                    //startAngle = Math.PI / 8;
                }

                Level level = Levels[i + 1];
                Level baseLevel = Levels[i];
                list.AddRange(MassInsertion.DeployArchGateAlongCircle2(
                                CachedDoc,
                                baseLevel,
                                level,
                                new XYZ(xBase, 0, level.Elevation),
                                radius,
                                startAngle,
                                factor,
                                "Metal - Aluminum")
                            );
            }

            return list;
        }

        private Element CreateRoof()
        {
            return RoofCreation.CreateRoundRoof(
                        CachedDoc,
                        Levels.Last(),
                        RoofCreation.FindRoofTypes(CachedDoc).First(),
                        new XYZ(xBase, 0, Levels.Last().Elevation),
                        XYZ.BasisZ,
                        maxRadius,
                        0.0,
                        0.0);
        }
    }
}

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