Salome HOME
untabify
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder_3.cxx
1 //  Copyright (C) 2007-2008  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_Builder_3.cxx
23 // Created:     
24 // Author:      Peter KURNEV
25 //
26 #include <GEOMAlgo_Builder.hxx>
27
28 #include <TopAbs_State.hxx>
29
30 #include <TopoDS.hxx>
31 #include <TopoDS_Iterator.hxx>
32 #include <TopoDS_Solid.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Solid.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopoDS_Shell.hxx>
38 #include <TopoDS_Compound.hxx>
39
40 #include <TopExp.hxx>
41 #include <TopExp_Explorer.hxx>
42
43 #include <BRep_Builder.hxx>
44 #include <BRepTools.hxx>
45 #include <BRepClass3d_SolidClassifier.hxx>
46
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_MapIteratorOfMapOfShape.hxx>
53 #include <TopTools_DataMapOfShapeShape.hxx>
54 #include <TopTools_DataMapOfShapeInteger.hxx>
55 #include <TopTools_DataMapIteratorOfDataMapOfShapeInteger.hxx>
56 #include <TopTools_MapOfShape.hxx>
57
58 #include <IntTools_Context.hxx>
59
60 #include <NMTDS_ShapesDataStructure.hxx>
61 #include <NMTTools_PaveFiller.hxx>
62
63 #include <GEOMAlgo_Tools3D.hxx>
64 #include <GEOMAlgo_BuilderSolid.hxx>
65 #include <GEOMAlgo_ShapeSet.hxx>
66 #include <GEOMAlgo_DataMapOfShapeShapeSet.hxx>
67 #include <GEOMAlgo_DataMapIteratorOfDataMapOfShapeShapeSet.hxx>
68
69
70
71 static
72   void OwnInternalShapes(const TopoDS_Shape& ,
73                          TopTools_IndexedMapOfShape& );
74
75 //=======================================================================
76 //function : FillImagesSolids
77 //purpose  : 
78 //=======================================================================
79   void GEOMAlgo_Builder::FillImagesSolids()
80 {
81   myErrorStatus=0;
82   //
83   FillIn3DParts();
84   BuildSplitSolids();
85   FillInternalShapes();
86 }
87 //=======================================================================
88 //function : BuildDraftSolid
89 //purpose  : 
90 //=======================================================================
91   void GEOMAlgo_Builder::BuildDraftSolid(const TopoDS_Shape& theSolid,
92                                         TopoDS_Shape& theDraftSolid,
93                                         TopTools_ListOfShape& theLIF)
94 {
95   myErrorStatus=0;
96   //
97   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
98   NMTTools_PaveFiller* pPF=myPaveFiller;
99   IntTools_Context& aCtx= pPF->ChangeContext();
100   //
101   Standard_Boolean bToReverse;
102   Standard_Integer  iFlag;
103   TopAbs_Orientation aOrF, aOrSh, aOrSd;
104   TopoDS_Iterator aIt1, aIt2;
105   TopTools_ListIteratorOfListOfShape aItS;      
106   BRep_Builder aBB;
107   TopoDS_Shell aShD;
108   TopoDS_Shape aFSDx, aFx;
109   //
110   aOrSd=theSolid.Orientation();
111   theDraftSolid.Orientation(aOrSd);
112   //
113   aIt1.Initialize(theSolid);
114   for (; aIt1.More(); aIt1.Next()) {
115     const TopoDS_Shape& aSh=aIt1.Value();
116     if(aSh.ShapeType()!=TopAbs_SHELL) {
117       continue; // mb internal edges,vertices
118     }
119     //
120     aOrSh=aSh.Orientation();
121     aBB.MakeShell(aShD);
122     aShD.Orientation(aOrSh);
123     iFlag=0;
124     //
125     aIt2.Initialize(aSh);
126     for (; aIt2.More(); aIt2.Next()) {
127       const TopoDS_Shape& aF=aIt2.Value();
128       aOrF=aF.Orientation();
129       //
130       if (myImages.HasImage(aF)) {
131         const TopTools_ListOfShape& aLSp=myImages.Image(aF);
132         aItS.Initialize(aLSp);
133         for (; aItS.More(); aItS.Next()) {
134           aFx=aItS.Value();
135           //
136           if (mySameDomainShapes.Contains(aFx)) {
137             aFSDx=mySameDomainShapes.FindFromKey(aFx);
138             //
139             if (aOrF==TopAbs_INTERNAL) {
140               aFSDx.Orientation(aOrF);
141               theLIF.Append(aFSDx);
142             }
143             else {
144               bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aFSDx, aF, aCtx); 
145               if (bToReverse) {
146                 aFSDx.Reverse();
147               }
148               //
149               iFlag=1;
150               aBB.Add(aShD, aFSDx);
151             }
152           }// if (mySameDomainShapes.Contains(aFx)) {
153           else {
154             aFx.Orientation(aOrF);
155             if (aOrF==TopAbs_INTERNAL) {
156               theLIF.Append(aFx);
157             }
158             else{
159               iFlag=1;
160               aBB.Add(aShD, aFx);
161             }
162           }
163         }
164       } //if (myImages.HasImage(aF)) {
165       //
166       else {
167         if (aOrF==TopAbs_INTERNAL) {
168           theLIF.Append(aF);
169         }
170         else{
171           iFlag=1;
172           aBB.Add(aShD, aF);
173         }
174       }
175     } //for (; aIt2.More(); aIt2.Next()) {
176     //
177     if (iFlag) {
178       aBB.Add(theDraftSolid, aShD);
179     }
180   } //for (; aIt1.More(); aIt1.Next()) {
181 }
182 //=======================================================================
183 //function : FillIn3DParts
184 //purpose  : 
185 //=======================================================================
186   void GEOMAlgo_Builder::FillIn3DParts()
187 {
188   myErrorStatus=0;
189   //
190   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
191   NMTTools_PaveFiller* pPF=myPaveFiller;
192   IntTools_Context& aCtx= pPF->ChangeContext();
193   //
194   Standard_Boolean bIsIN, bHasImage;
195   Standard_Integer aNbS, aNbSolids, i, j, aNbFaces, aNbFP, aNbFPx, aNbFIN, aNbLIF;// k,
196   TopAbs_ShapeEnum aType;  
197   TopAbs_State aState;
198   TopTools_IndexedMapOfShape aMSolids, aMS, aMFaces, aMFIN;
199   TopTools_MapOfShape aMFDone;
200   TopTools_IndexedDataMapOfShapeListOfShape aMEF;
201   TopTools_ListIteratorOfListOfShape aItS;      
202   TopoDS_Iterator aIt, aItF;
203   BRep_Builder aBB;
204   TopoDS_Solid aSolidSp; 
205   TopoDS_Face aFP;
206   //
207   myDraftSolids.Clear();
208   //
209   aNbS=aDS.NumberOfShapesOfTheObject();
210   for (i=1; i<=aNbS; ++i) {
211     const TopoDS_Shape& aS=aDS.Shape(i);
212     //
213     aType=aS.ShapeType();
214     if (aType==TopAbs_SOLID) { 
215       // all solids from DS
216       aMSolids.Add(aS);
217     }
218     else if (aType==TopAbs_FACE) {
219       // all faces (originals from DS or theirs images)
220       if (myImages.HasImage(aS)) {
221         const TopTools_ListOfShape& aLS=myImages.Image(aS);
222         aItS.Initialize(aLS);
223         for (; aItS.More(); aItS.Next()) {
224           const TopoDS_Shape& aFx=aItS.Value();
225           //
226           if (mySameDomainShapes.Contains(aFx)) {
227             const TopoDS_Shape& aFSDx=mySameDomainShapes.FindFromKey(aFx);
228             aMFaces.Add(aFSDx);
229           }
230           else {
231             aMFaces.Add(aFx);
232           }
233         }
234       }
235       else {
236         if (mySameDomainShapes.Contains(aS)) {
237           const TopoDS_Shape& aFSDx=mySameDomainShapes.FindFromKey(aS);
238           aMFaces.Add(aFSDx);
239         }
240         else {
241           aMFaces.Add(aS);
242         }
243       }
244     }
245   }
246   //
247   aNbFaces=aMFaces.Extent();
248   aNbSolids=aMSolids.Extent();
249   //
250   for (i=1; i<=aNbSolids; ++i) {
251     const TopoDS_Solid& aSolid=TopoDS::Solid(aMSolids(i));
252     aMFDone.Clear();
253     aMFIN.Clear();
254     aMEF.Clear();
255     //
256     aBB.MakeSolid(aSolidSp);
257     //
258     TopTools_ListOfShape aLIF;
259     //
260     BuildDraftSolid(aSolid, aSolidSp, aLIF);
261     aNbLIF=aLIF.Extent();
262     //
263     // 1 all faces/edges from aSolid [ aMS ]
264     bHasImage=Standard_False;
265     aMS.Clear();
266     aIt.Initialize(aSolid);
267     for (; aIt.More(); aIt.Next()) {
268       const TopoDS_Shape& aShell=aIt.Value();
269       //
270       if (myImages.HasImage(aShell)) {
271         bHasImage=Standard_True;
272         //
273         const TopTools_ListOfShape& aLS=myImages.Image(aShell);
274         aItS.Initialize(aLS);
275         for (; aItS.More(); aItS.Next()) {
276           const TopoDS_Shape& aSx=aItS.Value();
277           aMS.Add(aSx);
278           TopExp::MapShapes(aSx, TopAbs_FACE, aMS);
279           TopExp::MapShapes(aSx, TopAbs_EDGE, aMS);
280           TopExp::MapShapesAndAncestors(aSx, TopAbs_EDGE, TopAbs_FACE, aMEF);
281         }
282       }
283       else {
284         //aMS.Add(aShell);
285         TopExp::MapShapes(aShell, TopAbs_FACE, aMS);
286         TopExp::MapShapesAndAncestors(aShell, TopAbs_EDGE, TopAbs_FACE, aMEF);
287       }
288     }
289     //
290     // 2 all faces that are not from aSolid [ aLFP1 ]
291     Standard_Integer aNbEFP;
292     TopTools_IndexedDataMapOfShapeListOfShape aMEFP;
293     TopTools_ListIteratorOfListOfShape aItFP, aItEx;
294     TopTools_MapOfShape aMFence;
295     TopTools_ListOfShape aLFP1, aLFP2, aLFP, aLCBF, aLFIN, aLEx;//*pLFP,
296     //
297     // for all non-solid faces build EF map [ aMEFP ]
298     for (j=1; j<=aNbFaces; ++j) {
299       const TopoDS_Shape& aFace=aMFaces(j);
300       if (!aMS.Contains(aFace)) {
301         TopExp::MapShapesAndAncestors(aFace, TopAbs_EDGE, TopAbs_FACE, aMEFP);
302       }
303     }
304     //
305     // among all faces from aMEFP select these that have same edges
306     // with the solid (i.e aMEF). These faces will be treated first 
307     // to prevent the usage of 3D classifier.
308     // The full list of faces to process is aLFP1. 
309     aNbEFP=aMEFP.Extent();
310     for (j=1; j<=aNbEFP; ++j) {
311       const TopoDS_Shape& aE=aMEFP.FindKey(j);
312       //
313       if (aMEF.Contains(aE)) { // !!
314         const TopTools_ListOfShape& aLF=aMEFP(j);
315         aItFP.Initialize(aLF);
316         for (; aItFP.More(); aItFP.Next()) {
317           const TopoDS_Shape& aF=aItFP.Value();
318           if (aMFence.Add(aF)) {
319             aLFP1.Append(aF);
320           }
321         }
322       }
323       else {
324         aLEx.Append(aE);
325       }
326     }
327     //
328     aItEx.Initialize(aLEx);
329     for (; aItEx.More(); aItEx.Next()) {
330       const TopoDS_Shape& aE=aItEx.Value();
331       const TopTools_ListOfShape& aLF=aMEFP.FindFromKey(aE);
332       aItFP.Initialize(aLF);
333       for (; aItFP.More(); aItFP.Next()) {
334         const TopoDS_Shape& aF=aItFP.Value();
335         if (aMFence.Add(aF)) {
336           aLFP2.Append(aF);
337         }
338       }
339     }
340     aLFP1.Append(aLFP2);
341     //==========
342     //
343     // 3 Process faces aLFP1
344     aNbFP=aLFP1.Extent();
345     aItFP.Initialize(aLFP1);
346     for (; aItFP.More(); aItFP.Next()) {
347       const TopoDS_Shape& aSP=aItFP.Value();
348       if (!aMFDone.Add(aSP)) {
349         continue;
350       }
351       
352       //
353       // first face to process
354       aFP=TopoDS::Face(aSP);
355       bIsIN= GEOMAlgo_Tools3D::IsInternalFace(aFP, aSolidSp, aMEF, 1.e-14, aCtx);
356       aState=(bIsIN) ? TopAbs_IN : TopAbs_OUT;
357       //
358       // collect faces to process [ aFP is the first ]
359       aLFP.Clear();
360       aLFP.Append(aFP);
361       aItS.Initialize(aLFP1);
362       for (; aItS.More(); aItS.Next()) {
363         const TopoDS_Shape& aSk=aItS.Value();
364         if (!aMFDone.Contains(aSk)) {
365           aLFP.Append(aSk);
366         }
367       }
368       //
369       // Connexity Block that spreads from aFP the Bound 
370       // or till the end of the block itself
371       aLCBF.Clear();
372       GEOMAlgo_Tools3D::MakeConnexityBlock(aLFP, aMS, aLCBF);
373       //
374       // fill states for the Connexity Block 
375       aItS.Initialize(aLCBF);
376       for (; aItS.More(); aItS.Next()) {
377         const TopoDS_Shape& aSx=aItS.Value();
378         aMFDone.Add(aSx);
379         if (aState==TopAbs_IN) {
380           aMFIN.Add(aSx);
381         }
382       }
383       //
384       aNbFPx=aMFDone.Extent();
385       if (aNbFPx==aNbFP) {
386         break;
387       }
388     }//for (; aItFP.More(); aItFP.Next())
389     //
390     // faces Inside aSolid
391     aLFIN.Clear();
392     aNbFIN=aMFIN.Extent();
393     if (aNbFIN || aNbLIF) {
394       for (j=1; j<=aNbFIN; ++j) {
395         const TopoDS_Shape& aFIN=aMFIN(j);
396         aLFIN.Append(aFIN);
397       }
398       //
399       aItS.Initialize(aLIF);
400       for (; aItS.More(); aItS.Next()) {
401         const TopoDS_Shape& aFIN=aItS.Value();
402         aLFIN.Append(aFIN);
403       }
404       //
405       myInParts.Add(aSolid, aLFIN);
406     }
407     if (aNbFIN || bHasImage) {
408       myDraftSolids.Add(aSolid, aSolidSp);
409     }
410   }//for (i=1; i<=aNbSolids; ++i) { // next solid
411 }
412 //=======================================================================
413 //function : BuildSplitSolids
414 //purpose  : 
415 //=======================================================================
416   void GEOMAlgo_Builder::BuildSplitSolids()
417 {
418   myErrorStatus=0;
419   // 
420   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
421   NMTTools_PaveFiller* pPF=myPaveFiller;
422   IntTools_Context& aCtx= pPF->ChangeContext();
423   //
424   Standard_Integer i, aNbS, iErr;
425   TopExp_Explorer aExp;
426   TopTools_ListOfShape aSFS, aLSEmpty;
427   TopTools_MapOfShape aMFence;
428   TopTools_ListIteratorOfListOfShape aIt;
429   GEOMAlgo_BuilderSolid aSB;
430   GEOMAlgo_DataMapIteratorOfDataMapOfShapeShapeSet aItSS;
431   GEOMAlgo_DataMapOfShapeShapeSet aMSS;
432   GEOMAlgo_ShapeSet aSSi;
433   //
434   //modified by NIZNHY-PKV Wed Dec  6 17:08:03 2006f
435   //
436   // 0. Find same domain solids for non-interferred solids
437   aNbS=aDS.NumberOfShapesOfTheObject();
438   for (i=1; i<=aNbS; ++i) {
439     const TopoDS_Shape& aS=aDS.Shape(i);
440     if (aS.ShapeType()!=TopAbs_SOLID) {
441       continue;
442     }
443     if (!aMFence.Add(aS)) {
444       continue;
445     }
446     if(myDraftSolids.Contains(aS)) {
447       continue;
448     }
449     //
450     aSSi.Clear();
451     aSSi.Add(aS, TopAbs_FACE);
452     //
453     aMSS.Bind(aS, aSSi);
454   } //for (i=1; i<=aNbS; ++i) 
455   //
456   //modified by NIZNHY-PKV Wed Dec  6 17:08:09 2006t
457   //
458   // 1. Build solids for interferred source solids
459   //
460   aSB.SetContext(aCtx);
461   aNbS=myDraftSolids.Extent();
462   for (i=1; i<=aNbS; ++i) {
463     const TopoDS_Shape& aS =myDraftSolids.FindKey(i);
464     const TopoDS_Shape& aSD=myDraftSolids.FindFromIndex(i);
465     const TopTools_ListOfShape& aLFIN=
466       (myInParts.Contains(aS)) ? myInParts.FindFromKey(aS) : aLSEmpty;
467     //
468     // 1.1 Fill Shell Faces Set
469     aSFS.Clear();
470     
471     aExp.Init(aSD, TopAbs_FACE);
472     for (; aExp.More(); aExp.Next()) {
473       const TopoDS_Shape& aF=aExp.Current();
474       aSFS.Append(aF);
475     }
476     //
477     aIt.Initialize(aLFIN);
478     for (; aIt.More(); aIt.Next()) {
479       TopoDS_Shape aF=aIt.Value();
480       //
481       aF.Orientation(TopAbs_FORWARD);
482       aSFS.Append(aF);
483       aF.Orientation(TopAbs_REVERSED);
484       aSFS.Append(aF);
485     }
486     //
487     Standard_Integer aNbSFS;
488     aNbSFS=aSFS.Extent();
489     //
490     // 1.2
491     // Check whether aSFS contains a subsets of faces 
492     // of solids that have been already built. 
493     // If yes, shrink aSFS by these subsets.
494     aSSi.Clear();
495     aSSi.Add(aSFS);
496     //
497     aItSS.Initialize(aMSS);
498     for (; aItSS.More(); aItSS.Next()) {
499       const TopoDS_Shape& aSR=aItSS.Key(); 
500       const GEOMAlgo_ShapeSet& aSSR=aItSS.Value();
501       if (aSSi.Contains(aSSR)) {
502         // the aSR is SD solid for aS
503         aSSi.Subtract(aSSR);
504         // update images
505         if(myImages.HasImage(aS)) {
506           myImages.Add(aS, aSR);
507         }
508         else {
509           myImages.Bind(aS, aSR);
510         }
511         //
512         // update SD Shapes
513         mySameDomainShapes.Add(aSR, aSR);
514       }
515     }
516     const TopTools_ListOfShape& aSFS1=aSSi.GetSet();
517     aNbSFS=aSFS1.Extent();
518     //
519     // 1.3 Build new solids
520     aSB.SetShapes(aSFS1);
521     aSB.Perform();
522     iErr=aSB.ErrorStatus();
523     if (iErr) {
524       myErrorStatus=30; // SolidBuilder failed
525       return;
526     }
527     //
528     const TopTools_ListOfShape& aLSR=aSB.Areas();
529     //
530     // 1.4 Collect resulting solids and theirs set of faces 
531     aIt.Initialize(aLSR);
532     for (; aIt.More(); aIt.Next()) {
533       const TopoDS_Shape& aSR=aIt.Value();
534       //
535       aSSi.Clear();
536       aExp.Init(aSR, TopAbs_FACE);
537       for (; aExp.More(); aExp.Next()) {
538         const TopoDS_Shape& aF=aExp.Current();
539         aSSi.Add(aF);
540       }
541       aMSS.Bind(aSR, aSSi);
542     }
543     //
544     // Update images
545     if (myImages.HasImage(aS)) {
546       myImages.Add(aS, aLSR);
547     }
548     else {
549       myImages.Bind(aS, aLSR);
550     }
551   } // for (i=1; i<=aNbS; ++i) {
552   //modified by NIZNHY-PKV Wed Dec  6 17:07:47 2006f
553   /*
554   //
555   // 2. Find same domain solids for non-interferred solids
556   aNbS=aDS.NumberOfShapesOfTheObject();
557   for (i=1; i<=aNbS; ++i) {
558     const TopoDS_Shape& aS=aDS.Shape(i);
559     if (aS.ShapeType()!=TopAbs_SOLID) {
560       continue;
561     }
562     if (!aMFence.Add(aS)) {
563       continue;
564     }
565     if(myImages.HasImage(aS)) {
566       continue;
567     }
568     //
569     aSSi.Clear();
570     aSSi.Add(aS, TopAbs_FACE);
571     //
572     aItSS.Initialize(aMSS);
573     for (; aItSS.More(); aItSS.Next()) {
574       const TopoDS_Shape& aSR=aItSS.Key(); 
575       const GEOMAlgo_ShapeSet& aSSR=aItSS.Value();
576       if (aSSi.Contains(aSSR)) {
577         myImages.Bind(aS, aSR);
578         break;
579       }
580     }
581   } //for (i=1; i<=aNbS; ++i) 
582   */
583   //modified by NIZNHY-PKV Wed Dec  6 17:07:55 2006t
584 }
585 //=======================================================================
586 //function :FillInternalShapes 
587 //purpose  : 
588 //=======================================================================
589   void GEOMAlgo_Builder::FillInternalShapes()
590 {
591   myErrorStatus=0;
592   //
593   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
594   NMTTools_PaveFiller* pPF=myPaveFiller;
595   IntTools_Context& aCtx= pPF->ChangeContext();
596   //
597   //Standard_Boolean bHasImage;
598   Standard_Integer i, j, jT, aNbS, aNbSI, aNbSx, aNbSd;
599   TopAbs_ShapeEnum aType, aT[]={ TopAbs_VERTEX, TopAbs_EDGE };
600   TopAbs_State aState;
601   TopTools_ListIteratorOfListOfShape aIt, aIt1;
602   TopTools_IndexedDataMapOfShapeListOfShape aMSx;
603   TopTools_IndexedMapOfShape aMx;
604   TopTools_MapOfShape aMSI, aMFence, aMSOr;
605   TopTools_MapIteratorOfMapOfShape aItM;
606   TopTools_ListOfShape aLSI, aLSd;
607   TopoDS_Iterator aItS;
608   BRep_Builder aBB;
609   //
610   // 1. Shapes to process
611   //
612   // 1.1 Shapes from pure arguments aMSI 
613   // 1.1.1 vertex, edge
614   for (i=0; i<2; ++i) {
615     jT=(Standard_Integer)aT[i];
616     const TopTools_ListOfShape &aLS=myShapes1[jT];
617     aIt.Initialize(aLS);
618     for (; aIt.More(); aIt.Next()) {
619       const TopoDS_Shape& aS=aIt.Value();
620       if (aMFence.Add(aS)) {
621         aLSI.Append(aS);
622       }
623     }
624   }
625   // 1.1.2 wire
626   {  
627     jT=(Standard_Integer)TopAbs_WIRE;
628     const TopTools_ListOfShape &aLW=myShapes1[jT];
629     aIt.Initialize(aLW);
630     for (; aIt.More(); aIt.Next()) {
631       const TopoDS_Shape& aW=aIt.Value();
632       aItS.Initialize(aW);
633       for (; aItS.More(); aItS.Next()) {
634         const TopoDS_Shape& aE=aItS.Value();
635         if (aMFence.Add(aE)) {
636           aLSI.Append(aE);
637         }
638       }
639     }
640   }
641   // 1.1.3 theirs images/sources 
642   aIt1.Initialize(aLSI);
643   for (; aIt1.More(); aIt1.Next()) {
644     const TopoDS_Shape& aS=aIt1.Value();
645     if (myImages.HasImage(aS)) {
646       const TopTools_ListOfShape &aLSp=myImages.Image(aS);
647       aIt.Initialize(aLSp);
648       for (; aIt.More(); aIt.Next()) {
649         const TopoDS_Shape& aSI=aIt.Value();
650         aMSI.Add(aSI);
651       }
652     }
653     else {
654       aMSI.Add(aS);
655     }
656   }
657   aLSI.Clear();
658   aNbSI=aMSI.Extent();
659   //
660   // 2. Internal vertices, edges from source solids
661   aMFence.Clear();
662   aLSd.Clear();
663   //
664   aNbS=aDS.NumberOfShapesOfTheObject();
665   for (i=1; i<=aNbS; ++i) {
666     const TopoDS_Shape& aS=aDS.Shape(i);
667     aType=aS.ShapeType();
668     if (aType==TopAbs_SOLID) {
669       //
670       aMx.Clear();
671       OwnInternalShapes(aS, aMx);
672       //
673       aNbSx=aMx.Extent();
674       for (j=1; j<=aNbSx; ++j) {
675         const TopoDS_Shape& aSI=aMx(j);
676         if (myImages.HasImage(aSI)) {
677           const TopTools_ListOfShape &aLSp=myImages.Image(aSI);
678           aIt.Initialize(aLSp);
679           for (; aIt.More(); aIt.Next()) {
680             const TopoDS_Shape& aSp=aIt.Value();
681             aMSI.Add(aSp);
682           }
683         }
684         else {
685           aMSI.Add(aSI);
686         }
687       }
688       //
689       // build aux map from splits of solids
690       if (myImages.HasImage(aS)) {
691         const TopTools_ListOfShape &aLSp=myImages.Image(aS);
692         aIt.Initialize(aLSp);
693         for (; aIt.More(); aIt.Next()) {
694           const TopoDS_Shape& aSp=aIt.Value();
695           if (aMFence.Add(aSp)) { 
696             TopExp::MapShapesAndAncestors(aSp, TopAbs_VERTEX, TopAbs_EDGE, aMSx);
697             TopExp::MapShapesAndAncestors(aSp, TopAbs_VERTEX, TopAbs_FACE, aMSx);
698             TopExp::MapShapesAndAncestors(aSp, TopAbs_EDGE  , TopAbs_FACE, aMSx);
699             aLSd.Append(aSp);
700           }
701         }
702       }
703       else {
704         if (aMFence.Add(aS)) {
705           TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_EDGE, aMSx);
706           TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_FACE, aMSx);
707           TopExp::MapShapesAndAncestors(aS, TopAbs_EDGE  , TopAbs_FACE, aMSx);
708           aLSd.Append(aS);
709           aMSOr.Add(aS); 
710         }
711       }
712     }//if (aType==TopAbs_SOLID)
713   }
714   //
715   aNbSd=aLSd.Extent();
716   //
717   // 3. Some shapes of aMSI can be already tied with faces of 
718   //    split solids
719   aItM.Initialize(aMSI); 
720   for (; aItM.More(); aItM.Next()) {
721     const TopoDS_Shape& aSI=aItM.Key();
722     if (aMSx.Contains(aSI)) {
723       const TopTools_ListOfShape &aLSx=aMSx.FindFromKey(aSI);
724       aNbSx=aLSx.Extent();
725       if (aNbSx) {
726         aMSI.Remove(aSI);
727       }
728     }
729   }
730   //
731   // 4. Just check it
732   aNbSI=aMSI.Extent();
733   if (!aNbSI) {
734     return;
735   }
736   //
737   // 5 Settle internal vertices and edges into solids
738   aMx.Clear();
739   aIt.Initialize(aLSd);
740   for (; aIt.More(); aIt.Next()) {
741     TopoDS_Solid aSd=TopoDS::Solid(aIt.Value());
742     //
743     aItM.Initialize(aMSI); 
744     for (; aItM.More(); aItM.Next()) {
745       TopoDS_Shape aSI=aItM.Key();
746       aSI.Orientation(TopAbs_INTERNAL);
747       //
748       aState=GEOMAlgo_Tools3D::ComputeStateByOnePoint(aSI, aSd, 1.e-11, aCtx);
749       if (aState==TopAbs_IN) {
750         //
751         if(aMSOr.Contains(aSd)) {
752           //
753           TopoDS_Solid aSdx;
754           //
755           aBB.MakeSolid(aSdx);
756           aItS.Initialize(aSd);
757           for (; aItS.More(); aItS.Next()) {
758             const TopoDS_Shape& aSh=aItS.Value();
759             aBB.Add(aSdx, aSh);
760           }
761           //
762           aBB.Add(aSdx, aSI);
763           //
764           myImages.Bind(aSd, aSdx);
765           aMSOr.Remove(aSd);
766           aSd=aSdx;
767         }
768         else {
769           aBB.Add(aSd, aSI);
770         }
771         //
772         aMSI.Remove(aSI);
773       } //if (aState==TopAbs_IN) {
774     }// for (; aItM.More(); aItM.Next()) {
775   }//for (; aIt1.More(); aIt1.Next()) {
776 }
777 //=======================================================================
778 //function : OwnInternalShapes
779 //purpose  : 
780 //=======================================================================
781   void OwnInternalShapes(const TopoDS_Shape& theS,
782                          TopTools_IndexedMapOfShape& theMx)
783 {
784   TopoDS_Iterator aIt;
785   //
786   aIt.Initialize(theS);
787   for (; aIt.More(); aIt.Next()) {
788     const TopoDS_Shape& aSx=aIt.Value();
789     if (aSx.ShapeType()!=TopAbs_SHELL) {
790       theMx.Add(aSx);
791     }
792   }
793 }
794 //
795 // ErrorStatus
796 // 30 - SolidBuilder failed
797