Salome HOME
Bug 0020228: [CEA 331] the geompy.RemoveExtraEdges crash with the attached brep shape...
[modules/geom.git] / src / NMTAlgo / NMTAlgo_Splitter1.cxx
1 //  Copyright (C) 2007-2008  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 // File:        NMTAlgo_Splitter1.cxx
23 // Created:     Wed Feb 11 14:26:27 2004
24 // Author:      Peter KURNEV
25 //              <pkv@irinox>
26 //
27 #include <NMTAlgo_Splitter1.ixx>
28 #include <NMTAlgo_Splitter.hxx>
29 #include <TopAbs_ShapeEnum.hxx>
30 #include <TopoDS_Iterator.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
32 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
33 #include <TopTools_ListIteratorOfListOfShape.hxx>
34 #include <NMTTools_IndexedDataMapOfShapeIndexedMapOfShape.hxx>
35 #include <TopExp.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopExp_Explorer.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <TopTools_DataMapOfIntegerListOfShape.hxx>
43 #include <TopTools_DataMapIteratorOfDataMapOfShapeInteger.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <TopExp.hxx>
46 #include <TopTools_MapOfShape.hxx>
47 #include <NMTAlgo_Loop3d.hxx>
48 #include <TopTools_MapOfOrientedShape.hxx>
49 #include <BRep_Builder.hxx>
50 #include <TopoDS_Solid.hxx>
51 #include <NMTAlgo_Tools.hxx>
52
53 //=======================================================================
54 //function : NMTAlgo_Spliter1::NMTAlgo_Splitter1
55 //purpose  : 
56 //=======================================================================
57   NMTAlgo_Splitter1::NMTAlgo_Splitter1()
58 :
59   NMTAlgo_Splitter()
60 {
61   myRemoveWebs=Standard_False;
62 }
63 //=======================================================================
64 // function: ~NMTAlgo_Splitter1
65 // purpose: 
66 //=======================================================================
67   NMTAlgo_Splitter1::~NMTAlgo_Splitter1()
68 {
69   Clear();
70 }
71 //=======================================================================
72 //function : Clear
73 //purpose  : 
74 //=======================================================================
75   void NMTAlgo_Splitter1::Clear()
76 {
77   NMTAlgo_Splitter::Clear();
78   //
79   myRemoveWebs=Standard_False;
80   myMapSWM.Clear();
81   myMapSWMOut.Clear();
82 }
83 //=======================================================================
84 //function : SetRemoveWebs
85 //purpose  :
86 //=======================================================================
87   void NMTAlgo_Splitter1::SetRemoveWebs(const Standard_Boolean bFlag)
88 {
89   myRemoveWebs=bFlag;
90 }
91 //=======================================================================
92 //function : RemoveWebs
93 //purpose  :
94 //=======================================================================
95   Standard_Boolean NMTAlgo_Splitter1::RemoveWebs()const
96 {
97   return myRemoveWebs;
98 }
99 //=======================================================================
100 //function : GetMaterialTable
101 //purpose  :
102 //=======================================================================
103   const TopTools_DataMapOfShapeInteger& NMTAlgo_Splitter1::GetMaterialTable()const
104 {
105   return myMapSWMOut;
106 }
107 //=======================================================================
108 //function : SetMaterial
109 //purpose  :
110 //=======================================================================
111   void NMTAlgo_Splitter1::SetMaterial(const TopoDS_Shape& aS,
112                                       const Standard_Integer iMt)
113 {
114   TopExp_Explorer aExp;
115   //
116   aExp.Init(aS, TopAbs_SOLID);
117   for(; aExp.More(); aExp.Next()) {
118     const TopoDS_Shape& aSd=aExp.Current();
119     myMapSWM.Bind(aSd, iMt);
120   }
121 }
122 //=======================================================================
123 //function : AddShape
124 //purpose  :
125 //=======================================================================
126   void NMTAlgo_Splitter1::AddShape(const TopoDS_Shape& aS)
127 {
128   SetMaterial(aS, 0);
129   NMTAlgo_Splitter::AddShape(aS);
130 }
131 //=======================================================================
132 //function : AddToolWithMaterial
133 //purpose  :
134 //=======================================================================
135   void NMTAlgo_Splitter1::AddTool(const TopoDS_Shape& aS)
136                                             
137 {
138   SetMaterial(aS, 0);
139   NMTAlgo_Splitter::AddTool(aS);
140 }
141 //=======================================================================
142 //function : Build
143 //purpose  :
144 //=======================================================================
145   void NMTAlgo_Splitter1::Build(const TopAbs_ShapeEnum aLimit)
146 {
147   NMTAlgo_Splitter::Build(aLimit);
148   //
149   myMapSWMOut.Clear();
150   //
151   TreatSolids();
152   //
153   if (myRemoveWebs && myMapSWMOut.Extent()) {
154     RestParts();
155     TreatWebs();
156   }
157 }
158 //=======================================================================
159 //function : TreatWebs
160 //purpose  :
161 //=======================================================================
162   void NMTAlgo_Splitter1::TreatWebs()
163 {
164   Standard_Integer nMt;
165   TopTools_DataMapOfIntegerListOfShape aMMLS;
166   TopoDS_Iterator aIt;
167   TopTools_DataMapIteratorOfDataMapOfShapeInteger aIt2;
168   TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape aIt1;
169   TopTools_ListIteratorOfListOfShape aItS;
170   TopoDS_Compound aCx;
171   TopoDS_Shape aCSR;
172   //
173   aIt2.Initialize(myMapSWMOut);
174   for (; aIt2.More(); aIt2.Next()) {
175     const TopoDS_Shape& aS=aIt2.Key();
176     nMt=aIt2.Value();
177     //
178     if (aMMLS.IsBound(nMt)){
179       TopTools_ListOfShape& aLS=aMMLS.ChangeFind(nMt);
180       aLS.Append(aS);
181     }
182     else {
183       TopTools_ListOfShape aLS;
184       aLS.Append(aS);
185       aMMLS.Bind(nMt, aLS);
186     }
187   }
188   //
189   myMapSWMOut.Clear();
190   myBuilder.MakeCompound(aCx);
191   //
192   aIt1.Initialize(aMMLS);
193   for (; aIt1.More(); aIt1.Next()) {
194     nMt=aIt1.Key();
195     //
196     TopoDS_Compound aCS;
197     myBuilder.MakeCompound(aCS);
198     const TopTools_ListOfShape& aLS=aIt1.Value();
199     aItS.Initialize(aLS);
200     for (; aItS.More(); aItS.Next()) {
201       const TopoDS_Shape& aS=aItS.Value();
202       myBuilder.Add(aCS, aS);
203     }
204     //
205     NMTAlgo_Tools::BreakWebs(aCS, aCSR);
206     //
207     aIt.Initialize(aCSR);
208     for(; aIt.More(); aIt.Next()) {
209       const TopoDS_Shape& aS=aIt.Value();
210       myBuilder.Add(aCx, aS);
211       //
212       myMapSWMOut.Bind(aS, nMt);
213     }
214   }
215   //
216   aIt.Initialize(myRestParts);
217   for(; aIt.More(); aIt.Next()) {
218     const TopoDS_Shape& aS=aIt.Value();
219     myBuilder.Add(aCx, aS);
220   }
221   //
222   myShape=aCx;
223 }
224
225 //=======================================================================
226 //function : RestParts
227 //purpose  :
228 //=======================================================================
229   void NMTAlgo_Splitter1::RestParts()
230 {
231   TopoDS_Iterator aIt;
232   TopoDS_Compound aR;
233   //
234   myBuilder.MakeCompound(aR);
235   //
236   aIt.Initialize(myShape);
237   for(; aIt.More(); aIt.Next()) {
238     const TopoDS_Shape& aS=aIt.Value();
239     if (aS.ShapeType()==TopAbs_SOLID){
240       if (myMapSWMOut.IsBound(aS)) {
241         continue;
242       }
243     }
244     myBuilder.Add(aR, aS);
245   }
246   myRestParts=aR;
247 }
248 //=======================================================================
249 //function : TreatSolids
250 //purpose  :
251 //=======================================================================
252   void NMTAlgo_Splitter1::TreatSolids()
253 {
254   Standard_Integer i, j, aNbSd, aNbSdx, nMtMax, nMt;
255   TopAbs_ShapeEnum aType;
256   TopoDS_Iterator aIt;
257   TopTools_IndexedMapOfShape aMSo, aMSd;
258   TopTools_IndexedDataMapOfShapeListOfShape aMFS;
259   TopTools_ListIteratorOfListOfShape aItS, aItLS;
260   NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMCS;
261   //
262   TopExp::MapShapesAndAncestors(myShape, TopAbs_FACE, TopAbs_SOLID, aMFS);
263   TopExp::MapShapes(myShape, TopAbs_SOLID, aMSo);
264   //
265   aNbSd=aMSo.Extent();
266   if (!aNbSd) {
267     return;
268   }
269   //
270   aItS.Initialize(myListShapes);
271   for ( ;aItS.More(); aItS.Next()) {
272     const TopoDS_Shape& aS=aItS.Value();
273     //
274     aType=aS.ShapeType();
275     if (aType != TopAbs_SOLID) {
276       continue;
277     }
278     //
279     if (myToolShapes.Contains(aS)) {
280       continue;
281     }
282     //
283     if (!myMapSIFC.IsBound(aS)){
284       continue;
285     }
286     //
287     aMSd.Clear();
288     //
289     const TopoDS_Shape& aIFC=myMapSIFC.Find(aS);
290     //
291     aIt.Initialize(aIFC);
292     for (; aIt.More(); aIt.Next()) {
293       const TopoDS_Shape& aIF=aIt.Value();
294       if (aMFS.Contains(aIF)) {
295         const TopTools_ListOfShape& aLS=aMFS.FindFromKey(aIF);
296         aItLS.Initialize(aLS);
297         for ( ;aItLS.More(); aItLS.Next()) {
298           const TopoDS_Shape& aSd=aItLS.Value();
299           if (!aMSd.Contains(aSd)) {
300             aMSd.Add(aSd);
301           }
302         }
303       }
304     }
305     //
306     aNbSd=aMSd.Extent();
307     if (!aNbSd) {
308       //modified by NIZNHY-PKV Thu Dec 23 15:07:46 2004 f
309       Standard_Boolean bFound;
310       //modified by NIZNHY-PKV Thu Dec 23 15:07:49 2004 t
311       TopoDS_Shape aSd;
312       //
313       const TopoDS_Shape& aFC=myImageShape.Image(aS).First();
314       //modified by NIZNHY-PKV Thu Dec 23 15:08:17 2004 f
315       //NMTAlgo_Tools::FindImageSolid(aFC, aMSo, aSd);
316       //aMSd.Add(aSd);
317       bFound=NMTAlgo_Tools::FindImageSolid(aFC, aMSo, aSd);
318       if (!aSd.IsNull()) {
319         aMSd.Add(aSd);
320       }
321       //modified by NIZNHY-PKV Thu Dec 23 15:09:02 2004 t
322     }
323     aMCS.Add(aS, aMSd); 
324   } //for ( ;aItS.More(); aItS.Next())
325   //
326   aMFS.Clear();
327   //
328   aNbSd=aMCS.Extent();
329   for (i=1; i<=aNbSd; ++i) {
330     const TopoDS_Shape& aSd=aMCS.FindKey(i);
331     const TopTools_IndexedMapOfShape& aMSdx=aMCS(i);
332     aNbSdx=aMSdx.Extent();
333     for (j=1; j<=aNbSdx; ++j) {
334       const TopoDS_Shape& aSdx=aMSdx(j);
335       //
336       if (aMFS.Contains(aSdx)) {
337         TopTools_ListOfShape& aLS=aMFS.ChangeFromKey(aSdx);
338         aLS.Append(aSd);
339       }
340       else {
341         TopTools_ListOfShape aLS;
342         aLS.Append(aSd);
343         aMFS.Add(aSdx, aLS);
344       }
345     }
346   }
347   //
348   // Assign materials values to subsolids
349   //
350   myMapSWMOut.Clear();
351   //
352   aNbSdx=aMFS.Extent();
353   for (i=1; i<=aNbSdx; ++i) {
354     const TopoDS_Shape& aSdx=aMFS.FindKey(i);
355     const TopTools_ListOfShape& aLS=aMFS(i);
356     aItLS.Initialize(aLS);
357     for (j=0; aItLS.More(); aItLS.Next(), ++j) {
358       const TopoDS_Shape& aSd=aItLS.Value();
359       //
360       if (!myMapSWM.IsBound(aSd)){
361         continue;
362       }
363       //
364       nMt=myMapSWM.Find(aSd);
365       if (!j) {
366         nMtMax=nMt;
367         continue;
368       }
369       if (nMt>nMtMax) {
370         nMtMax=nMt;
371       }
372     }
373     myMapSWMOut.Bind(aSdx, nMtMax);
374   }
375   //
376 }