56e11740ac2434eeb97eb2db107a5e7ef6c68ed1
[occt.git] / src / BOPAlgo / BOPAlgo_Builder_3.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2014 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 //                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 //
7 // This file is part of Open CASCADE Technology software library.
8 //
9 // This library is free software; you can redistribute it and / or modify it
10 // under the terms of the GNU Lesser General Public version 2.1 as published
11 // by the Free Software Foundation, with special exception defined in the file
12 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
13 // distribution for complete text of the license and disclaimer of any warranty.
14 //
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
17
18 #include <BOPAlgo_Builder.hxx>
19
20 #include <NCollection_IncAllocator.hxx>
21
22 #include <TopAbs_State.hxx>
23
24 #include <TopoDS.hxx>
25 #include <TopoDS_Iterator.hxx>
26 #include <TopoDS_Solid.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Face.hxx>
29 #include <TopoDS_Edge.hxx>
30 #include <TopoDS_Solid.hxx>
31 #include <TopoDS_Iterator.hxx>
32 #include <TopoDS_Shell.hxx>
33 #include <TopoDS_Compound.hxx>
34
35 #include <TopExp.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #include <BRep_Builder.hxx>
39 #include <BRepTools.hxx>
40 #include <BRepClass3d_SolidClassifier.hxx>
41 //
42 #include <BOPCol_IndexedMapOfShape.hxx>
43 #include <BOPCol_MapOfShape.hxx>
44 #include <BOPCol_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <BOPCol_ListOfShape.hxx>
46 //
47 #include <BOPDS_DS.hxx>
48 #include <BOPDS_ShapeInfo.hxx>
49 //
50 #include <BOPTools.hxx>
51 #include <BOPTools_AlgoTools.hxx>
52 //
53 #include <BOPTools_MapOfSet.hxx>
54 #include <BOPTools_Set.hxx>
55 //
56 #include <BOPAlgo_BuilderSolid.hxx>
57
58 #include <BOPCol_DataMapOfIntegerShape.hxx>
59 #include <Bnd_Box.hxx>
60 #include <BRepBndLib.hxx>
61
62 #include <NCollection_UBTreeFiller.hxx>
63 #include <BOPDS_BoxBndTree.hxx>
64 #include <BOPCol_ListOfInteger.hxx>
65 #include <BOPInt_Context.hxx>
66 #include <BOPAlgo_Builder_2Cnt.hxx>
67
68
69 static
70   void OwnInternalShapes(const TopoDS_Shape& ,
71                          BOPCol_IndexedMapOfShape& );
72
73 static
74   void TreatCompound(const TopoDS_Shape& theS,
75                      BOPCol_MapOfShape& aMFence,
76                      BOPCol_ListOfShape& theLS);
77
78 //=======================================================================
79 //class     : BOPAlgo_ShapeBox
80 //purpose   : Auxiliary class
81 //=======================================================================
82 class BOPAlgo_ShapeBox {
83  public:
84   BOPAlgo_ShapeBox() {
85   };
86   //
87   ~BOPAlgo_ShapeBox() {
88   };
89   //
90   void SetShape(const TopoDS_Shape& aS) {
91     myShape=aS;
92   };
93   //
94   const TopoDS_Shape& Shape()const {
95     return myShape;
96   };
97   //
98   void SetBox(const Bnd_Box& aBox) {
99     myBox=aBox;
100   };
101   //
102   const Bnd_Box& Box()const {
103     return myBox;
104   };
105   //
106  protected:
107   TopoDS_Shape myShape;
108   Bnd_Box myBox;
109 };
110 //
111 typedef NCollection_DataMap\
112   <Standard_Integer, BOPAlgo_ShapeBox, TColStd_MapIntegerHasher> \
113   BOPAlgo_DataMapOfIntegerShapeBox; 
114 //
115 typedef BOPAlgo_DataMapOfIntegerShapeBox::Iterator \
116   BOPAlgo_DataMapIteratorOfDataMapOfIntegerShapeBox; 
117 // 
118
119 //=======================================================================
120 //function : FillImagesSolids
121 //purpose  : 
122 //=======================================================================
123 void BOPAlgo_Builder::FillImagesSolids()
124 {
125   Standard_Boolean bHasSolids;
126   Standard_Integer i, aNbS;
127   //
128   myErrorStatus=0;
129   //
130   bHasSolids=Standard_False;
131   aNbS=myDS->NbSourceShapes();
132   for (i=0; i<aNbS; ++i) {
133     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
134     if (aSI.ShapeType()==TopAbs_SOLID) {
135       bHasSolids=!bHasSolids;
136       break;
137     }
138   }
139   //
140   if (!bHasSolids) {
141     return;
142   }
143   //
144   Handle(NCollection_IncAllocator) aAlr;
145   //
146   aAlr=new NCollection_IncAllocator();
147   BOPCol_DataMapOfShapeListOfShape theInParts(100, aAlr);
148   BOPCol_DataMapOfShapeShape theDraftSolids(100, aAlr);
149   //
150   FillIn3DParts(theInParts, theDraftSolids, aAlr); 
151   BuildSplitSolids(theInParts, theDraftSolids, aAlr);
152   FillInternalShapes();
153   //
154   theInParts.Clear();
155   theDraftSolids.Clear();
156 }
157 //=======================================================================
158 //function : FillIn3DParts
159 //purpose  : 
160 //=======================================================================
161 void BOPAlgo_Builder::FillIn3DParts(BOPCol_DataMapOfShapeListOfShape& theInParts,
162                                     BOPCol_DataMapOfShapeShape& theDraftSolids,
163                                     const BOPCol_BaseAllocator& )
164 {
165   Standard_Boolean bHasImage;
166   Standard_Integer i, k, aNbS, aNbLIF, nFP, aNbFP, aNbFIN, iIsIN;
167   TopoDS_Solid aSD;
168   TopoDS_Iterator aIt;
169   BRep_Builder aBB; 
170   BOPCol_ListIteratorOfListOfInteger aItLI, aItLI1;
171   BOPCol_ListIteratorOfListOfShape aItLS;
172   BOPAlgo_ShapeBox aSB;
173   Handle(NCollection_IncAllocator) aAlr0;
174   //
175   aAlr0=new NCollection_IncAllocator();
176   BOPAlgo_DataMapOfIntegerShapeBox aDMISB(100, aAlr0);
177   BOPAlgo_DataMapIteratorOfDataMapOfIntegerShapeBox aItDMISB;
178   //
179   myErrorStatus=0;
180   theDraftSolids.Clear();
181   //
182   // 1. aDMISB map Index/FaceBox 
183   k=0;
184   aNbS=myDS->NbSourceShapes();
185   for (i=0; i<aNbS; ++i) {
186     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
187     if (aSI.ShapeType()!=TopAbs_FACE) {
188       continue;
189     }
190     //
191     const TopoDS_Shape& aS=aSI.Shape();
192     //
193     if (myImages.IsBound(aS)) {
194       const BOPCol_ListOfShape& aLS=myImages.Find(aS);
195       aItLS.Initialize(aLS);
196       for (; aItLS.More(); aItLS.Next()) {
197         const TopoDS_Shape& aSx=aItLS.Value();
198         //
199         Bnd_Box aBox;
200         BRepBndLib::Add(aSx, aBox);
201         //
202         aSB.SetShape(aSx);
203         aSB.SetBox(aBox);
204         //
205         aDMISB.Bind(k, aSB);
206         ++k;
207       }
208     }
209     else {
210       const Bnd_Box& aBox=aSI.Box();
211       //
212       aSB.SetShape(aS);
213       aSB.SetBox(aBox);
214       //
215       aDMISB.Bind(k, aSB);
216       ++k;
217     }
218   }//for (i=0; i<aNbS; ++i) {
219   //
220   // 1.2. Prepare TreeFiller
221   BOPDS_BoxBndTree aBBTree;
222   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
223   //
224   aItDMISB.Initialize(aDMISB);
225   for (; aItDMISB.More(); aItDMISB.Next()) {
226     k=aItDMISB.Key();
227     const BOPAlgo_ShapeBox& aSBk=aItDMISB.Value();
228     const Bnd_Box& aBk=aSBk.Box();
229     //
230     aTreeFiller.Add(k, aBk);
231   }
232   //
233   // 1.3. Shake TreeFiller
234   aTreeFiller.Fill();
235   //
236   //---------------------------------------------
237   // 2. Solids
238   for (i=0; i<aNbS; ++i) {
239     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
240     if (aSI.ShapeType()!=TopAbs_SOLID) {
241       continue;
242     }
243     // 
244     //---------------------------------------------
245     Handle(NCollection_IncAllocator) aAlr1;
246     //
247     aAlr1=new NCollection_IncAllocator();
248     //
249     BOPCol_ListOfShape aLFIN(aAlr1);
250     BOPCol_ListOfShape aLIF(aAlr1);
251     BOPCol_IndexedMapOfShape aMF(100, aAlr1);
252     BOPCol_IndexedDataMapOfShapeListOfShape aMEF(100, aAlr1);
253     BOPDS_BoxBndTreeSelector aSelector;
254     Bnd_Box aBoxS;
255     //
256     const TopoDS_Shape& aS=aSI.Shape();
257     const TopoDS_Solid& aSolid=(*(TopoDS_Solid*)(&aS));
258     //
259     // 2.0 Flag bHasImage
260     bHasImage=Standard_False;
261     aIt.Initialize(aS);
262     for (; aIt.More(); aIt.Next()) {
263       const TopoDS_Shape& aShell=aIt.Value();
264       bHasImage=myImages.IsBound(aShell);
265       if (bHasImage){
266         break;
267       }
268     }
269     //
270     // 2.1 Bounding box for the solid aS  [ aBoxS ]
271     aBoxS=aSI.Box();
272     //-----
273     //
274     // 2.2 Build Draft Solid [aSD]
275     aBB.MakeSolid(aSD);
276     //
277     BuildDraftSolid(aSolid, aSD, aLIF);
278     aNbLIF=aLIF.Extent();
279     //
280     BOPTools::MapShapesAndAncestors(aSD, TopAbs_EDGE, TopAbs_FACE, aMEF);
281     //
282     // 2.3 Faces from aSD and own internal faces => aMF 
283     BOPTools::MapShapes(aSD, TopAbs_FACE, aMF);
284     //
285     aItLS.Initialize(aLIF);
286     for (; aItLS.More(); aItLS.Next()) {
287       const TopoDS_Shape& aFI=aItLS.Value();
288       aMF.Add(aFI);
289     }
290     //
291     // 2.4. Select boxes of faces that are not out of aBoxS
292     aSelector.Clear();
293     aSelector.SetBox(aBoxS);
294     //
295     aNbFP=aBBTree.Select(aSelector);
296     //
297     const BOPCol_ListOfInteger& aLIFP=aSelector.Indices();
298     //
299     // 2.5. Collect faces that are IN aSolid [ aLFIN ]
300     BOPCol_ListOfShape aLFP(aAlr1);
301     BOPCol_ListOfShape aLCBF(aAlr1);
302     BOPCol_MapOfShape aMFDone(100, aAlr1);
303     BOPCol_IndexedMapOfShape aME(100, aAlr1);
304     //
305     BOPTools::MapShapes(aSD, TopAbs_EDGE, aME);
306     //
307     aItLI.Initialize(aLIFP);
308     for (; aItLI.More(); aItLI.Next()) {
309       nFP=aItLI.Value();
310       const BOPAlgo_ShapeBox& aSBF=aDMISB.Find(nFP);
311       const TopoDS_Face& aFP=(*(TopoDS_Face*)&aSBF.Shape());
312       if (aMF.Contains(aFP)) {
313         continue;
314       }
315       if (aMFDone.Contains(aFP)) {
316         continue;
317       }
318       //
319       aMFDone.Add(aFP);
320       //
321       iIsIN=BOPTools_AlgoTools::IsInternalFace(aFP, aSD, aMEF, 1.e-14, myContext);
322       //
323       aLFP.Clear();
324       aLFP.Append(aFP);
325       //
326       aItLI1.Initialize(aLIFP);
327       for (; aItLI1.More(); aItLI1.Next()) {
328         const TopoDS_Shape& aFx=aDMISB.Find(aItLI1.Value()).Shape();
329         if (!aMFDone.Contains(aFx)) {
330           aLFP.Append(aFx);
331         }
332       }
333       //
334       aLCBF.Clear();
335       //---------------------------------------- 
336       {
337         Handle(NCollection_IncAllocator) aAlr2;
338         aAlr2=new NCollection_IncAllocator();
339         //
340         BOPTools_AlgoTools::MakeConnexityBlock(aLFP, aME, aLCBF, aAlr2);
341       }
342       //----------------------------------------
343       aItLS.Initialize(aLCBF);
344       for (; aItLS.More(); aItLS.Next()) {
345         const TopoDS_Shape& aFx=aItLS.Value();
346         aMFDone.Add(aFx);
347         if (iIsIN) {
348           aLFIN.Append(aFx);
349         }
350       }
351     }// for (; aItLI.More(); aItLI.Next()) {
352     //
353     // 2.6. Store the results in theInParts, theDraftSolids
354     aNbFIN=aLFIN.Extent();
355     if (aNbFIN || aNbLIF) {
356       aItLS.Initialize(aLIF);
357       for (; aItLS.More(); aItLS.Next()) {
358         const TopoDS_Shape& aFI=aItLS.Value();
359         aLFIN.Append(aFI);
360       }
361       theInParts.Bind(aSolid, aLFIN);
362     }
363     //
364     if (aNbFIN || bHasImage) {
365       theDraftSolids.Bind(aSolid, aSD);
366     }
367     //---------------------------------------------
368   }// for (i=0; i<aNbS; ++i) {
369 }
370
371 //=======================================================================
372 //function : BuildDraftSolid
373 //purpose  : 
374 //=======================================================================
375 void BOPAlgo_Builder::BuildDraftSolid(const TopoDS_Shape& theSolid,
376                                       TopoDS_Shape& theDraftSolid,
377                                       BOPCol_ListOfShape& theLIF)
378 {
379   myErrorStatus=0;
380   //
381   Standard_Boolean bToReverse;
382   Standard_Integer iFlag;
383   TopAbs_Orientation aOrF, aOrSh, aOrSd;
384   TopoDS_Iterator aIt1, aIt2;
385   TopoDS_Shell aShD;
386   TopoDS_Shape aFSDx, aFx;
387   BRep_Builder aBB;
388   BOPCol_ListIteratorOfListOfShape aItS;        
389   //
390   aOrSd=theSolid.Orientation();
391   theDraftSolid.Orientation(aOrSd);
392   //
393   aIt1.Initialize(theSolid);
394   for (; aIt1.More(); aIt1.Next()) {
395     const TopoDS_Shape& aSh=aIt1.Value();
396     if(aSh.ShapeType()!=TopAbs_SHELL) {
397       continue; // mb internal edges,vertices
398     }
399     //
400     aOrSh=aSh.Orientation();
401     aBB.MakeShell(aShD);
402     aShD.Orientation(aOrSh);
403     iFlag=0;
404     //
405     aIt2.Initialize(aSh);
406     for (; aIt2.More(); aIt2.Next()) {
407       const TopoDS_Shape& aF=aIt2.Value();
408       aOrF=aF.Orientation();
409       //
410       if (myImages.IsBound(aF)) {
411         const BOPCol_ListOfShape& aLSp=myImages.Find(aF);
412         aItS.Initialize(aLSp);
413         for (; aItS.More(); aItS.Next()) {
414           aFx=aItS.Value();
415           //
416           if (myShapesSD.IsBound(aFx)) {
417             aFSDx=myShapesSD.Find(aFx);
418             //
419             if (aOrF==TopAbs_INTERNAL) {
420               aFSDx.Orientation(aOrF);
421               theLIF.Append(aFSDx);
422             }
423             else {
424               bToReverse=BOPTools_AlgoTools::IsSplitToReverse(aFSDx, aF, myContext); 
425               if (bToReverse) {
426                 aFSDx.Reverse();
427               }
428               //
429               iFlag=1;
430               aBB.Add(aShD, aFSDx);
431             }
432           }//if (myShapesSD.IsBound(aFx)) {
433           else {
434             aFx.Orientation(aOrF);
435             if (aOrF==TopAbs_INTERNAL) {
436               theLIF.Append(aFx);
437             }
438             else{
439               iFlag=1;
440               aBB.Add(aShD, aFx);
441             }
442           }
443         }
444       } // if (myImages.IsBound(aF)) { 
445       //
446       else {
447         if (aOrF==TopAbs_INTERNAL) {
448           theLIF.Append(aF);
449         }
450         else{
451           iFlag=1;
452           aBB.Add(aShD, aF);
453         }
454       }
455     } //for (; aIt2.More(); aIt2.Next()) {
456     //
457     if (iFlag) {
458       aBB.Add(theDraftSolid, aShD);
459     }
460   } //for (; aIt1.More(); aIt1.Next()) {
461 }
462 //=======================================================================
463 //function : BuildSplitSolids
464 //purpose  : 
465 //=======================================================================
466 void BOPAlgo_Builder::BuildSplitSolids(BOPCol_DataMapOfShapeListOfShape& theInParts,
467                                        BOPCol_DataMapOfShapeShape& theDraftSolids,
468                                        const BOPCol_BaseAllocator&  )
469 {
470   myErrorStatus=0;
471   //
472   Standard_Boolean bFlagSD;
473   Standard_Integer i, aNbS;
474   TopExp_Explorer aExp;
475   BOPCol_ListIteratorOfListOfShape aIt;
476   BOPCol_DataMapIteratorOfDataMapOfShapeShape aIt1;
477   //
478   Handle(NCollection_IncAllocator) aAlr0;
479   aAlr0=new NCollection_IncAllocator();
480   //
481   BOPCol_ListOfShape aSFS(aAlr0), aLSEmpty(aAlr0);
482   BOPCol_MapOfShape aMFence(100, aAlr0);
483   BOPTools_MapOfSet aMST(100, aAlr0);
484   BOPAlgo_VectorOfBuilderSolid aVBS;
485   //
486   // 0. Find same domain solids for non-interferred solids
487   aNbS=myDS->NbSourceShapes();
488   for (i=0; i<aNbS; ++i) {
489     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
490     //
491     if (aSI.ShapeType()!=TopAbs_SOLID) {
492       continue;
493     }
494     //
495     const TopoDS_Shape& aS=aSI.Shape();
496     if (!aMFence.Add(aS)) {
497       continue;
498     }
499     if(theDraftSolids.IsBound(aS)) {
500       continue;
501     }
502     //
503     BOPTools_Set aST;
504     //
505     aST.Add(aS, TopAbs_FACE);
506     aMST.Add(aST);
507     //
508   } //for (i=1; i<=aNbS; ++i) 
509   //
510   // 1. Build solids for interferred source solids
511   for (i=0; i<aNbS; ++i) {
512     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
513     //
514     if (aSI.ShapeType()!=TopAbs_SOLID) {
515       continue;
516     }
517     //
518     const TopoDS_Shape& aS=aSI.Shape();
519     const TopoDS_Solid& aSolid=(*(TopoDS_Solid*)(&aS));
520     if(!theDraftSolids.IsBound(aS)) {
521       continue;
522     }
523     const TopoDS_Shape& aSD=theDraftSolids.Find(aS);
524     const BOPCol_ListOfShape& aLFIN=
525       (theInParts.IsBound(aS)) ? theInParts.Find(aS) : aLSEmpty;
526     //
527     // 1.1 Fill Shell Faces Set
528     aSFS.Clear();
529     aExp.Init(aSD, TopAbs_FACE);
530     for (; aExp.More(); aExp.Next()) {
531       const TopoDS_Shape& aF=aExp.Current();
532       aSFS.Append(aF);
533     }
534     //
535     aIt.Initialize(aLFIN);
536     for (; aIt.More(); aIt.Next()) {
537       TopoDS_Shape aF=aIt.Value();
538       //
539       aF.Orientation(TopAbs_FORWARD);
540       aSFS.Append(aF);
541       aF.Orientation(TopAbs_REVERSED);
542       aSFS.Append(aF);
543     }
544     //
545     // 1.3 Build new solids   
546     Handle(NCollection_IncAllocator) aAlr1;
547     aAlr1=new NCollection_IncAllocator();  
548     //
549     BOPAlgo_BuilderSolid& aBS=aVBS.Append1();
550     aBS.SetSolid(aSolid);
551     aBS.SetShapes(aSFS);
552   }//for (i=0; i<aNbS; ++i) {
553   //
554   Standard_Integer k, aNbBS;
555   //
556   aNbBS=aVBS.Extent();
557   //
558   //===================================================
559   BOPAlgo_BuilderSolidCnt::Perform(myRunParallel, aVBS);
560   //===================================================
561   //
562   for (k=0; k<aNbBS; ++k) {
563     BOPAlgo_BuilderSolid& aBS=aVBS(k);
564     const TopoDS_Solid& aS=aBS.Solid();
565     const BOPCol_ListOfShape& aLSR=aBS.Areas();
566     //
567     if (!myImages.IsBound(aS)) {
568       BOPCol_ListOfShape aLSx;
569       //
570       myImages.Bind(aS, aLSx);
571       BOPCol_ListOfShape& aLSIm=myImages.ChangeFind(aS);
572       //
573       aIt.Initialize(aLSR);
574       for (; aIt.More(); aIt.Next()) {
575         BOPTools_Set aST;
576         //
577         const TopoDS_Shape& aSR=aIt.Value();
578         aST.Add(aSR, TopAbs_FACE);
579         //
580         bFlagSD=aMST.Contains(aST);
581         //
582         const BOPTools_Set& aSTx=aMST.Added(aST);
583         const TopoDS_Shape& aSx=aSTx.Shape();
584         aLSIm.Append(aSx);
585         //
586         if (bFlagSD) {
587           myShapesSD.Bind(aSR, aSx);
588         }
589       }
590     }
591   }
592 }
593 //=======================================================================
594 //function :FillInternalShapes 
595 //purpose  : 
596 //=======================================================================
597 void BOPAlgo_Builder::FillInternalShapes()
598 {
599   myErrorStatus=0;
600   //
601   Standard_Integer i, j,  aNbS, aNbSI, aNbSx;
602   TopAbs_ShapeEnum aType;
603   TopAbs_State aState; 
604   TopoDS_Iterator aItS;
605   BRep_Builder aBB;
606   BOPCol_MapIteratorOfMapOfShape aItM;
607   BOPCol_ListIteratorOfListOfShape aIt, aIt1;
608   //
609   Handle(NCollection_IncAllocator) aAllocator;
610   //-----------------------------------------------------scope f
611   aAllocator=new NCollection_IncAllocator();
612   //
613   BOPCol_IndexedDataMapOfShapeListOfShape aMSx(100, aAllocator);
614   BOPCol_IndexedMapOfShape aMx(100, aAllocator);
615   BOPCol_MapOfShape aMSI(100, aAllocator);
616   BOPCol_MapOfShape aMFence(100, aAllocator);
617   BOPCol_MapOfShape aMSOr(100, aAllocator);
618   BOPCol_ListOfShape aLSd(aAllocator);
619   BOPCol_ListOfShape aLArgs(aAllocator);
620   BOPCol_ListOfShape aLSC(aAllocator);
621   //
622   // 1. Shapes to process
623   //
624   // 1.1 Shapes from pure arguments aMSI 
625   // 1.1.1 vertex, edge, wire
626   //
627   aIt.Initialize(myArguments);
628   for (; aIt.More(); aIt.Next()) {
629     const TopoDS_Shape& aS=aIt.Value();
630     TreatCompound(aS, aMFence, aLSC);
631   }
632   aIt.Initialize(aLSC);
633   for (; aIt.More(); aIt.Next()) {
634     const TopoDS_Shape& aS=aIt.Value();
635     aType=aS.ShapeType();
636     if (aType==TopAbs_WIRE) {
637       aItS.Initialize(aS);
638       for(; aItS.More(); aItS.Next()) {
639         const TopoDS_Shape& aE=aItS.Value();
640         if (aMFence.Add(aE)) {
641           aLArgs.Append(aE);
642         }
643       }
644     }
645     else if (aType==TopAbs_VERTEX || aType==TopAbs_EDGE){
646       aLArgs.Append(aS);
647     } 
648   }
649   aMFence.Clear();
650   //
651   aIt.Initialize(aLArgs);
652   for (; aIt.More(); aIt.Next()) {
653     const TopoDS_Shape& aS=aIt.Value();
654     aType=aS.ShapeType();
655     if (aType==TopAbs_VERTEX || aType==TopAbs_EDGE ||aType==TopAbs_WIRE) {
656       if (aMFence.Add(aS)) {
657         if (myImages.IsBound(aS)) {
658           const BOPCol_ListOfShape &aLSp=myImages.Find(aS);
659           aIt1.Initialize(aLSp);
660           for (; aIt1.More(); aIt1.Next()) {
661             const TopoDS_Shape& aSp=aIt1.Value();
662             aMSI.Add(aSp);
663           }
664         }
665         else {
666           aMSI.Add(aS);
667         }
668       }
669     }
670   }
671   
672   aNbSI=aMSI.Extent();
673   //
674   // 2. Internal vertices, edges from source solids
675   aMFence.Clear();
676   aLSd.Clear();
677   //
678   aNbS=myDS->NbSourceShapes();
679   for (i=0; i<aNbS; ++i) {
680     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
681     //
682     if (aSI.ShapeType()!=TopAbs_SOLID) {
683       continue;
684     }
685     //
686     const TopoDS_Shape& aS=aSI.Shape();
687     //
688     aMx.Clear();
689     OwnInternalShapes(aS, aMx);
690     //
691     aNbSx=aMx.Extent();
692     for (j=1; j<=aNbSx; ++j) {
693       const TopoDS_Shape& aSi=aMx(j);
694       if (myImages.IsBound(aSi)) {
695         const BOPCol_ListOfShape &aLSp=myImages.Find(aSi);
696         aIt1.Initialize(aLSp);
697         for (; aIt1.More(); aIt1.Next()) {
698           const TopoDS_Shape& aSp=aIt1.Value();
699           aMSI.Add(aSp);
700         }
701       }
702       else {
703         aMSI.Add(aSi);
704       }
705     }
706     //
707     // build aux map from splits of solids
708     if (myImages.IsBound(aS)) {
709       const BOPCol_ListOfShape &aLSp=myImages.Find(aS);
710       aIt.Initialize(aLSp);
711       for (; aIt.More(); aIt.Next()) {
712         const TopoDS_Shape& aSp=aIt.Value();
713         if (aMFence.Add(aSp)) { 
714           BOPTools::MapShapesAndAncestors(aSp, TopAbs_VERTEX, TopAbs_EDGE, aMSx);
715           BOPTools::MapShapesAndAncestors(aSp, TopAbs_VERTEX, TopAbs_FACE, aMSx);
716           BOPTools::MapShapesAndAncestors(aSp, TopAbs_EDGE  , TopAbs_FACE, aMSx);
717           aLSd.Append(aSp);
718         }
719       }
720     }
721     else {
722       if (aMFence.Add(aS)) {
723         BOPTools::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_EDGE, aMSx);
724         BOPTools::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_FACE, aMSx);
725         BOPTools::MapShapesAndAncestors(aS, TopAbs_EDGE  , TopAbs_FACE, aMSx);
726         aLSd.Append(aS);
727         aMSOr.Add(aS); 
728       }
729     }
730   }// for (i=0; i<aNbS; ++i) {
731   //
732   // 3. Some shapes of aMSI can be already tied with faces of 
733   //    split solids
734   aItM.Initialize(aMSI); 
735   for (; aItM.More(); aItM.Next()) {
736     const TopoDS_Shape& aSI=aItM.Key();
737     if (aMSx.Contains(aSI)) {
738       const BOPCol_ListOfShape &aLSx=aMSx.FindFromKey(aSI);
739       aNbSx=aLSx.Extent();
740       if (aNbSx) {
741         aMSI.Remove(aSI);
742       }
743     }
744   }
745   //
746   // 4. Just check it
747   aNbSI=aMSI.Extent();
748   if (!aNbSI) {
749     return;
750   }
751   //
752   // 5 Settle internal vertices and edges into solids
753   aMx.Clear();
754   aIt.Initialize(aLSd);
755   for (; aIt.More(); aIt.Next()) {
756     TopoDS_Solid aSd=TopoDS::Solid(aIt.Value());
757     //
758     aItM.Initialize(aMSI); 
759     for (; aItM.More(); aItM.Next()) {
760       TopoDS_Shape aSI=aItM.Key();
761       aSI.Orientation(TopAbs_INTERNAL);
762       //
763       aState=BOPTools_AlgoTools::ComputeStateByOnePoint(aSI, aSd, 1.e-11, myContext);
764       if (aState==TopAbs_IN) {
765         //
766         if(aMSOr.Contains(aSd)) {
767           //
768           TopoDS_Solid aSdx;
769           //
770           aBB.MakeSolid(aSdx);
771           aItS.Initialize(aSd);
772           for (; aItS.More(); aItS.Next()) {
773             const TopoDS_Shape& aSh=aItS.Value();
774             aBB.Add(aSdx, aSh);
775           }
776           //
777           aBB.Add(aSdx, aSI);
778           //
779           if (myImages.IsBound(aSdx)) {
780             BOPCol_ListOfShape& aLS=myImages.ChangeFind(aSdx);
781             aLS.Append(aSdx);
782           } 
783           else {
784             BOPCol_ListOfShape aLS;
785             aLS.Append(aSdx);
786             myImages.Bind(aSd, aLS);
787           }
788           //
789           aMSOr.Remove(aSd);
790           aSd=aSdx;
791         }
792         else {
793           aBB.Add(aSd, aSI);
794         }
795         //
796         aMSI.Remove(aSI);
797       } //if (aState==TopAbs_IN) {
798     }// for (; aItM.More(); aItM.Next()) {
799   }//for (; aIt1.More(); aIt1.Next()) {
800   //
801   //-----------------------------------------------------scope t
802   aLArgs.Clear();
803   aLSd.Clear();
804   aMSOr.Clear();
805   aMFence.Clear();
806   aMSI.Clear();
807   aMx.Clear();
808   aMSx.Clear();
809 }
810 //=======================================================================
811 //function : OwnInternalShapes
812 //purpose  : 
813 //=======================================================================
814   void OwnInternalShapes(const TopoDS_Shape& theS,
815                          BOPCol_IndexedMapOfShape& theMx)
816 {
817   TopoDS_Iterator aIt;
818   //
819   aIt.Initialize(theS);
820   for (; aIt.More(); aIt.Next()) {
821     const TopoDS_Shape& aSx=aIt.Value();
822     if (aSx.ShapeType()!=TopAbs_SHELL) {
823       theMx.Add(aSx);
824     }
825   }
826 }
827 //=======================================================================
828 //function : TreatCompound
829 //purpose  : 
830 //=======================================================================
831 void TreatCompound(const TopoDS_Shape& theS,
832                    BOPCol_MapOfShape& aMFence,
833                    BOPCol_ListOfShape& theLS)
834 {
835   TopAbs_ShapeEnum aType;
836   //
837   aType = theS.ShapeType();
838   if (aType != TopAbs_COMPOUND) {
839     if (aMFence.Add(theS)) {
840       theLS.Append(theS);
841     }
842     return;
843   }
844   //
845   TopoDS_Iterator aIt;
846   //
847   aIt.Initialize(theS);
848   for (; aIt.More(); aIt.Next()) {
849     const TopoDS_Shape& aS = aIt.Value();
850     TreatCompound(aS, aMFence, theLS);
851   }
852 }
853
854 //
855 // ErrorStatus
856 // 30 - SolidBuilder failed
857 // A