The last #RTCEUR #Revit API Wish – Parts Want Levels

Here’s the last wish granted! RVT Links are used to model typical floors and parts are created from the walls and floors in the link instances. Here is how to use the API to set a user-created “Level” parameter for each part based on the name of the level in the host RVT nearest to the lowest point in the geometry of each part.

parts

public void setPartLevel()
{
    Document doc = this.ActiveUIDocument.Document;
    using (Transaction t = new Transaction(doc, "Set Part Levels"))
    {
        t.Start();
        foreach (Part part in new FilteredElementCollector(doc).OfClass(typeof(Part)))
        {
            Parameter levelParam = part.Parameters.Cast<Parameter>().FirstOrDefault(q => q.Definition.Name == "Level");
            if (levelParam == null)
                return;
            
            double minZ = double.PositiveInfinity;    
            foreach (Solid s in part.get_Geometry(new Options()))
            {
                foreach (Face f in s.Faces)
                {
                    foreach (EdgeArray ea in f.EdgeLoops)
                    {
                        foreach (Edge e in ea)
                        {
                            Curve c = e.AsCurve();
                            if (c.GetEndPoint(0).Z < minZ)
                                minZ = c.GetEndPoint(0).Z;
                            if (c.GetEndPoint(1).Z < minZ)
                                minZ = c.GetEndPoint(1).Z;
                        }
                    }
                }
            }
            Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>().OrderBy(q => Math.Abs(q.Elevation - minZ)).FirstOrDefault();
            levelParam.Set(level.Name);
        }
        t.Commit();
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s