Set Dimension type Color

longt61
Advocate

Set Dimension type Color

longt61
Advocate
Advocate

I am using Revit Api 2019.2 and have encountered this strange problem.

I tried to duplicate a dimension type and assign a red color to it as follow:

 

// I tried to set red color to dimension type
System.Drawing.Color sysColor = System.Drawing.Color .FromArgb(255, 0,0);
int intValue = sysColor.ToArgb();

dimType = originalDimType.Duplicate(typeName) as DimensionType;
Parameter param = dimType.get_Parameter(BuiltInParameter.LINE_COLOR);
param.Set(intValue); // the value is 16711680, which is blue

And I get a new dimension type with blue color.

However if I hardcode the int value as 255, I get a red color

param.Set(255); // red color

I don't understand what happened here and why there is a difference between System.Drawing.Color and Autodesk.Revit.DB.Color converting RGB value to int value?

How does this conversion process happen? Is there an other way to set line color for dimension type using Color / RGB value directly? Thank you all very much.

0 Likes
Reply
Accepted solutions (1)
1,107 Views
4 Replies
Replies (4)

jeremytammik
Autodesk
Autodesk

I would suggest setting various different colours manually in the user interface and using RevitLookup to analyse what results that has and how the different colours are represented in the database. Then all will become clear. Please let us know what you find out. Thank you!

  



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

0 Likes

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @longt61 ,

try using this below code

DimensionType originalType;
                DimensionType duplicateType = originalType.Duplicate("duplicated dimension type name") as DimensionType;
                Parameter colorParam = duplicateType.get_Parameter(BuiltInParameter.LINE_COLOR);
                if(colorParam!=null)
                {
                    System.Drawing.Color sysColor = System.Drawing.Color.FromArgb(255, 0,0 );
                    int intColor = GetLineColorFromSystemColor(sysColor);
                    colorParam.Set(intColor);
                }

Method

private int GetLineColorFromSystemColor(System.Drawing.Color color)
        {
             return (int)color.R + (int)color.G * (int)Math.Pow(2, 😎 + (int)color.B * (int)Math.Pow(2, 16);
        } 

 

I used the method(GetLineColorFromSystemColor()) mentioned in this below link 

https://thebuildingcoder.typepad.com/blog/2018/02/changing-text-colour.html?utm_source=feedburner&ut... 

 

I hope this helps.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes

longt61
Advocate
Advocate

Thank yo for the helpful information. With the provided formula, I can create the Int value for the color and compare them. 

0 Likes

longt61
Advocate
Advocate

Sorry for the late respond, I already try changing the dimension type color through Revit interface and get the same result. Since I don't know the formula to calculate the Revit color int value, I have to use the System.Drawing.Color methods FromArgb() and ToArgb() in hope that it will have the same formula as Revit color.  

Thanks to @naveen.kumar.t has point me to the correct direction and the help from one of your post, I can achieve what I want. However, that also proven that the int value for System.Drawing.Color and Autodesk.Revit.DB.Color are calculate differently. I can take a fuzzy guess that the parameter (R, G, B) are taken in reversed order so that the int value for Red and Blue are inter-changed (this is what I received when I set the alpha/opaque to 0).

0 Likes