前言
这个例子使用Revit显示样式功能将面显示成不同的颜色。分析显示样式参考官方文档。
内容
效果:
核心逻辑:
- 得到一个
SpatialFieldManager
- 拾取一系列的面:
uiDoc.Selection.PickObjects(ObjectType.Face)
- 计算面上的 UV 值,以及对应的颜色值
- 应用到对应的面:
sfm.UpdateSpatialFieldPrimitive
核心代码:
// 获取或者创建 SpatialFieldManager
SpatialFieldManager sfm = SpatialFieldManager.GetSpatialFieldManager(doc.ActiveView);
if (sfm == null) sfm = SpatialFieldManager.CreateSpatialFieldManager(doc.ActiveView, 1);
// 选择面
IList<Reference> refList = new List<Reference>();
refList = uiDoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Face);
// 对每一个面进行计算和设置
foreach (Reference reference in refList){
//
IList<UV> uvPts = new List<UV>();
List<double> doubleList = new List<double>();
IList<ValueAtPoint> valList = new List<ValueAtPoint>();
Face face = doc.GetElement(reference).GetGeometryObjectFromReference(reference)as Face;
BoundingBoxUV bb = face.GetBoundingBox();
UV min = bb.Min;
UV max = bb.Max;
for (double u = min.U; u < max.U; u += (max.U - min.U) / 10){
for (double v = min.V; v < max.V; v += (max.V - min.V) / 10){
UV uv = new UV(u, v);
if (face.IsInside(uv)){
uvPts.Add(uv);
// 用时间和v值计算一个颜色数值
doubleList.Add(v + DateTime.Now.Second);
valList.Add(new ValueAtPoint(doubleList));
doubleList.Clear();
}
}
}
// 更新分析模型,即显示颜色
FieldDomainPointsByUV pnts = new FieldDomainPointsByUV(uvPts);
FieldValues vals = new FieldValues(valList);
int idx = sfm.AddSpatialFieldPrimitive(reference);
AnalysisResultSchema resultSchema = new AnalysisResultSchema("Schema 1", "Schema 1 Description");
sfm.UpdateSpatialFieldPrimitive(idx, pnts, vals, sfm.RegisterResult(resultSchema));
}
其它
分析显示样式:
- SpatialFieldManager 是显示样式对应的主要API;
- UI入口参考:创建分析显示样式