Construct a TopDS_Face on a torus delimited by ellipse projection

Hello,

my goal is to project an ellipse (or a closed wire made of ellipse arcs and segments) on a tore, and to obtain the face bounded by this projected curve on the tore.

1. I was not able to obtain the expected curve when projecting on an unbounded tore, but I succeeded with a bounded tore:


    // Bounded torus
    gp_Pnt center(0, -387.5, 0);
    gp_Dir axis(0, 0, 1);
    gp_Ax2 ax(center, axis);
    TopoDS_Face tore = BRepPrimAPI_MakeTorus(ax, 387.5 - 177.7, 177.7, -1.57, 1.57, 3.14);

    // Written in a STEP file in order to check the result
    TopoDS_Shape s(tore);
    STEPControl_Writer aWriter;
    aWriter.Transfer(s, STEPControl_AsIs);
    aWriter.Write("tore.step");

2. The ellipse in (X, Z) plane:


    // The ellipse construction
    gp_Pnt center_e(0, 0, 0) ;
    gp_Dir normal(0, 1, 0);
    gp_Dir axex(0, 0, 1);
    gp_Elips el(gp_Ax2(center_e, normal, axex), 44.93, 42.39);
    TopoDS_Edge e = BRepBuilderAPI_MakeEdge(el);
    TopoDS_Wire w = BRepBuilderAPI_MakeWire(e);

    // Written in a STEP file in order to check the result
    TopoDS_Shape sw(w);
    STEPControl_Writer aWriter2;
    aWriter2.Transfer(sw, STEPControl_AsIs);
    aWriter2.Write("ellipse.step");

3. The ellipse projection on the torus

    gp_Dir d(0, -1, 0);
    BRepProj_Projection p = BRepProj_Projection (sw, s, d);
    TopoDS_Wire nwr = p.Current();

    // Written in a STEP file in order to check the result
    TopoDS_Shape projected(nwr);
    STEPControl_Writer aWriter3;
    aWriter3.Transfer(projected, STEPControl_AsIs);
    aWriter3.Write("projected.step");

4. The creation of an unbounded torus (if I use the above bounded torus to construct the face, I obtain a hole in the torus instead of the wanted face — see point 6; I suspect that it is because the torus is already bounded?):


    gp_Ax3 ax3(ax);
    gp_Torus full_tore(ax3, 387.5 - 177.7, 177.7);

    // Written in a STEP file in order to check the result
    TopoDS_Shape full_tore_shape = BRepBuilderAPI_MakeFace(full_tore);
    STEPControl_Writer aWriter4;
    aWriter4.Transfer(full_tore_shape, STEPControl_AsIs);
    aWriter4.Write("full_tore.step");

5. The beginning of my problems: trying to construct a Face, bounded by the ellipse projection nwr.


    BRepBuilderAPI_MakeFace face_builder = BRepBuilderAPI_MakeFace(full_tore, nwr);
    if (face_builder.IsDone())
      cout << "Point 2 : OK\n";
    else
      cout << "Point 2 : KO\n";
    fflush(stdout);

“Point 2 : OK” is written on the standard output.

But I can’t save the face in a STEP file?


    TopoDS_Face f = face_builder.Face();
    TopoDS_Shape sf(f);

    if (sf.IsNull())
      cout << "sf is null\n";
    else
      cout << "sf is not null\n";  // <---- sf is not null
    fflush(stdout);

    STEPControl_Writer aWriter5;
    aWriter5.Transfer(sf, STEPControl_AsIs); // <---- segmentation error
    cout << "After transfer\n"; fflush(stdout);
    aWriter5.Write("face.step");
    fflush (stdout);

“sf is not null” is written on the standard output, but my test program crashes with a segmentation error!

6. If I try to make a face on the bounded torus tore, it kinda work, but the face I’m obtaining is the tore with one hole corresponding the projected ellipse.


    BRepBuilderAPI_MakeFace face_builder = BRepBuilderAPI_MakeFace(tore, nwr);
    if (face_builder.IsDone())
      cout << "Point 2 : OK\n";
    else
      cout << "Point 2 : KO\n";
    fflush(stdout);
    TopoDS_Face f = face_builder.Face();
    TopoDS_Shape sf(f);

    if (sf.IsNull())
      cout << "sf is null\n";
    else
      cout << "sf is not null\n";
    fflush(stdout);

    STEPControl_Writer aWriter5;
    aWriter5.Transfer(sf, STEPControl_AsIs); // <---- segmentation error
    cout << "After transfer\n"; fflush(stdout);
    aWriter5.Write("face.step");
    cout << "After write\n"; fflush(stdout);

What am I doing wrong in step 5 (or 6)?

Thanks for any help.

I’m attaching a FreeCAD vue of the produced STEP files when point 5 is commented out.

Matthieu Dubuget's picture

Numbering corrections

Matthieu Dubuget's picture

I had it!
Instead of

  TopoDS_Face face = BRepBuilderAPI_MakeFace(tore, nwr);

I did

 Handle_Geom_Surface surf_tore = BRep_Tool::Surface(tore);
 TopoDS_Face face = BRepBuilderAPI_MakeFace(surf_tore, nwr);

Now, trying with a more complex wire…

Guido van Hilst not specified's picture

Hi Mathieu,

I have made an example to make the face on the torus: FaceOnTorus

Best regards,​

Guido

Matthieu Dubuget's picture

Nice!

1/ I do not understand tore.Surface(), line 21

`tore` is a TopoDS_Face. I can't find Surface() method in TopoDS_Face documentation?

2/ if I replace line 35 :

        //OCTopoDS_Face tore = new OCBRepPrimAPI_MakeTorus(ax, 387.5 - 177.7, 177.7, -1.57, 1.57, 3.14).Face();
        OCTopoDS_Face tore = new OCBRepPrimAPI_MakeTorus(ax, 387.5 - 177.7, 177.7).Face();

I do not understand what nwr is?

Thanks

Guido van Hilst not specified's picture

The Surface() function on the face is an C# extension method, it returns: BRep_Tool::Surface(face);

Matthieu Dubuget's picture

Ahhhh. Thanks for the explanation!

Matthieu Dubuget's picture

I have another projection problem.

http://www.creativecadtechnology.com/OCC/ShowScript?userName=makeBoundeTorus&groupName=demo&scriptName=ProjectLineOnTorus

I'm projecting two edges on a bounded torus.

The results of the projection is not similar for both edges.

This is my main current problem when trying to create all my faces.