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