首頁>技術>

如何在revit中得到一個專案的三角面片座標資料?以生成這個桌子為例子。

文章開始前,先看下gif效果圖:

1.打開個Revit專案,該專案就上圖一張桌子,執行下面程式將會生成一個檔案,位置在:E:\gra.txt

/// <summary>/// 三角面片/// </summary>public void DrawMesh(){    StreamWriter sw = new StreamWriter(@"E:\gra.txt", false);    var familiyInstances = this.Document.GetFamilyInstanceList();    foreach (var familyInstance in familiyInstances)    {        GeometryElement geoElements = familyInstance.get_Geometry(new Options        {            //開啟計算幾何引用            ComputeReferences = true,            //檢視詳細程度為最好            DetailLevel = ViewDetailLevel.Fine,            IncludeNonVisibleObjects = true        });        foreach (GeometryInstance geometryInstance in geoElements)        {            GeometryElement symbolGeometry = geometryInstance.SymbolGeometry;            foreach (var item in symbolGeometry)            {                if (item is Solid solid)                {                    var faces = solid.Faces;                    //迴圈每個面                    foreach (Face face in faces)                    {                        // 對每個面進行三角面片化                        Mesh mesh = face.Triangulate();                        for (int i = 0; i < mesh.NumTriangles; i++)                        {                            string txt = "";                            MeshTriangle triangular = mesh.get_Triangle(i);                            //每個三角形都是3個頂點,所以迴圈3                            for (int n = 0; n < 3; n++)                            {                                XYZ point = triangular.get_Vertex(n);                                txt += point.ToString();                            }                            sw.WriteLine(txt);                        }                    }                }                else if (item is Autodesk.Revit.DB.Line line)                {                }                else                {                    var type = item.GetType().ToString();                }            }        }    }    sw.Flush();    sw.Close();    sw.Dispose();}

2.生成的檔案內容如下,本檔案內容僅作示例:

3.然後讀取該三角面片的資料,您可以在其它軟體重新創建出這張桌子。下圖左邊綠色的桌子,是根據第2步的各點座標還原生成的。

4.生成該綠色桌子,本示例用的是圖石軟體,它的生成程式碼如下所示。本還原示例僅供參考,您應根據自己可以用的軟體進行還原。原理都是一樣的。

public void CreateGra(){    Segment segment = viewer.ModelSegment.Subsegment("test");    List<GraStone.Math.Geometry.Point> points = new List<GraStone.Math.Geometry.Point>();    List<int> indices = new List<int>();    var contents = File.ReadAllLines(@"E:\gra.txt");    foreach (var line in contents)    {        int one = -1, two = -1, three = -1;        var coordinates = line.Split(new string[] { ")(" }, StringSplitOptions.RemoveEmptyEntries);        for (var i = 0; i < 3; i++)        {            var item = coordinates[i];            var array = item.Trim(')', '(').Split(',');            var x = Convert.ToDouble(array[0]);            var y = Convert.ToDouble(array[1]);            var z = Convert.ToDouble(array[2]);            var point = new GraStone.Math.Geometry.Point(x, y, z);            points.Add(point);            var index = points.Count - 1;            if (i == 0)            {                one = index;            }            else if (i == 1)            {                two = index;            }            else if (i == 2)            {                three = index;            }        }        indices.Add(3);        indices.Add(one);        indices.Add(two);        indices.Add(three);    }                segment.InsertShell(points, indices);    viewer.UpdateDisplay();}

祝您用餐愉快。

21
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • Jsp相關技術常見面試題