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