OpenCASCADE中获取曲面上的曲线(如边界)的方法和注意点
转载请注明出处 ltr199010@163.com
本人在处理计算几何一些算法时,由于模型有时是由其他软件建模,再读入的,所以经常会遇到获取曲面上的曲线(如边界)以及参数域2D曲线和空间3D曲线之间的转化等问题。今天有人来问我相关的问题,想着还是总结分享一下吧。
1. 根据 TopoDS_Face 类型的面,获取 TopoDS_Edge 类型的边界。
利用 TopExp_Explorer 类,可以探索一个拓扑对象中包含的下级拓扑对象,找到Face中的Wire,再找Wire中的Edge。具体代码实现如下。
1 TopoDS_Face aFace, 2 for (TopExp_Explorer wireExp(aFace, TopAbs_WIRE); wireExp.More(); wireExp.Next()){ 3 for (TopExp_Explorer edgeExp(wireExp.Current(), TopAbs_EDGE); edgeExp.More(); edgeExp.Next()){ 4 TopoDS_Edge anEdge = TopoDS::Edge(edgeExp.Current()); 5 // Next step 6 } 7 }
如果你需要的只是 TopoDS_Edge 类型的边界,那么到此为止。如果同时你还需要这条曲线的参数域2D曲线的信息,那么继续往下做。
2. 根据 TopoDS_Face 类型的面和 TopoDS_Edge 类型的边界,生成 Handle(Geom2d_BSplineCurve) 类型的曲面参数域上的2D曲线。
利用 BRep_Tool::CurveOnSurface 函数获取 Handle(Geom2d_Curve) 类型的曲面参数域曲线,再利用 Geom2dConvert::CurveToBSplineCurve 函数将其转换为我们更常用的 Handle(Geom2d_BSplineCurve) 类型。具体代码实现如下。
该步的注意点:如果原来的曲线是某曲线裁剪得到的,那么转出来的 Handle(Geom2d_Curve) 类型的曲线是完整的曲线,需要利用首尾参数再人为裁剪一次。这可能是OCC的一个bug,后期版本也许会修正。
1 double aFirst; 2 double aLast; 3 // get the 2d curve of the edge on the face 4 Handle(Geom2d_Curve) aCurve2d = BRep_Tool::CurveOnSurface(anEdge, aFace, aFirst, aLast); 5 6 Handle(Geom2d_BSplineCurve) aPCurve; 7 // if the curve does not exist 8 if (aCurve2d == NULL){ 9 fprintf(stderr, "The curve does not exist.\n"); 10 } 11 // 12 else{ 13 fprintf(stderr, "The curve is an infinite line.\n"); 14 // trim the curve by the first and last parameter 15 Handle(Geom2d_TrimmedCurve) aTrimmedCurve = new Geom2d_TrimmedCurve(aCurve2d, aFirst, aLast); 16 17 aPCurve = Geom2dConvert::CurveToBSplineCurve(aTrimmedCurve); 18 }
3. 重新生成3D曲线
这里我们利用B样条曲面和获取到的参数域上的2DB样条曲线来生成3D曲线。这么做有什么好处呢?在第一步中获得的Edge只包含了空间曲线的信息,如果你的程序需要对参数域上的曲线进行操作,那么这一步就能把参数域2D曲线和Edge联系起来了。具体代码实现如下。
该步的注意点:BRepLib::BuildCurve3d这个函数一定要执行,这样你的Edge里面才会有3d的曲线。这样这条曲线才可以被显示。
1 Handle(Geom_BSplineSurface) aSurface; 2 Handle(Geom2d_BSplineCurve) aPCurve; 3 // use the surface and the 2d parameter curve to create a TopoDS_Edge 4 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPCurve, aSurface).Edge(); 5 // create 3d edge 6 // this line is significance, otherwise the edge can not be displayed 7 BRepLib::BuildCurve3d(anEdge);

浙公网安备 33010602011771号