using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
namespace _2018._7._23练习
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;//获取活动文档
Reference ref1 = uidoc.Selection.PickObject(ObjectType.Element, "选择一个族实例");
Element elem = doc.GetElement(ref1);
Wall aWall = elem as Wall;
//Wall aWall = doc.GetElement(new ElementId(336263)) as Wall;
//创建几何选项
Options opt = new Options();
opt.ComputeReferences = true;
//Options.ComputeReferences必须为true,否是拿到的几何体的Reference都将是null
opt.DetailLevel = ViewDetailLevel.Fine;
GeometryElement geometryElement = aWall.get_Geometry(opt);//墙转换为几何元素
double FaceArea = 0;
double EdgeLength = 0;
foreach (GeometryObject geomObj in geometryElement)//获取到几何元素的边和面
{
Solid geomSolid = geomObj as Solid;
if (null != geomSolid)
{
foreach (Face geoFace in geomSolid.Faces)
{
//得到墙面
if (geoFace is PlanarFace)
{
FaceArea += geoFace.Area;
}
}
foreach (Edge geoEdge in geomSolid.Edges)
{
EdgeLength += geoEdge.ApproximateLength;
//得到墙边
}
}
}
TaskDialog.Show("Revit", "墙边长一共:" + EdgeLength.ToString() + "\n" + "墙面积一共:" + FaceArea.ToString());
return Result.Succeeded;
}
}
}