Random Material generator

The post “Randomize Color and Material in Revit” seemed like a good reason for me to write a post about randomly setting materials.

First, some random number humor:

public void RandomMaterial()
{
    Document doc = this.ActiveUIDocument.Document;

    // get all glass materials in document
    IEnumerable<Element> materials = new FilteredElementCollector(doc)
        .OfClass(typeof(Material))
        .Cast<Material>()
        .Where(q => q.MaterialClass == "Glass");

    using (Transaction t = new Transaction(doc,"Random Materials"))
    {
        t.Start();
        foreach (Element e in new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_CurtainWallPanels))
        {
            Parameter materialParam = e.get_Parameter("Material");
            if (materialParam == null)
                continue;

            int randomNumber = new Random(Guid.NewGuid().GetHashCode()).Next(0,materials.Count());

            materialParam.Set(materials.ElementAt(randomNumber).Id);
        }
        t.Commit();
    }
}
Advertisements

One thought on “Random Material generator

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