案例:近日,笔者在获得构件信息的时候,无法获得实体的真实颜色,对此进行了多方面的调查,发现MS平台的颜色系统还是有点拐弯的。
代码如下(示例):
ElementPropertiesSetter elePropSet = new ElementPropertiesSetter();
RgbColorDef rgbColor = new RgbColorDef((byte)AutoDimFormatInfo.rgbline.R, (byte)AutoDimFormatInfo.rgbline.G, (byte)AutoDimFormatInfo.rgbline.B);
uint colormap = DgnColorMap.CreateElementColor(rgbColor, null, null, dgnfile);
elePropSet.SetColor(colormap);
elePropSet.Apply(el);
listEle.Add(el);
C++版的代码如下(示例):
ElementPropertiesSetterPtr propEle = ElementPropertiesSetter::Create();
LevelId level = dgnModelPtr->GetLevelCache().GetLevelByName(L"CW_hatch 填充线").GetLevelId();
propEle->SetLevel(level);
propEle->SetColor(COLOR_BYLEVEL);
propEle->SetLinestyle(STYLE_BYLEVEL,NULL);
propEle->SetWeight(WEIGHT_BYLEVEL);
propEle->Apply(eeh);
通过AnnounceElementDisplayParameters获得的ElementDisplayParameters,
然后ElementDisplayParameters里面含有FillColorTBGR信息,但是如果元素的颜色设置为随层的话,这个颜色基本就是一个无意义的数字。因此需要拿到它所在图层的颜色,通过图层的GetByLevelColor方法:
public LevelDefinitionColor GetByLevelColor();
此时,还未完结,因为获得的这个颜色是MS中特定的一个内部颜色InternalColor。其最低字节表示索引色表中与该颜色最接近的颜色的索引值,其余字节表示当前模型中真彩色表中的颜色索引值。
转换方法:
rv = ((byte)((fillColor) & 0xff));
gv = ((byte)((fillColor >> 8) & 0xff));
bv = ((byte)((fillColor >> 16) & 0xff));
int t = ((byte)((fillColor >> 24) & 0xff));
ColorInformation infoColor = DgnColorMap.ExtractElementColorInfo(fillColor, mDgnModel.GetDgnFile());
rv = infoColor.ColorDefinition.R;
gv = infoColor.ColorDefinition.G;
bv = infoColor.ColorDefinition.B;
SymbologyReporter symbReporter(eh);
UInt32 clrId, clrIdx;
bool isTrueClr;
WString bookName, clrName;
double transparency;
UInt index = 0;
while (SUCCESS == symbReporter.GetColorID(clrId, index))
index++;
index--;
symbReporter.GetTransparency(transparency, index);
if (COLOR_BYLEVEL == clrId)
{
LevelId lvlID;
symbReporter.GetLevelId(lvlID, index);
}
else
{
DgnColorMapP pClrMap = DgnColorMap::GetForDisplay(eh.GetDgnModelP());
IntColorDef clrDef = pClrMap->GetColor(clrId);
pClrMap->ExtractElementColorInfo(&clrDef, &clrIdx, &isTrueClr, &bookName, &clrName, clrId, *eh.GetDgnFileP());
RgbaColorDef rgbColor = clrDef.m_rgba;
unsigned long red = (unsigned long)rgbColor.red;
unsigned long green = (unsigned long)rgbColor.green;
unsigned long blue = (unsigned long)rgbColor.blue;
Byte alphaRevs = ~rgbColor.alpha;
unsigned long alpha = (unsigned long)alphaRevs;
unsigned long color = red | (green << 8) | (blue << 16) | (alpha << 24);
}
贴几个具体颜色的验证数值图片示例一下:
上图可见,通过按位与得到的颜色RGB是不对的。
通过ExtractElementColorInfo获得的浅青色的颜色 ,如上图所示RGB数值。