Do you want to change the color of an annotation in Revit? If so, have you tried opening a transaction to do that? You can only change anything in the model if it is inside a transaction.
Hi,
private int GetRevitTextColorFromSystemColor(System.Drawing.Color color)
{
return (int)color.R * (int)Math.Pow(2, 0) + (int)color.G * (int)Math.Pow(2, 8) + (int)color.B * (int)Math.Pow(2, 16);
}
...I know that 2^0 is just 1, so you could better use:
private int GetRevitTextColorFromSystemColor(System.Drawing.Color color)
{
return (int)color.R + (int)color.G * (int)Math.Pow(2, 8) + (int)color.B * (int)Math.Pow(2, 16);
}
Then, for example:
int color= GetRevitTextColorFromSystemColor(yourSystemColor);
textNoteType.get_Parameter(BuiltInParameter.LINE_COLOR).Set(color);
Also note that starting with Revit 2017, there is a ColorSelectionDialog available.
You can use its SelectedColor property to get a Revit color instead of a system color.
Revitalizer
Dear Rudi,
Would this be equivalent (and faster) using the bitwise shift operator?
/// <summary> /// Revit text colour parameter value stored as an integer /// in text note type BuiltInParameter.LINE_COLOR. /// </summary> private int GetRevitTextColorFromSystemColor( System.Drawing.Color color ) { //return (int) color.R // + (int) color.G * (int) Math.Pow( 2, 8 ) // + (int) color.B * (int) Math.Pow( 2, 16 ); return (int) color.R + (int) color.G << 8 + (int) color.B << 16; }
Thank you!
Cheers,
Jeremy
P.S. Probably not... cf. https://stackoverflow.com/questions/1933597/when-to-use-shift-operators-in-c
Hi Jeremy,
I didn't use the bitwise operators so far,
but if providing the same result, it is the more elegant way.
One could replace the System.Drawing.Color by Autodesk.Revit.DB.Color,
the R, G, B properties by their Red, Green, Blue equivalent.
Rudi
Dear Rudi,
I tried it out, but it seems to have no effect:
using( Transaction t = new Transaction( doc ) ) { t.Start( "Change Text Colour" ); int color = Util.ToColorParameterValue( 255, 0, 0 ); Element textNoteType = doc.GetElement( txNote.GetTypeId() ); textNoteType.get_Parameter( BuiltInParameter.LINE_COLOR ) .Set( color ); t.Commit(); }
Any idea why?
Thank you!
Cheers,
Jeremy
the return value for the GetRevitTextColorFromSystemColor() method needs to be:
return (int)( color.R + (color.G << 8) + (color.B << 16));
Quite convenient as extension methods:
<Extension()> _ Public Function AsRGB(ByVal Parameter As Parameter) As Byte() Dim I As Integer = Parameter.AsInteger Dim Red As Byte = I Mod 256 I = I \ 256 Dim Green As Byte = I Mod 256 I = I \ 256 Dim Blue As Byte = I Mod 256 Return New Byte(2) {Red, Green, Blue} End Function <Extension()> _ Public Function AsParameterValue(ByVal Color As Color) As Integer Return Color.Red + (256 * Color.Green) + (65536 * Color.Blue) End Function <Extension()> _ Public Function AsParameterValue(ByVal Color As Windows.Media.Color) As Integer Return Color.R + (256 * Color.G) + (65536 * Color.B) End Function <Extension()> _ Public Function AsParameterValue(ByVal Color As System.Drawing.Color) As Integer Return Color.R + (256 * Color.G) + (65536 * Color.B) End Function
Thank you for the suggestions.
The problem was the .NET shift operator precedence.
Plus '+' tkaes higher precedence than left shift '<<', so I had to add parentheses like this:
/// <summary> /// Revit text colour parameter value stored as an integer /// in text note type BuiltInParameter.LINE_COLOR. /// </summary> public static int ToColorParameterValue( int red, int green, int blue ) { return red + (green << 8) + (blue << 16); }
Final result tested and working in The Building Coder samples:
Cheers,
Jeremy
Can't find what you're looking for? Ask the community or share your knowledge.