How can I trim a cylinder using two paralle circles?

Forums: 

I want to trim a cylinder through two parallel circles defined on the cylinder. How can I trim it using the BRep-related method? I tried lots of combinations and read the document carefully. But I still cannot get the concept or the examples. Thanks in advance for helping me to solve this problem.

I have attached the code as belows:

// Define the cylinder
gp_Cylinder cy(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 1);
BRepBuilderAPI_MakeFace cyf(cy);

// Define the circle
gp_Circ circle1(gp_Ax2(gp_Pnt(1, 0, 0), gp_Dir(0, 0, 1)), 1);
gp_Circ circle2(gp_Ax2(gp_Pnt(1, 0, 1), gp_Dir(0, 0, 1)), 1);

// Define the edge and wire
BRepBuilderAPI_MakeEdge edge1(circle1);
BRepBuilderAPI_MakeEdge edge2(circle2);

BRepBuilderAPI_MakeWire makeWire1(edge1);
BRepBuilderAPI_MakeWire makeWire2(edge2);

cyf1.Add(wiremaker1.Wire());
cyf1.Add(wiremaker2.Wire());

// Meshing
MeshSave(cyf1, "test1.ply");

Dmitrii Pasukhin's picture

Hello, you need to prepare the wire with the next order:

Sample cylinder: order of edges in wire - Forum Open Cascade Technology

Best regards, Dmitrii.

Lin Lin's picture

Hi Dmitrii, thanks for your reply. Can you see this post below? Sorry, I leave the comments in the wrong place.

Lin Lin's picture

Thanks for your reply. I tried to add four edges into a single wire and trim the cylinder with that wire, but I still got nothing. Is there any misunderstanding about the concept?

I have checked the connectivity of the wire using BRepTools_WireExplorer, and the order seems to be equal to the post you sent me.

Here is the code:

gp_Circ circle1(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 1);
gp_Circ circle2(gp_Ax2(gp_Pnt(0, 0, 1), gp_Dir(0, 0, -1)), 1);

Standard_Real radius = 1.0;
Standard_Real height = 5.0;
gp_Pnt center(0, 0, 0);
gp_Dir axisDirection(0, 0, 1);
gp_Ax2 axis(center, axisDirection);
BRepPrimAPI_MakeCylinder makeCylinder(axis, radius, height);
BRepBuilderAPI_MakeFace cyf(makeCylinder.Face());

BRepBuilderAPI_MakeEdge edge1(circle1);
BRepBuilderAPI_MakeEdge edge2(gp_Pnt(1, 0, 0), gp_Pnt(1, 0, 1));
BRepBuilderAPI_MakeEdge edge3(circle2, gp_Pnt(1, 0, 1), gp_Pnt(1, 0, 1));
BRepBuilderAPI_MakeEdge edge4(gp_Pnt(1, 0, 1), gp_Pnt(1, 0, 0));

BRepBuilderAPI_MakeWire makeWire1;
makeWire1.Add(edge1);
makeWire1.Add(edge2);
makeWire1.Add(edge3);
makeWire1.Add(edge4);

auto wire = makeWire1.Wire();

BRepTools_WireExplorer wireExplorer(wire);

// Iterate through the edges of the wire
for (; wireExplorer.More(); wireExplorer.Next()) {
    TopoDS_Edge edge = wireExplorer.Current();
    TopoDS_Vertex startVertex, endVertex;
    TopExp::Vertices(edge, startVertex, endVertex);
}

cyf.Add(wire);
cyf.Build();
std::cout << cyf.Error() << std::endl;
TopoDS_Face f = cyf.Face();

MeshSave(f, "test1.ply");
Dmitrii Pasukhin's picture
gp_Circ circle1(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 1);
gp_Circ circle2(gp_Ax2(gp_Pnt(0, 0, 1), gp_Dir(0, 0, -1)), 1);

Standard_Real radius = 1.0;
Standard_Real height = 5.0;
gp_Pnt center(0, 0, 0);
gp_Dir axisDirection(0, 0, 1);
gp_Ax2 axis(center, axisDirection);
gp_Cylinder cy(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 1);

BRepBuilderAPI_MakeEdge edge1(circle1);
BRepBuilderAPI_MakeEdge edge2(gp_Pnt(1, 0, 0), gp_Pnt(1, 0, 1));
BRepBuilderAPI_MakeEdge edge3(circle2, gp_Pnt(1, 0, 1), gp_Pnt(1, 0, 1));
BRepBuilderAPI_MakeEdge edge4(edge2.Reversed());

BRepBuilderAPI_MakeWire makeWire1;
makeWire1.Add(edge1);
makeWire1.Add(edge2);
makeWire1.Add(edge3);
makeWire1.Add(edge4);
auto wire = makeWire1.Wire();

// cylindrical face with limits in V direction.
TopoDS_Face cylinder_face = BRepBuilderAPI_MakeFace(cy, 0, 2 * M_PI, 0, 1.0).Face();
// Limit cylinder by wired edges
BRepBuilderAPI_MakeFace cylface(cy, wire);

// Something like that. edge1 and edge3 should be the same

Best regards, Dmitrii.

Lin Lin's picture

Thanks for your reply. But I still cannot get any output from the face. I check the return code and the `Error` but everything seems to be fine. I leave the full code below. Can you help me to figure the mistake?

A strange thing is that I can output a model if I use the u,v coordinate to trim a cylinder, but I cannot get it if I use the wire. I also checked the orientation of the wire but it seems ok.

```
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Face.hxx>
#include <BRepLib.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <BRepBuilderAPI_MakeShell.hxx>
#include <BRep_Builder.hxx>
#include <StlAPI_Writer.hxx>

int main(int argc, char* argv[])
{
BRepLib::Precision(1e-2);
// Define two circle with opposite orientation
gp_Circ circle1(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 1);
gp_Circ circle2(gp_Ax2(gp_Pnt(0, 0, 1), gp_Dir(0, 0, -1)), 1);

// Define a cylinder
gp_Cylinder cy(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 1);

// Build edge
BRepBuilderAPI_MakeEdge edge1(circle1);
BRepBuilderAPI_MakeEdge edge2(gp_Pnt(1, 0, 0), gp_Pnt(1, 0, 1));
BRepBuilderAPI_MakeEdge edge3(circle2, gp_Pnt(1, 0, 1), gp_Pnt(1, 0, 1));
BRepBuilderAPI_MakeEdge edge4(gp_Pnt(1, 0, 1), gp_Pnt(1, 0, 0));

BRepBuilderAPI_MakeWire makeWire1;
makeWire1.Add(edge1);
makeWire1.Add(edge2);
makeWire1.Add(edge3);
makeWire1.Add(edge4);
TopoDS_Wire wire = makeWire1.Wire();

// Limit cylinder by wired edges
BRepBuilderAPI_MakeFace cylface(cy, wire, true);

std::cout << cylface.Error() << std::endl;
std::cout << cylface.IsDone() << std::endl;
TopoDS_Face output = cylface.Face();

BRepMesh_IncrementalMesh mesh(output, 5e-2, false, 0.5);
StlAPI_Writer objWriter;
bool write_flag = objWriter.Write(output, "1.stl");
std::cout << write_flag << std::endl;
}
```

Dmitrii Pasukhin's picture

It is result from your code. The parameters is not correct. Later i fix the code.