[MSCE U16 C++] change Element color to Colorbook Color.

Hi Guys,

I neet to set the color of an Element to a the color CLASSIC 840-HR:6006.

At the moment I'm only capable of passing a Color Index to the element using the Code:

	DgnFileP activeDgnFile = ISessionMgr::GetActiveDgnFile();



	ColorBookPtr colorBook;
	RgbColorDef colorDef;

	DgnHostR  dgnHost = ISessionMgr::GetManager().GetMicroStationDgnHost();
	
	ColorBook::LoadFromDgn(colorBook, L"RAL Classic 840-HR", activeDgnFile, true, dgnHost);
	colorBook->FindColorByName(&colorDef, L"5007");
	
	int colorID = mdlColor_convertRGBtoIndex(0, &colorDef);
	
	ElementPropertiesSetterPtr propsSetter = ElementPropertiesSetter::Create();
	propsSetter->SetColor(colorID);
	auto succ = propsSetter->Apply(eeh);
	auto stat = eeh.ReplaceInModel(eeh.GetElementRef());

Is it possible to set the Colorbook Color Directly without using "mdlColor_convertRGBtoIndex".

Similar to  the KeyIn "ACTIVE COLOR RAL CLASSIC 840-HR:6006"

Greetings 

Manu

  • Hi Manu,

    no time for testing, but you can try to use IntColorDef, which has IntColorDef (RgbColorDef rgb) constructor and through m_int you can access the same color information in UInt32 format (that is accepted by SetColor).

    Regards,

      Jan

  • Hi Jan,

    Thanks for your immediate answer.

    I tried your approach with following code:

    	EditElementHandle eeh(elmDescrP, true, true);
    	DgnFileP activeDgnFile = ISessionMgr::GetActiveDgnFile();
    	ColorBookPtr colorBook;
    	RgbColorDef colorDef;
    	DgnHostR  dgnHost = ISessionMgr::GetManager().GetMicroStationDgnHost();
    	ColorBook::LoadFromDgn(colorBook, L"RAL Classic 840-HR", activeDgnFile, true, dgnHost);
    
    	colorBook->FindColorByName(&colorDef, L"3000");
    	IntColorDef iColor(colorDef);
    	int colorID = mdlColor_convertRGBtoIndex(0, &colorDef);
    	ElementPropertiesSetterPtr propsSetter = ElementPropertiesSetter::Create();
    	propsSetter->SetColor(iColor.m_int);
    	auto succ = propsSetter->Apply(eeh);
    	auto stat = eeh.ReplaceInModel(eeh.GetElementRef());

    MS applies a Color, but sadly not the color I suspected

    The color RAL CLASSIC 840-HR:3000 [167,41,32] should look like: (manually applied)

    But the color which is applied is: (with API)

    As you can see, the correct color is not displayed in the properties and is even different from the wrong! element color.

    Am I missing anything or is it simply not possible to apply the color with the MS API like the "RAL Classic" example above?

  • Hi Manu,

    Am I missing anything or is it simply not possible to apply the color with the MS API like the "RAL Classic" example above?

    In my opinion it should work, on the other hand I do not recall any example how to work with color book colors.

    In the first step I would debug the code to ensure the color from color book is retrieved with correct RGB values. And that IColorDef is also right. But, no fourther ideas :-(

    Maybe to do "a backward analysis" and to check what color is reported by API for the correctly colored element.

    Regards,

      Jan

  • s it possible to set the Colorbook Color Directly without using "mdlColor_convertRGBtoIndex".

    What can be stored in Disp_hdr.symb.color is a 0-255 index into the file's color table, an extended index representing a book or rgb color, and special values like COLOR_BYLEVEL and COLOR_BYCELL.

    So, before you can use a color representing a color book entry, an extended index for that color needs to be added to the file's extended color table.

    You do not however want to use mdlColor_convertRGBtoIndex for this purpose as that function returns the closest match for the supplied rgb in the 0-255 color table only.

    Have a look at the DgnColorMap class.

    After using ColorBook::FindColorByName to get the rgb value, use DgnColorMap::CreateElementColor to return the color index value you can apply to your element (this will create a new extended index for the color if it doesn't already exist).

    //! Get a element color id that can be stored on an element from the supplied IntColorDef.
    //! @param[in] colorDef Color table name.
    //! @param[in] bookName Color book name for the supplied IntColorDef (can be NULL).
    //! @param[in] colorName Color name in color book of the supplied IntColorDef (can be NULL).
    //! @param[in] dgnFile The file for the new element color.
    //! @note A valid element color id is one of the following:
    //! \li COLOR_BYLEVEL
    //! \li COLOR_BYCELL
    //! \li An index into a files DgnColorMap (value from 0 to INDEX_Background).
    //! \li A color book or rgb color id as returned by CreateElementColor.
    //! @return element color or INVALID_COLOR.
    static DGNPLATFORM_EXPORT UInt32 CreateElementColor (IntColorDef const& colorDef, WCharCP bookName, WCharCP colorName, DgnFileR dgnFile);

    HTH

    -B