我在改变构件的颜色时发现当我修改一个构件时,同一种材料的构件的颜色都被改变了,于是查找问题,答案如下:
(1)通过材料属性更改构件颜色
通过材料属性更改构件颜色主要关注点是材料的”材质“,而不是构件,所以不同构件只要材质相同,在修改它颜色时,文档中所有相同材质构件都会被修改。通过材质修改颜色是修改下图中着色中的颜色,修改后的颜色属性值可以通过Lookup查到。
代码:
Element ele = doc.GetElement(sel.PickObject(ObjectType.Element, "选择元素"));
Transaction ts = new Transaction(doc, "12");
ts.Start();
Color color = new Color(250,200,100);
Material material = doc.GetElement(ele.GetMaterialIds(false).First()) as Material;
material.Color = color;
ts.Commit();
(2)直接改变构件的颜色
直接改变构件的颜色是通过OverrideGraphicSettings类来设置覆盖视图中元素的显示。不会修改构件材质,其修改的颜色通过Lookup查找不到,可以通过OverrideGraphicSettings.ProjectionFillColor属性来获取修改的颜色值。
代码:
//1.修改构件颜色
Element ele = doc.GetElement(sel.PickObject(ObjectType.Element, "选择元素"));
FilteredElementCollector fillPatternElementFilter = new FilteredElementCollector(doc);
fillPatternElementFilter.OfClass(typeof(FillPatternElement));
//获取实体填充
Element ele = doc.GetElement(sel.PickObject(ObjectType.Element, "选择元素"));
Transaction ts = new Transaction(doc, "12");
ts.Start();
FillPatternElement fillPatternElement = fillPatternElementFilter.First(f =>
(f as FillPatternElement).GetFillPattern().IsSolidFill) as FillPatternElement;
OverrideGraphicSettings overrideGraphicSettings = new OverrideGraphicSettings();
overrideGraphicSettings = doc.ActiveView.GetElementOverrides(ele.Id);
overrideGraphicSettings.SetSurfaceForegroundPatternId(fillPatternElement.Id);
overrideGraphicSettings.SetSurfaceForegroundPatternColor(new Autodesk.Revit.DB.Color(250, 50, 10));
uidoc.ActiveView.SetElementOverrides(ele.Id, overrideGraphicSettings);
string red = overrideGraphicSettings.ProjectionFillColor.Red.ToString();
string blue = overrideGraphicSettings.ProjectionFillColor.Blue.ToString();
string green = overrideGraphicSettings.ProjectionFillColor.Green.ToString();
TaskDialog.Show("1", red + "," + blue + "," + green);
ts.Commit();