Revit开发 - 几何信息(GeometryElement)的获取

    本文演示如何获取Revit文件中墙和柱各个面的面积。

1.用Revit绘制模型

    Revit开发 - 几何信息(GeometryElement)的获取_第1张图片

2.获取墙各个面的面积

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Windows.Forms;

namespace RevitAddin4
{
    [TransactionAttribute(TransactionMode.Manual)]
    public class RevitAddin : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Document doc = uiDoc.Document;
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            var walls = collector.OfClass(typeof(Wall));

            string info = "";
            foreach (var item in walls)
            {
                Options options = new Options();
                GeometryElement geometry = item.get_Geometry(options);
                int i = 0;
                foreach (GeometryObject obj in geometry)
                {
                    Solid solid = obj as Solid;
                    if (solid != null)
                    {
                        FaceArray faceArray = solid.Faces;
                        foreach (Face face in faceArray)
                        {
                            info += "Face"+ i +" 的面积: " + face.Area.ToString() + "\n";
                            i++;
                        }
                    }
                }
            }

            MessageBox.Show(info, "信息");
            return Result.Succeeded;
        }
    }
}

    执行结果:

Revit开发 - 几何信息(GeometryElement)的获取_第2张图片

 

3.获取柱各个面的面积

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Windows.Forms;

namespace RevitAddin4
{
    [TransactionAttribute(TransactionMode.Manual)]
    public class RevitAddin : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Document doc = uiDoc.Document;
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            var columns = collector.OfClass(typeof(FamilyInstance));

            string info = "";
            foreach (var item in columns)
            {
                info += "名称: " + item.ToString() + "\n";
                Options options = new Options();
                GeometryElement geometry = item.get_Geometry(options);
                int i = 0;
                foreach (GeometryObject obj in geometry)
                {
                    GeometryInstance instance = obj as GeometryInstance;
                    if (instance == null)
                        continue;
                    GeometryElement geometryElement = instance.GetInstanceGeometry();
                    if (geometryElement == null)
                        continue;
                    foreach (GeometryObject elem in geometryElement)
                    {
                        Solid solid = elem as Solid;
                        if (solid == null || solid.Volume.ToString() == "0")
                            continue;
                        FaceArray faceArray = solid.Faces;
                        foreach (Face face in faceArray)
                        {
                            info += "Face" + i + " 的面积: " + face.Area.ToString() + "\n";
                            i++;
                        }
                    }
                }
            }
            MessageBox.Show(info, "信息");
            return Result.Succeeded;
        }
    }
}

执行结果:

Revit开发 - 几何信息(GeometryElement)的获取_第3张图片

 

 

你可能感兴趣的:(Revit)