Bentley MicroStation CE版的颜色变换(CONNECT Edition)


前言

案例:近日,笔者在获得构件信息的时候,无法获得实体的真实颜色,对此进行了多方面的调查,发现MS平台的颜色系统还是有点拐弯的。


一、怎样设置构件颜色?

代码如下(示例):

1.直接设置颜色

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);

2.设置为图层的颜色

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。其最低字节表示索引色表中与该颜色最接近的颜色的索引值,其余字节表示当前模型中真彩色表中的颜色索引值。
转换方法:

2.1 如果在自动化com开发途径的话,要想获得RGB值,需要调用ActiveModelReference下的InternalColorToRGBColor得到颜色值,然后转为十六进制,然后通过与运算得到十进制的RGB值。
rv = ((byte)((fillColor) & 0xff));
gv = ((byte)((fillColor >> 8) & 0xff));
bv = ((byte)((fillColor >> 16) & 0xff));
int t = ((byte)((fillColor >> 24) & 0xff));
2.2 如果是C#的CE开发dll工程,通过ExtractElementColorInfo可得到RGB数值
ColorInformation infoColor = DgnColorMap.ExtractElementColorInfo(fillColor, mDgnModel.GetDgnFile());
rv = infoColor.ColorDefinition.R;
gv = infoColor.ColorDefinition.G;
bv = infoColor.ColorDefinition.B;
2.3 C++版获得颜色的示例(Bentley官网论坛摘抄),在此一并贴一下
 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数值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值