OpenCASCADE Make Face With Holes
OpenCASCADE提供了构造Face的类BRepBuilderAPI_MakeFace,使用这个类可以构造出带孔的面。如下图所示:
当然,要得到上图所示的结果,还可以使用Boolean操作,用一个面去Cut几个圆柱。当使用布尔操作就会涉及到一些复杂算法,如求交,重构Topo体等,比较耗时。既然可以直接在生成面的时候挖孔,这个不涉及复杂算法,速度、稳定性都比使用布尔操作要好。本文主要来介绍如何使用BRepBuilderAPI_MakeFace来生成带孔的面,及其注意事项。直接上代码:
#include <gp_Circ.hxx> #include <gp_Pln.hxx> #include <BRepBuilderAPI_MakeEdge.hxx> #include <BRepBuilderAPI_MakeWire.hxx> #include <BRepBuilderAPI_MakeFace.hxx> #include <BRepTools.hxx> #pragma comment(lib, \"TKernel.lib\") #pragma comment(lib, \"TKMath.lib\") #pragma comment(lib, \"TKG2d.lib\") #pragma comment(lib, \"TKG3d.lib\") #pragma comment(lib, \"TKGeomBase.lib\") #pragma comment(lib, \"TKGeomAlgo.lib\") #pragma comment(lib, \"TKBRep.lib\") #pragma comment(lib, \"TKTopAlgo.lib\") void makeFaceTest() { gp_Pln aPlane; gp_Circ aCircle1(gp::XOY(), 1.0); gp_Circ aCircle2(gp::XOY(), 1.0); gp_Circ aCircle3(gp::XOY(), 1.0); aCircle1.SetLocation(gp_Pnt(3.0, 3.0, 0.0)); aCircle2.SetLocation(gp_Pnt(7.0, 3.0, 0.0)); aCircle3.SetLocation(gp_Pnt(3.0, 7.0, 0.0)); BRepBuilderAPI_MakeEdge anEdgeMaker1(aCircle1); BRepBuilderAPI_MakeEdge anEdgeMaker2(aCircle2); BRepBuilderAPI_MakeEdge anEdgeMaker3(aCircle3); BRepBuilderAPI_MakeWire aWireMaker1(anEdgeMaker1.Edge()); BRepBuilderAPI_MakeWire aWireMaker2(anEdgeMaker2.Edge()); BRepBuilderAPI_MakeWire aWireMaker3(anEdgeMaker3.Edge()); BRepBuilderAPI_MakeFace aFaceMaker(aPlane, 0.0, 10.0, 0.0, 10.0); if (aWireMaker1.IsDone()) { aFaceMaker.Add(aWireMaker1.Wire()); } if (aWireMaker2.IsDone()) { aFaceMaker.Add(aWireMaker2.Wire()); } if (aWireMaker3.IsDone()) { aFaceMaker.Add(aWireMaker3.Wire()); } if (aFaceMaker.IsDone()) { BRepTools::Write(aFaceMaker.Shape(), \"d:/face.brep\"); } } int main(int argc, char* argv[]) { makeFaceTest(); return 0; }
上面代码就是在一个平面上开三个孔,最后在D盘生成face.brep文件。在Draw Test Harness中加载并显示这个文件得到:
当切换到线框显示模式时,和预期效果一致。但是当切换到着色显示模式时,发现生成的面和预期的效果刚好相反。这是什么原因呢?
在Draw Test Harness中输入命令pcruve来检查,如下图所示:
根据pcurve命令的提示得知,蓝色的方向应该反向,红色的为外环。所以得知,内环孔的方向反了。修改代码,直接将Wire的方向Reverse。修改后的代码如下:
#include <gp_Circ.hxx> #include <gp_Pln.hxx> #include <TopoDS_Wire.hxx> #include <BRepBuilderAPI_MakeEdge.hxx> #include <BRepBuilderAPI_MakeWire.hxx> #include <BRepBuilderAPI_MakeFace.hxx> #include <BRepTools.hxx> #pragma comment(lib, \"TKernel.lib\") #pragma comment(lib, \"TKMath.lib\") #pragma comment(lib, \"TKG2d.lib\") #pragma comment(lib, \"TKG3d.lib\") #pragma comment(lib, \"TKGeomBase.lib\") #pragma comment(lib, \"TKGeomAlgo.lib\") #pragma comment(lib, \"TKBRep.lib\") #pragma comment(lib, \"TKTopAlgo.lib\") void makeFaceTest() { gp_Pln aPlane; gp_Circ aCircle1(gp::XOY(), 1.0); gp_Circ aCircle2(gp::XOY(), 1.0); gp_Circ aCircle3(gp::XOY(), 1.0); aCircle1.SetLocation(gp_Pnt(3.0, 3.0, 0.0)); aCircle2.SetLocation(gp_Pnt(7.0, 3.0, 0.0)); aCircle3.SetLocation(gp_Pnt(3.0, 7.0, 0.0)); BRepBuilderAPI_MakeEdge anEdgeMaker1(aCircle1); BRepBuilderAPI_MakeEdge anEdgeMaker2(aCircle2); BRepBuilderAPI_MakeEdge anEdgeMaker3(aCircle3); BRepBuilderAPI_MakeWire aWireMaker1(anEdgeMaker1.Edge()); BRepBuilderAPI_MakeWire aWireMaker2(anEdgeMaker2.Edge()); BRepBuilderAPI_MakeWire aWireMaker3(anEdgeMaker3.Edge()); BRepBuilderAPI_MakeFace aFaceMaker(aPlane, 0.0, 10.0, 0.0, 10.0); if (aWireMaker1.IsDone()) { TopoDS_Wire aWire1 = aWireMaker1.Wire(); aWire1.Reverse(); aFaceMaker.Add(aWire1); } if (aWireMaker2.IsDone()) { TopoDS_Wire aWire2 = aWireMaker2.Wire(); aWire2.Reverse(); aFaceMaker.Add(aWire2); } if (aWireMaker3.IsDone()) { TopoDS_Wire aWire3 = aWireMaker3.Wire(); aWire3.Reverse(); aFaceMaker.Add(aWire3); } if (aFaceMaker.IsDone()) { BRepTools::Write(aFaceMaker.Shape(), \"d:/face.brep\"); } } int main(int argc, char* argv[]) { makeFaceTest(); return 0; }
重新加载新生成的face.brep并使用pcurve查看,得到如下图所示的效果:
从上图可知,这时得到的为预期的效果。综上所述,如果要直接使用BRepBuilderAPI_MakeFace来生成带有孔的面,需要自己为外环和内环的方向负责,opencascade对此不作检查。当面显示不正确时,可以使用Draw Test Harness的pcurve命令来检查。一个带孔的面的pcurve的规则为:外环为逆时针方向;内环孔的方向为顺时针方向。
基于直接生成的带孔的面,还可以进一步使用放样算法来造型,如拉伸,旋转等,如下图所示。这样就可以避免使用布尔操作,提高造型算法的性能和稳定性。
暂无评论内容