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