Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to change text color ?

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
3261 Views, 9 Replies

How to change text color ?

Hi everyone,

I am using the ColorDialog control from VisualStudio 2008 in order to
select a color and then I retrieve the RGB components in three variables:

ColorComponentRed
ColorComponentGreen
ColorComponentBlue

What I want is to assign this color to some text (it is text because it's
filtered as this) but the following code is not working:

[code]
Dim colorparam As Parameter
colorparam =
elem.ObjectType.Parameter(parameterId:=Parameters.BuiltInParameter.TEXT_COLOR)

Dim app As New Autodesk.Revit.Creation.Application()
Dim color As Autodesk.Revit.Color = app.NewColor()

color.Red = ColorComponentRed
color.Green = ColorComponentGreen
color.Blue = ColorComponentBlue

colorparam.Set(color)
[/code]

Can anybody tell me what I am doing wrong and how to do it, please ?

TIA

--
Humans are born with a wide horizon.
As time goes by, the horizon narrows and
narrows, until it becomes a point of view.
9 REPLIES 9
Message 2 of 10
Anonymous
in reply to: Anonymous

I have the same problem. Did you find a solution?

Message 3 of 10
Anonymous
in reply to: Anonymous

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.

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/Revit-API/files/GUID-...

Message 4 of 10
Revitalizer
in reply to: Anonymous

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




Rudolf Honke
Software Developer
Mensch und Maschine





Message 5 of 10
jeremytammik
in reply to: 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



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 6 of 10
Revitalizer
in reply to: jeremytammik

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




Rudolf Honke
Software Developer
Mensch und Maschine





Message 7 of 10
jeremytammik
in reply to: Revitalizer

Dear Rudi,

 

I tried it out, but it seems to have no effect:

 

https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/C...

 

      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



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 8 of 10
FAIR59
in reply to: jeremytammik

the return value for the GetRevitTextColorFromSystemColor() method needs to be:

          return (int)( color.R + (color.G << 8)  + (color.B << 16));

 

Message 9 of 10
RPTHOMAS108
in reply to: jeremytammik

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
Message 10 of 10
jeremytammik
in reply to: Anonymous

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:

 

https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/U...

 

https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/C...

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Customer Advisory Groups


Rail Community