Salome HOME
ad59c732d22e9ec8353ce7b450822fc05e023c37
[modules/shaper.git] / src / GeomAlgoImpl / GEOMAlgo_Splitter.cxx
1 // Copyright (C) 2007-2021  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 void GEOMAlgo_Splitter::PostTreat()
170 {
171   if (myLimit!=TopAbs_SHAPE) {
172     Standard_Integer i, aNbS;
173     BRep_Builder aBB;
174     TopoDS_Compound aC;
175     TopTools_IndexedMapOfShape aMx;
176     //
177     aBB.MakeCompound(aC);
178     //
179     TopExp::MapShapes(myShape, myLimit, aMx);
180     aNbS=aMx.Extent();
181     for (i=1; i<=aNbS; ++i) {
182       const TopoDS_Shape& aS=aMx(i);
183       aBB.Add(aC, aS);
184     }
185     if (myLimitMode) {
186       Standard_Integer iType, iLimit, iTypeX;
187       TopAbs_ShapeEnum aType, aTypeX;
188       NCollection_List<TopoDS_Shape> aLSP, aLSX;
189       NCollection_List<TopoDS_Shape>::Iterator aIt, aItX, aItIm;
190       NCollection_Map<TopoDS_Shape>  aM;
191       //
192       iLimit=(Standard_Integer)myLimit; 
193       //
194       // 1. Collect the shapes to process aLSP
195       aIt.Initialize(myArguments);
196       for (; aIt.More(); aIt.Next()) {
197         const TopoDS_Shape& aS=aIt.Value();
198         if (myMapTools.Contains(aS)) {
199           continue;
200         }
201         //
202         aType=aS.ShapeType();
203         iType=(Standard_Integer)aType;
204         //
205         if (iType>iLimit) {
206           aLSP.Append(aS);
207         }
208         //
209         else if (aType==TopAbs_COMPOUND) {
210           aLSX.Clear();
211           //
212           TreatCompound(aS, aLSX);
213           //
214           aItX.Initialize(aLSX);
215           for (; aItX.More(); aItX.Next()) {
216             const TopoDS_Shape& aSX=aItX.Value();
217             aTypeX=aSX.ShapeType();
218             iTypeX=(Standard_Integer)aTypeX;
219             //
220             if (iTypeX>iLimit) {
221               aLSP.Append(aSX);
222             }
223           }
224         }
225       }// for (; aIt.More(); aIt.Next()) {
226       //
227       aMx.Clear();
228       TopExp::MapShapes(aC, aMx);
229        // 2. Add them to aC
230       aIt.Initialize(aLSP);
231       for (; aIt.More(); aIt.Next()) {
232         const TopoDS_Shape& aS=aIt.Value();
233         if (myImages.IsBound(aS)) {
234           const NCollection_List<TopoDS_Shape>& aLSIm=myImages.Find(aS);
235           aItIm.Initialize(aLSIm);
236           for (; aItIm.More(); aItIm.Next()) {
237             const TopoDS_Shape& aSIm=aItIm.Value();
238             if (aM.Add(aSIm)) {
239               if (!aMx.Contains(aSIm)) {
240                 aBB.Add(aC, aSIm);
241               }
242             }
243           }
244         }
245         else {
246           if (aM.Add(aS)) {
247             if (!aMx.Contains(aS)) {
248               aBB.Add(aC, aS);
249             }
250           }
251         }
252       }
253     }// if (myLimitMode) {
254     myShape=aC;
255   }//if (myLimit!=TopAbs_SHAPE) {
256   //
257   Standard_Integer aNbS;
258   TopoDS_Iterator aIt;
259   NCollection_List<TopoDS_Shape> aLS;
260   //
261   aIt.Initialize(myShape);
262   for (; aIt.More(); aIt.Next()) {
263     const TopoDS_Shape& aS=aIt.Value();
264     aLS.Append(aS);
265   }
266   aNbS=aLS.Extent();
267   if (aNbS==1) {
268     myShape=aLS.First();
269   }
270   //
271   BOPAlgo_Builder::PostTreat();
272 }
273 //=======================================================================
274 //function : TreatCompound
275 //purpose  : 
276 //=======================================================================
277 void TreatCompound(const TopoDS_Shape& aC1, 
278                    NCollection_List<TopoDS_Shape>& aLSX)
279 {
280   Standard_Integer aNbC1;
281   TopAbs_ShapeEnum aType;
282   NCollection_List<TopoDS_Shape> aLC, aLC1;
283   NCollection_List<TopoDS_Shape>::Iterator aIt, aIt1;
284   TopoDS_Iterator aItC;
285   //
286   aLC.Append (aC1);
287   while(1) {
288     aLC1.Clear();
289     aIt.Initialize(aLC);
290     for (; aIt.More(); aIt.Next()) {
291       const TopoDS_Shape& aC=aIt.Value(); //C is compound
292       //
293       aItC.Initialize(aC);
294       for (; aItC.More(); aItC.Next()) {
295         const TopoDS_Shape& aS=aItC.Value();
296         aType=aS.ShapeType();
297         if (aType==TopAbs_COMPOUND) {
298           aLC1.Append(aS);
299         }
300         else {
301           aLSX.Append(aS);
302         }
303       }
304     }
305     //
306     aNbC1=aLC1.Extent();
307     if (!aNbC1) {
308       break;
309     }
310     //
311     aLC.Clear();
312     aIt.Initialize(aLC1);
313     for (; aIt.More(); aIt.Next()) {
314       const TopoDS_Shape& aSC=aIt.Value();
315       aLC.Append(aSC);
316     }
317   }// while(1)
318 }
319 //
320 // myErrorStatus
321 // 
322 // 0  - Ok
323 // 1  - The object is just initialized
324 // 2  - PaveFiller is failed
325 // 10 - No shapes to process
326 // 30 - SolidBuilder failed