这个例子来源于Revit SDK samples中的NewRebar例子,例子中有一个GeometrySupport类,用于判断你选择的梁或者柱是否是正方形,这个类涉及到的知识点非常多,非常有趣,主要可以概况为:
1、获取梁柱的轮廓面Face信息
2、如何判断一个edge是否是一条线
3、如何进行局部全局的坐标变换
4、如何判断两个向量方向同向或者异向
5、如何判断一条线与面是否垂直
6、判断两个点是否相同
类GeometrySupport的主要逻辑:
public GeometrySupport(FamilyInstance element)
{
// get the geometry element of the ed element
// 获取元素的几何,判断是否存在几何元素
Autodesk.Revit.DB.GeometryElement geoElement = element.get_Geometry(new Options());
IEnumerator Objects = geoElement.GetEnumerator();
if (null == geoElement || !Objects.MoveNext())
{
throw new Exception("Can't get the geometry of ed element.");
}
// 获取元素的轮廓信息
SweptProfile swProfile = element.GetSweptProfile();
// 判断路径是否是直线
if (swProfile == null || !(swProfile.GetDrivingCurve() is Line))
{
throw new Exception("The ed element driving curve is not a line.");
}
// get the driving path and vector of the beam or column
Line line = swProfile.GetDrivingCurve() as Line;
if (null != line)
{
m_drivingLine = line; // driving path 路径
// 根据两个点求一个方向向量,末尾点减去初始点
m_drivingVector = GeomUtil.SubXYZ(line.GetEndPoint(1), line.GetEndPoint(0));
m_drivingLength = m_drivingVector.GetLength();
}
//get the geometry object
//foreach (GeometryObject geoObject in geoElement.Objects)
Objects.Reset();
while (Objects.MoveNext())
{
GeometryObject geoObject = Objects.Current;
//get the geometry instance which contains the geometry information
GeoInstance instance = geoObject as GeoInstance;
if (null != instance)
{
//foreach (GeometryObject o in instance.SymbolGeometry.Objects)
// 都是从SymbolGeometry这个属性拿到具体的几何对象
IEnumerator Objects1 = instance.SymbolGeometry.GetEnumerator();
while (Objects1.MoveNext())
{
GeometryObject o = Objects1.Current;
// get the solid of beam of column
Solid solid = o as Solid;
// do some checks.
if (null == solid)
{
continue;
}
if (0 == solid.Faces.Size || 0 == solid.Edges.Size)
{
continue;
}
m_solid = solid;
//get the transform value of instance
// 这个是GeometryObject的局部坐标系的转换矩阵
m_transform = instance.Transform;
// Get the swept profile curves information
// 获取轮廓信息
if (!GetSweptProfile(solid))
{
throw new Exception("Can't get the swept profile curves.");
}
break;
}
}
}
// do some checks about profile curves information
if (null == m_edges)
{
throw new Exception("Can't get the geometry edge information.");
}
// 通过四个点个数来判断是否是方形
if (4 != m_points.Count)
{
throw new Exception("The sample only works for rectangle beam or column.");
}
}
如何获取拉伸的轮廓信息:
主要是根据传入的solid信息来获取拉伸面Face的信息
private Face GetSweptProfileFace(Solid solid)
{
// Get a point on the swept profile from all points in solid
Autodesk.Revit.DB.XYZ refPoint = new Autodesk.Revit.DB.XYZ(); // the point on swept profile
// 遍历solid的每一条边edge
foreach (Edge edge in solid.Edges)
{
// 如果判断edge是一条线,先离散,再判断点个数是否是2个
List points = edge.Tessellate() as List; //get end points of the edge<
文章来源:https://www.bimsq.com