1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_Partition.h"
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
26 #include <GEOMAlgo_Splitter.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopoDS_Builder.hxx>
30 #include <TopTools_MapOfShape.hxx>
32 //=================================================================================================
33 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
34 const ListOfShape& theTools)
36 GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
37 if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
38 return aPartitionAlgo.shape();
40 return std::shared_ptr<GeomAPI_Shape>();
43 //=================================================================================================
44 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
45 const ListOfShape& theTools)
47 build(theObjects, theTools);
50 static void prepareShapes(const TopoDS_Shape& theShape,
51 TopTools_ListOfShape& theSimpleList)
53 if (theShape.ShapeType() != TopAbs_COMPOUND) {
54 theSimpleList.Append(theShape);
58 // explode compound on simple shapes to allow their intersections
59 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
60 for (; It.More(); It.Next()) {
61 TopoDS_Shape curSh = It.Value();
62 prepareShapes(curSh, theSimpleList);
66 //=================================================================================================
67 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
68 const ListOfShape& theTools)
70 if (theObjects.empty()) {
74 // Creating partition operation.
75 GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
76 this->setImpl(anOperation);
77 this->setBuilderType(OCCT_BOPAlgo_Builder);
79 TopTools_MapOfShape ShapesMap;
81 for(ListOfShape::const_iterator
82 anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
83 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
84 // #2240: decompose compounds to get the valid result
85 TopTools_ListOfShape aSimpleShapes;
86 prepareShapes(aShape, aSimpleShapes);
87 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
88 for (; aSimpleIter.More(); aSimpleIter.Next()) {
89 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
90 if (ShapesMap.Add(aSimpleSh)) {
91 anOperation->AddArgument(aSimpleSh);
97 for (ListOfShape::const_iterator
98 aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
99 const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
100 anOperation->AddTool(aShape);
103 // Building and getting result.
104 anOperation->Perform();
105 if(anOperation->ErrorStatus() != 0) {
108 TopoDS_Shape aResult = anOperation->Shape();
110 if(aResult.ShapeType() == TopAbs_COMPOUND) {
111 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
112 aGeomShape->setImpl(new TopoDS_Shape(aResult));
113 aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
117 if(aResult.IsNull()) {
120 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
121 aShape->setImpl(new TopoDS_Shape(aResult));
122 this->setShape(aShape);