Salome HOME
Avoid linking to the native omniORB
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Splitter.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:        GEOMAlgo_Splitter.cxx
21 // Created:     
22 // Author:      Peter KURNEV
23 //              <pkv@irinox>
24
25 #include <GEOMAlgo_Splitter.ixx>
26
27 #include <TopAbs_ShapeEnum.hxx>
28
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Compound.hxx>
31 #include <TopoDS_Iterator.hxx>
32
33 #include <TopExp.hxx>
34
35 #include <BRep_Builder.hxx>
36 #include <BRepLib.hxx>
37
38 #include <TopTools_MapOfShape.hxx>
39 #include <TopTools_ListOfShape.hxx>
40 #include <TopTools_ListIteratorOfListOfShape.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <BOP_CorrectTolerances.hxx>
44
45
46 static 
47   void TreatCompound(const TopoDS_Shape& aC, 
48                      TopTools_ListOfShape& aLSX);
49
50 //=======================================================================
51 //function : 
52 //purpose  : 
53 //=======================================================================
54   GEOMAlgo_Splitter::GEOMAlgo_Splitter()
55 :
56   GEOMAlgo_Builder()
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 : AddToolCompound
70 //purpose  : 
71 //=======================================================================
72   void GEOMAlgo_Splitter::AddToolCompound(const TopoDS_Shape& theShape)
73 {
74   TopoDS_Iterator aIt;
75   //
76   aIt.Initialize(theShape);
77   for (; aIt.More(); aIt.Next()) {
78     const TopoDS_Shape& aS=aIt.Value();
79     AddTool(aS);
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     AddShape(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   GEOMAlgo_Builder::Clear();
144 }
145 //=======================================================================
146 //function : BuildResult
147 //purpose  : 
148 //=======================================================================
149   void GEOMAlgo_Splitter::BuildResult(const TopAbs_ShapeEnum theType)
150 {
151   myErrorStatus=0;
152   //
153   TopAbs_ShapeEnum aType;
154   BRep_Builder aBB;
155   TopTools_MapOfShape aM;
156   TopTools_ListIteratorOfListOfShape aIt, aItIm;
157   //
158   aIt.Initialize(myShapes);
159   for (; aIt.More(); aIt.Next()) {
160     const TopoDS_Shape& aS=aIt.Value();
161     aType=aS.ShapeType();
162     if (aType==theType && !myMapTools.Contains(aS)) {
163       if (myImages.HasImage(aS)) {
164         const TopTools_ListOfShape& aLSIm=myImages.Image(aS);
165         aItIm.Initialize(aLSIm);
166         for (; aItIm.More(); aItIm.Next()) {
167           const TopoDS_Shape& aSIm=aItIm.Value();
168           if (aM.Add(aSIm)) {
169             aBB.Add(myShape, aSIm);
170           }
171         }
172       }
173       else {
174         if (aM.Add(aS)) {
175           aBB.Add(myShape, aS);
176         }
177       }
178     }
179   }
180 }
181 //=======================================================================
182 //function : PostTreat
183 //purpose  : 
184 //=======================================================================
185   void GEOMAlgo_Splitter::PostTreat()
186 {
187   if (myLimit!=TopAbs_SHAPE) {
188     Standard_Integer i, aNbS;
189     BRep_Builder aBB;
190     TopoDS_Compound aC;
191     TopTools_IndexedMapOfShape aMx;
192     //
193     aBB.MakeCompound(aC);
194     //
195     TopExp::MapShapes(myShape, myLimit, aMx);
196     aNbS=aMx.Extent();
197     for (i=1; i<=aNbS; ++i) {
198       const TopoDS_Shape& aS=aMx(i);
199       aBB.Add(aC, aS);
200     }
201     //modified by NIZNHY-PKV Thu Feb 15 17:09:32 2007f
202     if (myLimitMode) {
203       Standard_Integer iType, iLimit, iTypeX;
204       TopAbs_ShapeEnum aType, aTypeX;
205       TopTools_ListOfShape aLSP, aLSX;
206       TopTools_ListIteratorOfListOfShape aIt, aItX, aItIm;
207       TopTools_MapOfShape  aM;
208       //
209       iLimit=(Standard_Integer)myLimit; 
210       //
211       // 1. Collect the shapes to process aLSP
212       aIt.Initialize(myShapes);
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       // 2. Add them to aC
245       aIt.Initialize(aLSP);
246       for (; aIt.More(); aIt.Next()) {
247         const TopoDS_Shape& aS=aIt.Value();
248         if (myImages.HasImage(aS)) {
249           const TopTools_ListOfShape& aLSIm=myImages.Image(aS);
250           aItIm.Initialize(aLSIm);
251           for (; aItIm.More(); aItIm.Next()) {
252             const TopoDS_Shape& aSIm=aItIm.Value();
253             if (aM.Add(aSIm)) {
254               aBB.Add(aC, aSIm);
255             }
256           }
257         }
258         else {
259           if (aM.Add(aS)) {
260             aBB.Add(aC, aS);
261           }
262         }
263       }
264     }// if (myLimitMode) {
265     //modified by NIZNHY-PKV Thu Feb 15 17:09:34 2007t
266     myShape=aC;
267   }//if (myLimit!=TopAbs_SHAPE) {
268   //
269   GEOMAlgo_Builder::PostTreat();
270 }
271 //=======================================================================
272 //function : TreatCompound
273 //purpose  : 
274 //=======================================================================
275 void TreatCompound(const TopoDS_Shape& aC1, 
276                    TopTools_ListOfShape& aLSX)
277 {
278   Standard_Integer aNbC1;
279   TopAbs_ShapeEnum aType;
280   TopTools_ListOfShape aLC, aLC1;
281   TopTools_ListIteratorOfListOfShape aIt, aIt1;
282   TopoDS_Iterator aItC;
283   //
284   aLC.Append (aC1);
285   while(1) {
286     aLC1.Clear();
287     aIt.Initialize(aLC);
288     for (; aIt.More(); aIt.Next()) {
289       const TopoDS_Shape& aC=aIt.Value(); //C is compound
290       //
291       aItC.Initialize(aC);
292       for (; aItC.More(); aItC.Next()) {
293         const TopoDS_Shape& aS=aItC.Value();
294         aType=aS.ShapeType();
295         if (aType==TopAbs_COMPOUND) {
296           aLC1.Append(aS);
297         }
298         else {
299           aLSX.Append(aS);
300         }
301       }
302     }
303     //
304     aNbC1=aLC1.Extent();
305     if (!aNbC1) {
306       break;
307     }
308     //
309     aLC.Clear();
310     aIt.Initialize(aLC1);
311     for (; aIt.More(); aIt.Next()) {
312       const TopoDS_Shape& aSC=aIt.Value();
313       aLC.Append(aSC);
314     }
315   }// while(1)
316 }
317 //
318 // myErrorStatus
319 // 
320 // 0  - Ok
321 // 1  - The object is just initialized
322 // 2  - PaveFiller is failed
323 // 10 - No shapes to process
324 // 30 - SolidBuilder failed