For all issues regarding the Forums use, please, refer to the Forum Rules.

Our Solutions

Need professional assistance?
Consider our:

Support Offerings

 

Need to speed up your development?
Have a look at our:

Samples & Tools

 

Need some functionality extending standard OCCT capabilities?
Check out our:

Adv. Components

Related pages

BRepFeat_SplitShape::Build() does not work

Anonymous's picture
Forums: 

Hi All,

I downloaded the OCC and its demonstration samples (aparently not available in the source code form. If anyone knows how to get the source for them I'll appreciate a lot).
I am particularly interest in the splitshape example. So I run that particular sample (available in the 101_allSamples.zip) and it shows the code as follows;
// --- Code from the OCC demo samples ---
// define initial shell
TopoDS_Shell myShell;
// myShell = ...;

// define contour of the hole and its normal direction
TopoDS_Wire aCont;
gp_Dir aNormal(-0.2, -0.7, 0.4);
// aCont = ... ;

// project the contour on the shell along aNormal;
// for that create prism from the contour
// and find intersection with the shell
Standard_Real dist = 8;
gp_Vec aPrismVec(aNormal.Reversed().XYZ()*dist*2);
BRepPrimAPI_MakePrism aSweeper(aCont,aPrismVec);
if (!aSweeper.IsDone())
return;
TopoDS_Shape aPrism = aSweeper.Shape();

// make section of myShell by the prism
TopoDS_Wire aSecWire;
Standard_Boolean performNow = Standard_False;
BRepAlgoAPI_Section aSecAlgo (myShell, aPrism, performNow);
aSecAlgo.Approximation(Standard_True); // approximate new geometries
aSecAlgo.ComputePCurveOn1(Standard_True); // pcurves must be computed on shell
aSecAlgo.Build();
if (!aSecAlgo.IsDone())
return;
TopoDS_Shape aSection = aSecAlgo.Shape(); // get compound of section edges

// connect section edges to a wire
TopTools_ListOfShape aEdges;
TopExp_Explorer ex(aSection, TopAbs_EDGE);
if (!ex.More()) // is section empty?
return;
for (; ex.More(); ex.Next())
aEdges.Append(ex.Current());
BRepBuilderAPI_MakeWire aWMaker;
aWMaker.Add(aEdges);
if (!aWMaker.IsDone())
return;
TopoDS_Wire aSecWire = aWMaker.Wire();

aSecWire.Reverse(); // orient wire to have the left side as you need

// initialize splitting algo with the shell
BRepFeat_SplitShape aSplitter(myShell);

// add splitting edges
TopoDS_Iterator its(aSecWire);
for (; its.More(); its.Next())
{
const TopoDS_Edge& aEdge = TopoDS::Edge(its.Value());
// determine an ancestor face of this edge
TopoDS_Face aFace;
if (aSecAlgo.HasAncestorFaceOn1(aEdge, aFace))
aSplitter.Add (aEdge, aFace);
}

// perform splitting
aSplitter.Build();
if (!aSplitter.IsDone())
return;

// compose a new shell from the left side faces
TopoDS_Shell aNewShell;
aBld.MakeShell(aNewShell);
const TopTools_ListOfShape& aLF = aSplitter.Left();
TopTools_ListIteratorOfListOfShape it(aLF);
for (; it.More(); it.Next())
aBld.Add (aNewShell, it.Value());
// --- Code from the OCC demo samples ---

Here is my code where I have created a Dummy Shell and a profile. All appears well but the BRepFeat_SplitShape::Build() does not work.
// ------ My code starts --------
void CTopologyBuildingDoc::MakeDummyShell(TopoDS_Shell &aShell){
// AIS_ListOfInteractive aList;
// myAISContext->DisplayedObjects(aList);
// AIS_ListIteratorOfListOfInteractive aListIterator;
// for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
// myAISContext->Remove(aListIterator.Value());
// }

TColgp_Array2OfPnt Poles(1,2,1,4);
Poles.SetValue(1,1,gp_Pnt(0,0,0));
Poles.SetValue(1,2,gp_Pnt(0,10,2));
Poles.SetValue(1,3,gp_Pnt(0,20,10));
Poles.SetValue(1,4,gp_Pnt(0,30,0));
Poles.SetValue(2,1,gp_Pnt(10,0,5));
Poles.SetValue(2,2,gp_Pnt(10,10,3));
Poles.SetValue(2,3,gp_Pnt(10,20,20));
Poles.SetValue(2,4,gp_Pnt(10,30,0));

TColStd_Array1OfReal UKnots(1,2);
UKnots.SetValue(1,0);
UKnots.SetValue(2,1);
//UKnots.SetValue(3,2);

TColStd_Array1OfInteger UMults(1,2);
UMults.SetValue(1,2);
UMults.SetValue(2,2);

TColStd_Array1OfReal VKnots(1,3);
VKnots.SetValue(1,0);
VKnots.SetValue(2,1);
VKnots.SetValue(3,2);

TColStd_Array1OfInteger VMults(1,3);
VMults.SetValue(1,3);
VMults.SetValue(2,1);
VMults.SetValue(3,3);

Standard_Integer UDegree(1);
Standard_Integer VDegree(2);

Handle (Geom_BSplineSurface) BSpline = new Geom_BSplineSurface(Poles,UKnots,VKnots,UMults,VMults,UDegree,VDegree);
aShell = BRepBuilderAPI_MakeShell(BSpline);
Handle(AIS_Shape) anAISShell = new AIS_Shape(aShell);
myAISContext->SetDisplayMode(anAISShell, 0);
myAISContext->Display(anAISShell, Standard_False);
myAISContext->SetCurrentObject(anAISShell, Standard_False);
}

void CTopologyBuildingDoc::MakeDummyWire(TopoDS_Wire &aWire){
// AIS_ListOfInteractive aList;
// myAISContext->DisplayedObjects(aList);
// AIS_ListIteratorOfListOfInteractive aListIterator;
// for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
// myAISContext->Remove(aListIterator.Value());
// }

gp_Pnt pt1 = gp_Pnt(2.5, 5.0, 0.0);
gp_Pnt pt2 = gp_Pnt(7.5, 5.0, 0.0);
gp_Pnt pt3 = gp_Pnt(7.5, 15.0, 0.0);
gp_Pnt pt4 = gp_Pnt(2.5, 15.0, 0.0);

TopoDS_Edge E1 = BRepBuilderAPI_MakeEdge(pt1, pt2);
TopoDS_Edge E2 = BRepBuilderAPI_MakeEdge(pt2, pt3);
TopoDS_Edge E3 = BRepBuilderAPI_MakeEdge(pt3, pt4);
TopoDS_Edge E4 = BRepBuilderAPI_MakeEdge(pt4, pt1);

aWire = BRepBuilderAPI_MakeWire(E1, E2, E3, E4);
Handle(AIS_Shape) anAISWire = new AIS_Shape(aWire);
myAISContext->SetDisplayMode(anAISWire,0);
myAISContext->Display(anAISWire,Standard_False);
}

void CTopologyBuildingDoc::OnShell()
{
AfxMessageBox("New Test");
// define initial shell
TopoDS_Shell myShell;
MakeDummyShell(myShell);
//TopoDS_Shape myShell = BRepPrimAPI_MakeBox(gp_Pnt(0,6,8),15,20,17);
//Handle(AIS_Shape) ais1 = new AIS_Shape(myShell);
//myAISContext->SetColor(ais1,Quantity_NOC_RED,Standard_False);
//myAISContext->SetMaterial(ais1,Graphic3d_NOM_PLASTIC,Standard_False);
//myAISContext->Display(ais1,Standard_False);
//myAISContext->SetCurrentObject(ais1,Standard_False);
//Fit();

// define contour of the hole and its normal direction
TopoDS_Wire aCont;
MakeDummyWire(aCont);

gp_Dir aNormal(0.0, 0.0, -1.0);

// project the contour on the shell along aNormal;
// for that create prism from the contour
// and find intersection with the shell
Standard_Real dist = 8;
gp_Vec aPrismVec(aNormal.Reversed().XYZ()*dist*2);
BRepPrimAPI_MakePrism aSweeper(aCont,aPrismVec);
if (!aSweeper.IsDone())
return;
TopoDS_Shape aPrism = aSweeper.Shape();
//Handle(AIS_Shape) anAISPrism = new AIS_Shape(aPrism);
//myAISContext->SetDisplayMode(anAISPrism, 0);
//myAISContext->Display(anAISPrism, Standard_False);

// make section of myShell by the prism
Standard_Boolean performNow = Standard_False;
//BRepAlgoAPI_Section aSecAlgo (myShell, aPrism, performNow);
BRepAlgoAPI_Section aSecAlgo(myShell, gp_Pln(1,2,1,-15), performNow);
aSecAlgo.Approximation(Standard_True); // approximate new geometries
aSecAlgo.ComputePCurveOn1(Standard_True); // pcurves must be computed on shell
aSecAlgo.Build();
if (!aSecAlgo.IsDone())
return;
TopoDS_Shape aSection = aSecAlgo.Shape(); // get compound of section edges
Handle(AIS_Shape) anAISSection = new AIS_Shape(aSection);
myAISContext->SetDisplayMode(anAISSection, 0);
myAISContext->Display(anAISSection, Standard_False);
// connect section edges to a wire
TopTools_ListOfShape aEdges;
TopExp_Explorer ex(aSection, TopAbs_EDGE);
if (!ex.More()) // is section empty?
return;
for (; ex.More(); ex.Next())
aEdges.Append(ex.Current());
BRepBuilderAPI_MakeWire aWMaker;
aWMaker.Add(aEdges);
if (!aWMaker.IsDone())
return;
TopoDS_Wire aSecWire = aWMaker.Wire();

aSecWire.Reverse(); // orient wire to have the left side as you need

// initialize splitting algo with the shell
BRepFeat_SplitShape aSplitter(myShell);

// add splitting edges
TopoDS_Iterator its(aSecWire);
for (; its.More(); its.Next()){
const TopoDS_Edge& aEdge = TopoDS::Edge(its.Value());
// determine an ancestor face of this edge
TopoDS_Face aFace;
if (aSecAlgo.HasAncestorFaceOn1(aEdge, aFace))
aSplitter.Add (aEdge, aFace);
}

// BRepFeat_SplitShape aSplitter(myShell);
// for (TopExp_Explorer Ex(aSection,TopAbs_EDGE); Ex.More(); Ex.Next()) {
// TopoDS_Shape anEdge = Ex.Current();
// TopoDS_Shape aFace;
// if (aSecAlgo.HasAncestorFaceOn1(anEdge, aFace)) {
// TopoDS_Face F = TopoDS::Face(aFace);
// TopoDS_Edge E = TopoDS::Edge(anEdge);
// aSplitter.Add(E, F);
// }
// }
aSplitter.Build();

if (!aSplitter.IsDone()){
AfxMessageBox("BRepFeat_SplitShape::Build() failed");
return;
}
Handle(AIS_Shape) anAISSplitter = new AIS_Shape(aSplitter);
myAISContext->SetDisplayMode(anAISSplitter, 0);
myAISContext->Display(anAISSplitter, Standard_False);

BRep_Builder aBld;
TopoDS_Compound Comp;
aBld.MakeCompound(Comp);
// compose a new shell from the left side faces
TopoDS_Shell aNewShell;
aBld.MakeShell(aNewShell);
const TopTools_ListOfShape& aLF = aSplitter.Left();
TopTools_ListIteratorOfListOfShape it(aLF);
for (; it.More(); it.Next())
aBld.Add (aNewShell, it.Value());

Handle(AIS_Shape) white = new AIS_Shape(Comp);
myAISContext->SetDisplayMode(white, 0);
myAISContext->Display(white, Standard_False);
Fit();
}
// ------ My code ends --------

Thanks,
Yogesh Dhakad