Fail to transfrom a wire to a bspline curve.

jianbaoxia

CAD master
Hi Bros, I'm JianBaoxia.
I get a .step file, it's a wire and consist of several TopoDS_Edges.
First I try to sort the edges, and build it as a wire. o_O
I use "ShapeAnalysis_WireOrder", and it failed and give the status"some edges are reversed, but no gaps remain".
Then I try another way to get a bspline curve.
FIrst I sample some points from every edge, and then use "GeomAPI_PointsToBSpline", still failed, maybe the points should be sorted too.
As show below, fig1 is the .step file, fig 2 is the points I sampled, fig 3 is the bspline curve build by "GeomAPI_PointsToBSpline".
1667885374562.png1667885413323.png1667885492621.png
There is my demo: may I get some suggestions.
Code:
bool WriteStepFile(const TopoDS_Shape& shape, const std::string filepath)
{
    bool output_flag = true;
    STEPControl_Writer writer;
    Interface_Static::SetCVal("write.step.schema", "AP203");
    IFSelect_ReturnStatus status = writer.Transfer(shape, STEPControl_AsIs);
    if (status != IFSelect_RetDone) output_flag = false;
    status = writer.Write(filepath.c_str());
    if (status != IFSelect_RetDone) output_flag = false;

    return output_flag;
}
TopoDS_Shape ReadStepFile(const std::string& filepath)
{
   
    STEPControl_Reader reader;

    reader.ReadFile(filepath.c_str());

    reader.TransferRoots();

    TopoDS_Shape shape = reader.OneShape();

    return shape;

}
std::vector<gp_Pnt> DiscreteEdge2Pnt(const TopoDS_Edge& edge,const int& density)
{
    Standard_Real length = GetLengthOfEdge(edge);
    Standard_Integer num_sample = floor(length * density);
    if (num_sample <= 1)
        std::cout << "DiscreteEdge2Pnt() :density too small" << std::endl;
    Standard_Real gap = length / (num_sample - 1);   
    

    BRepAdaptor_Curve brepAdaptorCur(edge);
    const GeomAdaptor_Curve& geomAdaptorCur = brepAdaptorCur.Curve();
    Handle(Geom_Curve) geoCur = geomAdaptorCur.Curve();

    TopoDS_Vertex firstVer = TopExp::FirstVertex(edge);
    TopoDS_Vertex lastVer = TopExp::LastVertex(edge);
    Standard_Real uFirst;
    GeomLib_Tool::Parameter(geoCur, BRep_Tool::Pnt(firstVer), Precision::Confusion(), uFirst);
        
    std::vector<gp_Pnt> pointList;
    for (int i = 1; i < num_sample - 1; i++)
    {
        Standard_Real dis = gap * i;
        GCPnts_AbscissaPoint absPnt(brepAdaptorCur, dis, uFirst);
        if (!absPnt.IsDone())
            std::cout << "not done" << std::endl;
        else
        {
            Standard_Real u_absPnt = absPnt.Parameter();
            pointList.push_back(geoCur->Value(u_absPnt));
        }
    }
    return pointList;
}


TopoDS_Edge Points2BSplineCurve(const std::vector<gp_Pnt>& pointList)
{
    Standard_Integer numPnt = pointList.size();  
    TColgp_Array1OfPnt pntArr(1, numPnt);
    for (int i = 0; i < numPnt; i++)
    {
        pntArr.SetValue(i + 1, pointList[i]);
    }

    Handle(Geom_Curve) bsplineCurve = GeomAPI_PointsToBSpline(pntArr).Curve();
    TopoDS_Edge bspEdge = BRepBuilderAPI_MakeEdge(bsplineCurve).Edge();

    return bspEdge;
}

int main()
{
    std::string filePath = "C:/Users/14656/Desktop/Program/Open Cascade/Demo/";

    TopoDS_Shape shape = ReadStepFile(filePath + "theWire.step");
    TopoChecker(shape);
    Standard_Integer density = 1000;   
    std::vector<gp_Pnt> pointList;  
    for (TopExp_Explorer exp(shape, TopAbs_EDGE); exp.More(); exp.Next())
    {
        TopoDS_Edge aEdge = TopoDS::Edge(exp.Current());
        std::vector<gp_Pnt> points = DiscreteEdge2Pnt(aEdge, density);
        for (int i = 0; i < points.size(); i++)
            pointList.push_back(points[i]);
    }

    TopoDS_Edge bsplineEdge = Points2BSplineCurve(pointList);
    WriteStepFile(bsplineEdge, filePath + "bsplineEdge.step");

    BRepBuilderAPI_Sewing aSew;
    for (int i = 0; i < pointList.size(); i++)
    {
        aSew.Add(BRepBuilderAPI_MakeVertex(pointList[i]));

    }
    aSew.Perform();
    TopoDS_Shape aSewShape = aSew.SewedShape();
    WriteStepFile(aSewShape, filePath + "aSewShape.step");

    return 0;
}
 

Attachments

  • aSewShape.step
    115.1 KB · Views: 1
  • bsplineEdge.step
    19.5 KB · Views: 1
  • theWire.step
    30.1 KB · Views: 2
Top