]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Partition.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into Dev_2.8.0
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Partition.h"
22
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25
26 #include <GEOMAlgo_Splitter.hxx>
27
28 #include <TopExp_Explorer.hxx>
29 #include <TopoDS_Builder.hxx>
30 #include <TopTools_MapOfShape.hxx>
31
32 //=================================================================================================
33 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
34                                                            const ListOfShape& theTools)
35 {
36   GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
37   if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
38     return aPartitionAlgo.shape();
39   }
40   return std::shared_ptr<GeomAPI_Shape>();
41 }
42
43 //=================================================================================================
44 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
45                                              const ListOfShape& theTools)
46 {
47   build(theObjects, theTools);
48 }
49
50 static void prepareShapes(const TopoDS_Shape&   theShape,
51                            TopTools_ListOfShape& theSimpleList)
52 {
53   if (theShape.ShapeType() != TopAbs_COMPOUND) {
54       theSimpleList.Append(theShape);
55     return;
56   }
57
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);
63   }
64 }
65
66 //=================================================================================================
67 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
68                                   const ListOfShape& theTools)
69 {
70   if (theObjects.empty()) {
71     return;
72   }
73
74   // Creating partition operation.
75   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
76   this->setImpl(anOperation);
77   this->setBuilderType(OCCT_BOPAlgo_Builder);
78
79   TopTools_MapOfShape ShapesMap;
80   // Getting objects.
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);
92       }
93     }
94   }
95
96   // Getting tools.
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);
101   }
102
103   // Building and getting result.
104   anOperation->Perform();
105   if(anOperation->ErrorStatus() != 0) {
106     return;
107   }
108   TopoDS_Shape aResult = anOperation->Shape();
109
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>();
114   }
115
116   // Setting result.
117   if(aResult.IsNull()) {
118     return;
119   }
120   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
121   aShape->setImpl(new TopoDS_Shape(aResult));
122   this->setShape(aShape);
123   this->setDone(true);
124 }