Salome HOME
CCAR: improve speed of getAIS by calling CreatePrs that uses entry2aisobjects map
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Splitter.cxx
1 //  Copyright (C) 2007-2010  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.
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 //  Author: Peter KURNEV
25 //
26 #include <GEOMAlgo_Splitter.ixx>
27
28 #include <TopAbs_ShapeEnum.hxx>
29
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_Iterator.hxx>
33
34 #include <TopExp.hxx>
35
36 #include <BRep_Builder.hxx>
37 #include <BRepLib.hxx>
38
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43
44 #include <BOP_CorrectTolerances.hxx>
45
46
47 static
48   void TreatCompound(const TopoDS_Shape& aC,
49                      TopTools_ListOfShape& aLSX);
50
51 //=======================================================================
52 //function :
53 //purpose  :
54 //=======================================================================
55   GEOMAlgo_Splitter::GEOMAlgo_Splitter()
56 :
57   GEOMAlgo_Builder()
58 {
59   myLimit=TopAbs_SHAPE;
60   myLimitMode=0;
61 }
62 //=======================================================================
63 //function : ~
64 //purpose  :
65 //=======================================================================
66   GEOMAlgo_Splitter::~GEOMAlgo_Splitter()
67 {
68 }
69 //=======================================================================
70 //function : AddToolCompound
71 //purpose  :
72 //=======================================================================
73   void GEOMAlgo_Splitter::AddToolCompound(const TopoDS_Shape& theShape)
74 {
75   TopoDS_Iterator aIt;
76   //
77   aIt.Initialize(theShape);
78   for (; aIt.More(); aIt.Next()) {
79     const TopoDS_Shape& aS=aIt.Value();
80     AddTool(aS);
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     AddShape(theShape);
93   }
94 }
95 //=======================================================================
96 //function : Tools
97 //purpose  :
98 //=======================================================================
99   const TopTools_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   GEOMAlgo_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   TopTools_MapOfShape aM;
157   TopTools_ListIteratorOfListOfShape aIt, aItIm;
158   //
159   aIt.Initialize(myShapes);
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.HasImage(aS)) {
165         const TopTools_ListOfShape& aLSIm=myImages.Image(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     TopTools_IndexedMapOfShape aMx;
193     //
194     aBB.MakeCompound(aC);
195     //
196     TopExp::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     //
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(myShapes);
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       //modified by NIZNHY-PKV Fri Oct 30 11:07:08 2009 f
246       aMx.Clear();
247       TopExp::MapShapes(aC, aMx);
248       //modified by NIZNHY-PKV Fri Oct 30 11:12:30 2009t
249       //
250       // 2. Add them to aC
251       aIt.Initialize(aLSP);
252       for (; aIt.More(); aIt.Next()) {
253         const TopoDS_Shape& aS=aIt.Value();
254         if (myImages.HasImage(aS)) {
255           const TopTools_ListOfShape& aLSIm=myImages.Image(aS);
256           aItIm.Initialize(aLSIm);
257           for (; aItIm.More(); aItIm.Next()) {
258             const TopoDS_Shape& aSIm=aItIm.Value();
259             if (aM.Add(aSIm)) {
260               //modified by NIZNHY-PKV Fri Oct 30 11:09:57 2009f
261               if (!aMx.Contains(aSIm)) {
262                 aBB.Add(aC, aSIm);
263               }
264               //aBB.Add(aC, aSIm);
265               //modified by NIZNHY-PKV Fri Oct 30 11:10:02 2009
266             }
267           }
268         }
269         else {
270           if (aM.Add(aS)) {
271             //modified by NIZNHY-PKV Fri Oct 30 11:10:46 2009f
272             if (!aMx.Contains(aS)) {
273               aBB.Add(aC, aS);
274             }
275             //aBB.Add(aC, aS);
276             //modified by NIZNHY-PKV Fri Oct 30 11:11:00 2009t
277           }
278         }
279       }
280     }// if (myLimitMode) {
281     myShape=aC;
282   }//if (myLimit!=TopAbs_SHAPE) {
283   //
284   GEOMAlgo_Builder::PostTreat();
285 }
286 //=======================================================================
287 //function : TreatCompound
288 //purpose  :
289 //=======================================================================
290 void TreatCompound(const TopoDS_Shape& aC1,
291                    TopTools_ListOfShape& aLSX)
292 {
293   Standard_Integer aNbC1;
294   TopAbs_ShapeEnum aType;
295   TopTools_ListOfShape aLC, aLC1;
296   TopTools_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