Hole in solid

Hello, I shall have wanted to know how I can make a hole in a cube?

Paul Jimenez's picture

Define the cutting Solid, then Cut the cube with it. Just avoid having Faces of the cutting Solid overlap those of the cube for better results.

duduch24's picture

I have create a cube and a cylinder, I make:

TopoDS_Shape cut_shape = BRepAlgoAPI_Cut(cube,cylinder);

And it to give me an error when I throw(launch) my program.

Paul Jimenez's picture

It is really hard to help you with that information. What is the error message? How did you create the cube? How the cylinder?

duduch24's picture

//Profile : Define Support Points
gp_Pnt aPnt1(-30,0,0);
gp_Pnt aPnt2(-30,-30,0);
gp_Pnt aPnt3(30,-30,0);
gp_Pnt aPnt4(30,0,0);
//The Hole
gp_Pnt hole(0 , 0 , 30);
gp_Dir direction = gp::DZ();
gp_Ax2 neckAx2(hole , direction);
Standard_Real myNeckRadius = 5;
Standard_Real myNeckHeight = -15;

BRepPrimAPI_MakeCylinder MKCylinder(neckAx2 , myNeckRadius , myNeckHeight);
TopoDS_Shape aHole = MKCylinder.Shape();

//Profile : Define the Geometry
Handle(Geom_TrimmedCurve) aSegment1 = GC_MakeSegment(aPnt1 , aPnt2);
Handle(Geom_TrimmedCurve) aSegment2 = GC_MakeSegment(aPnt2 , aPnt3);
Handle(Geom_TrimmedCurve) aSegment3 = GC_MakeSegment(aPnt3 , aPnt4);
Handle(Geom_TrimmedCurve) aSegment4 = GC_MakeSegment(aPnt4 , aPnt1);

//Profile : Define the Topology
TopoDS_Edge aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1);
TopoDS_Edge aEdge2 = BRepBuilderAPI_MakeEdge(aSegment2);
TopoDS_Edge aEdge3 = BRepBuilderAPI_MakeEdge(aSegment3);
TopoDS_Edge aEdge4 = BRepBuilderAPI_MakeEdge(aSegment4);

TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(aEdge1 , aEdge2);
TopoDS_Wire bWire = BRepBuilderAPI_MakeWire(aEdge3 , aEdge4);

BRepBuilderAPI_MakeWire mkWire;
mkWire.Add(aWire);
mkWire.Add(bWire);
TopoDS_Wire myWireProfile = mkWire.Wire();

//Body : Prism the Profile
TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile);
gp_Vec aPrismVec(0 , 0 , 30);

TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile , aPrismVec);

Here is my code, where to place the function which allows to make the hole?

Paul Jimenez's picture

I would suggest you to use the algorithm for a box instead of trying to build it "by hand":

TopoDS_Solid box = BRepPrimAPI_MakeBox(gp_Pnt(-30, 0, 0), 60, 30, 30);

For the Cylinder, avoid having one of the Faces overlap with one of the Box. Change hole to this:

gp_Pnt hole(0, 0, 30.1);

To compensate that 0.1 extra, change myNeckHeight to:

Standard_Integer myNeckHeight = -15.1;

Once you have box and cylinder, use cut with them:

TopoDS_Shape result = BRepAlgoAPI_Cut(box, aHole); // cut box with aHole

My guess of why it was not working for you is that "myBody" was either a Shell or somewhere during the building process of the profile for the box something went wrong.

duduch24's picture

I have just tried this code:

TopoDS_Solid box = BRepPrimAPI_MakeBox(gp_Pnt(-30, 0, 0), 60, 30, 30);
//The Hole
gp_Pnt hole(0 , 0 , 30.1);
gp_Dir direction = gp::DZ();
gp_Ax2 neckAx2(hole , direction);
Standard_Real myNeckRadius = 5;
Standard_Real myNeckHeight = -15.1;

BRepPrimAPI_MakeCylinder MKCylinder(neckAx2 , myNeckRadius , myNeckHeight);
TopoDS_Shape aHole = MKCylinder.Shape();
//gp_Pnt hole(0 , 0 , 30);
//Standard_Integer myNeckHeight = -15.1;

TopoDS_Shape result = BRepAlgoAPI_Cut(box, aHole); // cut box with aHole

When I launch, Error : " An exception was generated "

Paul Jimenez's picture

Running in debug mode is the next step. The exception's internal message and where it occurs is the key to solve the problem.

duduch24's picture

The exception is generated here:

TopoDS_Shape result = BRepAlgoAPI_Cut(box, aHole); // cut box with aHole

Paul Jimenez's picture

My guess is that aHole is not a Solid (probably a Shell).

duduch24's picture

Thank you for your help, I have finally to find the solution of my problem.
I put my solution for those who the same problem would have:

TopoDS_Shape
MakeHole()
{
//The cylinder
gp_Pnt neckLocation(gp_Pnt(30,30,30));
gp_Dir neckNormal = gp::DZ();
gp_Ax2 neckAx2(neckLocation , neckNormal);
TopoDS_Shape aHole = BRepPrimAPI_MakeCylinder(neckAx2,5,40);
//The cube
TopoDS_Shape box = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), 60,60 , 60);
//Cube - Cylinder
TopoDS_Shape result = BRepAlgoAPI_Cut(box,aHole);
//Building the resulting compound
TopoDS_Compound aRes;
BRep_Builder aBuilder;
aBuilder.MakeCompound (aRes);

aBuilder.Add (aRes, result);

return aRes;
}

StefanKunze's picture

Ok when I change your sample code to this I get a Fuse and not a Cut.... Somebody know why????

gp_Pnt neckLocation(gp_Pnt(30,30,-30));
gp_Dir neckNormal = gp::DZ();
gp_Ax2 neckAx2(neckLocation , neckNormal);
TopoDS_Shape aHole = BRepPrimAPI_MakeCylinder(neckAx2,5,100);
//The cube
TopoDS_Shape box = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), 60,60 , 60);
//Cube - Cylinder
TopoDS_Shape result = BRepAlgoAPI_Cut(box,aHole);
//Building the resulting compound
TopoDS_Compound aRes;
BRep_Builder aBuilder;
aBuilder.MakeCompound (aRes);

aBuilder.Add (aRes, aHole);
aBuilder.Add (aRes, box);
aBuilder.Add (aRes, result);

Maili's picture

Why do you add hole and box to your compound?

StefanKunze's picture

^^
Good question???? (i think because it was friday afternoon)

StefanKunze's picture

For all facing a common problem - Check/Switch the orientation of the substraction solid!