Salome HOME
Coding rules satisfaction
[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 #include <BRepTools_History.hxx>
34
35
36 //=================================================================================================
37 static bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
38 {
39   for(TopExp_Explorer anExp(theShape, theSubShape.ShapeType()); anExp.More(); anExp.Next()) {
40     if(theSubShape.IsSame(anExp.Current())) {
41       return true;
42     }
43   }
44
45   return false;
46 }
47
48 //=================================================================================================
49 void getHistorySupportedType(const TopoDS_Shape& theShape, TopTools_ListOfShape& theResult) {
50   if (BRepTools_History::IsSupportedType(theShape)) {
51     theResult.Append(theShape);
52   } else {
53     for (TopoDS_Iterator aSubIter(theShape); aSubIter.More(); aSubIter.Next()) {
54       getHistorySupportedType(aSubIter.Value(), theResult);
55     }
56   }
57 }
58
59 //=================================================================================================
60 // Operation is used for production of ordered sorting: generated/modified from the first argument
61 // must be located in hte result first, etc. THis is for the issue #2517
62 static void sortCompound(TopoDS_Shape& theCompound, GEOMAlgo_Splitter* theOperation)
63 {
64   TopoDS_Compound aResCompound;
65   TopoDS_Builder aBuilder;
66   aBuilder.MakeCompound(aResCompound);
67
68   TopTools_MapOfShape anAlreadyThere; // to avoid duplications if it was produced by two arguments
69
70   // flag to add to result also results which were not produced by any argument
71   bool aNotProduced = true;
72   TopTools_ListOfShape::Iterator anArgs(theOperation->Arguments());
73   while(aNotProduced || anArgs.More()) {
74     // collect shapes that were produced from the current argument
75     TopTools_MapOfShape aProducedByArg;
76     if (anArgs.More()) {
77       TopTools_ListOfShape allArgs;
78       getHistorySupportedType(anArgs.Value(), allArgs);
79       for (TopTools_ListOfShape::Iterator argsIter(allArgs); argsIter.More(); argsIter.Next()) {
80         // if argument was not modified, it is fully in the result
81         aProducedByArg.Add(argsIter.Value());
82         const TopTools_ListOfShape& aModified = theOperation->Modified(argsIter.Value());
83         for (TopTools_ListOfShape::Iterator aModIter(aModified); aModIter.More(); aModIter.Next()) {
84           aProducedByArg.Add(aModIter.Value());
85         }
86         const TopTools_ListOfShape& aGenerated = theOperation->Generated(argsIter.Value());
87         for (TopTools_ListOfShape::Iterator aGenIter(aGenerated); aGenIter.More(); aGenIter.Next())
88         {
89           aProducedByArg.Add(aGenIter.Value());
90         }
91       }
92       anArgs.Next();
93     }
94     else {
95       aNotProduced = false;
96     }
97
98     ListOfShape aCombiningShapes;
99     for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) {
100       bool aProducedContains = false;
101       if (aNotProduced) { // collect all supported type-shapes of result
102         TopTools_ListOfShape allRes;
103         getHistorySupportedType(anIt.Value(), allRes);
104         for (TopTools_ListOfShape::Iterator aResIter(allRes); aResIter.More(); aResIter.Next()) {
105           if (aProducedByArg.Contains(aResIter.Value())) {
106             aProducedContains = true;
107             break;
108           }
109         }
110       }
111       if ((!aNotProduced || aProducedContains) && anAlreadyThere.Add(anIt.Value())) {
112         GeomShapePtr aSub(new GeomAPI_Shape);
113         aSub->setImpl(new TopoDS_Shape(anIt.Value()));
114         aCombiningShapes.push_back(aSub);
115       }
116     }
117
118     // sort sub-shapes of compound to stabilize the sequence of the Partition's results
119     GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes);
120
121     for (ListOfShape::iterator anIt = aCombiningShapes.begin();
122       anIt != aCombiningShapes.end(); ++anIt)
123       aBuilder.Add(aResCompound, (*anIt)->impl<TopoDS_Shape>());
124   }
125
126   theCompound = aResCompound;
127 }
128
129 //=================================================================================================
130 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
131                                                            const ListOfShape& theTools)
132 {
133   GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
134   if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
135     return aPartitionAlgo.shape();
136   }
137   return std::shared_ptr<GeomAPI_Shape>();
138 }
139
140 //=================================================================================================
141 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
142                                              const ListOfShape& theTools)
143 {
144   build(theObjects, theTools);
145 }
146
147 static void prepareShapes(const TopoDS_Shape&   theShape,
148                            TopTools_ListOfShape& theSimpleList)
149 {
150   if (theShape.ShapeType() != TopAbs_COMPOUND) {
151       theSimpleList.Append(theShape);
152     return;
153   }
154
155   // explode compound on simple shapes to allow their intersections
156   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
157   for (; It.More(); It.Next()) {
158     TopoDS_Shape curSh = It.Value();
159     prepareShapes(curSh, theSimpleList);
160   }
161 }
162
163 //=================================================================================================
164 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
165                                   const ListOfShape& theTools)
166 {
167   if (theObjects.empty()) {
168     return;
169   }
170
171   // Creating partition operation.
172   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
173   this->setImpl(anOperation);
174   this->setBuilderType(OCCT_BOPAlgo_Builder);
175
176   TopTools_MapOfShape ShapesMap;
177   // Getting objects.
178   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
179       anObjectsIt != theObjects.end();
180       anObjectsIt++)
181   {
182     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
183     // #2240: decompose compounds to get the valid result
184     TopTools_ListOfShape aSimpleShapes;
185     prepareShapes(aShape, aSimpleShapes);
186     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
187     for (; aSimpleIter.More(); aSimpleIter.Next()) {
188       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
189       if (ShapesMap.Add(aSimpleSh)) {
190         anOperation->AddArgument(aSimpleSh);
191       }
192     }
193   }
194
195   // Getting tools.
196   for (ListOfShape::const_iterator
197        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
198     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
199     // #2419: decompose compounds to get the valid result
200     TopTools_ListOfShape aSimpleShapes;
201     prepareShapes(aShape, aSimpleShapes);
202     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
203     for (; aSimpleIter.More(); aSimpleIter.Next()) {
204       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
205       if (ShapesMap.Add(aSimpleSh)) {
206         anOperation->AddTool(aSimpleSh);
207       }
208     }
209   }
210
211   // Building and getting result.
212   anOperation->Perform();
213   if (anOperation->HasErrors())
214     return;
215   TopoDS_Shape aResult = anOperation->Shape();
216
217   if(aResult.ShapeType() == TopAbs_COMPOUND) {
218     // Exclude faces and edges which are shared as another sub-shape.
219     NCollection_Vector<TopoDS_Shape> aFaces;
220     NCollection_Vector<TopoDS_Shape> anEdges;
221     TopoDS_Compound aTempCompound;
222     TopoDS_Builder aBuilder;
223     aBuilder.MakeCompound(aTempCompound);
224     for(TopoDS_Iterator anIt(aResult);
225         anIt.More();
226         anIt.Next()) {
227       const TopoDS_Shape& aSubShape = anIt.Value();
228       if (aSubShape.ShapeType() == TopAbs_FACE) {
229         aFaces.Append(aSubShape);
230       } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
231         anEdges.Append(aSubShape);
232       } else {
233         aBuilder.Add(aTempCompound, aSubShape);
234       }
235     }
236
237     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
238         anIt.More();
239         anIt.Next())
240     {
241       const TopoDS_Shape& aSubShape = anIt.Value();
242       if (!isSubShape(aTempCompound, aSubShape))
243       {
244         aBuilder.Add(aTempCompound, aSubShape);
245       }
246     }
247
248     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
249         anIt.More();
250         anIt.Next())
251     {
252       const TopoDS_Shape& aSubShape = anIt.Value();
253       if (!isSubShape(aTempCompound, aSubShape))
254       {
255         aBuilder.Add(aTempCompound, aSubShape);
256       }
257     }
258
259     aResult = aTempCompound;
260   }
261
262   if(aResult.ShapeType() == TopAbs_COMPOUND) {
263     // sort sub-shapes of compound before creation of a compsolid
264     sortCompound(aResult, anOperation);
265
266     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
267     aGeomShape->setImpl(new TopoDS_Shape(aResult));
268     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
269   }
270
271   // Setting result.
272   if(aResult.IsNull()) {
273     return;
274   }
275   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
276   aShape->setImpl(new TopoDS_Shape(aResult));
277   this->setShape(aShape);
278   this->setDone(true);
279 }