|  | 
 
 
 楼主|
发表于 2024-11-1 09:09:01
|
显示全部楼层 
| #include <BRepBuilderAPI_MakeFace.hxx> #include <BRepBuilderAPI_MakeSolid.hxx>
 #include <BRepBuilderAPI_Sewing.hxx>
 #include <BRep_Builder.hxx>
 #include <StlAPI_Reader.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Compound.hxx>
 #include <TopoDS_Face.hxx>
 #include <TopoDS_Shell.hxx>
 #include <TopoDS_Solid.hxx>
 #include <TopExp_Explorer.hxx>
 #include <Poly_Triangulation.hxx>
 #include <Poly_Triangle.hxx>
 #include <TopoDS_Wire.hxx>
 #include <TopTools_ListOfShape.hxx>
 #include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
 
 int main()
 {
 // 1. 读取 STL 文件
 TopoDS_Shape shape;
 StlAPI_Reader reader;
 if (!reader.Read(shape, "example.stl")) {
 std::cerr << "Failed to read STL file." << std::endl;
 return -1;
 }
 
 // 2. 创建缝合工具,准备将面组合为实体
 BRepBuilderAPI_Sewing sewingTool;
 TopTools_ListOfShape facesList;
 
 // 3. 将 STL 的三角形网格转换为面并添加到缝合工具
 TopExp_Explorer explorer(shape, TopAbs_FACE);
 while (explorer.More()) {
 TopoDS_Face face = TopoDS::Face(explorer.Current());
 sewingTool.Add(face);
 facesList.Append(face);
 explorer.Next();
 }
 
 // 4. 执行缝合操作,将面缝合成闭合形状
 sewingTool.Perform();
 
 // 5. 获取缝合后的形状
 TopoDS_Shape sewedShape = sewingTool.SewedShape();
 
 // 6. 尝试将缝合后的形状转换为实体
 TopoDS_Solid solid;
 BRepBuilderAPI_MakeSolid solidMaker;
 
 TopExp_Explorer shellExplorer(sewedShape, TopAbs_SHELL);
 while (shellExplorer.More()) {
 TopoDS_Shell shell = TopoDS::Shell(shellExplorer.Current());
 solidMaker.Add(shell);
 shellExplorer.Next();
 }
 
 solid = solidMaker.Solid();
 
 if (solid.IsNull()) {
 std::cerr << "Failed to create solid from STL file." << std::endl;
 return -1;
 } else {
 std::cout << "Successfully converted STL to solid!" << std::endl;
 }
 
 // 7. 将 solid 输出或进行进一步处理
 // 例如,显示或保存为 BREP/STEP 格式
 BRepTools::Write(solid, "output.brep");  // 将生成的实体保存为 BREP 文件
 
 return 0;
 }
 
 | 
 |