Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[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 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
131                                              const ListOfShape& theTools)
132 {
133   build(theObjects, theTools);
134 }
135
136 static void prepareShapes(const TopoDS_Shape&   theShape,
137                            TopTools_ListOfShape& theSimpleList)
138 {
139   if (theShape.ShapeType() != TopAbs_COMPOUND) {
140       theSimpleList.Append(theShape);
141     return;
142   }
143
144   // explode compound on simple shapes to allow their intersections
145   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
146   for (; It.More(); It.Next()) {
147     TopoDS_Shape curSh = It.Value();
148     prepareShapes(curSh, theSimpleList);
149   }
150 }
151
152 //=================================================================================================
153 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
154                                   const ListOfShape& theTools)
155 {
156   if (theObjects.empty()) {
157     return;
158   }
159
160   // Creating partition operation.
161   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
162   this->setImpl(anOperation);
163   this->setBuilderType(OCCT_BOPAlgo_Builder);
164
165   TopTools_MapOfShape ShapesMap;
166   // Getting objects.
167   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
168       anObjectsIt != theObjects.end();
169       anObjectsIt++)
170   {
171     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
172     // #2240: decompose compounds to get the valid result
173     TopTools_ListOfShape aSimpleShapes;
174     prepareShapes(aShape, aSimpleShapes);
175     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
176     for (; aSimpleIter.More(); aSimpleIter.Next()) {
177       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
178       if (ShapesMap.Add(aSimpleSh)) {
179         anOperation->AddArgument(aSimpleSh);
180       }
181     }
182   }
183
184   // Getting tools.
185   for (ListOfShape::const_iterator
186        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
187     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
188     // #2419: decompose compounds to get the valid result
189     TopTools_ListOfShape aSimpleShapes;
190     prepareShapes(aShape, aSimpleShapes);
191     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
192     for (; aSimpleIter.More(); aSimpleIter.Next()) {
193       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
194       if (ShapesMap.Add(aSimpleSh)) {
195         anOperation->AddTool(aSimpleSh);
196       }
197     }
198   }
199
200   // Building and getting result.
201   anOperation->Perform();
202   if (anOperation->HasErrors())
203     return;
204   TopoDS_Shape aResult = anOperation->Shape();
205
206   if(aResult.ShapeType() == TopAbs_COMPOUND) {
207     // Exclude faces and edges which are shared as another sub-shape.
208     NCollection_Vector<TopoDS_Shape> aFaces;
209     NCollection_Vector<TopoDS_Shape> anEdges;
210     TopoDS_Compound aTempCompound;
211     TopoDS_Builder aBuilder;
212     aBuilder.MakeCompound(aTempCompound);
213     for(TopoDS_Iterator anIt(aResult);
214         anIt.More();
215         anIt.Next()) {
216       const TopoDS_Shape& aSubShape = anIt.Value();
217       if (aSubShape.ShapeType() == TopAbs_FACE) {
218         aFaces.Append(aSubShape);
219       } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
220         anEdges.Append(aSubShape);
221       } else {
222         aBuilder.Add(aTempCompound, aSubShape);
223       }
224     }
225
226     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
227         anIt.More();
228         anIt.Next())
229     {
230       const TopoDS_Shape& aSubShape = anIt.Value();
231       if (!isSubShape(aTempCompound, aSubShape))
232       {
233         aBuilder.Add(aTempCompound, aSubShape);
234       }
235     }
236
237     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
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     aResult = aTempCompound;
249   }
250
251   if(aResult.ShapeType() == TopAbs_COMPOUND) {
252     // sort sub-shapes of compound before creation of a compsolid
253     sortCompound(aResult, anOperation);
254
255     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
256     aGeomShape->setImpl(new TopoDS_Shape(aResult));
257     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
258   }
259
260   // Setting result.
261   if(aResult.IsNull()) {
262     return;
263   }
264   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
265   aShape->setImpl(new TopoDS_Shape(aResult));
266   this->setShape(aShape);
267   this->setDone(true);
268 }