#region 获得元素的所有面
public List<Face> GetGeoFaces(Element ele)
{
//存放集合元素的所有面
List<Face> geoFaces = new List<Face>();
//用上一节的方法取得所有几何体solid
List<Solid> solids = GetSolidsOfElement(ele);
//从集合体重提取所有face,存进集合
foreach (Solid solid in solids)
{
foreach (Face face in solid.Faces)
{
if (face.Area > 0)
geoFaces.Add(face);
}
}
return geoFaces;
}
#endregion
#region GetSolidsOfElement:从element里面获取实体的方法
public List<Solid> GetSolidsOfElement(Element ele)
{
//生成事件,指定返回数据的特征
Options options = new Options();
options.DetailLevel = ViewDetailLevel.Fine;
options.ComputeReferences = true;
options.IncludeNonVisibleObjects = true;
//取得构件元素
GeometryElement geoElement = ele.get_Geometry(options);
List<GeometryObject> geoObj = new List<GeometryObject>();
//递归获取集合元素的所有geometryobject
GetAllObj(geoElement, ref geoObj);
//转为solid的集合
List<Solid> solids = geoObj.ConvertAll(m => m as Solid);
return solids;
}
#endregion
#region GetAllObj获得geometry的方法
//获得geometryobject的递归算法
public void GetAllObj(GeometryElement gele, ref List<GeometryObject> gobjs)
{
if (gele == null)
{
return;
}
//遍历geometryelement里面的geometryobject
IEnumerator<GeometryObject> enumerator = gele.GetEnumerator();
while (enumerator.MoveNext())
{
GeometryObject geoObject = enumerator.Current;
Type type = geoObject.GetType();
//如果是嵌套的GeometryElement
if (type.Equals(typeof(GeometryElement)))
{
//则递归
GetAllObj(geoObject as GeometryElement, ref gobjs);
}
//如果嵌套的geometryinstance
else if (type.Equals(typeof(GeometryInstance)))
{
//则用getinstancegeometry取得其中的geometryelement再递归
GetAllObj((geoObject as GeometryInstance).GetInstanceGeometry(), ref gobjs);
}
//如果是solid,则存入集合,递归结束
else
{
if (type.Equals(typeof(Solid)))
{
Solid solid = geoObject as Solid;
//去掉可能存在的空Solid
if (solid.Faces.Size > 0 || solid.Edges.Size > 0)
{
gobjs.Add(geoObject);
}
}
}
}
}
#endregion