Salome HOME
Initial (compilation) porting to SALOME 8.5.0 current on Debian 8
[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 #include <GeomAlgoAPI_SortListOfShapes.h>
26
27 #include <GEOMAlgo_Splitter.hxx>
28
29 #include <NCollection_Vector.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <TopoDS_Builder.hxx>
32 #include <TopTools_MapOfShape.hxx>
33
34
35 //=================================================================================================
36 static bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
37 {
38   for(TopExp_Explorer anExp(theShape, theSubShape.ShapeType()); anExp.More(); anExp.Next()) {
39     if(theSubShape.IsSame(anExp.Current())) {
40       return true;
41     }
42   }
43
44   return false;
45 }
46
47 //=================================================================================================
48 static void sortCompound(TopoDS_Shape& theCompound)
49 {
50   ListOfShape aCombiningShapes;
51   for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) {
52     GeomShapePtr aSub(new GeomAPI_Shape);
53     aSub->setImpl(new TopoDS_Shape(anIt.Value()));
54     aCombiningShapes.push_back(aSub);
55   }
56
57   // sort sub-shapes of compound to stabilize the sequence of the Partition's results
58   GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes);
59
60   TopoDS_Compound aTempCompound;
61   TopoDS_Builder aBuilder;
62   aBuilder.MakeCompound(aTempCompound);
63   for (ListOfShape::iterator anIt = aCombiningShapes.begin();
64        anIt != aCombiningShapes.end(); ++anIt)
65     aBuilder.Add(aTempCompound, (*anIt)->impl<TopoDS_Shape>());
66   theCompound = aTempCompound;
67 }
68
69 //=================================================================================================
70 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
71                                                            const ListOfShape& theTools)
72 {
73   GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
74   if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
75     return aPartitionAlgo.shape();
76   }
77   return std::shared_ptr<GeomAPI_Shape>();
78 }
79
80 //=================================================================================================
81 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
82                                              const ListOfShape& theTools)
83 {
84   build(theObjects, theTools);
85 }
86
87 static void prepareShapes(const TopoDS_Shape&   theShape,
88                            TopTools_ListOfShape& theSimpleList)
89 {
90   if (theShape.ShapeType() != TopAbs_COMPOUND) {
91       theSimpleList.Append(theShape);
92     return;
93   }
94
95   // explode compound on simple shapes to allow their intersections
96   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
97   for (; It.More(); It.Next()) {
98     TopoDS_Shape curSh = It.Value();
99     prepareShapes(curSh, theSimpleList);
100   }
101 }
102
103 //=================================================================================================
104 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
105                                   const ListOfShape& theTools)
106 {
107   if (theObjects.empty()) {
108     return;
109   }
110
111   // Creating partition operation.
112   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
113   this->setImpl(anOperation);
114   this->setBuilderType(OCCT_BOPAlgo_Builder);
115
116   TopTools_MapOfShape ShapesMap;
117   // Getting objects.
118   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
119       anObjectsIt != theObjects.end();
120       anObjectsIt++)
121   {
122     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
123     // #2240: decompose compounds to get the valid result
124     TopTools_ListOfShape aSimpleShapes;
125     prepareShapes(aShape, aSimpleShapes);
126     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
127     for (; aSimpleIter.More(); aSimpleIter.Next()) {
128       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
129       if (ShapesMap.Add(aSimpleSh)) {
130         anOperation->AddArgument(aSimpleSh);
131       }
132     }
133   }
134
135   // Getting tools.
136   for (ListOfShape::const_iterator
137        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
138     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
139     // #2419: decompose compounds to get the valid result
140     TopTools_ListOfShape aSimpleShapes;
141     prepareShapes(aShape, aSimpleShapes);
142     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
143     for (; aSimpleIter.More(); aSimpleIter.Next()) {
144       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
145       if (ShapesMap.Add(aSimpleSh)) {
146         anOperation->AddTool(aSimpleSh);
147       }
148     }
149   }
150
151   // Building and getting result.
152   anOperation->Perform();
153   if (anOperation->HasErrors())
154     return;
155   TopoDS_Shape aResult = anOperation->Shape();
156
157   if(aResult.ShapeType() == TopAbs_COMPOUND) {
158     // Exclude faces and edges which are shared as another sub-shape.
159     NCollection_Vector<TopoDS_Shape> aFaces;
160     NCollection_Vector<TopoDS_Shape> anEdges;
161     TopoDS_Compound aTempCompound;
162     TopoDS_Builder aBuilder;
163     aBuilder.MakeCompound(aTempCompound);
164     for(TopoDS_Iterator anIt(aResult);
165         anIt.More();
166         anIt.Next()) {
167       const TopoDS_Shape& aSubShape = anIt.Value();
168       if (aSubShape.ShapeType() == TopAbs_FACE) {
169         aFaces.Append(aSubShape);
170       } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
171         anEdges.Append(aSubShape);
172       } else {
173         aBuilder.Add(aTempCompound, aSubShape);
174       }
175     }
176
177     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
178         anIt.More();
179         anIt.Next())
180     {
181       const TopoDS_Shape& aSubShape = anIt.Value();
182       if (!isSubShape(aTempCompound, aSubShape))
183       {
184         aBuilder.Add(aTempCompound, aSubShape);
185       }
186     }
187
188     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
189         anIt.More();
190         anIt.Next())
191     {
192       const TopoDS_Shape& aSubShape = anIt.Value();
193       if (!isSubShape(aTempCompound, aSubShape))
194       {
195         aBuilder.Add(aTempCompound, aSubShape);
196       }
197     }
198
199     aResult = aTempCompound;
200   }
201
202   if(aResult.ShapeType() == TopAbs_COMPOUND) {
203     // sort sub-shapes of compound before creation of a compsolid
204     sortCompound(aResult);
205
206     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
207     aGeomShape->setImpl(new TopoDS_Shape(aResult));
208     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
209   }
210
211   // Setting result.
212   if(aResult.IsNull()) {
213     return;
214   }
215   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
216   aShape->setImpl(new TopoDS_Shape(aResult));
217   this->setShape(aShape);
218   this->setDone(true);
219 }