Creating Color Schemes through the API (Revit 2022)

Creating Color Schemes through the API (Revit 2022)

sofia_feist
Enthusiast Enthusiast
4,652 Views
10 Replies
Message 1 of 11

Creating Color Schemes through the API (Revit 2022)

sofia_feist
Enthusiast
Enthusiast

Hi,

 

I'm trying to figure out how to create a color scheme through the newly developed Color Fill API for Revit 2022 but I'm not sure what I'm doing wrong. Here's a snippet of my code:

 

using (Transaction t = new Transaction(doc))
{
                t.Start("Create Color Scheme");
                Category roomCategory = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Rooms);

                // Create Color Scheme by duplicating an existing one 
                // (randomly chosen from category: Room)
                var firstOrDefaultColorScheme = new FilteredElementCollector(doc)
                    .OfCategory(BuiltInCategory.OST_ColorFillSchema)
                    .Cast<ColorFillScheme>()
                    .Where(c => c.CategoryId == roomCategory.Id)
                    .FirstOrDefault();

                ElementId newColorSchemeId = firstOrDefaultColorScheme.Duplicate("Name");
                ColorFillScheme colorScheme = doc.GetElement(newColorSchemeId) as ColorFillScheme;

                // Create entry
                ColorFillSchemeEntry entry = new ColorFillSchemeEntry(colorScheme.StorageType);
                entry.Caption = "Entry Caption";
                entry.Color = new Color(0, 0, 0);
                colorScheme.AddEntry(entry);

                // Apply color scheme to view
                View currentView = uiapp.ActiveUIDocument.ActiveView;
                currentView.SetColorFillSchemeId(roomCategory.Id, colorScheme.Id);
                t.Commit();
}

 

 

Using this code, I'm getting an error on colorScheme.AddEntry(entry): "The entry value is out of range. Parameter name: colorFillData". I can't find that parameter neither on the color scheme nor the entry. Anyone knows how to fix this?

 

Thanks,

Sofia

0 Likes
Accepted solutions (1)
4,653 Views
10 Replies
Replies (10)
Message 2 of 11

sofia_feist
Enthusiast
Enthusiast
Accepted solution

Turns out, I was missing two things: the error was due to the fact that I never gave a value to the color scheme entry and if you want the colors to actually show in the view, you also have to specify the fill pattern. In sum, here is the working code for someone looking to do something similar:

 

using (Transaction t = new Transaction(doc))
{
                t.Start("Create Color Scheme");
                Category roomCategory = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Rooms);

                // Create Color Scheme by duplicating an existing one 
                // (randomly chosen from category: Room)
                var firstOrDefaultColorScheme = new FilteredElementCollector(doc)
                    .OfCategory(BuiltInCategory.OST_ColorFillSchema)
                    .Cast<ColorFillScheme>()
                    .Where(c => c.CategoryId == roomCategory.Id)
                    .FirstOrDefault();

                ElementId newColorSchemeId = firstOrDefaultColorScheme.Duplicate("Name");
                ColorFillScheme colorScheme = doc.GetElement(newColorSchemeId) as ColorFillScheme;

                var solidPattern = new FilteredElementCollector(doc)
                    .OfClass(typeof(FillPatternElement))
                    .Cast<FillPatternElement>()
                    .First(a => a.GetFillPattern().IsSolidFill == true); ;

                // Create entry
                ColorFillSchemeEntry entry = new ColorFillSchemeEntry(StorageType.Integer);
                entry.Caption = "Entry Caption";
                entry.Color = new Color(0, 0, 0);
                entry.FillPatternId = solidPattern.Id;
                entry.SetIntegerValue(1);
                colorScheme.AddEntry(entry);

                // Apply color scheme to view
                View currentView = uiapp.ActiveUIDocument.ActiveView;
                currentView.SetColorFillSchemeId(roomCategory.Id, colorScheme.Id);
                t.Commit();
}

 

Cheers,

Sofia

Message 3 of 11

Anonymous
Not applicable

After a day trawling the internet to find a way to create a new ColorFillScheme and add it to the current view, this was extremely helpful!

 

The only thing I'm now battling with is figuring out how to change the variable that the ColorFillLegend is looking at to assign colors based on the ColorFillScheme. Any clue how to to this?

 

Cheers!

Tomé

0 Likes
Message 4 of 11

sofia_feist
Enthusiast
Enthusiast

Hello @Anonymous ,

 

I didn't have to use ColorFillLegends in my code so I'm not entirely sure but one thing that is missing from my code above (that I found out later) is that, if you have a specific parameter you want to assign the color scheme to, you can assign it using: 

colorScheme.ParameterDefinition = parameter.Id

 

After that, you just have to make sure that all ColorFillSchemeEntries have the same storageType as the given parameter.

I hope that helps!

 

Cheers,

Sofia

0 Likes
Message 5 of 11

Anonymous
Not applicable

Hi @sofia_feist 

 

Yes, I got to that in the end. You can also check what parameters you can use with

 

colorScheme.GetSupportedParameterIds()

 

 

Currently, I'm searching through the parameters and selecting the one I want by name

 

selected_parId = None
for parId in colorScheme.GetSupportedParameterIds():
    par = doc.GetElement(parId)
    if par is not None:
        if par.GetDefinition().Name == selected_name:
            selected_parId = parId

colorScheme.ParameterDefinition = selected_parId

 

 

I'm now more worried about if there is no default color scheme available in the model, then there's nothing to .Duplicate from. But I guess I'll get there when I do!

 

Cheers once again for your help!

Tomé

Message 6 of 11

Anonymous
Not applicable
I'm also coding in python, not C#, but the syntax is very very similar...
0 Likes
Message 7 of 11

sofia_feist
Enthusiast
Enthusiast

I understand what you mean; I would love to have the option of creating a new color scheme without duplicating an existing one.

 

However, Revit templates usually come with 1 or more default colorScheme templates for each category since, even in the UI, there is no way to create a new color scheme without duplicating an existing one. You also cannot delete the last color scheme if only one exists so there should always be at least one color scheme to duplicate from. That shouldn't be a problem. 

 

The properties of the old color scheme are also overriden when you change the parameter definition so you can basically start from scratch.

Message 8 of 11

Anonymous
Not applicable

Oh that's very good to know!

You've saved my day! 🙂

Message 9 of 11

mofalk9CB4P
Community Visitor
Community Visitor

Is this the complete code?

0 Likes
Message 10 of 11

sofia_feist
Enthusiast
Enthusiast

For creating a new color scheme of storage type integer with a singular entry, yes.

0 Likes
Message 11 of 11

jchristel9Y7MH
Observer
Observer

Hi Sofia,

Hopefully not too late for an answer 🙂

One issue with the above code is the storage type assignment on the area scheme entry.
There is some autodesk sample code provided here:


This row (in this case it storing it as a string)

ColorFillSchemeEntry entry = new ColorFillSchemeEntry(StorageType.String)

 I'm not sure as to whether the ColorFillScheme itself got a storage type (as per your code)

I tried the above and that works...However, you need to ensure that the entry you are adding actually has a value set. In my case it was empty, and I got the same error message...why I ended up here in the first place


ahhhh didnt see all the answers above ... ignore