Salome HOME
IMP 0021129: EDF 1541 GEOM: Problem with GetInPlace. A new GetInPlace implementation...
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_GetInPlace.cxx
1 // Copyright (C) 2007-2011  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:        GEOMAlgo_GetInPlace.cxx
23 // Created:     
24 // Author:      Peter KURNEV
25
26 #include <GEOMAlgo_GetInPlace.hxx>
27
28 #include <NMTDS_BoxBndTree.hxx>
29 #include <NCollection_UBTreeFiller.hxx>
30
31 #include <Bnd_Box.hxx>
32 #include <gp_Pnt.hxx>
33
34 #include <TColStd_ListOfInteger.hxx>
35 #include <TColStd_ListIteratorOfListOfInteger.hxx>
36
37 #include <TopAbs_ShapeEnum.hxx>
38
39 #include <TopoDS_Iterator.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Compound.hxx>
45
46 #include <BRep_Tool.hxx>
47 #include <BRep_Builder.hxx>
48
49 #include <BRepBndLib.hxx>
50
51 #include <TopExp.hxx>
52
53 #include <TopTools_IndexedMapOfShape.hxx>
54 #include <TopTools_ListOfShape.hxx>
55 #include <TopTools_DataMapOfShapeListOfShape.hxx>
56 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
57 #include <TopTools_ListIteratorOfListOfShape.hxx>
58 #include <TopTools_MapOfShape.hxx>
59 #include <TopTools_MapIteratorOfMapOfShape.hxx>
60
61
62 #include <NMTTools_CoupleOfShape.hxx>
63 #include <GEOMAlgo_Tools.hxx>
64
65
66 static 
67   void MapBRepShapes(const TopoDS_Shape& aS,
68                      TopTools_IndexedMapOfShape& aM);
69
70
71 //=======================================================================
72 //function : GEOMAlgo_GetInPlace
73 //purpose  : 
74 //=======================================================================
75 GEOMAlgo_GetInPlace::GEOMAlgo_GetInPlace()
76 :
77   GEOMAlgo_GluerAlgo(),
78   GEOMAlgo_Algo()
79 {
80   myTolerance=0.0001;
81   myTolMass=0.0001;
82   myTolCG=0.0001;
83   myFound=Standard_False;
84   myCheckGeometry=Standard_True;
85 }
86 //=======================================================================
87 //function : ~
88 //purpose  : 
89 //=======================================================================
90 GEOMAlgo_GetInPlace::~GEOMAlgo_GetInPlace()
91 {
92 }
93 //=======================================================================
94 //function : SetTolMass
95 //purpose  : 
96 //=======================================================================
97 void GEOMAlgo_GetInPlace::SetTolMass(const Standard_Real theTol)
98 {
99   myTolMass=theTol;
100 }
101 //=======================================================================
102 //function : TolMass
103 //purpose  : 
104 //=======================================================================
105 Standard_Real GEOMAlgo_GetInPlace::TolMass()const
106 {
107   return myTolMass;
108 }
109 //=======================================================================
110 //function : SetTolCG
111 //purpose  : 
112 //=======================================================================
113 void GEOMAlgo_GetInPlace::SetTolCG(const Standard_Real theTol)
114 {
115   myTolCG=theTol;
116 }
117 //=======================================================================
118 //function : TolCG
119 //purpose  : 
120 //=======================================================================
121 Standard_Real GEOMAlgo_GetInPlace::TolCG()const
122 {
123   return myTolCG;
124 }
125 //=======================================================================
126 //function : IsFound
127 //purpose  : 
128 //=======================================================================
129 Standard_Boolean GEOMAlgo_GetInPlace::IsFound()const
130 {
131   return myFound;
132 }
133 //=======================================================================
134 //function : SetShapeWhere
135 //purpose  : 
136 //=======================================================================
137 void GEOMAlgo_GetInPlace::SetShapeWhere(const TopoDS_Shape& theShape)
138 {
139   myShapeWhere=theShape;
140 }
141 //=======================================================================
142 //function : ShapeWhere
143 //purpose  : 
144 //=======================================================================
145 const TopoDS_Shape& GEOMAlgo_GetInPlace::ShapeWhere()const
146 {
147   return myShapeWhere;
148 }
149 //=======================================================================
150 //function : ShapesIn
151 //purpose  : 
152 //=======================================================================
153 const GEOMAlgo_DataMapOfShapeMapOfShape& GEOMAlgo_GetInPlace::ShapesIn()const
154 {
155   return myShapesIn;
156 }
157 //=======================================================================
158 //function : ShapesOn
159 //purpose  : 
160 //=======================================================================
161 const GEOMAlgo_DataMapOfShapeMapOfShape& GEOMAlgo_GetInPlace::ShapesOn()const
162 {
163   return myShapesOn;
164 }
165 //=======================================================================
166 //function : Clear
167 //purpose  : 
168 //=======================================================================
169 void GEOMAlgo_GetInPlace::Clear()
170 {
171   myErrorStatus=0;
172   myWarningStatus=0;
173   //
174   GEOMAlgo_GluerAlgo::Clear();
175   myIterator.Clear();
176   myShapesIn.Clear();
177   myShapesOn.Clear();
178   myMapShapePnt.Clear();
179 }
180 //=======================================================================
181 //function : Perform
182 //purpose  : 
183 //=======================================================================
184 void GEOMAlgo_GetInPlace::Perform()
185 {
186   myFound=Standard_False;
187   myErrorStatus=0;
188   myWarningStatus=0;
189   //
190   Clear();
191   if (myErrorStatus) {
192     return;
193   }
194   //
195   CheckData();
196   if (myErrorStatus) {
197     return;
198   }
199   //
200   Intersect();
201   if (myErrorStatus) {
202     return;
203   }
204   //
205   PerformVV();
206   if (myErrorStatus) {
207     return;
208   }
209   //
210   FillEdgesOn();
211   if (myErrorStatus) {
212     return;
213   }
214   //
215   PerformVE();
216   if (myErrorStatus) {
217     return;
218   }
219   //
220   PerformEE();
221   if (myErrorStatus) {
222     return;
223   }
224   //
225   PerformVF();
226   if (myErrorStatus) {
227     return;
228   }
229   //
230   FillFacesOn();
231   if (myErrorStatus) {
232     return;
233   }
234   //
235   PerformEF();
236   if (myErrorStatus) {
237     return;
238   }
239   //
240   PerformFF();
241   if (myErrorStatus) {
242     return;
243   }
244   //
245   FillSolidsOn();
246   if (myErrorStatus) {
247     return;
248   }
249   //
250   PerformZF();
251   if (myErrorStatus) {
252     return;
253   }
254   //
255   PerformZZ();
256   if (myErrorStatus) {
257     return;
258   }
259   //
260   FillImages();
261   if (myErrorStatus) {
262     return;
263   }
264   //
265   CheckGProps();
266   if (myErrorStatus) {
267     return;
268   }
269 }
270 //=======================================================================
271 //function : CheckData
272 //purpose  : 
273 //=======================================================================
274 void GEOMAlgo_GetInPlace::CheckData()
275 {
276   myErrorStatus=0;
277   myWarningStatus=0;
278   //
279   if (myArgument.IsNull()) {
280     myErrorStatus=2;
281     return;
282   }
283   //
284   if (myShapeWhere.IsNull()) {
285     myErrorStatus=3;
286     return;
287   }
288 }
289 //=======================================================================
290 //function : Intersect
291 //purpose  : 
292 //=======================================================================
293 void GEOMAlgo_GetInPlace::Intersect()
294 {
295   Standard_Integer i, j, aNbS1, aNbS2, aNbSD;
296   TColStd_ListIteratorOfListOfInteger aItLI;
297   TopTools_IndexedMapOfShape aMS1, aMS2;
298   TopTools_DataMapOfShapeListOfShape aDMSLS;
299   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
300   TopTools_ListIteratorOfListOfShape aItLS;
301   NMTTools_CoupleOfShape aCS;
302   //
303   NMTDS_BoxBndTreeSelector aSelector;
304   NMTDS_BoxBndTree aBBTree;
305   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
306   //
307   myErrorStatus=0;
308   myWarningStatus=0;
309   //
310   myIterator.Clear();
311   //
312   MapBRepShapes(myArgument, aMS1);
313   aNbS1=aMS1.Extent();
314   for (i=1; i<=aNbS1; ++i) {
315     Bnd_Box aBox1;
316     //
317     const TopoDS_Shape& aS1=aMS1(i);
318     BRepBndLib::Add(aS1, aBox1);
319     aBox1.SetGap(myTolerance);
320     //
321     aTreeFiller.Add(i, aBox1);
322   }
323   //
324   aTreeFiller.Fill();
325   //
326   MapBRepShapes(myShapeWhere, aMS2);
327   aNbS2=aMS2.Extent();
328   for (j=1; j<=aNbS2; ++j) {
329     Bnd_Box aBox2;
330     //
331     const TopoDS_Shape& aS2=aMS2(j);
332     BRepBndLib::Add(aS2, aBox2);
333     aBox2.SetGap(myTolerance);
334     //
335     aSelector.Clear();
336     aSelector.SetBox(aBox2);
337     aNbSD=aBBTree.Select(aSelector);
338     if (!aNbSD) {
339       continue;  // it should not be 
340     }
341     //
342     const TColStd_ListOfInteger& aLI=aSelector.Indices();
343     aItLI.Initialize(aLI);
344     for (; aItLI.More(); aItLI.Next()) {
345       i=aItLI.Value();
346       const TopoDS_Shape& aS1=aMS1(i);
347       //
348       if (aDMSLS.IsBound(aS1)) {
349         TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aS1);
350         aLS.Append(aS2);
351       }
352       else {
353         TopTools_ListOfShape aLS;
354         //
355         aLS.Append(aS2);
356         aDMSLS.Bind(aS1, aLS);
357       }
358     }
359   }// for (j=1; j<=aNbS2; ++j) {
360   //
361   aItDMSLS.Initialize(aDMSLS);
362   for (; aItDMSLS.More(); aItDMSLS.Next()) {
363     const TopoDS_Shape& aS1=aItDMSLS.Key();
364     const TopTools_ListOfShape& aLS2=aItDMSLS.Value();
365     aCS.SetShape1(aS1);
366     aItLS.Initialize(aLS2);
367     for (; aItLS.More(); aItLS.Next()) {
368       const TopoDS_Shape& aS2=aItLS.Value();
369       aCS.SetShape2(aS2);
370       myIterator.AppendPair(aCS);
371     }
372   }
373 }
374 //=======================================================================
375 //function : PerformVV
376 //purpose  : 
377 //=======================================================================
378 void GEOMAlgo_GetInPlace::PerformVV()
379 {
380   myErrorStatus=0;
381   myWarningStatus=0;
382   //
383   myIterator.Initialize(TopAbs_VERTEX, TopAbs_VERTEX);
384   for (; myIterator.More(); myIterator.Next()) {
385     const NMTTools_CoupleOfShape& aCS=myIterator.Value();
386     const TopoDS_Shape& aV1=aCS.Shape1();
387     const TopoDS_Shape& aV2=aCS.Shape2();
388     //
389     FillShapesOn(aV1, aV2);
390   }
391 }
392 //=======================================================================
393 //function : FillEdgesOn
394 //purpose  : 
395 //=======================================================================
396 void GEOMAlgo_GetInPlace::FillEdgesOn()
397 {
398   Standard_Integer i, aNbE, aNbSOn;
399   TopoDS_Iterator aIt;
400   TopTools_IndexedMapOfShape aME;
401   TopTools_MapIteratorOfMapOfShape aItMS;
402   //
403   TopExp::MapShapes(myArgument, TopAbs_EDGE, aME);
404   aNbE=aME.Extent();
405   for (i=1; i<=aNbE; ++i) {
406     const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aME(i));
407     if (BRep_Tool::Degenerated(aE1)) {
408       continue;
409     }
410     //
411     aIt.Initialize(aE1);
412     for (; aIt.More(); aIt.Next()) {
413       const TopoDS_Shape& aV1=aIt.Value();
414       if (myShapesOn.IsBound(aV1)) {
415         const TopTools_MapOfShape& aMSOn=myShapesOn.Find(aV1);
416         aNbSOn=aMSOn.Extent();
417         aItMS.Initialize(aMSOn);
418         for (; aItMS.More(); aItMS.Next()) {
419           const TopoDS_Shape& aV2=aItMS.Key();
420           FillShapesOn(aE1, aV2);
421         }
422       }
423     }
424   }
425 }
426 //=======================================================================
427 //function : PerformVE
428 //purpose  : 
429 //=======================================================================
430 void GEOMAlgo_GetInPlace::PerformVE()
431 {
432   Standard_Boolean bFound;
433   //
434   myErrorStatus=0;
435   myWarningStatus=0;
436   //
437   // 2. Fill Shapes In
438   myIterator.Initialize(TopAbs_EDGE, TopAbs_VERTEX);
439   for (; myIterator.More(); myIterator.Next()) {
440     const NMTTools_CoupleOfShape& aCS=myIterator.Value();
441     const TopoDS_Shape& aE1=aCS.Shape1();
442     const TopoDS_Shape& aV2=aCS.Shape2();
443     //
444     if (myShapesOn.IsBound(aE1)) {
445       const TopTools_MapOfShape& aMSOn=myShapesOn.Find(aE1);
446       if (aMSOn.Contains(aV2)) {
447         continue;
448       }
449     }
450     //
451     bFound=CheckCoincidence(aE1, aV2);
452     if (myErrorStatus) {
453       return;
454     }
455     if (bFound) {
456       FillShapesIn(aE1, aV2);
457     }
458   }
459 }
460 //=======================================================================
461 //function : PerformEE
462 //purpose  : 
463 //=======================================================================
464 void GEOMAlgo_GetInPlace::PerformEE()
465 {
466   Standard_Boolean bHasOn, bHasIn, bFound;
467   TopoDS_Iterator aIt;
468   TopTools_MapOfShape aMSX;
469   //
470   myErrorStatus=0;
471   myWarningStatus=0;
472   //
473   myIterator.Initialize(TopAbs_EDGE, TopAbs_EDGE);
474   for (; myIterator.More(); myIterator.Next()) {
475     const NMTTools_CoupleOfShape& aCS=myIterator.Value();
476     const TopoDS_Shape& aE1=aCS.Shape1();
477     const TopoDS_Shape& aE2=aCS.Shape2();
478     //
479     bHasOn=myShapesOn.IsBound(aE1);
480     bHasIn=myShapesIn.IsBound(aE1);
481     const TopTools_MapOfShape& aMSOn=(bHasOn) ? myShapesOn.Find(aE1) : aMSX;
482     const TopTools_MapOfShape& aMSIn=(bHasIn) ? myShapesIn.Find(aE1) : aMSX;
483     //
484     bFound=Standard_True;
485     aIt.Initialize(aE2);
486     for (; aIt.More(); aIt.Next()) {
487       const TopoDS_Shape& aV2=aIt.Value();
488       if (!(aMSOn.Contains(aV2) || aMSIn.Contains(aV2))) {
489         bFound=!bFound;
490         break;
491       }
492     }
493     if (!bFound) {
494       continue;
495     }
496     //
497     bFound=CheckCoincidence(aE1, aE2);
498     if (myErrorStatus) {
499       return;
500     }
501     if (bFound) {
502       FillShapesIn(aE1, aE2);
503     }
504   }
505 }
506 //=======================================================================
507 //function : PerformVF
508 //purpose  : 
509 //=======================================================================
510 void GEOMAlgo_GetInPlace::PerformVF()
511 {
512   Standard_Boolean bHasOn, bHasIn, bFound;
513   Standard_Integer i, aNbE;
514   TopTools_MapOfShape aMSX;
515   TopTools_IndexedMapOfShape aME;
516   //
517   myErrorStatus=0;
518   myWarningStatus=0;
519   //
520   myIterator.Initialize(TopAbs_FACE, TopAbs_VERTEX);
521   for (; myIterator.More(); myIterator.Next()) {
522     const NMTTools_CoupleOfShape& aCS=myIterator.Value();
523     const TopoDS_Shape& aF1=aCS.Shape1();
524     const TopoDS_Shape& aV2=aCS.Shape2();
525     //
526     aME.Clear();
527     TopExp::MapShapes(aF1, TopAbs_EDGE, aME);
528     //
529     bFound=Standard_False;
530     aNbE=aME.Extent();
531     for (i=1; i<=aNbE; ++i) {
532       const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aME(i));
533       if (BRep_Tool::Degenerated(aE1)) {
534         continue;
535       }
536       //
537       bHasOn=myShapesOn.IsBound(aE1);
538       bHasIn=myShapesIn.IsBound(aE1);
539       const TopTools_MapOfShape& aMSOn=(bHasOn) ? myShapesOn.Find(aE1) : aMSX;
540       const TopTools_MapOfShape& aMSIn=(bHasIn) ? myShapesIn.Find(aE1) : aMSX;
541       bFound= (aMSOn.Contains(aV2) || aMSIn.Contains(aV2));
542       if (bFound) {
543         break;
544       }
545     }
546     //
547     if (bFound) {
548       continue;
549     }
550     //
551     bFound=CheckCoincidence(aF1, aV2);
552     if (myErrorStatus) {
553       return;
554     }
555     if (bFound) {
556       FillShapesIn(aF1, aV2);
557     }
558   }
559 }
560 //=======================================================================
561 //function : FillFacesOn
562 //purpose  : 
563 //=======================================================================
564 void GEOMAlgo_GetInPlace::FillFacesOn()
565 {
566   Standard_Integer i, j, aNbF, aNbE, aNbSOn;
567   TopoDS_Iterator aIt;
568   TopTools_IndexedMapOfShape aMF, aME;
569   TopTools_MapIteratorOfMapOfShape aItMS;
570   //
571   TopExp::MapShapes(myArgument, TopAbs_FACE, aMF);
572   aNbF=aMF.Extent();
573   for (i=1; i<=aNbF; ++i) {
574     const TopoDS_Face& aF1=*((TopoDS_Face*)&aMF(i));
575     //
576     aME.Clear();
577     TopExp::MapShapes(aF1, TopAbs_EDGE, aME);
578     aNbE=aME.Extent();
579     for (j=1; j<=aNbE; ++j) {
580       const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aME(j));
581       if (BRep_Tool::Degenerated(aE1)) {
582         continue;
583       }
584       //
585       if (myShapesOn.IsBound(aE1)) {
586         const TopTools_MapOfShape& aMSOn=myShapesOn.Find(aE1);
587         aItMS.Initialize(aMSOn);
588         for (; aItMS.More(); aItMS.Next()) {
589           const TopoDS_Shape& aS2=aItMS.Key();
590           FillShapesOn(aF1, aS2);
591         }
592       }
593       //
594       if (myShapesIn.IsBound(aE1)) {
595         const TopTools_MapOfShape& aMSIn=myShapesIn.Find(aE1);
596         aItMS.Initialize(aMSIn);
597         for (; aItMS.More(); aItMS.Next()) {
598           const TopoDS_Shape& aS2=aItMS.Key();
599           FillShapesOn(aF1, aS2);
600         }
601       }
602     }//for (j=1; j<=aNbE; ++j) {
603   }//for (i=1; i<=aNbF; ++i) {
604 }
605 //=======================================================================
606 //function : PerformEF
607 //purpose  : 
608 //=======================================================================
609 void GEOMAlgo_GetInPlace::PerformEF()
610 {
611   Standard_Boolean  bFound, bHasOnF, bHasInF;
612   Standard_Integer i, aNbE;
613   TopoDS_Iterator aIt;
614   TopTools_MapOfShape aMSX;
615   //
616   myErrorStatus=0;
617   myWarningStatus=0;
618   //
619   myIterator.Initialize(TopAbs_FACE, TopAbs_EDGE);
620   for (; myIterator.More(); myIterator.Next()) {
621     const NMTTools_CoupleOfShape& aCS=myIterator.Value();
622     const TopoDS_Shape& aF1=aCS.Shape1();
623     const TopoDS_Shape& aE2=aCS.Shape2();
624     //
625     // 1.
626     bHasOnF=myShapesOn.IsBound(aF1);
627     const TopTools_MapOfShape& aMSOnF=(bHasOnF) ? myShapesOn.Find(aF1) : aMSX;
628     bFound=aMSOnF.Contains(aE2);
629     if (bFound) {
630       continue;
631     }
632     // 
633     // 2.
634     bHasInF=myShapesIn.IsBound(aF1);
635     const TopTools_MapOfShape& aMSInF=(bHasInF) ? myShapesIn.Find(aF1) : aMSX;
636     //
637     aIt.Initialize(aE2);
638     for (; aIt.More(); aIt.Next()) {
639       const TopoDS_Shape& aV2=aIt.Value();
640       bFound=(aMSOnF.Contains(aV2) || aMSInF.Contains(aV2));
641       if (!bFound) {
642         break;
643       }
644     }
645     if (!bFound) {
646       continue;
647     }
648     //------------------------------
649     bFound=CheckCoincidence(aF1, aE2);
650     if (myErrorStatus) {
651       return;
652     }
653     if (bFound) {
654       FillShapesIn(aF1, aE2);
655     }
656   }
657 }
658 //=======================================================================
659 //function : PerformFF
660 //purpose  : 
661 //=======================================================================
662 void GEOMAlgo_GetInPlace::PerformFF()
663 {
664   Standard_Boolean  bFound, bHasOnF, bHasInF;
665   Standard_Integer i, aNbS2;
666   TopTools_MapOfShape aMSX;
667   TopTools_IndexedMapOfShape aMS2;
668   //
669   myErrorStatus=0;
670   myWarningStatus=0;
671   //
672   myIterator.Initialize(TopAbs_FACE, TopAbs_FACE);
673   for (; myIterator.More(); myIterator.Next()) {
674     const NMTTools_CoupleOfShape& aCS=myIterator.Value();
675     const TopoDS_Shape& aF1=aCS.Shape1();
676     const TopoDS_Shape& aF2=aCS.Shape2();
677     //
678     bHasOnF=myShapesOn.IsBound(aF1);
679     const TopTools_MapOfShape& aMSOnF=(bHasOnF) ? myShapesOn.Find(aF1) : aMSX;
680     //
681     bHasInF=myShapesIn.IsBound(aF1);
682     const TopTools_MapOfShape& aMSInF=(bHasInF) ? myShapesIn.Find(aF1) : aMSX;
683     //
684     aMS2.Clear();
685     MapBRepShapes(aF2, aMS2);
686     //
687     bFound=Standard_False;
688     aNbS2=aMS2.Extent();
689     for (i=1; i<=aNbS2; ++i) {
690       const TopoDS_Shape& aS2=aMS2(i);
691       if (aS2.IsSame(aF2)) {
692         continue;
693       }
694       bFound=(aMSOnF.Contains(aS2) || aMSInF.Contains(aS2));
695       if (!bFound) {
696         break;
697       }
698     }
699     if (!bFound) {
700       continue;
701     }
702     //
703     bFound=CheckCoincidence(aF1, aF2);
704     if (myErrorStatus) {
705       return;
706     }
707     if (bFound) {
708       FillShapesIn(aF1, aF2);
709     }
710   }
711 }
712 //=======================================================================
713 //function : FillSolidsOn
714 //purpose  : 
715 //=======================================================================
716 void GEOMAlgo_GetInPlace::FillSolidsOn()
717 {
718   Standard_Integer i, j, aNbS, aNbF, aNbSOn;
719   TopTools_IndexedMapOfShape aMS, aMF;
720   TopTools_MapIteratorOfMapOfShape aItMS;
721   //
722   TopExp::MapShapes(myArgument, TopAbs_SOLID, aMS);
723   //
724   aNbS=aMS.Extent();
725   for (i=1; i<=aNbS; ++i) {
726     const TopoDS_Shape& aSD1=aMS(i);
727     //
728     aMF.Clear();
729     TopExp::MapShapes(aSD1, TopAbs_FACE, aMF);
730     aNbF=aMF.Extent();
731     for (j=1; j<=aNbF; ++j) {
732       const TopoDS_Shape& aF1=aMF(j);
733       //
734       if (myShapesOn.IsBound(aF1)) {
735         const TopTools_MapOfShape& aMSOn=myShapesOn.Find(aF1);
736         aItMS.Initialize(aMSOn);
737         for (; aItMS.More(); aItMS.Next()) {
738           const TopoDS_Shape& aS2=aItMS.Key();
739           FillShapesOn(aSD1, aS2);
740         }
741       }
742       //
743       if (myShapesIn.IsBound(aF1)) {
744         const TopTools_MapOfShape& aMSIn=myShapesIn.Find(aF1);
745         aItMS.Initialize(aMSIn);
746         for (; aItMS.More(); aItMS.Next()) {
747           const TopoDS_Shape& aS2=aItMS.Key();
748           FillShapesOn(aSD1, aS2);
749         }
750       }
751     }//for (j=1; j<=aNbF; ++j) {
752   }//for (i=1; i<=aNbS; ++i) {
753 }
754 //=======================================================================
755 //function : PerformZF
756 //purpose  : 
757 //=======================================================================
758 void GEOMAlgo_GetInPlace::PerformZF()
759 {
760   Standard_Boolean  bFound, bHasOnF, bHasInF;
761   Standard_Integer i, aNbE;
762   TopTools_MapOfShape aMSX;
763   //
764   myErrorStatus=0;
765   myWarningStatus=0;
766   //
767   myIterator.Initialize(TopAbs_SOLID, TopAbs_FACE);
768   for (; myIterator.More(); myIterator.Next()) {
769     const NMTTools_CoupleOfShape& aCS=myIterator.Value();
770     const TopoDS_Shape& aSo1=aCS.Shape1();
771     const TopoDS_Shape& aF2=aCS.Shape2();
772     //
773     bHasOnF=myShapesOn.IsBound(aSo1);
774     const TopTools_MapOfShape& aMSOnF=(bHasOnF) ? myShapesOn.Find(aSo1) : aMSX;
775     bFound=aMSOnF.Contains(aF2);
776     if (bFound) {
777       continue;
778     }
779     //------------------------------
780     bFound=CheckCoincidence(aSo1, aF2);
781     if (myErrorStatus) {
782       return;
783     }
784     if (bFound) {
785       FillShapesIn(aSo1, aF2);
786     }
787   }
788 }
789 //=======================================================================
790 //function : PerformZZ
791 //purpose  : 
792 //=======================================================================
793 void GEOMAlgo_GetInPlace::PerformZZ()
794 {
795   Standard_Boolean bFound, bHasOn, bHasIn;
796   Standard_Integer i, aNbS2, iCntOn, iCntIn, iCntOut;
797   TopTools_MapOfShape aMSX;
798   TopTools_IndexedMapOfShape aMS2;
799   //
800   myErrorStatus=0;
801   myWarningStatus=0;
802   //
803   myIterator.Initialize(TopAbs_SOLID, TopAbs_SOLID);
804   for (; myIterator.More(); myIterator.Next()) {
805     const NMTTools_CoupleOfShape& aCS=myIterator.Value();
806     const TopoDS_Shape& aSo1=aCS.Shape1();
807     const TopoDS_Shape& aSo2=aCS.Shape2();
808     //
809     bHasOn=myShapesOn.IsBound(aSo1);
810     const TopTools_MapOfShape& aMSOn=(bHasOn) ? myShapesOn.Find(aSo1) : aMSX;
811     //
812     bHasIn=myShapesIn.IsBound(aSo1);
813     const TopTools_MapOfShape& aMSIn=(bHasIn) ? myShapesIn.Find(aSo1) : aMSX;
814     //
815     aMS2.Clear();
816     TopExp::MapShapes(aSo2, TopAbs_FACE, aMS2);
817     //
818     iCntIn=0;
819     iCntOn=0;
820     iCntOut=0;
821     bFound=Standard_False;
822     aNbS2=aMS2.Extent();
823     for (i=1; i<=aNbS2; ++i) {
824       const TopoDS_Shape& aF2=aMS2(i);
825       //
826       if (aMSIn.Contains(aF2)) {
827         ++iCntIn;
828         bFound=Standard_True;
829         break;
830       }
831       else if (!aMSOn.Contains(aF2)) {
832         ++iCntOut;
833         bFound=Standard_False;// out
834         break;
835       }
836       else {
837         ++iCntOn; //on
838       }
839     }
840     //
841     if (!bFound && iCntOut) {
842       continue;
843     }
844     //
845     if (!iCntIn) {
846       bFound=CheckCoincidence(aSo1, aSo2);
847       if (myErrorStatus) {
848         return;
849       }
850     }
851     if (bFound) {
852       FillShapesIn(aSo1, aSo2);
853     }
854   }// for (; myIterator.More(); myIterator.Next()) {
855 }
856 //=======================================================================
857 //function : FillImages
858 //purpose  : 
859 //=======================================================================
860 void GEOMAlgo_GetInPlace::FillImages()
861 {
862   Standard_Integer i, aNbS, aNbSx, aNbSi;
863   TopAbs_ShapeEnum aType;
864   TopoDS_Iterator aIt;
865   TopTools_ListOfShape aLSx;
866   TopTools_ListIteratorOfListOfShape aItLS;
867   TopTools_IndexedMapOfShape aMS;
868   TopTools_MapIteratorOfMapOfShape aItMS;
869   //
870   myErrorStatus=0;
871   myWarningStatus=0;
872   //
873   myImages.Clear();  
874   //
875   // 1. Vertices
876   aMS.Clear();
877   TopExp::MapShapes(myArgument, TopAbs_VERTEX, aMS);
878   aNbS=aMS.Extent();
879   for (i=1; i<=aNbS; ++i) {
880     const TopoDS_Shape& aV=aMS(i);
881     if (myShapesOn.IsBound(aV)) {
882       const TopTools_MapOfShape& aMSx=myShapesOn.Find(aV);
883       aNbSx=aMSx.Extent();
884       //
885       aLSx.Clear();
886       aItMS.Initialize(aMSx);
887       for (; aItMS.More(); aItMS.Next()) {
888         const TopoDS_Shape& aVx=aItMS.Key();
889         aLSx.Append(aVx);
890       }
891       //
892       myImages.Bind(aV, aLSx);
893     }
894   }
895   //
896   // 2. Edges
897   aMS.Clear();
898   TopExp::MapShapes(myArgument, TopAbs_EDGE, aMS);
899   aNbS=aMS.Extent();
900   for (i=1; i<=aNbS; ++i) {
901     const TopoDS_Shape& aE=aMS(i);
902     if (myShapesIn.IsBound(aE)) {
903       const TopTools_MapOfShape& aMSx=myShapesIn.Find(aE);
904       aNbSx=aMSx.Extent();
905       //
906       aLSx.Clear();
907       aItMS.Initialize(aMSx);
908       for (; aItMS.More(); aItMS.Next()) {
909         const TopoDS_Shape& aEx=aItMS.Key();
910         aType=aEx.ShapeType();
911         if (aType==TopAbs_EDGE){
912           aLSx.Append(aEx);
913         }
914       }
915       //
916       myImages.Bind(aE, aLSx);
917     }
918   }
919   //
920   // 3. Wires
921   aMS.Clear();
922   TopExp::MapShapes(myArgument, TopAbs_WIRE, aMS);
923   aNbS=aMS.Extent();
924   for (i=1; i<=aNbS; ++i) {
925     const TopoDS_Shape& aW=aMS(i);
926     aLSx.Clear();
927     aIt.Initialize(aW);
928     for(; aIt.More(); aIt.Next()) {
929       const TopoDS_Shape& aE=aIt.Value();
930       if (myImages.IsBound(aE)) {
931         const TopTools_ListOfShape& aLSi=myImages.Find(aE);
932         aNbSi=aLSi.Extent();
933         //
934         aItLS.Initialize(aLSi);
935         for (; aItLS.More(); aItLS.Next()) {
936           const TopoDS_Shape& aEi=aItLS.Value();
937           aLSx.Append(aEi);
938         }
939       }
940     }
941     myImages.Bind(aW, aLSx);
942   }
943   //
944   // 4. Faces
945   aMS.Clear();
946   TopExp::MapShapes(myArgument, TopAbs_FACE, aMS);
947   aNbS=aMS.Extent();
948   for (i=1; i<=aNbS; ++i) {
949     const TopoDS_Shape& aF=aMS(i);
950     if (myShapesIn.IsBound(aF)) {
951       const TopTools_MapOfShape& aMSx=myShapesIn.Find(aF);
952       aNbSx=aMSx.Extent();
953       //
954       aLSx.Clear();
955       aItMS.Initialize(aMSx);
956       for (; aItMS.More(); aItMS.Next()) {
957         const TopoDS_Shape& aFx=aItMS.Key();
958         aType=aFx.ShapeType();
959         if (aType==TopAbs_FACE){
960           aLSx.Append(aFx);
961         }
962       }
963       //
964       myImages.Bind(aF, aLSx);
965     }
966   }
967   //
968   // 5. Shells
969   aMS.Clear();
970   TopExp::MapShapes(myArgument, TopAbs_SHELL, aMS);
971   aNbS=aMS.Extent();
972   for (i=1; i<=aNbS; ++i) {
973     const TopoDS_Shape& aSh=aMS(i);
974     aLSx.Clear();
975     aIt.Initialize(aSh);
976     for(; aIt.More(); aIt.Next()) {
977       const TopoDS_Shape& aF=aIt.Value();
978       if (myImages.IsBound(aF)) {
979         const TopTools_ListOfShape& aLSi=myImages.Find(aF);
980         aNbSi=aLSi.Extent();
981         //
982         aItLS.Initialize(aLSi);
983         for (; aItLS.More(); aItLS.Next()) {
984           const TopoDS_Shape& aFi=aItLS.Value();
985           aLSx.Append(aFi);
986         }
987       }
988     }
989     myImages.Bind(aSh, aLSx);
990   }
991   //
992   // 6. Solids
993   aMS.Clear();
994   TopExp::MapShapes(myArgument, TopAbs_SOLID, aMS);
995   aNbS=aMS.Extent();
996   for (i=1; i<=aNbS; ++i) {
997     const TopoDS_Shape& aZ=aMS(i);
998     if (myShapesIn.IsBound(aZ)) {
999       const TopTools_MapOfShape& aMSx=myShapesIn.Find(aZ);
1000       aNbSx=aMSx.Extent();
1001       //
1002       aLSx.Clear();
1003       aItMS.Initialize(aMSx);
1004       for (; aItMS.More(); aItMS.Next()) {
1005         const TopoDS_Shape& aZx=aItMS.Key();
1006         aType=aZx.ShapeType();
1007         if (aType==TopAbs_SOLID){
1008           aLSx.Append(aZx);
1009         }
1010       }
1011       //
1012       myImages.Bind(aZ, aLSx);
1013     }
1014   }
1015   //
1016   // 7. CompSolids
1017   aMS.Clear();
1018   TopExp::MapShapes(myArgument, TopAbs_COMPSOLID, aMS);
1019   aNbS=aMS.Extent();
1020   for (i=1; i<=aNbS; ++i) {
1021     const TopoDS_Shape& aCs=aMS(i);
1022     aLSx.Clear();
1023     aIt.Initialize(aCs);
1024     for(; aIt.More(); aIt.Next()) {
1025       const TopoDS_Shape& aZ=aIt.Value();
1026       if (myImages.IsBound(aZ)) {
1027         const TopTools_ListOfShape& aLSi=myImages.Find(aZ);
1028         aNbSi=aLSi.Extent();
1029         //
1030         aItLS.Initialize(aLSi);
1031         for (; aItLS.More(); aItLS.Next()) {
1032           const TopoDS_Shape& aZi=aItLS.Value();
1033           aLSx.Append(aZi);
1034         }
1035       }
1036     }
1037     myImages.Bind(aCs, aLSx);
1038   }
1039   //
1040   // 8. Compounds
1041   aType=myArgument.ShapeType();
1042   if (aType==TopAbs_COMPOUND) {
1043     FillImagesCompound(myArgument);
1044   }
1045 }
1046 //=======================================================================
1047 //function : FillImagesCompound
1048 //purpose  : 
1049 //=======================================================================
1050 void GEOMAlgo_GetInPlace::FillImagesCompound(const TopoDS_Shape& aS)
1051 {
1052   TopAbs_ShapeEnum aType;
1053   TopoDS_Iterator aIt;
1054   TopTools_ListOfShape aLSx;
1055   TopTools_ListIteratorOfListOfShape aItLS;
1056   //
1057   aIt.Initialize(aS);
1058   for(; aIt.More(); aIt.Next()) {
1059     const TopoDS_Shape& aSx=aIt.Value();
1060     aType=aSx.ShapeType();
1061     //
1062     if (aType==TopAbs_COMPOUND) {
1063       FillImagesCompound(aSx);
1064     }
1065     //
1066     if (myImages.IsBound(aSx)) {
1067       const TopTools_ListOfShape& aLSi=myImages.Find(aSx);
1068       aItLS.Initialize(aLSi);
1069       for (; aItLS.More(); aItLS.Next()) {
1070         const TopoDS_Shape& aSi=aItLS.Value();
1071         aLSx.Append(aSi);
1072       }
1073     }
1074   }
1075   myImages.Bind(aS, aLSx);
1076 }
1077
1078 //=======================================================================
1079 //function : FillShapesIn
1080 //purpose  : 
1081 //=======================================================================
1082 void GEOMAlgo_GetInPlace::FillShapesIn(const TopoDS_Shape& aS1,
1083                                        const TopoDS_Shape& aS2)
1084 {
1085   if (myShapesIn.IsBound(aS1)) {
1086     TopTools_MapOfShape& aMS=myShapesIn.ChangeFind(aS1);
1087     aMS.Add(aS2);
1088   }
1089   else {
1090     TopTools_MapOfShape aMS;
1091     //
1092     aMS.Add(aS2);
1093     myShapesIn.Bind(aS1, aMS);
1094   }
1095 }
1096 //=======================================================================
1097 //function : FillShapesOn
1098 //purpose  : 
1099 //=======================================================================
1100 void GEOMAlgo_GetInPlace::FillShapesOn(const TopoDS_Shape& aS1,
1101                                        const TopoDS_Shape& aS2)
1102 {
1103   if (myShapesOn.IsBound(aS1)) {
1104     TopTools_MapOfShape& aMS=myShapesOn.ChangeFind(aS1);
1105     aMS.Add(aS2);
1106   }
1107   else {
1108     TopTools_MapOfShape aMS;
1109     //
1110     aMS.Add(aS2);
1111     myShapesOn.Bind(aS1, aMS);
1112   }
1113 }
1114 //=======================================================================
1115 //function : MapBRepShapes
1116 //purpose  : 
1117 //=======================================================================
1118 void MapBRepShapes(const TopoDS_Shape& aS,
1119                    TopTools_IndexedMapOfShape& aM)
1120 {
1121   Standard_Boolean bHasBRep, bDegenerated;
1122   TopAbs_ShapeEnum aType;
1123   TopoDS_Iterator aIt;
1124   //
1125   aType=aS.ShapeType();
1126   if (aType==TopAbs_VERTEX || aType==TopAbs_EDGE || 
1127       aType==TopAbs_FACE   || aType==TopAbs_SOLID) {
1128     bDegenerated=Standard_False;
1129     if (aType==TopAbs_EDGE) {
1130       TopoDS_Edge *pE=(TopoDS_Edge*)&aS;
1131       bDegenerated=BRep_Tool::Degenerated(*pE);
1132     }
1133     if (!bDegenerated) {
1134       aM.Add(aS);
1135     }
1136   }
1137   //
1138   aIt.Initialize(aS);
1139   for(; aIt.More(); aIt.Next()) {
1140     const TopoDS_Shape& aSx=aIt.Value();
1141     aType=aSx.ShapeType();
1142     MapBRepShapes(aSx, aM);
1143   }
1144 }