+#include <BOPAlgo_PaveFiller.hxx>
+#include <BOPAlgo_MakerVolume.hxx>
+
+#include <list>
+
+/**
+ * \brief This static function converts the list of shapes into an array
+ * of their IDs. If the input list is empty, null handle will be returned.
+ * this method doesn't check if a shape presents in theIndices map.
+ *
+ * \param theListOfShapes the list of shapes.
+ * \param theIndices the indexed map of shapes.
+ * \return the array of shape IDs.
+ */
+static Handle(TColStd_HArray1OfInteger) GetShapeIDs
+ (const TopTools_ListOfShape &theListOfShapes,
+ const TopTools_IndexedMapOfShape &theIndices)
+{
+ Handle(TColStd_HArray1OfInteger) aResult;
+
+ if (!theListOfShapes.IsEmpty()) {
+ const Standard_Integer aNbShapes = theListOfShapes.Extent();
+ TopTools_ListIteratorOfListOfShape anIter(theListOfShapes);
+ Standard_Integer i;
+
+ aResult = new TColStd_HArray1OfInteger(1, aNbShapes);
+
+ for (i = 1; anIter.More(); anIter.Next(), ++i) {
+ const TopoDS_Shape &aShape = anIter.Value();
+ const Standard_Integer anIndex = theIndices.FindIndex(aShape);
+
+ aResult->SetValue(i, anIndex);
+ }
+ }
+
+ return aResult;
+}
+
+namespace
+{
+ // check that compound includes only shapes of expected type
+ bool checkCompound( TopoDS_Shape& c, TopAbs_ShapeEnum t )
+ {
+ TopoDS_Iterator it( c, Standard_True, Standard_True );
+
+ // empty compound is OK only if we explicitly create a compound of shapes
+ bool result = true;
+
+ // => if expected type is TopAbs_SHAPE, we allow compound consisting of any shapes, this above check is enough
+ // => otherwise we have to check compound's content
+ // => compound sometimes can contain enclosed compound(s), we process them recursively and rebuild initial compound
+
+ if ( t != TopAbs_SHAPE ) {
+ result = it.More();
+ std::list<TopoDS_Shape> compounds, shapes;
+ compounds.push_back( c );
+ while ( !compounds.empty() && result ) {
+ // check that compound contains only shapes of expected type
+ TopoDS_Shape cc = compounds.front();
+ compounds.pop_front();
+ it.Initialize( cc, Standard_True, Standard_True );
+ for ( ; it.More() && result; it.Next() ) {
+ TopAbs_ShapeEnum tt = it.Value().ShapeType();
+ if ( tt == TopAbs_COMPOUND || tt == TopAbs_COMPSOLID ) {
+ compounds.push_back( it.Value() );
+ continue;
+ }
+ shapes.push_back( it.Value() );
+ result = tt == t;
+ }
+ }
+ if ( result ) {
+ if ( shapes.empty() ) {
+ result = false;
+ }
+ else if ( shapes.size() == 1 ) {
+ c = shapes.front();
+ }
+ else {
+ BRep_Builder b;
+ TopoDS_Compound newc;
+ b.MakeCompound( newc );
+ std::list<TopoDS_Shape> ::const_iterator sit;
+ for ( sit = shapes.begin(); sit != shapes.end(); ++sit )
+ b.Add( newc, *sit );
+ c = newc;
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * This function adds faces from the input shape into the list of faces. If
+ * the input shape is a face, it is added itself. If it is a shell, its
+ * sub-shapes (faces) are added. If it is a compound, its sub-shapes
+ * (faces or shells) are added in the list. For null shapes and for other
+ * types of shapes an exception is thrown.
+ *
+ * @param theShape the shape to be added. Either face or shell or a compound
+ * of faces and/or shells.
+ * @param theListFaces the list of faces that is modified on output.
+ * @param theMapFence the map that protects from adding the same faces in
+ * the list.
+ */
+ void addFaces(const TopoDS_Shape &theShape,
+ TopTools_ListOfShape &theListFaces,
+ TopTools_MapOfShape &theMapFence)
+ {
+ if (theShape.IsNull()) {
+ Standard_NullObject::Raise("Face for shell construction is null");
+ }
+
+ // Append the shape is the mapFence
+ if (theMapFence.Add(theShape)) {
+ // Shape type
+ const TopAbs_ShapeEnum aType = theShape.ShapeType();
+
+ if (aType == TopAbs_FACE) {
+ theListFaces.Append(theShape);
+ } else if (aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
+ TopoDS_Iterator anIter(theShape);
+
+ for (; anIter.More(); anIter.Next()) {
+ // Add sub-shapes: faces for shell or faces/shells for compound.
+ const TopoDS_Shape &aSubShape = anIter.Value();
+
+ addFaces(aSubShape, theListFaces, theMapFence);
+ }
+ } else {
+ Standard_TypeMismatch::Raise
+ ("Shape for shell construction is neither a shell nor a face");
+ }
+ }
+ }
+
+ /**
+ * This function constructs a shell or a compound of shells
+ * from a set of faces and/or shells.
+ *
+ * @param theShapes is a set of faces, shells and/or
+ * compounds of faces/shells.
+ * @return a shell or a compound of shells.
+ */
+ TopoDS_Shape makeShellFromFaces
+ (const Handle(TColStd_HSequenceOfTransient) &theShapes)
+ {
+ const Standard_Integer aNbShapes = theShapes->Length();
+ Standard_Integer i;
+ TopTools_ListOfShape aListFaces;
+ TopTools_MapOfShape aMapFence;
+ BRep_Builder aBuilder;
+
+ // Fill the list of unique faces
+ for (i = 1; i <= aNbShapes; ++i) {
+ // Function
+ const Handle(GEOM_Function) aRefShape =
+ Handle(GEOM_Function)::DownCast(theShapes->Value(i));
+
+ if (aRefShape.IsNull()) {
+ Standard_NullObject::Raise("Face for shell construction is null");
+ }
+
+ // Shape
+ const TopoDS_Shape aShape = aRefShape->GetValue();
+
+ addFaces(aShape, aListFaces, aMapFence);
+ }
+
+ // Perform computation of shells.
+ TopTools_ListOfShape aListShells;
+ TopTools_ListIteratorOfListOfShape anIter;
+
+ while (!aListFaces.IsEmpty()) {
+ // Perform sewing
+ BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
+
+ for (anIter.Initialize(aListFaces); anIter.More(); anIter.Next()) {
+ aSewing.Add(anIter.Value());
+ }
+
+ aSewing.Perform();
+
+ // Fill list of shells.
+ const TopoDS_Shape &aSewed = aSewing.SewedShape();
+ TopExp_Explorer anExp(aSewed, TopAbs_SHELL);
+ Standard_Boolean isNewShells = Standard_False;
+
+ // Append shells
+ for (; anExp.More(); anExp.Next()) {
+ aListShells.Append(anExp.Current());
+ isNewShells = Standard_True;
+ }
+
+ // Append single faces.
+ anExp.Init(aSewed, TopAbs_FACE, TopAbs_SHELL);
+
+ for (; anExp.More(); anExp.Next()) {
+ TopoDS_Shell aShell;
+
+ aBuilder.MakeShell(aShell);
+ aBuilder.Add(aShell, anExp.Current());
+ aListShells.Append(aShell);
+ isNewShells = Standard_True;
+ }
+
+ if (!isNewShells) {
+ // There are no more shell can be obtained. Break the loop.
+ break;
+ }
+
+ // Remove faces that are in the result from the list.
+ TopTools_IndexedMapOfShape aMapFaces;
+
+ TopExp::MapShapes(aSewed, TopAbs_FACE, aMapFaces);
+
+ // Add deleted faces to the map
+ const Standard_Integer aNbDelFaces = aSewing.NbDeletedFaces();
+
+ for (i = 1; i <= aNbDelFaces; ++i) {
+ aMapFaces.Add(aSewing.DeletedFace(i));
+ }
+
+ for (anIter.Initialize(aListFaces); anIter.More();) {
+ const TopoDS_Shape &aFace = anIter.Value();
+ Standard_Boolean isFaceUsed = Standard_False;
+
+ if (aMapFaces.Contains(aFace) || aSewing.IsModified(aFace)) {
+ // Remove face from the list.
+ aListFaces.Remove(anIter);
+ } else {
+ // Go to the next face.
+ anIter.Next();
+ }
+ }
+ }
+
+ // If there are faces not used in shells create a shell for each face.
+ for (anIter.Initialize(aListFaces); anIter.More(); anIter.Next()) {
+ TopoDS_Shell aShell;
+
+ aBuilder.MakeShell(aShell);
+ aBuilder.Add(aShell, anIter.Value());
+ aListShells.Append(aShell);
+ }
+
+ // Construct the result that can be either a shell or a compound of shells
+ TopoDS_Shape aResult;
+
+ if (!aListShells.IsEmpty()) {
+ if (aListShells.Extent() == 1) {
+ aResult = aListShells.First();
+ } else {
+ // There are more then one shell.
+ TopoDS_Compound aCompound;
+
+ aBuilder.MakeCompound(aCompound);
+
+ for (anIter.Initialize(aListShells); anIter.More(); anIter.Next()) {
+ aBuilder.Add(aCompound, anIter.Value());
+ }
+
+ aResult = aCompound;
+ }
+ }
+
+ return aResult;
+ }
+
+ // End of namespace
+}
+