边界表示法(Boundary Representation,简称B-Rep)是几何造型中最成熟、无二义的表示法。它主要用于描述物体的几何信息和拓扑信息。在边界表示法中,一个实体(Solid)由一组封闭的面(Face)组成,而每个面又由其所在的曲面定义加上其边界来表示。面的边界是边的并集,而边又是由点来表示的。
边界表示法中的拓扑信息描述形体上的顶点(Vertex)、边(Edge)、面(Face)的连接关系,形成物体边界表示的“骨架”。拓扑的目的在于描述对象的局限性和连接关系,比如物体的边界以及对象之间的连接(通过公共边界)。例如,相交、相邻、相切、垂直、平行等关系都属于拓扑信息。
TopAbs 包含了 TopAbs_ShapeEnum 枚举类型,列举了不同的拓扑种类,包括COMPOUND(一组任意类型的拓扑物体)、COMPSOLID(复合刚体)、SOLID(由壳限制的空间的一部分)、SHELL(由边相连的面集)、FACE(在2D它是一个平面的一部分;在3D它是一个曲面的一部分)、WIRE(由顶点连接的边的集合)、EDGE(一个与被约束的曲线对应的拓扑元素)以及VERTEX(一个与一个点对应的拓扑元素)。这些拓扑类型形成了一个从复杂到简单的层次结构,因为复杂的物体在其表述中可以包含更简单的物体。
#include <Geom_CylindricalSurface.hxx>
#include <gp_Ax3.hxx>
#include <GeomAPI_Interpolate.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <GCE2d_MakeSegment.hxx>#include <GeomAPI_PointsToBSpline.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <GC_MakeCircle.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepOffsetAPI_MakePipe.hxx>
#include <GC_MakeArcOfCircle.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <TopExp_Explorer.hxx>
#include <gp_GTrsf.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <GC_MakeSegment.hxx>
#include <IntAna2d_AnaIntersection.hxx>
#include <TopoDS.hxx>
#include <BRepPrimAPI_MakeRevol.hxx>#include"Viewer.h"TopoDS_Shape createGrindingwheel2()
{Standard_Real Line1_angle = 280 * M_PI / 180;Standard_Real Line1_length = 0.5031;Standard_Real Line2_angle = 236 * M_PI / 180;Standard_Real Line2_length = 0.5925;Standard_Real Arc1_r = 0.112;Standard_Real Arc1_angle = (180 + 10 + 50) * M_PI / 180;gp_Pnt Line1_p1(-0.6822 / 2, 0, 0);gp_Pnt Line2_p1(0.6822 / 2, 0, 0);gp_Lin Line1(Line1_p1, gp_Dir(cos(Line1_angle), sin(Line1_angle), 0.));gp_Lin Line2(Line2_p1, gp_Dir(cos(Line2_angle), sin(Line2_angle), 0.));Handle(Geom_TrimmedCurve) L1 = GC_MakeSegment(Line1, 0., Line1_length);TopoDS_Edge L1e = BRepBuilderAPI_MakeEdge(L1);Handle(Geom_TrimmedCurve) L2 = GC_MakeSegment(Line2, 0., Line2_length);TopoDS_Edge L2e = BRepBuilderAPI_MakeEdge(L2);gp_Pnt l1end = L1->EndPoint();gp_Pnt l2end = L2->EndPoint();gp_Lin Line1v(l1end, gp_Dir(cos(Line1_angle + M_PI_2), sin(Line1_angle + M_PI_2), 0.));gp_Lin2d Line2v(gp_Pnt2d(l2end.X(), l2end.Y()), gp_Dir2d(cos(Line2_angle - M_PI_2), sin(Line2_angle - M_PI_2)));gp_Lin Line2v3d(l2end, gp_Dir(cos(Line2_angle - M_PI_2), sin(Line2_angle - M_PI_2), 0.));Handle(Geom_TrimmedCurve) L1v = GC_MakeSegment(Line1v, 0., Arc1_r);gp_Pnt l1vend = L1v->EndPoint();gp_Circ c1(gp_Ax2(l1vend, gp_Dir(0, 0, 1)), Arc1_r);Handle(Geom_TrimmedCurve) c1c = GC_MakeArcOfCircle(c1, l1end, Arc1_angle, 1);gp_Pnt c1end = c1c->EndPoint();gp_Lin2d Line3(gp_Pnt2d(c1end.X(), c1end.Y()), gp_Dir2d(l2end.X() - c1end.X(), l2end.Y() - c1end.Y()));gp_Lin2d Line3v = Line3.Normal(gp_Pnt2d((l2end.X() + c1end.X()) / 2, (l2end.Y() + c1end.Y()) / 2));IntAna2d_AnaIntersection aIntAna;aIntAna.Perform(Line2v, Line3v);IntAna2d_IntPoint aIntPoint = aIntAna.Point(1);gp_Pnt o2(aIntPoint.Value().X(), aIntPoint.Value().Y(), 0.);Handle(Geom_TrimmedCurve) L2v = GC_MakeSegment(Line2v3d, l2end, o2);Standard_Real r2 = L2v->LastParameter();gp_Circ c2(gp_Ax2(o2, gp_Dir(0, 0, 1)), r2);Handle(Geom_TrimmedCurve) c2c = GC_MakeArcOfCircle(c2, c1end, l2end, 0);gp_Pnt c2low = c2c->Value(M_PI_2);TopoDS_Edge c1ce = BRepBuilderAPI_MakeEdge(c1c);TopoDS_Edge L1ev = BRepBuilderAPI_MakeEdge(L1v);TopoDS_Edge c2ce = BRepBuilderAPI_MakeEdge(c2c);gp_Pnt Line1_up(-0.9832 / 2, 5, 0);gp_Pnt Line2_up(0.9832 / 2, 5, 0);TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(Line1_p1, Line1_up);TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(Line1_up, Line2_up);TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(Line2_up, Line2_p1);TopTools_ListOfShape listEdge;listEdge.Append(anEdge1);listEdge.Append(anEdge2);listEdge.Append(anEdge3);listEdge.Append(L1e);listEdge.Append(c1ce);listEdge.Append(c2ce);listEdge.Append(L2e);BRepBuilderAPI_MakeWire mw;mw.Add(listEdge);mw.Build();TopoDS_Shape gwheel = BRepPrimAPI_MakeRevol(mw, gp_Ax1(gp_Pnt(0, 5, 0), gp_Dir(1, 0, 0)), 2 * M_PI);//平移:gp_Trsf theTransformation1;gp_Vec theVectorOfTranslation1(-c2low.X(), -c2low.Y(), 0.);theTransformation1.SetTranslation(theVectorOfTranslation1);BRepBuilderAPI_Transform myBRepTransformation1(gwheel, theTransformation1);TopoDS_Shape outzero = myBRepTransformation1.Shape();gp_Trsf theTransformation2;gp_Vec theVectorOfTranslation2(0., 0.125 / 2, 0.);theTransformation2.SetTranslation(theVectorOfTranslation2);//绕一个轴旋转:gp_Trsf theTransformation3;gp_Ax1 axez = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0., 0., 1.));theTransformation3.SetRotation(axez, -90 * M_PI / 180);gp_Trsf theTransformation4;gp_Ax1 axex = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(1., 0., 0.));theTransformation4.SetRotation(axex, -50 * M_PI / 180);BRepBuilderAPI_Transform myBRepTransformation(outzero, theTransformation4 * theTransformation3 * theTransformation2);TopoDS_Shape TransformedShape = myBRepTransformation.Shape();return TransformedShape;
}int main(int argc, char* argv[])
{gp_Dir Z(0.0, 0.0, 1.0);gp_Pnt center(0, 0, 0.0);gp_Pnt xr(0.5, 0, 0.0);gp_Pnt yr(0.0, 1.0, 0.0);gp_Pnt zr(0.0, 0.0, 7.0);gp_Ax2 wb(center, Z);gp_Circ wbcircle(wb, 0.125 / 2);TopoDS_Edge wbe = BRepBuilderAPI_MakeEdge(wbcircle);TopoDS_Edge xline = BRepBuilderAPI_MakeEdge(center, xr);TopoDS_Edge yline = BRepBuilderAPI_MakeEdge(center, yr);TopoDS_Edge zline = BRepBuilderAPI_MakeEdge(center, zr);//creat a profile of gringing wheelTopoDS_Shape gw = createGrindingwheel2();TopExp_Explorer anExp1(gw, TopAbs_SOLID);TopExp_Explorer anExp2(gw, TopAbs_SHELL);TopExp_Explorer anExp3(gw, TopAbs_FACE);TopExp_Explorer anExp4(gw, TopAbs_WIRE);TopExp_Explorer anExp5(gw, TopAbs_EDGE);int i = 0;Viewer vout(50, 50, 500, 500);for (; anExp1.More(); anExp1.Next()){TopoDS_Solid anSolid = TopoDS::Solid(anExp1.Current());i++;}std::cout << "numbers of TopoDS_Solid:" << i << std::endl;i = 0;for (; anExp2.More(); anExp2.Next()){TopoDS_Shell anShell = TopoDS::Shell(anExp2.Current());i++;}std::cout << "numbers of TopoDS_Shell:" << i << std::endl;i = 0;for (; anExp3.More(); anExp3.Next()){TopoDS_Face anFace = TopoDS::Face(anExp3.Current());i++;}std::cout << "numbers of TopoDS_Face:" <<i<< std::endl;i = 0;for (; anExp4.More(); anExp4.Next()){TopoDS_Wire anWire = TopoDS::Wire(anExp4.Current());i++;}std::cout << "numbers of TopoDS_Wire:" << i << std::endl;i = 0;for (; anExp5.More(); anExp5.Next()){TopoDS_Edge anEdge = TopoDS::Edge(anExp5.Current());i++;}std::cout << "numbers of TopoDS_Edge:" << i << std::endl;vout << wbe;vout << xline;vout << yline;vout << zline;vout << gw;vout.StartMessageLoop();return 0;
}
numbers of TopoDS_Solid:0
numbers of TopoDS_Shell:1
numbers of TopoDS_Face:6
numbers of TopoDS_Wire:6
numbers of TopoDS_Edge:24