- if (theFeatures.empty())
- return ;
-
- // Create the list of edges with shared vertexes
- BOPAlgo_Builder aBuilder;
- BOPAlgo_PaveFiller aPF;
- TopoDS_Shape aFeaturesCompound;
-
- if (theFeatures.size() == 1)
- { // If there is only one feature, BOPAlgo_Builder will decline to work. Need to process it anyway
- aFeaturesCompound = theFeatures.front()->impl<TopoDS_Shape>();
- }
- else
- {
- std::list< boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theFeatures.begin();
- for (; anIt != theFeatures.end(); anIt++)
- {
- boost::shared_ptr<GeomAPI_Shape> aPreview(*anIt);
- aBuilder.AddArgument(aPreview->impl<TopoDS_Edge>());
- }
- aPF.SetArguments(aBuilder.Arguments());
- aPF.Perform();
- int aErr = aPF.ErrorStatus();
- if (aErr) return ;
- aBuilder.PerformWithFiller(aPF);
- aErr = aBuilder.ErrorStatus();
- if (aErr) return ;
- aFeaturesCompound = aBuilder.Shape();
- }
-
- BOPCol_IndexedDataMapOfShapeListOfShape aMapVE; // map between vertexes and edges
- BOPTools::MapShapesAndAncestors(aFeaturesCompound, TopAbs_VERTEX, TopAbs_EDGE, aMapVE);
- if (aMapVE.IsEmpty()) // in case of not-initialized circle
- return;
-
- gp_Dir aDirX = theDirX->impl<gp_Dir>();
- gp_Dir aDirY = theDirY->impl<gp_Dir>();
- gp_Dir aNorm = theNorm->impl<gp_Dir>();
-
- gp_Pln aPlane(theOrigin->impl<gp_Pnt>(), aNorm);
-
- // Set of edges used in loops
- std::set<Handle(TopoDS_TShape)> anEdgesInLoops;
- // Lists for processed vertexes and edges
- std::list<TopoDS_Shape> aProcVertexes;
- std::list<TopoDS_Shape> aProcEdges;
-
- // Search the start vertex
- TopoDS_Shape aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
- aProcVertexes.push_back(aStartVertex);
-
- TopoDS_Shape aCurVertex = aStartVertex;
- gp_Dir aCurDir = aDirY.Reversed();
- gp_Dir aCurNorm = aNorm.Reversed();
-
- // Go through the edges and find loops
- TopoDS_Shape aNextVertex;
- TopoDS_Shape aBindingEdge;
- gp_Dir aNextDir;
- while (aMapVE.Extent() > 0)
- {
- findNextVertex(aCurVertex, aMapVE, aCurDir, aCurNorm, aNextVertex, aBindingEdge, aNextDir);
- aCurNorm = aNorm;
-
- // Try to find next vertex in the list of already processed
- std::list<TopoDS_Shape>::iterator aVertIter = aProcVertexes.begin();
- std::list<TopoDS_Shape>::iterator anEdgeIter = aProcEdges.begin();
- for ( ; aVertIter != aProcVertexes.end(); aVertIter++)
- {
- if (aVertIter->TShape() == aNextVertex.TShape())
- break;
- if (anEdgeIter != aProcEdges.end())
- anEdgeIter++;
- }
-
- aProcVertexes.push_back(aNextVertex);
- aProcEdges.push_back(aBindingEdge);
-
- // The loop was found
- if (aVertIter != aProcVertexes.end())
- {
- // If the binding edge is a full circle, then the list may be empty before addition. Need to update edge iterator
- if (aProcEdges.size() == 1)
- anEdgeIter = aProcEdges.begin();
-
- if (aVertIter != aProcVertexes.begin())
- {
- // Check the orientation of the loop
- gp_Dir aCN = getOuterEdgeDirection(*anEdgeIter, *aVertIter);
- gp_Dir aCP = getOuterEdgeDirection(aProcEdges.back(), *aVertIter);
- aCN.Reverse();
- aCP.Reverse();
- if (aCN.DotCross(aCP, aNorm) < -tolerance)
- {
- // The found loop has wrong orientation and may contain sub-loops.
- // Need to check it onle again with another initial direction.
- aCurVertex = *aVertIter;
- do {
- aProcVertexes.pop_back();
- aProcEdges.pop_back();
- } while (aCurVertex != aProcVertexes.back());
- aCurDir = getOuterEdgeDirection(aProcEdges.back(), aCurVertex);
- aCurNorm = aNorm.Reversed();
- continue;
- }
- }
-
- // When the orientation is correct or the edges looped through
- // the first element, create new face and remove unnecessary edges.
- TopoDS_Face aPatch;
- createFace(*aVertIter, anEdgeIter, aProcEdges.end(), aPlane, aPatch);
- if (!aPatch.IsNull())
- {
- boost::shared_ptr<GeomAPI_Shape> aFace(new GeomAPI_Shape);
- aFace->setImpl(new TopoDS_Shape(aPatch));
- theResultFaces.push_back(aFace);
- }
- // push the edges used in the loop to the map
- std::list<TopoDS_Shape>::iterator anIter;
- for (anIter = anEdgeIter; anIter != aProcEdges.end(); anIter++)
- anEdgesInLoops.insert(anIter->TShape());
- // remove unnecessary edges
- std::list<TopoDS_Shape>::iterator aCopyVLoop = aVertIter;
- std::list<TopoDS_Shape>::iterator aCopyELoop = anEdgeIter;
- removeWasteEdges(aVertIter, anEdgeIter, aProcVertexes.end(), aProcEdges.end(), aMapVE);
-
- // revert the list of remaining edges
- std::list<TopoDS_Shape> aRemainVertexes;
- for ( ; aVertIter != aProcVertexes.end(); aVertIter++)
- aRemainVertexes.push_front(*aVertIter);
- std::list<TopoDS_Shape> aRemainEdges;
- for ( ; anEdgeIter != aProcEdges.end(); anEdgeIter++)
- aRemainEdges.push_front(*anEdgeIter);
- // remove edges and vertexes used in the loop and add remaining ones
- aProcVertexes.erase(aCopyVLoop, aProcVertexes.end());
- aProcVertexes.insert(aProcVertexes.end(), aRemainVertexes.begin(), aRemainVertexes.end());
- aProcEdges.erase(aCopyELoop, aProcEdges.end());
- aProcEdges.insert(aProcEdges.end(), aRemainEdges.begin(), aRemainEdges.end());
-
- // Recalculate current vertex and current direction
- if (!aProcVertexes.empty())
- {
- aNextVertex = aProcVertexes.back();
- if (!aProcEdges.empty())
- aNextDir = getOuterEdgeDirection(aProcEdges.back(), aNextVertex);
- else aNextDir = aDirY;
- }