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