How to reverse the TopoDS_Edge, the TopoDS_Edge. Reversed() seems don't work.

jianbaoxia

CAD master
Hi Bros, I'm JianBaoxia.
I try to connect some edges to become a closed wire today.
I use "ShapeAnalysis_WireOrder" to sort the edges, and some edges should be reversed.
The edges are "TopoDS_Edge", so I use the TopoDS_Edge.Reversed().
However, it seems something beyond my expection.
The Orientation of edge had reversed, but the firstVertex and lastVertex didn't change.

After I sort the edges and reverse the edges need to reverse, I use the "ShapeAnalysis_WireOrder" to test it again,
and it remind me the edges need to be reversed didn't be reversed. And I faild in build the TopoDS_Wire through "BRepBuilderAPI_MakeWire".

I'am confusing about this, hope for you suggesstions.
 
Last edited:

blobfish

CAD community veteran
I use "ShapeAnalysis_WireOrder" to sort the edges, and some edges should be reversed.
The edges are "TopoDS_Edge", so I use the TopoDS_Edge.Reversed().
However, it seems something beyond my expection.
TopoDS_Shape::Reversed() is a const function and returns a copy. Did you mean to call Reverse() instead?. If you would post some code, the people trying to help won't have to guess.
 

jianbaoxia

CAD master
@blobfish I'm sorry for my careless. I try to write a Demo in the first when I saw your reply.
I was thought I make the wire failed bucause the orientation of the edge, and the gap between edges.
However, after I run the demo, the truth seems beyond my expectation. I'm wrong, and I must failed cause other bug, and I'll keep try.
Even so, I still decide to give the demo out of respect for all of you, althought it's not the real problem to make me failed.
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;
}
int main()
{
    std::string filePath = "C:/Users/14656/Desktop/Program/Open Cascade/Demo/";

    gp_Pnt p1(0, 0, 1.e-4);
    gp_Pnt p2(1.0001, 0, 0);
    gp_Pnt p3(1, 1.0001, 0);
    gp_Pnt p4(0, 1, 0);

    TopoDS_Edge e1 = BRepBuilderAPI_MakeEdge(p1, p2);
    TopoDS_Edge e2 = BRepBuilderAPI_MakeEdge(p3, p2);
    TopoDS_Edge e3 = BRepBuilderAPI_MakeEdge(p3, p4);
    TopoDS_Edge e4 = BRepBuilderAPI_MakeEdge(p4, p1);

    BRepBuilderAPI_MakeWire mkWier;
    mkWier.Add(e1);
    mkWier.Add(e2);
    mkWier.Add(e3);
    mkWier.Add(e4);
    TopoDS_Wire w = mkWier.Wire();

    WriteStepFile(w, filePath + "wire.step");

    return 0;
}

There is my model, I try to connected it to a TopoDS_Wire, and I'll give another Demo to describe my problem soon.
Thank you from the bottom of my heart for your generosity and help.
 

Attachments

  • edgesSewShape.step
    21.9 KB · Views: 1
Last edited:

jianbaoxia

CAD master
@blobfish The model was build with some edges through the "BRepBuilderAPI_Sewing", I try to connect these edges to TopoDS_Wire, if just add the TopoDS_Edge to BRepBuilderAPI_MakeWire object simply, it will failed, like this:
Code:
int main()
{
    std::string edgesFilePath = "C:/Users/14656/Desktop/Program/Open Cascade/Slicer/edgesSewShape.step";
    std::string filePath = "C:/Users/14656/Desktop/Program/Open Cascade/Slicer/";

    TopoDS_Shape modelShape = ReadStepFile(edgesFilePath);

    BRepBuilderAPI_MakeWire mkWier;
    for (TopExp_Explorer exp(modelShape, TopAbs_EDGE); exp.More(); exp.Next())
    {
        TopoDS_Edge e = TopoDS::Edge(exp.Current());
        mkWier.Add(e);
    }

    TopoDS_Wire aWire = mkWier.Wire();

  

    return 0;
}

So I try to use the "ShapeAnalysis_WireOrder" to sort these edges first, like this:
Code:
int main()
{
    std::string edgesFilePath = "C:/Users/14656/Desktop/Program/Open Cascade/Demo/edgesSewShape.step";
    std::string filePath = "C:/Users/14656/Desktop/Program/Open Cascade/Demo/";

    TopoDS_Shape modelShape = ReadStepFile(edgesFilePath);

    std::vector<TopoDS_Edge> edgeList;                                // store edges in the model,just for sort
    ShapeAnalysis_WireOrder sawo(Standard_True, Precision::Confusion());
    for (TopExp_Explorer exp(modelShape, TopAbs_EDGE); exp.More(); exp.Next())
    {
        TopoDS_Edge e = TopoDS::Edge(exp.Current());
        edgeList.push_back(e);

        TopoDS_Vertex firstVer = TopExp::FirstVertex(e);
        TopoDS_Vertex lastVer = TopExp::LastVertex(e);
        gp_Pnt startPoint = BRep_Tool::Pnt(firstVer);
        gp_Pnt endPoint = BRep_Tool::Pnt(lastVer);
        gp_XYZ startXYZ(startPoint.X(), startPoint.Y(), startPoint.Z());
        gp_XYZ endXYZ(endPoint.X(), endPoint.Y(), endPoint.Z());
        sawo.Add(startXYZ, endXYZ);
    }
    sawo.Perform();
    if (sawo.IsDone())    std::cout << "sort edges finish!" << std::endl;
    else std::cout << "the error is :" << sawo.Status() << std::endl;

    TopoDS_Edge* edgeSortList = new TopoDS_Edge[edgeList.size()];    // store edges with order,just for buile a wire
    for (int i = 0; i < edgeList.size(); i++)
    {
        Standard_Integer order = sawo.Ordered(i + 1);                // OCCT index begin from 1

        TopoDS_Edge e;
        if (order < 0) e = TopoDS::Edge(edgeList[abs(order) - 1].Reversed());    // if the order is negative,the edge need reverse
        else e = TopoDS::Edge(edgeList[abs(order) - 1]);
        edgeSortList[abs(order) - 1] = e;
    }

   
    //! after sort edges, build wire with BRepBuilderAPI_MakeWire
    BRepBuilderAPI_MakeWire mkWier;
    for (int i = 0; i < edgeList.size(); i++)
        mkWier.Add(edgeSortList[i]);

    TopoDS_Wire aWire = mkWier.Wire();    //▲ failed here

    BRepBuilderAPI_Sewing aSewMaker_edgeTest;
    for (int j = 0; j < edgeList.size(); j++)
    {
        aSewMaker_edgeTest.Add(edgeSortList[j]);
    }
    aSewMaker_edgeTest.Perform();
    TopoDS_Shape edgesSewShape = aSewMaker_edgeTest.SewedShape();
    WriteStepFile(edgesSewShape, filePath + "edgesSewShape.step");

    return 0;
}
 

Attachments

  • edgesSewShape.step
    22 KB · Views: 0
Last edited:

blobfish

CAD community veteran
I made notes as I was looking at the code. Don't take what I wrote on faith and don't be offended, if I come across to opinionated.

Why are you writing a step file from opencascade? use opencascades native format '.brep' for exchanging to other occt users.

You should pass the occt allocator to standard containers. std::vector<TopoDS_Edge, NCollection_StdAllocator<TopoDS_Edge>> edgeList;

TopExp::FirstVertex() and TopExp::LastVertex() ignore orientation by default.
I think you should pass Standard_True for second parameter.
see orientation discussion below.

TopoDS_Edge* edgeSortList = new TopoDS_Edge[edgeList.size()];
Where is delete? Memory leak?
Won't this work?
std::vector<TopoDS_Edge, NCollection_StdAllocator<TopoDS_Edge>> edgeSortList(edgeList.size());
//use operator[] like with array
Or TopTools_ListOfShape
Or TopTools_IndexedMapOfShape
Anything but that.

BRepBuilderAPI_MakeWire doesn't care about orientation.
So you can ignore orientation until everything else is working.
MakeWire::Add

With all that being said, there might not be anything in your code preventing the creation of the wire.
Referencing my picture below: I can't get anything to 'connect' to the yellow ellipse. Everything
else will 'connect' fine as long as I leave out the yellow ellipse. I can't even connect the yellow
ellipse to either of the white lines in individual operations. I don't know what is going on with
that yellow ellipse, but I am guessing an occt bug?
yellowEllipse.png
 

jianbaoxia

CAD master
@blobfish Thank you so much for your suggestion, it's helpful.
Actually, I failed because the yellow edge was not connect with other edges.
And forgive my pool code, when I allocate memoty with "new", I still remind myself to use "delete[]", and I forgot it next second. o_O

I just learn C++ and OCCT by myself, so sometime I use it in a strange way, thanks for your suggestion let me learn a lot.
 
Top