OpenCASCADE 7.7.0 完全教程(二):BRep 数据结构详解
# OpenCASCADE 7.7.0 完全教程(二):BRep 数据结构详解
BRep 核心概念
BRep(Boundary Representation,边界表示法)是 OCCT 的核心数据结构。
几何与拓扑的关系
| 几何 (Geometry) | 拓扑 (Topology) |
|---|---|
| 数学描述(曲线/曲面) | 关系绑定(顶点/边/面) |
| 例如:圆心 (0,0,0) 半径 10 | 例如:这条边是圆形的 |
拓扑元素层次
Compound (复合体)
└─ CompSolid (复合实体)
└─ Solid (实体)
└─ Shell (壳)
└─ Face (面)
├─ Wire (外轮廓)
│ └─ Edge (边)
│ └─ Vertex (顶点)
└─ Wire (内孔)
各元素说明
- Vertex (顶点): 0 维元素,空间中的一个点
- Edge (边): 1 维元素,由曲线和两个顶点定义
- Wire (线框): 多条边连接成的轮廓
- Face (面): 2 维元素,由线框边界和曲面定义
- Shell (壳): 多个面组成的集合
- Solid (实体): 闭合壳包围的 3D 体积
- Compound (复合体): 任意形状的组合
几何类型
曲线类型
- 直线 (Geom_Line)
- 圆 (Geom_Circle)
- 椭圆 (Geom_Ellipse)
- NURBS 曲线 (Geom_BSplineCurve)
- 贝塞尔曲线 (Geom_BezierCurve)
曲面类型
- 平面 (Geom_Plane)
- 圆柱面 (Geom_CylindricalSurface)
- 圆锥面 (Geom_ConicalSurface)
- 球面 (Geom_SphericalSurface)
- NURBS 曲面 (Geom_BSplineSurface)
代码示例
创建顶点
#include <gp_Pnt.hxx>
#include <TopoDS_Vertex.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
// 创建点 (0, 0, 0)
gp_Pnt p(0, 0, 0);
TopoDS_Vertex v = BRepBuilderAPI_MakeVertex(p);
创建边(直线)
#include <gp_Pnt.hxx>
#include <TopoDS_Edge.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
// 创建从 (0,0,0) 到 (10,0,0) 的直线边
gp_Pnt p1(0, 0, 0);
gp_Pnt p2(10, 0, 0);
TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(p1, p2);
创建圆形边
#include <gp_Circ.hxx>
#include <Geom_Circle.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
// 创建半径为 5 的圆
gp_Ax2 axis(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1));
gp_Circ circ(axis, 5);
TopoDS_Edge circle = BRepBuilderAPI_MakeEdge(circ);
创建面
#include <gp_Pln.hxx>
#include <Geom_Plane.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
// 创建 XY 平面
gp_Pln plane(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1));
TopoDS_Face face = BRepBuilderAPI_MakeFace(plane);
创建立方体
#include <BRepPrimAPI_MakeBox.hxx>
// 创建 10x20x30 的立方体
TopoDS_Solid box = BRepPrimAPI_MakeBox(10, 20, 30);
// 验证
if (box.IsNull()) {
std::cerr << "创建失败" << std::endl;
}
BRep 文件格式
OCCT 原生 BREP 格式示例:
DBRep_DrawableShape
CASCADE Topology V1, (c) Open Cascade
Locations 1
1
1 0 0 0
0 1 0 0
0 0 1 0
Curve2ds 1
1 0 0 1 0
Curves 1
1 0 0 0 0 0 1
Polygon3D 0
PolygonOnTriangulations 0
Surfaces 1
1 0 0 0 0 0 1 0 1 0 1 0
Triangulations 0
TShapes 7
Ve
1e-07 1
0 0 0
01000000
...
拓扑查询示例
遍历实体的所有面
#include <TopExp_Explorer.hxx>
#include <TopAbs_ShapeEnum.hxx>
TopExp_Explorer exp(solid, TopAbs_FACE);
while (exp.More()) {
TopoDS_Face face = TopoDS::Face(exp.Current());
// 处理面
exp.Next();
}
总结
BRep 是 OCCT 的核心,理解几何与拓扑的关系是掌握 OCCT 的关键。
字数:约 1800 字 | 分类:OpenCASCADE 教程 | 难度:入门 | 版本:7.7.0