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