项目完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
namespace SolidPractise
{
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;//UIDocument:代表在Revit用户界面中打开的Autodesk Revit项目的对象。
Autodesk.Revit.DB.Document revitDoc = uidoc.Document;//Document:表示打开的Autodesk Revit项目的对象
ICollection<ElementId> ids = uidoc.Selection.GetElementIds();//ICollection:元素Id收集器
Element selElem = uidoc.Document.GetElement(ids.First());//获取第一个元素
GeometryElement ge = selElem.get_Geometry(new Options());
double area = 0;//定义几何面积
double volume = 0;//定义几何体积
int triangleCount = 0; //三角网格数
foreach(GeometryObject gObj in ge) //遍历集合ge中每一个元素
{
if(gObj is Solid) //如果元素gObj是实体
{
Solid sd = gObj as Solid; //将gObj转换成实体
foreach(Face face in sd.Faces) //遍历sd.Faces中每一个面
{
area += face.Area * 0.3048 * 0.3048;//Revit中的单位是英尺,需进行单位转换
Mesh mesh = face.Triangulate(0.5);
triangleCount += mesh.NumTriangles; //计算三角网格数
}
volume += sd.Volume * 0.3048 * 0.3048 * 0.3048;
}
}
TaskDialog.Show("计算", "面积总和为" + area.ToString() + "平方米" + "\n"
+ "体积为" + volume.ToString("0.000") + "立方米" + "\n"
+ "三角网格数为" + triangleCount.ToString());
return Result.Succeeded;
}
}
}
效果:
在Revit中画一道墙,点击选中此道墙,加载应用程序: