public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Autodesk.Revit.Application app = commandData.Application;
Document doc = app.ActiveDocument;
Autodesk.Revit.Elements.View view = doc.ActiveView;
List<ElementIdSet> gruppiID = new List<ElementIdSet>();
GruppoMateriali controlla = new GruppoMateriali();
ElementIterator iter = doc.get_Elements(typeof(FamilyInstance));
iter.Reset();
while (iter.MoveNext())
{
FamilyInstance fam = iter.Current as FamilyInstance;
MaterialSet mats = fam.Materials;
MaterialSetIterator itermats = mats.ForwardIterator();
while (itermats.MoveNext())
{
Material mat = itermats.Current as Material;
ElementId matId = mat.Id;
ElementId elemId = fam.Id;
if (!controlla.yetExists(mat))
{
ElementIdSet gruppo = app.Create.NewElementIdSet();
gruppo.Insert(ref matId);
gruppo.Insert(ref elemId);
gruppiID.Add(gruppo);
}
else
{
for (int i = 0; i < gruppiID.Count; i++)
{
if (gruppiID[i].Contains(ref matId))
{
gruppiID[i].Insert(ref elemId);
}
}
}
}
}
for (int i = 0; i < gruppiID.Count; i++)
{
Random casual = new Random(unchecked((int)(DateTime.Now.Ticks >> i)));
int number1 = casual.Next(0,255);
int number2 = casual.Next(0, 255);
int number3 = casual.Next(0, 255);
Color color = app.Create.NewColor();
// now I'm defining the RGB
color.Red = (byte)(number1);
color.Green = (byte)(number2);
color.Blue = (byte)(number3);
view.set_ProjColorOverrideByElement(gruppiID[i], color);
}
return IExternalCommand.Result.Succeeded;
}
class GruppoMateriali
{
List<int> materiali = new List<int>();
//List<List<ElementIdSet>> elems = new List<List<ElementIdSet>>();
public bool yetExists(Material mat)
{
if (materiali.Contains(mat.Id.Value))
{
return true;
}
else
{
materiali.Add(mat.Id.Value);
return false;
}
}
}