Salome HOME
e7b8adb3808b98a9a4adbdd8357235eefdcefac1
[modules/shaper.git] / src / GeomAlgoImpl / GEOMAlgo_Splitter.cxx
1 // Copyright (C) 2007-2022  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <GEOMAlgo_Splitter.hxx>
21
22 #include <TopAbs_ShapeEnum.hxx>
23 #include <TopExp.hxx>
24 #include <TopoDS_Shape.hxx>
25 #include <TopoDS_Compound.hxx>
26 #include <TopoDS_Iterator.hxx>
27
28 #include <BRep_Builder.hxx>
29
30 static 
31   void TreatCompound(const TopoDS_Shape& aC, 
32                      NCollection_List<TopoDS_Shape>& aLSX);
33
34 //=======================================================================
35 //function : 
36 //purpose  : 
37 //=======================================================================
38 GEOMAlgo_Splitter::GEOMAlgo_Splitter()
39 :
40   BOPAlgo_Builder(),
41   myTools(myAllocator),
42   myMapTools(100, myAllocator)
43 {
44   myLimit=TopAbs_SHAPE;
45   myLimitMode=0;
46 }
47 //=======================================================================
48 //function : 
49 //purpose  : 
50 //=======================================================================
51 GEOMAlgo_Splitter::GEOMAlgo_Splitter
52   (const Handle(NCollection_BaseAllocator)& theAllocator)
53 :
54   BOPAlgo_Builder(theAllocator),
55   myTools(myAllocator),
56   myMapTools(100, myAllocator)
57 {
58   myLimit=TopAbs_SHAPE;
59   myLimitMode=0;
60 }
61 //=======================================================================
62 //function : ~
63 //purpose  : 
64 //=======================================================================
65 GEOMAlgo_Splitter::~GEOMAlgo_Splitter()
66 {
67 }
68 //=======================================================================
69 //function : AddTool
70 //purpose  : 
71 //=======================================================================
72 void GEOMAlgo_Splitter::AddTool(const TopoDS_Shape& theShape)
73 {
74   if (myMapTools.Add(theShape)) {
75     myTools.Append(theShape);
76     //
77     AddArgument(theShape);
78   }
79 }
80 //=======================================================================
81 //function : Tools
82 //purpose  : 
83 //=======================================================================
84 const NCollection_List<TopoDS_Shape>& GEOMAlgo_Splitter::Tools()const
85 {
86   return myTools;
87 }
88 //=======================================================================
89 //function : SetLimit
90 //purpose  : 
91 //=======================================================================
92 void GEOMAlgo_Splitter::SetLimit(const TopAbs_ShapeEnum aLimit) 
93 {
94   myLimit=aLimit;
95 }
96 //=======================================================================
97 //function : Limit
98 //purpose  : 
99 //=======================================================================
100 TopAbs_ShapeEnum GEOMAlgo_Splitter::Limit()const
101 {
102   return myLimit;
103 }
104 //=======================================================================
105 //function : SetLimitMode
106 //purpose  : 
107 //=======================================================================
108 void GEOMAlgo_Splitter::SetLimitMode(const Standard_Integer aMode)
109 {
110   myLimitMode=aMode;
111 }
112 //=======================================================================
113 //function : LimitMode
114 //purpose  : 
115 //=======================================================================
116 Standard_Integer GEOMAlgo_Splitter::LimitMode()const
117 {
118   return myLimitMode;
119 }
120 //=======================================================================
121 //function : Clear
122 //purpose  : 
123 //=======================================================================
124 void GEOMAlgo_Splitter::Clear()
125 {
126   myTools.Clear();
127   myMapTools.Clear();
128   myLimit=TopAbs_SHAPE;
129   BOPAlgo_Builder::Clear();
130 }
131 //=======================================================================
132 //function : BuildResult
133 //purpose  : 
134 //=======================================================================
135 void GEOMAlgo_Splitter::BuildResult(const TopAbs_ShapeEnum theType)
136 {
137   TopAbs_ShapeEnum aType;
138   BRep_Builder aBB;
139   NCollection_Map<TopoDS_Shape> aM;
140   NCollection_List<TopoDS_Shape>::Iterator aIt, aItIm;
141   //
142   aIt.Initialize(myArguments);
143   for (; aIt.More(); aIt.Next()) {
144     const TopoDS_Shape& aS=aIt.Value();
145     aType=aS.ShapeType();
146     if (aType==theType && !myMapTools.Contains(aS)) {
147       if (myImages.IsBound(aS)) {
148         const NCollection_List<TopoDS_Shape>& aLSIm=myImages.Find(aS);
149         aItIm.Initialize(aLSIm);
150         for (; aItIm.More(); aItIm.Next()) {
151           const TopoDS_Shape& aSIm=aItIm.Value();
152           if (aM.Add(aSIm)) {
153             aBB.Add(myShape, aSIm);
154           }
155         }
156       }
157       else {
158         if (aM.Add(aS)) {
159           aBB.Add(myShape, aS);
160         }
161       }
162     }
163   }
164 }
165 //=======================================================================
166 //function : PostTreat
167 //purpose  : 
168 //=======================================================================
169 #if OCC_VERSION_LARGE < 0x07070000
170 void GEOMAlgo_Splitter::PostTreat()
171 #else
172 void GEOMAlgo_Splitter::PostTreat(const Message_ProgressRange& theRange)
173 #endif
174 {
175   if (myLimit!=TopAbs_SHAPE) {
176     Standard_Integer i, aNbS;
177     BRep_Builder aBB;
178     TopoDS_Compound aC;
179     TopTools_IndexedMapOfShape aMx;
180     //
181     aBB.MakeCompound(aC);
182     //
183     TopExp::MapShapes(myShape, myLimit, aMx);
184     aNbS=aMx.Extent();
185     for (i=1; i<=aNbS; ++i) {
186       const TopoDS_Shape& aS=aMx(i);
187       aBB.Add(aC, aS);
188     }
189     if (myLimitMode) {
190       Standard_Integer iType, iLimit, iTypeX;
191       TopAbs_ShapeEnum aType, aTypeX;
192       NCollection_List<TopoDS_Shape> aLSP, aLSX;
193       NCollection_List<TopoDS_Shape>::Iterator aIt, aItX, aItIm;
194       NCollection_Map<TopoDS_Shape>  aM;
195       //
196       iLimit=(Standard_Integer)myLimit; 
197       //
198       // 1. Collect the shapes to process aLSP
199       aIt.Initialize(myArguments);
200       for (; aIt.More(); aIt.Next()) {
201         const TopoDS_Shape& aS=aIt.Value();
202         if (myMapTools.Contains(aS)) {
203           continue;
204         }
205         //
206         aType=aS.ShapeType();
207         iType=(Standard_Integer)aType;
208         //
209         if (iType>iLimit) {
210           aLSP.Append(aS);
211         }
212         //
213         else if (aType==TopAbs_COMPOUND) {
214           aLSX.Clear();
215           //
216           TreatCompound(aS, aLSX);
217           //
218           aItX.Initialize(aLSX);
219           for (; aItX.More(); aItX.Next()) {
220             const TopoDS_Shape& aSX=aItX.Value();
221             aTypeX=aSX.ShapeType();
222             iTypeX=(Standard_Integer)aTypeX;
223             //
224             if (iTypeX>iLimit) {
225               aLSP.Append(aSX);
226             }
227           }
228         }
229       }// for (; aIt.More(); aIt.Next()) {
230       //
231       aMx.Clear();
232       TopExp::MapShapes(aC, aMx);
233        // 2. Add them to aC
234       aIt.Initialize(aLSP);
235       for (; aIt.More(); aIt.Next()) {
236         const TopoDS_Shape& aS=aIt.Value();
237         if (myImages.IsBound(aS)) {
238           const NCollection_List<TopoDS_Shape>& aLSIm=myImages.Find(aS);
239           aItIm.Initialize(aLSIm);
240           for (; aItIm.More(); aItIm.Next()) {
241             const TopoDS_Shape& aSIm=aItIm.Value();
242             if (aM.Add(aSIm)) {
243               if (!aMx.Contains(aSIm)) {
244                 aBB.Add(aC, aSIm);
245               }
246             }
247           }
248         }
249         else {
250           if (aM.Add(aS)) {
251             if (!aMx.Contains(aS)) {
252               aBB.Add(aC, aS);
253             }
254           }
255         }
256       }
257     }// if (myLimitMode) {
258     myShape=aC;
259   }//if (myLimit!=TopAbs_SHAPE) {
260   //
261   Standard_Integer aNbS;
262   TopoDS_Iterator aIt;
263   NCollection_List<TopoDS_Shape> aLS;
264   //
265   aIt.Initialize(myShape);
266   for (; aIt.More(); aIt.Next()) {
267     const TopoDS_Shape& aS=aIt.Value();
268     aLS.Append(aS);
269   }
270   aNbS=aLS.Extent();
271   if (aNbS==1) {
272     myShape=aLS.First();
273   }
274   //
275 #if OCC_VERSION_LARGE < 0x07070000
276   BOPAlgo_Builder::PostTreat();
277 #else
278   BOPAlgo_Builder::PostTreat(theRange);
279 #endif
280 }
281 //=======================================================================
282 //function : TreatCompound
283 //purpose  : 
284 //=======================================================================
285 void TreatCompound(const TopoDS_Shape& aC1, 
286                    NCollection_List<TopoDS_Shape>& aLSX)
287 {
288   Standard_Integer aNbC1;
289   TopAbs_ShapeEnum aType;
290   NCollection_List<TopoDS_Shape> aLC, aLC1;
291   NCollection_List<TopoDS_Shape>::Iterator aIt, aIt1;
292   TopoDS_Iterator aItC;
293   //
294   aLC.Append (aC1);
295   while(1) {
296     aLC1.Clear();
297     aIt.Initialize(aLC);
298     for (; aIt.More(); aIt.Next()) {
299       const TopoDS_Shape& aC=aIt.Value(); //C is compound
300       //
301       aItC.Initialize(aC);
302       for (; aItC.More(); aItC.Next()) {
303         const TopoDS_Shape& aS=aItC.Value();
304         aType=aS.ShapeType();
305         if (aType==TopAbs_COMPOUND) {
306           aLC1.Append(aS);
307         }
308         else {
309           aLSX.Append(aS);
310         }
311       }
312     }
313     //
314     aNbC1=aLC1.Extent();
315     if (!aNbC1) {
316       break;
317     }
318     //
319     aLC.Clear();
320     aIt.Initialize(aLC1);
321     for (; aIt.More(); aIt.Next()) {
322       const TopoDS_Shape& aSC=aIt.Value();
323       aLC.Append(aSC);
324     }
325   }// while(1)
326 }
327 //
328 // myErrorStatus
329 // 
330 // 0  - Ok
331 // 1  - The object is just initialized
332 // 2  - PaveFiller is failed
333 // 10 - No shapes to process
334 // 30 - SolidBuilder failed