Salome HOME
364834d40408dee94994d04ad16b54d7ba64d637
[modules/shaper.git] / src / GeomAlgoImpl / GEOMAlgo_Splitter.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <GEOMAlgo_Splitter.hxx>
24
25 #include <TopAbs_ShapeEnum.hxx>
26
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Compound.hxx>
29 #include <TopoDS_Iterator.hxx>
30
31 #include <BRep_Builder.hxx>
32
33 #include <BOPCol_MapOfShape.hxx>
34 #include <BOPCol_ListOfShape.hxx>
35
36 #include <BOPTools.hxx>
37
38
39 static 
40   void TreatCompound(const TopoDS_Shape& aC, 
41                      BOPCol_ListOfShape& aLSX);
42
43 //=======================================================================
44 //function : 
45 //purpose  : 
46 //=======================================================================
47 GEOMAlgo_Splitter::GEOMAlgo_Splitter()
48 :
49   BOPAlgo_Builder(),
50   myTools(myAllocator),
51   myMapTools(100, myAllocator)
52 {
53   myLimit=TopAbs_SHAPE;
54   myLimitMode=0;
55 }
56 //=======================================================================
57 //function : 
58 //purpose  : 
59 //=======================================================================
60 GEOMAlgo_Splitter::GEOMAlgo_Splitter
61   (const Handle(NCollection_BaseAllocator)& theAllocator)
62 :
63   BOPAlgo_Builder(theAllocator),
64   myTools(myAllocator),
65   myMapTools(100, myAllocator)
66 {
67   myLimit=TopAbs_SHAPE;
68   myLimitMode=0;
69 }
70 //=======================================================================
71 //function : ~
72 //purpose  : 
73 //=======================================================================
74 GEOMAlgo_Splitter::~GEOMAlgo_Splitter()
75 {
76 }
77 //=======================================================================
78 //function : AddTool
79 //purpose  : 
80 //=======================================================================
81 void GEOMAlgo_Splitter::AddTool(const TopoDS_Shape& theShape)
82 {
83   if (myMapTools.Add(theShape)) {
84     myTools.Append(theShape);
85     //
86     AddArgument(theShape);
87   }
88 }
89 //=======================================================================
90 //function : Tools
91 //purpose  : 
92 //=======================================================================
93 const BOPCol_ListOfShape& GEOMAlgo_Splitter::Tools()const
94 {
95   return myTools;
96 }
97 //=======================================================================
98 //function : SetLimit
99 //purpose  : 
100 //=======================================================================
101 void GEOMAlgo_Splitter::SetLimit(const TopAbs_ShapeEnum aLimit) 
102 {
103   myLimit=aLimit;
104 }
105 //=======================================================================
106 //function : Limit
107 //purpose  : 
108 //=======================================================================
109 TopAbs_ShapeEnum GEOMAlgo_Splitter::Limit()const
110 {
111   return myLimit;
112 }
113 //=======================================================================
114 //function : SetLimitMode
115 //purpose  : 
116 //=======================================================================
117 void GEOMAlgo_Splitter::SetLimitMode(const Standard_Integer aMode)
118 {
119   myLimitMode=aMode;
120 }
121 //=======================================================================
122 //function : LimitMode
123 //purpose  : 
124 //=======================================================================
125 Standard_Integer GEOMAlgo_Splitter::LimitMode()const
126 {
127   return myLimitMode;
128 }
129 //=======================================================================
130 //function : Clear
131 //purpose  : 
132 //=======================================================================
133 void GEOMAlgo_Splitter::Clear()
134 {
135   myTools.Clear();
136   myMapTools.Clear();
137   myLimit=TopAbs_SHAPE;
138   BOPAlgo_Builder::Clear();
139 }
140 //=======================================================================
141 //function : BuildResult
142 //purpose  : 
143 //=======================================================================
144 void GEOMAlgo_Splitter::BuildResult(const TopAbs_ShapeEnum theType)
145 {
146 #ifndef USE_OCCT_720
147   myErrorStatus=0;
148 #endif
149   //
150   TopAbs_ShapeEnum aType;
151   BRep_Builder aBB;
152   BOPCol_MapOfShape aM;
153   BOPCol_ListIteratorOfListOfShape aIt, aItIm;
154   //
155   aIt.Initialize(myArguments);
156   for (; aIt.More(); aIt.Next()) {
157     const TopoDS_Shape& aS=aIt.Value();
158     aType=aS.ShapeType();
159     if (aType==theType && !myMapTools.Contains(aS)) {
160       if (myImages.IsBound(aS)) {
161         const BOPCol_ListOfShape& aLSIm=myImages.Find(aS);
162         aItIm.Initialize(aLSIm);
163         for (; aItIm.More(); aItIm.Next()) {
164           const TopoDS_Shape& aSIm=aItIm.Value();
165           if (aM.Add(aSIm)) {
166             aBB.Add(myShape, aSIm);
167           }
168         }
169       }
170       else {
171         if (aM.Add(aS)) {
172           aBB.Add(myShape, aS);
173         }
174       }
175     }
176   }
177 }
178 //=======================================================================
179 //function : PostTreat
180 //purpose  : 
181 //=======================================================================
182 void GEOMAlgo_Splitter::PostTreat()
183 {
184   if (myLimit!=TopAbs_SHAPE) {
185     Standard_Integer i, aNbS;
186     BRep_Builder aBB;
187     TopoDS_Compound aC;
188     BOPCol_IndexedMapOfShape aMx;
189     //
190     aBB.MakeCompound(aC);
191     //
192     BOPTools::MapShapes(myShape, myLimit, aMx);
193     aNbS=aMx.Extent();
194     for (i=1; i<=aNbS; ++i) {
195       const TopoDS_Shape& aS=aMx(i);
196       aBB.Add(aC, aS);
197     }
198     if (myLimitMode) {
199       Standard_Integer iType, iLimit, iTypeX;
200       TopAbs_ShapeEnum aType, aTypeX;
201       BOPCol_ListOfShape aLSP, aLSX;
202       BOPCol_ListIteratorOfListOfShape aIt, aItX, aItIm;
203       BOPCol_MapOfShape  aM;
204       //
205       iLimit=(Standard_Integer)myLimit; 
206       //
207       // 1. Collect the shapes to process aLSP
208       aIt.Initialize(myArguments);
209       for (; aIt.More(); aIt.Next()) {
210         const TopoDS_Shape& aS=aIt.Value();
211         if (myMapTools.Contains(aS)) {
212           continue;
213         }
214         //
215         aType=aS.ShapeType();
216         iType=(Standard_Integer)aType;
217         //
218         if (iType>iLimit) {
219           aLSP.Append(aS);
220         }
221         //
222         else if (aType==TopAbs_COMPOUND) {
223           aLSX.Clear();
224           //
225           TreatCompound(aS, aLSX);
226           //
227           aItX.Initialize(aLSX);
228           for (; aItX.More(); aItX.Next()) {
229             const TopoDS_Shape& aSX=aItX.Value();
230             aTypeX=aSX.ShapeType();
231             iTypeX=(Standard_Integer)aTypeX;
232             //
233             if (iTypeX>iLimit) {
234               aLSP.Append(aSX);
235             }
236           }
237         }
238       }// for (; aIt.More(); aIt.Next()) {
239       //
240       aMx.Clear();
241       BOPTools::MapShapes(aC, aMx);
242        // 2. Add them to aC
243       aIt.Initialize(aLSP);
244       for (; aIt.More(); aIt.Next()) {
245         const TopoDS_Shape& aS=aIt.Value();
246         if (myImages.IsBound(aS)) {
247           const BOPCol_ListOfShape& aLSIm=myImages.Find(aS);
248           aItIm.Initialize(aLSIm);
249           for (; aItIm.More(); aItIm.Next()) {
250             const TopoDS_Shape& aSIm=aItIm.Value();
251             if (aM.Add(aSIm)) {
252               if (!aMx.Contains(aSIm)) {
253                 aBB.Add(aC, aSIm);
254               }
255             }
256           }
257         }
258         else {
259           if (aM.Add(aS)) {
260             if (!aMx.Contains(aS)) {
261               aBB.Add(aC, aS);
262             }
263           }
264         }
265       }
266     }// if (myLimitMode) {
267     myShape=aC;
268   }//if (myLimit!=TopAbs_SHAPE) {
269   //
270   Standard_Integer aNbS;
271   TopoDS_Iterator aIt;
272   BOPCol_ListOfShape aLS;
273   //
274   aIt.Initialize(myShape);
275   for (; aIt.More(); aIt.Next()) {
276     const TopoDS_Shape& aS=aIt.Value();
277     aLS.Append(aS);
278   }
279   aNbS=aLS.Extent();
280   if (aNbS==1) {
281     myShape=aLS.First();
282   }
283   //
284   BOPAlgo_Builder::PostTreat();
285 }
286 //=======================================================================
287 //function : TreatCompound
288 //purpose  : 
289 //=======================================================================
290 void TreatCompound(const TopoDS_Shape& aC1, 
291                    BOPCol_ListOfShape& aLSX)
292 {
293   Standard_Integer aNbC1;
294   TopAbs_ShapeEnum aType;
295   BOPCol_ListOfShape aLC, aLC1;
296   BOPCol_ListIteratorOfListOfShape aIt, aIt1;
297   TopoDS_Iterator aItC;
298   //
299   aLC.Append (aC1);
300   while(1) {
301     aLC1.Clear();
302     aIt.Initialize(aLC);
303     for (; aIt.More(); aIt.Next()) {
304       const TopoDS_Shape& aC=aIt.Value(); //C is compound
305       //
306       aItC.Initialize(aC);
307       for (; aItC.More(); aItC.Next()) {
308         const TopoDS_Shape& aS=aItC.Value();
309         aType=aS.ShapeType();
310         if (aType==TopAbs_COMPOUND) {
311           aLC1.Append(aS);
312         }
313         else {
314           aLSX.Append(aS);
315         }
316       }
317     }
318     //
319     aNbC1=aLC1.Extent();
320     if (!aNbC1) {
321       break;
322     }
323     //
324     aLC.Clear();
325     aIt.Initialize(aLC1);
326     for (; aIt.More(); aIt.Next()) {
327       const TopoDS_Shape& aSC=aIt.Value();
328       aLC.Append(aSC);
329     }
330   }// while(1)
331 }
332 //
333 // myErrorStatus
334 // 
335 // 0  - Ok
336 // 1  - The object is just initialized
337 // 2  - PaveFiller is failed
338 // 10 - No shapes to process
339 // 30 - SolidBuilder failed