# Topology Shapes of OpenCascade BRep

eryar@163.com

## 一、引言 Introduction

Figure 1.1 BRep Shape demo

l 曲线的边界由位于曲线上的一对点来确定；

l 曲面的边界由位于曲面上的一组曲线来确定；

l 哪些点界定哪些曲线；

l 哪些曲线界定哪些曲面；

## 二、边界表示形状中的几何数据 Geometry of BRep shapes

```//=================================================================
//purpose  :
//=================================================================
{

if (S.ShapeType() == TopAbs_VERTEX) {

Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());

while (itrp.More()) {
const Handle(BRep_PointRepresentation)& PR = itrp.Value();

if (PR->IsPointOnCurve()) {
}

else if (PR->IsPointOnCurveOnSurface()) {
}

else if (PR->IsPointOnSurface()) {
}

itrp.Next();
}

}
else if (S.ShapeType() == TopAbs_EDGE) {

Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
BRep_ListIteratorOfListOfCurveRepresentation itrc(TE->Curves());

while (itrc.More()) {
const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
if (CR->IsCurve3D()) {
if (!CR->Curve3D().IsNull()) {
}
}
else if (CR->IsCurveOnSurface()) {
if (CR->IsCurveOnClosedSurface())
}
else if (CR->IsRegularity()) {
}
else if (myWithTriangles) { // for XML Persistence
if (CR->IsPolygon3D()) {
if (!CR->Polygon3D().IsNull()) {
}
}
else if (CR->IsPolygonOnTriangulation()) {
if (CR->IsPolygonOnClosedTriangulation())
}
else if (CR->IsPolygonOnSurface()) {
if (CR->IsPolygonOnClosedSurface())
}
}
itrc.Next();
}
}

else if (S.ShapeType() == TopAbs_FACE) {

Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());

if (myWithTriangles) { // for XML Persistence
Handle(Poly_Triangulation) Tr = TF->Triangulation();
}

}
}```

## 三、边界表示形状中的拓朴数据 Topology of BRep shapes

#### 3.1 顶点 TopoDS_Vertex

Figure 3.1.1 NBF-like definition of Vertex

<vertex data representation u parameter>u的使用方法说明如下：

<vertex data representation data 1> 和参数u定义了三维曲线C上的点V的位置。参数u是曲线C上点V对应的参数：C（u）＝V。对应的类是：BRep_PointOnCurve；

<vertex data representation data 2>和参数u定义了曲面上的二维曲线C上点V的位置。参数u是曲线C上点V对应的参数：C（u）＝V。对应的类是：BRep_PointOnCurveOnSurface；

<vertex data representation data 3>和参数u及<vertex data representation v parameter>v定义了曲面S上的点V：S（u，v）＝V。对应的类是：BRep_PointOnSurface；

<vertex data tolerance>t定义如下所示：

```void TestVertex(void)
{
ofstream dumpFile(\"vertex.brep\");

TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex(gp_Pnt(1.0, 2.0, 3.0));

BRepTools::Dump(aVertex, std::cout);
BRepTools::Write(aVertex, dumpFile);
}```

Figure 3.1.2 Import a Vertex from brep file

#### 3.2 边 TopoDS_Edge

<edge data representation data 1>表示一个三维曲线，对应类：Geom_Curve；

<edge data representation data 2>表示曲面上的一个二维曲线，

<curve values for parameter minimal and maximal values>只在2版本中使用；

<edge data representation data 3>表示闭合曲面上的一个二维曲线；

<curve values for parameter minimal and maximal values>只在2版本中使用；

<edge data representation data 4>表示Regularity的边，使用到的类有：

Geom_Curve/Geom_Surface；

<edge data representation data 5>表示一个三维的多段线（3D polyline）；

<edge data representation data 6>表示三角剖分上一条多段线；

<edge data tolerance> t的定义如下所示：

```void TestEdge(bool bSubdivision = false)
{
ofstream dumpFile(\"edge.brep\");

TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(gp_Circ(gp::XOY(), 6.0));

if (bSubdivision)
{
BRepMesh::Mesh(anEdge, 1.0);
}

BRepTools::Dump(anEdge, std::cout);
BRepTools::Write(anEdge, dumpFile);
}```

Figure 3.2.1 Import a Face from brep file

#### 3.3 面 TopoDS_Face

<face data>描述了面F的曲面S和三角剖分T。曲面S可能为空：<surface number>＝0.

<face data tolerance> t的定义如下所示：

```void TestFace(bool bSubdivision = false)
{
ofstream dumpFile(\"face.brep\");

TopoDS_Face aFace = BRepBuilderAPI_MakeFace(gp_Sphere(gp::XOY(), 6.0));

if (bSubdivision)
{
BRepMesh::Mesh(aFace, 1.0);
}

BRepTools::Dump(aFace, std::cout);
BRepTools::Write(aFace, dumpFile);
}```

Figure 3.3.1 Import a Face from brep

## 四、示例程序 Example Code

```/*
*
*           File : Main.cpp
*         Author : eryar@163.com
*           Date : 2013-12-21 21:18
*        Version : 1.0v
*
*    Description : Use BRepTools to dump and write BRep files.
*
*      Key Words : OpenCascade, BRep, Vertex, Edge, Face
*
*/

#define WNT
#include <gp_Pnt.hxx>
#include <gp_Circ.hxx>
#include <gp_Sphere.hxx>

#include <TopoDS_Vertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>

#include <BRepMesh.hxx>
#include <BRepTools.hxx>

#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>

#pragma comment(lib, \"TKernel.lib\")
#pragma comment(lib, \"TKMath.lib\")
#pragma comment(lib, \"TKBRep.lib\")
#pragma comment(lib, \"TKMesh.lib\")
#pragma comment(lib, \"TKTopAlgo.lib\")

void TestVertex(void)
{
ofstream dumpFile(\"vertex.brep\");

TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex(gp_Pnt(1.0, 2.0, 3.0));

BRepTools::Dump(aVertex, std::cout);
BRepTools::Write(aVertex, dumpFile);
}

void TestEdge(bool bSubdivision = false)
{
ofstream dumpFile(\"edge.brep\");

TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(gp_Circ(gp::XOY(), 6.0));

if (bSubdivision)
{
BRepMesh::Mesh(anEdge, 1.0);
}

BRepTools::Dump(anEdge, std::cout);
BRepTools::Write(anEdge, dumpFile);
}

void TestFace(bool bSubdivision = false)
{
ofstream dumpFile(\"face.brep\");

TopoDS_Face aFace = BRepBuilderAPI_MakeFace(gp_Sphere(gp::XOY(), 6.0));

if (bSubdivision)
{
BRepMesh::Mesh(aFace, 1.0);
}

BRepTools::Dump(aFace, std::cout);
BRepTools::Write(aFace, dumpFile);
}

int main(void)
{
TestVertex();

TestEdge(true);

TestFace(true);

return 0;
}```

```Shape : 7, FORWARD

Dump of 7 TShapes

-----------------

Flags : Free, Modified, Checked, Orientable, Closed, Infinite, Convex

TShape # 1 : FACE      1101000 01807868
+2
NaturalRestriction
Tolerance : 1e-007
- Surface : 1

TShape # 2 : WIRE      0101100 01807CE8
-5 +4 +5 -3

TShape # 3 : EDGE      0101000 01808C10
+6 -6
Tolerance : 1e-007
same parametrisation of curves
same range on curves
degenerated
- PCurve : 4 on surface 1, range : 0 6.28319
UV Points : 0, 1.5708 6.28319, 1.5708

TShape # 4 : EDGE      0101000 01807AA0
+7 -7
Tolerance : 1e-007
same parametrisation of curves
same range on curves
degenerated
- PCurve : 3 on surface 1, range : 0 6.28319
UV Points : 0, -1.5708 6.28319, -1.5708

TShape # 5 : EDGE      0101000 018078C8
+7 -6
Tolerance : 1e-007
same parametrisation of curves
same range on curves
- Curve 3D : 1, range : -1.5708 1.5708
- PCurve : 1, 2 (C0) on surface 1, range : -1.5708 1.5708
UV Points : 6.28319, -1.5708 6.28319, 1.5708
UV Points : 0, -1.5708 0, 1.5708

TShape # 6 : VERTEX    0101101 018076F0

Tolerance : 1e-007
- Point 3D : 3.67394e-016, 0, 6

TShape # 7 : VERTEX    0101101 01807680

Tolerance : 1e-007
- Point 3D : 3.67394e-016, 0, -6

-------
Dump of 4 Curve2ds
-------

1 : Line
Origin :6.28319, 0
Axis   :0, 1

2 : Line
Origin :0, 0
Axis   :0, 1

3 : Line
Origin :0, -1.5708
Axis   :1, 0

4 : Line
Origin :0, 1.5708
Axis   :1, 0

-------
Dump of 1 Curves
-------

1 : Trimmed curve
Parameters : 4.71239 7.85398
Basis curve :
Circle
Center :0, 0, 0
Axis   :0, -1, 0
XAxis  :1, 0, 0
YAxis  :-0, 0, 1

-------
Dump of 0 Polygon3Ds
-------
-------
Dump of 0 PolygonOnTriangulations
-------

-------
Dump of 1 surfaces
-------

1 : SphericalSurface
Center :0, 0, 0
Axis   :0, 0, 1
XAxis  :1, 0, -0
YAxis  :-0, 1, 0

-------
Dump of 0 Triangulations
-------

-------
Dump of 0 Locations
-------```

```//=================================================================//function : D0
//purpose  :
//=================================================================
void BRep_CurveOnSurface::D0(const Standard_Real U, gp_Pnt& P) const
{
// shoud be D0 NYI
gp_Pnt2d P2d = myPCurve->Value(U);
P = mySurface->Value(P2d.X(),P2d.Y());
P.Transform(myLocation.Transformation());
}```

## 六、参考资料 References

4. BRep Format Description White Paper of OpenCascade

5. 孙家广等. 计算机图形学. 清华大学出版社, 2000

6. 詹海生等, 基于ACIS的几何造型技术与系统开发, 清华大学出版社, 2002