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

Revit .NET API: MassInsertion 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 MassInsertion 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 MassInsertion
    {
        public static Family LoadMassFamily(Document doc, string name)
        {
            string libPath = doc.Application.GetLibraryPaths()["Imperial Library"];
            string path = Path.Combine(libPath, @"Mass\" + name + ".rfa");
            Family family;
            doc.LoadFamily(path, out family);

            return family;
        }

        public static FamilyInstance InsertMass(Document doc, FamilySymbol symbol, Level baseLevel, XYZ location, double rotation)
        {
            FamilyInstance famInst = doc.Create.NewFamilyInstance(location, symbol, baseLevel, StructuralType.NonStructural);
            Line axis = doc.Application.Create.NewLine(location, XYZ.BasisZ, false);
            ElementTransformUtils.RotateElement(doc, famInst.Id, axis, rotation);

            return famInst;
        }

        public static FamilyInstance InsertArch(Document doc, Level baseLevel, XYZ location, double rotation)
        {
            Family family = DoorWindowOpeningCreation.FindFamilies(doc, BuiltInCategory.OST_Mass).FirstOrDefault(e => e.Name == "Arch");
            if (family == null)
                family = LoadMassFamily(doc, "Arch");

            FamilySymbol symbol = ColumnCreation.GetFirstSymbol(family);
            return InsertMass(doc, symbol, baseLevel, location, rotation);
        }

        public static FamilyInstance InsertArch(Document doc, Level baseLevel, double offset, XYZ location, double rotation, double depth, double height, double width, double openingheight, double openingwidth)
        {
            FamilyInstance famInst = InsertArch(doc, baseLevel, location, rotation);

            famInst.get_Parameter("Offset").Set(offset);
            famInst.get_Parameter("Depth").Set(depth);
            famInst.get_Parameter("Height").Set(height);
            famInst.get_Parameter("Width").Set(width);
            famInst.get_Parameter("Opening Width").Set(openingwidth);
            famInst.get_Parameter("Opening Height").Set(openingheight);

            return famInst;
        }

        public static Material AssignMassMaterial(Element e, string name)
        {
            FilteredElementCollector collector = new FilteredElementCollector(e.Document);
            Material material = collector
                .WherePasses(new ElementClassFilter(typeof(Material)))
                .Where(mass => mass.Name == name)
                .FirstOrDefault() as Material;

            e.get_Parameter("Mass Material").Set(material.Id);

            return material;
        }

        public static List<FamilyInstance> InsertArchGate(Document doc, Level baseLevel, Level topLevel, XYZ location, double rotation, double width)
        {
            List<FamilyInstance> list = new List<FamilyInstance>();
            double factor = width / 14.0;
            double levelHeight = topLevel.Elevation - baseLevel.Elevation;
            double topOffset = levelHeight * 0.2;

            list.Add(ColumnCreation.InsertMetalCladColumn(doc, location - new XYZ(6, 0, 0), topLevel, -topOffset, baseLevel, 0));
            list.Add(ColumnCreation.InsertMetalCladColumn(doc, location + new XYZ(6, 0, 0), topLevel, -topOffset, baseLevel, 0));

            list.Add(MassInsertion.InsertArch(doc, null, topLevel.Elevation - topOffset, location, 0, 2.0, 6.0 * factor, 14.0 * factor, 5.1 * factor, 14.0 * factor - 4.0));

            Line line = doc.Application.Create.NewLine(location, XYZ.BasisZ, false);
            ElementTransformUtils.RotateElements(doc, (from FamilyInstance fi in list select fi.Id).ToList(), line, rotation);

            return list;
        }

        public static List<FamilyInstance> DeployArchGateAlongCircle(Document doc, Level baseLevel, Level topLevel, XYZ center, double radius, double startAngle = 0.0)
        {
            List<FamilyInstance> list = new List<FamilyInstance>();

            int number = (int)(Math.PI * radius * 2.0 / 14.0);
            for (int i = 0; i < number; i++)
            {
                double angle = startAngle + Math.PI * 2 / number * (i + 0.5);
                XYZ location = center + (new XYZ(Math.Cos(angle), Math.Sin(angle), 0)).Normalize() * radius;
                double rotation = Math.PI / 2 + angle;
                double width = radius * Math.Sin(Math.PI / number) * 2;
                double factor = width / 13.0;
                double levelHeight = topLevel.Elevation - baseLevel.Elevation;
                double topOffset = levelHeight * 0.2;

                list.AddRange(InsertArchGate(doc, baseLevel, topLevel, location, rotation, width));
                AssignMassMaterial(list.Last(), "Metal - Aluminum");
            }

            return list;
        }

        public static List<FamilyInstance> InsertArchGate2(Document doc, Level baseLevel, Level topLevel, XYZ location, double rotation, double width, double sizeFactor = 1.0)
        {
            List<FamilyInstance> list = new List<FamilyInstance>();
            double factor = width / 14.0;
            double levelHeight = topLevel.Elevation - baseLevel.Elevation;
            double topOffset = levelHeight * 0.2;

            list.Add(ColumnCreation.InsertMetalCladColumn(doc, location - new XYZ(7.2 * sizeFactor, 0, 0), topLevel, -topOffset, baseLevel, 0, 2.4 * sizeFactor));
            list.Add(ColumnCreation.InsertMetalCladColumn(doc, location + new XYZ(7.2 * sizeFactor, 0, 0), topLevel, -topOffset, baseLevel, 0, 2.4 * sizeFactor));

            list.Add(MassInsertion.InsertArch(doc, null, topLevel.Elevation - topOffset, location, 0, 2.0 * sizeFactor, 6.0 * factor, 14.0 * factor, 5.1 * factor, 14.0 * factor - 4.0));

            Line line = doc.Application.Create.NewLine(location, XYZ.BasisZ, false);
            ElementTransformUtils.RotateElements(doc, (from FamilyInstance fi in list select fi.Id).ToList(), line, rotation);

            return list;
        }

        public static List<FamilyInstance> DeployArchGateAlongCircle2(Document doc, Level baseLevel, Level topLevel, XYZ center, double radius, double startAngle = 0.0, double sizeFactor =1.0, string materialName = null)
        {
            List<FamilyInstance> list = new List<FamilyInstance>();

            int number = (int)(Math.PI * radius * 2.0 / 14.0 / sizeFactor);
            for (int i = 0; i < number; i++)
            {
                double angle = startAngle + Math.PI * 2 / number * (i + 0.5);
                XYZ location = center + (new XYZ(Math.Cos(angle), Math.Sin(angle), 0)).Normalize() * radius;
                double rotation = Math.PI / 2 + angle;
                double width = radius * Math.Sin(Math.PI / number) * 2 + 2.4 * sizeFactor;
                double levelHeight = topLevel.Elevation - baseLevel.Elevation;
                double topOffset = levelHeight * 0.2;

                list.AddRange(InsertArchGate2(doc, baseLevel, topLevel, location, rotation, width, sizeFactor));
                if( !string.IsNullOrEmpty(materialName) )
                    AssignMassMaterial(list.Last(), materialName);
            }

            return list;
        }
    }


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