Salome HOME
Issue #2419: Fill ends in error "Resulting shape is not valid."
[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 #ifdef USE_OCCT_720
154   if (anOperation->HasErrors())
155     return;
156 #else
157   if(anOperation->ErrorStatus() != 0) {
158     return;
159   }
160 #endif
161   TopoDS_Shape aResult = anOperation->Shape();
162
163   if(aResult.ShapeType() == TopAbs_COMPOUND) {
164     // Exclude faces and edges which are shared as another sub-shape.
165     NCollection_Vector<TopoDS_Shape> aFaces;
166     NCollection_Vector<TopoDS_Shape> anEdges;
167     TopoDS_Compound aTempCompound;
168     TopoDS_Builder aBuilder;
169     aBuilder.MakeCompound(aTempCompound);
170     for(TopoDS_Iterator anIt(aResult);
171         anIt.More();
172         anIt.Next()) {
173       const TopoDS_Shape& aSubShape = anIt.Value();
174       if (aSubShape.ShapeType() == TopAbs_FACE) {
175         aFaces.Append(aSubShape);
176       } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
177         anEdges.Append(aSubShape);
178       } else {
179         aBuilder.Add(aTempCompound, aSubShape);
180       }
181     }
182
183     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
184         anIt.More();
185         anIt.Next())
186     {
187       const TopoDS_Shape& aSubShape = anIt.Value();
188       if (!isSubShape(aTempCompound, aSubShape))
189       {
190         aBuilder.Add(aTempCompound, aSubShape);
191       }
192     }
193
194     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
195         anIt.More();
196         anIt.Next())
197     {
198       const TopoDS_Shape& aSubShape = anIt.Value();
199       if (!isSubShape(aTempCompound, aSubShape))
200       {
201         aBuilder.Add(aTempCompound, aSubShape);
202       }
203     }
204
205     aResult = aTempCompound;
206   }
207
208   if(aResult.ShapeType() == TopAbs_COMPOUND) {
209     // sort sub-shapes of compound before creation of a compsolid
210     sortCompound(aResult);
211
212     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
213     aGeomShape->setImpl(new TopoDS_Shape(aResult));
214     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
215   }
216
217   // Setting result.
218   if(aResult.IsNull()) {
219     return;
220   }
221   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
222   aShape->setImpl(new TopoDS_Shape(aResult));
223   this->setShape(aShape);
224   this->setDone(true);
225 }