前言
这个例子通过从图片中获取颜色,将颜色转换成数值,赋值给分割表面上对应族实例的对应参数。
内容
获取颜色的代码:
Bitmap image = new Bitmap(doc.PathName + "_grayscale.bmp");
System.Drawing.Color pixelColor = new System.Drawing.Color();
pixelColor = image.GetPixel(image.Width - v, image.Height - u);
获取族实例的代码:
DividedSurface ds = element as DividedSurface;
GridNode gn = new GridNode();
gn.UIndex = u;
gn.VIndex = v;
FamilyInstance familyinstance = ds.GetTileFamilyInstance(gn, 0);
设置族实例参数的代码:
double grayscale = 255 - ((pixelColor.R + pixelColor.G + pixelColor.B) / 3);
Parameter param = null;
param = familyinstance.LookupParameter("Grayscale");
param.Set(grayscale / 255);
保留核心逻辑,去除错误处理相关逻辑:
Parameter param = null;
Bitmap image = new Bitmap(doc.PathName + "_grayscale.bmp");
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> collection = collector.OfClass(typeof(DividedSurface)).ToElements();
foreach (Element element in collection){
DividedSurface ds = element as DividedSurface;
GridNode gn = new GridNode();
for (int u = 0; u < ds.NumberOfUGridlines; u++){
gn.UIndex = u;
for (int v = 0; v < ds.NumberOfVGridlines; v++){
gn.VIndex = v;
if (ds.IsSeedNode(gn)){
FamilyInstance familyinstance = ds.GetTileFamilyInstance(gn, 0);
if (familyinstance != null){
param = familyinstance.LookupParameter("Grayscale");
System.Drawing.Color pixelColor = image.GetPixel(image.Width - v, image.Height - u);
double grayscale = 255 - ((pixelColor.R + pixelColor.G + pixelColor.B) / 3);
param.Set(grayscale / 255);
}
}
}
}
}
结果