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