A-9 2025/5/26
OpenCasCade读取STEP文件中的NURBS曲面
- OpenCasCade读取NURBS曲面并输出NURBS曲面的所有信息,包括控制点,节点向量,权重等等。
- 也输出NURBS曲面的离散化边界轮廓线。也就是线段连接而成的轮廓线,线段的顺序有待商榷。

#include <STEPControl_Reader.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Edge.hxx>
#include <Geom_Surface.hxx>
#include <Geom_BSplineSurface.hxx>
#include <TopExp_Explorer.hxx>
#include <BRep_Tool.hxx>
#include<TopExp.hxx>
#include<TopoDS.hxx>#include <GCPnts_UniformAbscissa.hxx>
#include <GCPnts_UniformDeflection.hxx>
#include <Geom_BSplineCurve.hxx>
#include <BRepAdaptor_Curve.hxx> #include <iostream>
#include<fstream>std::vector<gp_Pnt> DiscretizeEdge_Uniform(const TopoDS_Edge& edge, int numPoints) {std::vector<gp_Pnt> points;BRepAdaptor_Curve curve(edge);GCPnts_UniformAbscissa abscissa(curve, numPoints);if (!abscissa.IsDone()) {std::cerr << "离散失败!" << std::endl;return points;}for (int i = 1; i <= abscissa.NbPoints(); ++i) {gp_Pnt p = curve.Value(abscissa.Parameter(i));points.push_back(p);}return points;
}
void ReadNurbs(TopoDS_Shape shape) {// 遍历所有的面TopTools_IndexedMapOfShape faceMap;TopExp::MapShapes(shape, TopAbs_FACE, faceMap);int NURBSFace_number = 0;for (int i = 1; i <= faceMap.Extent(); i++) {TopoDS_Face face = TopoDS::Face(faceMap(i));// 获取面上的曲面Handle(Geom_Surface) surface = BRep_Tool::Surface(face);// 如果是 NURBS 曲面if (surface->IsKind(STANDARD_TYPE(Geom_BSplineSurface))) {NURBSFace_number++;std::ofstream file("NurbsFace_"+std::to_string( NURBSFace_number)+".txt");Handle(Geom_BSplineSurface) bsplineSurface = Handle(Geom_BSplineSurface)::DownCast(surface);TColStd_Array1OfReal uKnots = bsplineSurface->UKnotSequence();TColStd_Array1OfReal vKnots = bsplineSurface->VKnotSequence();int uDegree = bsplineSurface->UDegree();int vDegree = bsplineSurface->VDegree();file<< "UDegree: " << uDegree <<std::endl<< "VDegree: " << vDegree << std::endl << std::endl;file << "UKnots: ";for (int i = uKnots.Lower(); i <= uKnots.Upper(); ++i) {file << uKnots(i) << " ";}file << std::endl;file << "VKnots: ";for (int i = vKnots.Lower(); i <= vKnots.Upper(); ++i) {file << vKnots(i) << " ";}file << std::endl << std::endl;file << "ControlPoints:" << std::endl;// 输出曲面的控制点TColgp_Array2OfPnt controlPoints;Standard_Integer uCount = bsplineSurface->NbUPoles();Standard_Integer vCount = bsplineSurface->NbVPoles();for (Standard_Integer u = 1; u <= uCount; ++u) {for (Standard_Integer v = 1; v <= vCount; ++v) {file << bsplineSurface->Pole(u, v).X() << " " << bsplineSurface->Pole(u, v).Y()<< " " << bsplineSurface->Pole(u, v).Z() << " " << bsplineSurface->Weight(u, v) << std::endl;}}file << std::endl;file << " BoundaryPoints: " << std::endl;// 遍历面的所有边(轮廓线)TopExp_Explorer edgeExplorer(face, TopAbs_EDGE);int edgeCount = 0;while (edgeExplorer.More()) {TopoDS_Edge edge = TopoDS::Edge(edgeExplorer.Current());edgeCount++;auto v=DiscretizeEdge_Uniform(edge,50);for (int i = 0; i < v.size(); i++) {file << v[i].X() <<" " << v[i].Y() << " " << v[i].Z() << std::endl;}edgeExplorer.Next();}file.close();std::cout << "第" << NURBSFace_number << "个Nurbs曲面,输出成功" << std::endl;}}
}
TopoDS_Shape ReadFile(char* path) {STEPControl_Reader reader;reader.ReadFile(path);// Loads file MyFile.stp Standard_Integer NbRoots = reader.NbRootsForTransfer();// gets the number of transferable roots Standard_Integer NbTrans = reader.TransferRoots();// translates all transferable roots, and returns the number of //successful translations return reader.OneShape();
}int main() {// 创建一个顶点或其他形状TopoDS_Shape shape= ReadFile((char*)"C:\\Model\\t.STEP"); std::cout << "读取成功" << std::endl;ReadNurbs(shape);return 0;
}