private ColorFillSchemeEntry CreateEntry(ColorFillScheme scheme, StorageType type, ElementId fillPatternId, Color color) { var entries = scheme.GetEntries(); bool isbyRange = scheme.IsByRange; ColorFillSchemeEntry lastEntry = null; if (entries.Count > 0) { lastEntry = entries.Last(); } var entry = new ColorFillSchemeEntry(type); entry.FillPatternId = fillPatternId; switch (type) { case Autodesk.Revit.DB.StorageType.Double: double doubleValue = 0; if (lastEntry != null) { doubleValue = lastEntry.GetDoubleValue() + 20.00; } entry.SetDoubleValue(doubleValue); break; case Autodesk.Revit.DB.StorageType.String: string strValue = string.Format("New entry {0}", entries.Count); entry.SetStringValue(strValue); break; case Autodesk.Revit.DB.StorageType.Integer: int intValue = 0; if (lastEntry != null) { intValue = lastEntry.GetIntegerValue() + 20; } entry.SetIntegerValue(intValue); break; case Autodesk.Revit.DB.StorageType.ElementId: var level = new FilteredElementCollector(document) .OfClass(typeof(Level)) .Where(lv => !levelIds.Contains(lv.Id) && lv.Name != "Level 1") .FirstOrDefault(); levelIds.Add(level.Id); entry.SetElementIdValue(level.Id); break; default: throw (new Exception("The type is not correct!")); } return(entry); }
/// <summary> /// Make modify to existing color fill scheme /// </summary> /// <param name="scheme"></param> public void ModifyByValueScheme(ColorFillScheme scheme) { List <ColorFillSchemeEntry> entries = scheme.GetEntries().ToList(); List <ColorFillSchemeEntry> newEntries = new List <ColorFillSchemeEntry>(); StorageType storageType = entries[0].StorageType; Random random = new Random(); int seed = random.Next(0, 256); foreach (var entry in entries) { seed++; ColorFillSchemeEntry newEntry = CreateEntry(scheme, storageType, entry.FillPatternId, GenerateRandomColor(seed)); switch (storageType) { case StorageType.Double: newEntry.SetDoubleValue(entry.GetDoubleValue()); break; case StorageType.Integer: newEntry.SetIntegerValue(entry.GetIntegerValue()); break; case StorageType.String: newEntry.SetStringValue(entry.GetStringValue()); break; case StorageType.ElementId: newEntry.SetElementIdValue(entry.GetElementIdValue()); break; default: break; } newEntries.Add(newEntry); } using (Transaction tr = new Transaction(document)) { tr.Start("update entries"); scheme.SetEntries(newEntries); tr.Commit(); } externalCommandData.Application.ActiveUIDocument.RefreshActiveView(); }