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