Sample demonstrating Boolean operations between two solids using BRepAlgoAPI_Common, BRepAlgoAPI_Cut and BRepAlgoAPI_Fuse algorithms (which are aliases to BRepAlgoAPI_BooleanOperation).
Perform Boolean operation:
pload MODELING VISUALIZATION
# Make a box b1 with a corner at [0, 0, 0] and the specified sizes
box b1 10.0 15.0 20.0
# Make a box b2 by two points
box b2 -min 5.0 7.5 10.0 -max 20.0 25.0 30.0
# find common between two boxes
bcommon r b1 b2
# cut box b2 from b1
#bcut r b1 b2
# fuse two boxes
#bfuse r b1 b2
# display in the viewer
vinit View1
vdisplay -dispMode 0 b1 b2
vaspects b1 -color YELLOW
vaspects b2 -color GREEN
vdisplay -dispMode 1 r
vaspects r -color RED
vfit
// Make a box #1 with a corner at [0, 0, 0] and the specified sizes.Standard_RealaSizeX=10.0;Standard_RealaSizeY=15.0;Standard_RealaSizeZ=20.0;TopoDS_ShapeaShape1=BRepPrimAPI_MakeBox(aSizeX,aSizeY,aSizeZ);myResult<<"Box at corner [0, 0, 0] and sizes ["<<aSizeX<<", "<<aSizeY<<", "<<aSizeZ<<"] was created in yellow wireframe"<<std::endl;// Make a box #2 by two points.gp_PntaPnt1(5.0,7.5,10.0);gp_PntaPnt2(20.0,25.0,30.0);TopoDS_ShapeaShape2=BRepPrimAPI_MakeBox(aPnt1,aPnt2);myResult<<"Box with corners ["<<aPnt1.X()<<", "<<aPnt1.Y()<<", "<<aPnt1.Z()<<"] and ["<<aPnt2.X()<<", "<<aPnt2.Y()<<", "<<aPnt2.Z()<<"] was created in green wirefreme"<<std::endl;// Create a boolean algo.BRepAlgoAPI_CommonanAlgo(aShape1,aShape2);//BRepAlgoAPI_Cut anAlgo(aShape1, aShape2);//BRepAlgoAPI_Fuse anAlgo(aShape1, aShape2);// Make operation.anAlgo.Build();if(!anAlgo.IsDone())// Process errors{myResult<<"Errors : "<<std::endl;anAlgo.DumpErrors(myResult);}if(anAlgo.HasWarnings())// Process warnings{myResult<<"Warnings : "<<std::endl;anAlgo.DumpErrors(myResult);}if(anAlgo.IsDone()){// Get result.TopoDS_ShapeaResultShape=anAlgo.Shape();myResult<<"Result shape was created in red shading"<<std::endl;Handle(AIS_ColoredShape)anAisShape1=newAIS_ColoredShape(aShape1);Handle(AIS_ColoredShape)anAisShape2=newAIS_ColoredShape(aShape2);anAisShape1->SetColor(Quantity_Color(Quantity_NOC_YELLOW));anAisShape2->SetColor(Quantity_Color(Quantity_NOC_GREEN));Handle(AIS_ColoredShape)anAisResult=newAIS_ColoredShape(aResultShape);anAisResult->SetColor(Quantity_Color(Quantity_NOC_RED));myObject3d.Append(anAisShape1);myObject3d.Append(anAisShape2);myObject3d.Append(anAisResult);myContext->SetDisplayMode(anAisShape1,0,Standard_True);myContext->SetDisplayMode(anAisShape2,0,Standard_True);myContext->SetDisplayMode(anAisResult,1,Standard_True);}