RevitAPI之获取元素的几何数据

来源:互联网 发布:数据冗余 编辑:程序博客网 时间:2024/05/21 06:50

几何实例表示了储存在Revit中默认配置的几何组合,通过各种变换到适当的位置成为一个元素的属性。


但是并不是所有的族实例都会有几何实例。当由于局部相连、相交、以及其他种种因素影响到实例位置时, 这种情况就不会有几何实例,而是用实体来表示几何。几何实例中有SymbolGeometry属性,该属性是生成这个几何实例的类型的几何表示, 它使用的是局部坐标系。

几何实例也提供了一个Transform属性,表示了从类型的局部坐 标系到实例的世界坐标系的坐标变换。

几何实例也提供了 GetInstanceGeometry()和GetSymbolGeometry()方法来分别获取其族实例的几何元素和族类型的几何元素。

—般来说,从几何实例中可以得到三种类型的数据:
•线(Curve):基类线或者子类的线。
•实体(Solid):包含了面和边。
•几何实例(Instance):另一个几何实例,用来构造本几何实例。

用户有时候需要使用Transform属性对取到的几何数据进行坐标变换。

获取几何数据有以下两种方法:

1、方法①:

首先从族实例中获取到它的几何元素,然后从几何元素中找到其几何实例,再从几何实例的SymbolGeometry属性中获取族元素类型的几何元素,从类型的几何元素中取到所有的线,然后进行坐标变换。

示例代码

void GetInstanceGeometry_Curve(Autodesk.Revit.DB.Document doc, Autodesk.Revit.ApplicationServices.Application app)        {            FamilyInstance familyInstance = doc.GetElement(new ElementId(187032)) as FamilyInstance;            Options option = GetGeometryOption(app);            Autodesk.Revit.DB.GeometryElement geomElement = familyInstance.get_Geometry(option);            foreach (GeometryObject geomObj in geomElement)            {                GeometryInstance geomInstance = geomObj as GeometryInstance;                if (null != geomInstance)                {                    foreach (GeometryObject instObj in geomInstance.SymbolGeometry)                    {                        Curve curve = instObj as Curve;                        if (null != curve)                        {                            // 把取到的线变换到实例的坐标系中                             curve = curve.CreateTransformed(geomInstance.Transform);                            // …                         }                    }                }            }        }        Options GetGeometryOption(Autodesk.Revit.ApplicationServices.Application app)        {            Autodesk.Revit.DB.Options option = app.Create.NewGeometryOptions();            option.ComputeReferences = true;      //打开计算几何引用             option.DetailLevel = ViewDetailLevel.Fine;      //视图详细程度为最好             return option;        }
注意:从一个族实例中找线是不常见的行为,如果需要从实例中拿到一根线,需要编辑该族,然后添加一根线。

2)方法②:
首先获取族元素类型的几何元素,步骤和方法1的一样。然后从类型的几何元素中取到它的实体,遍历实体中的所有面和边,把面转化为网格后对网格的顶点进行坐标变换;
同样,对获取到的边进行镶嵌细分后的点进行坐标变换。

示例代码:

Options GetGeometryOption(Autodesk.Revit.ApplicationServices.Application app)        {            Autodesk.Revit.DB.Options option = app.Create.NewGeometryOptions();            option.ComputeReferences = true;      //打开计算几何引用             option.DetailLevel = ViewDetailLevel.Fine;      //视图详细程度为最好             return option;        }        public void GetInstanceGeometry_Solid(Autodesk.Revit.DB.Document doc, Autodesk.Revit.ApplicationServices.Application app)        {            FamilyInstance familyInstance = doc.GetElement(new ElementId(187758)) as FamilyInstance;            Options option = GetGeometryOption(app);            Autodesk.Revit.DB.GeometryElement geomElement = familyInstance.get_Geometry(option);            foreach (GeometryObject geomObj in geomElement)            {                GeometryInstance geomInstance = geomObj as GeometryInstance;                if (null != geomInstance)                {                    foreach (GeometryObject instObj in geomInstance.SymbolGeometry)                    {                        Solid solid = instObj as Solid;                        if (null == solid || 0 == solid.Faces.Size || 0 == solid.Edges.Size)                        {                            continue;                        }                        Transform instTransform = geomInstance.Transform;                        // 从实体Solid获取面和边,然后对点进行变换                         foreach (Face face in solid.Faces)                        {                            Mesh mesh = face.Triangulate();                            foreach (XYZ ii in mesh.Vertices)                            {                                XYZ point = ii;                                XYZ transformedPoint = instTransform.OfPoint(point);                            }                        }                        foreach (Edge edge in solid.Edges)                        {                            foreach (XYZ ii in edge.Tessellate())                            {                                XYZ point = ii;                                XYZ transformedPoint = instTransform.OfPoint(point);                            }                        }                    }                }            }        }

注意:当获取门、窗和其他族元素的几何数据时,步驟是相似的。

=========【更多高级应用请关注公众号】========


===================================



阅读全文
0 0
原创粉丝点击