summaryrefslogtreecommitdiff
path: root/src/BOP/BOP_ShellSplitter.cxx
blob: dc77c1acb135a9c86f277f82dcf03d4fa0d4138e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
// File:	BOP_ShellSplitter.cxx
// Created:	Mon Apr  9 11:00:15 2001
// Author:	Peter KURNEV
//		<pkv@irinox>


#include <BOP_ShellSplitter.ixx>

#include <TColStd_SequenceOfInteger.hxx>

#include <Geom_Surface.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Compound.hxx>

#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <TopLoc_Location.hxx>

#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeShape.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TopTools_MapIteratorOfMapOfShape.hxx>
#include <TopTools_DataMapOfShapeShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_IndexedMapOfShape.hxx>

#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>

//
static
  void RemoveInternals(const TopoDS_Face& ,
		       TopoDS_Face& );

static
  Standard_Boolean GetShells(TopTools_SequenceOfShape& ,
			     const TopTools_MapOfShape& ,
			     TopTools_SequenceOfShape& ,
			     TopTools_DataMapOfShapeShape& ,
			     TopTools_SequenceOfShape& ) ;

static 
  Standard_Boolean AddMultiConexityFaces(TopTools_SequenceOfShape& ,
					 const TopTools_MapOfShape& ,
					 TopTools_SequenceOfShape& ,
					 const TopTools_DataMapOfShapeShape& ,
					 const TopTools_IndexedDataMapOfShapeListOfShape& ,
					 TopTools_SequenceOfShape& );

static
   Standard_Boolean SplitShell(const TopoDS_Shell& ,
			       TopoDS_Shape& );
static 
  void CreateClosedShell(TopTools_SequenceOfShape& ,
			 const TopTools_MapOfShape& ,
			 const TopTools_IndexedDataMapOfShapeListOfShape& );
// 
//=======================================================================
// function: BOP_ShellSplitter::BOP_ShellSplitter
// purpose: 
//=======================================================================
  BOP_ShellSplitter::BOP_ShellSplitter()
:
  myIsDone(Standard_False),
  myNothingToDo(Standard_False)
{
}

//=======================================================================
// function: IsNothingToDo
// purpose: 
//=======================================================================
  Standard_Boolean BOP_ShellSplitter::IsNothingToDo()const
{
  return myNothingToDo;
}

//=======================================================================
// function: IsDone
// purpose: 
//=======================================================================
  Standard_Boolean BOP_ShellSplitter::IsDone()const
{
  return myIsDone;
}

//=======================================================================
// function: Shapes
// purpose: 
//=======================================================================
  const BOPTColStd_ListOfListOfShape& BOP_ShellSplitter::Shapes()const
{
  return myShapes;
}

//=======================================================================
// function: SetShell
// purpose: 
//=======================================================================
   void BOP_ShellSplitter::SetShell(const TopoDS_Shell& aShell)
{
  myShell=aShell;
}
//=======================================================================
// function: Shell
// purpose: 
//=======================================================================
  const TopoDS_Shell& BOP_ShellSplitter::Shell()const 
{
  return myShell;
}

//=======================================================================
// function: DoWithShell
// purpose: 
//=======================================================================
  void BOP_ShellSplitter::DoWithShell ()
{
  myFaces.Clear();

  TopExp_Explorer anExpFaces (myShell, TopAbs_FACE);
  for (; anExpFaces.More(); anExpFaces.Next()) {
    const TopoDS_Face& aF = TopoDS::Face(anExpFaces.Current());
    myFaces.Append(aF);
  }
  Do();
}

//=======================================================================
// function: DoWithListOfEdges
// purpose: 
//=======================================================================
  void BOP_ShellSplitter::DoWithListOfEdges(const TopTools_ListOfShape& aLE)
{
  myFaces.Clear();
 
  TopTools_ListIteratorOfListOfShape anItList;

  anItList.Initialize(aLE);
  for (; anItList.More(); anItList.Next()) {
    const TopoDS_Face& aF = TopoDS::Face(anItList.Value());
    myFaces.Append(aF);
  }
  Do();
}

//=======================================================================
// function: Do
// purpose: 
//=======================================================================
  void BOP_ShellSplitter::Do()
{
  myIsDone=Standard_False;
  myNothingToDo=Standard_False;
  //
  TopTools_ListIteratorOfListOfShape anItList;
  TopTools_IndexedDataMapOfShapeShape  aMFNewOld;
  TopoDS_Shell aShell;
  BRep_Builder aBB;
  //
  // insert the code about myNothingToDo
  //
  // 1. Make the formal shell  
  aBB.MakeShell(aShell);
  //
  anItList.Initialize(myFaces);
  for (; anItList.More(); anItList.Next()) {
    const TopoDS_Face& aF = TopoDS::Face(anItList.Value());
    TopoDS_Face aFNew;
    RemoveInternals (aF, aFNew);
    aMFNewOld.Add (aFNew, aF);

    aBB.Add(aShell, aFNew);
  }
  //
  // 2. Split the Shell
  
  TopoDS_Shape aShape;
  SplitShell (aShell, aShape);
  //
  // 3. Post-pro the result aShape
  //    and filling the myShapes field .
  TopExp_Explorer aShellExp(aShape, TopAbs_SHELL);
  for (; aShellExp.More(); aShellExp.Next()) {
    const TopoDS_Shape& aSh= aShellExp.Current();

    TopTools_ListOfShape aLF;
    TopExp_Explorer aFaceExp(aSh, TopAbs_FACE);
    for (; aFaceExp.More(); aFaceExp.Next()) {
      const TopoDS_Shape& aFNew= aFaceExp.Current();
      
      const TopoDS_Shape& aFOld=aMFNewOld.FindFromKey(aFNew);
      aLF.Append(aFOld);
    }
    
    if (aLF.Extent()) {
      myShapes.Append(aLF);
    }
  }
  
  myIsDone=Standard_True;
}

//=======================================================================
// function: RemoveInternals
// purpose: 
//=======================================================================
void RemoveInternals(const TopoDS_Face& aF,
		     TopoDS_Face& aFNew)
{
  BRep_Builder aBB;
  Standard_Integer iCnt;
  Standard_Real    aTol;
  

  TopLoc_Location aLoc;
  Handle(Geom_Surface) aSurface=BRep_Tool::Surface(aF, aLoc);
  aTol=BRep_Tool::Tolerance(aF);
  aBB.MakeFace (aFNew, aSurface, aLoc, aTol);
  aFNew.Orientation(aF.Orientation());

  TopExp_Explorer aFExp(aF, TopAbs_WIRE);
  for (; aFExp.More(); aFExp.Next()) {
    const TopoDS_Wire& aW= TopoDS::Wire(aFExp.Current());
    TopoDS_Wire aWNew;
    aBB.MakeWire(aWNew);
    aWNew.Orientation(aW.Orientation());

    iCnt=0;
    TopExp_Explorer aWExp(aW, TopAbs_EDGE);
    for (; aWExp.More(); aWExp.Next()) {
      const TopoDS_Edge& aE=TopoDS::Edge(aWExp.Current());
      if (aE.Orientation()!=TopAbs_INTERNAL) {
	aBB.Add(aWNew, aE);
	iCnt++;
      }
    }
    if (iCnt) {
      aBB.Add(aFNew, aWNew);
    }
  }
}

////////
//
//=======================================================================
// function : SplitShell
// purpose  : 
//=======================================================================
  Standard_Boolean SplitShell    (const TopoDS_Shell& aShellIn,
				  TopoDS_Shape& aShellsOut) 
{
  Standard_Boolean done;
  Standard_Integer i, j, aNumMultShell;

  TopTools_SequenceOfShape aSeqShells, aErrFaces, Lface;
  TopTools_DataMapOfShapeShape aMapFaceShells;
  TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
  TopTools_MapOfShape aMapMultiConnectEdges;
  TopoDS_Compound aCmpErrFaces;
  //
  done = Standard_False;
  aNumMultShell =0;
  aShellsOut = aShellIn;

  TopoDS_Iterator iter(aShellIn);
  for (; iter.More(); iter.Next()) {
    Lface.Append(iter.Value());
  }
  //
  TopExp::MapShapesAndAncestors(aShellIn, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
  //
  //Finds multishared edges
  Standard_Integer aNbEdges, aNbFaces;
  aNbEdges=aMapEdgeFaces.Extent();
  for(j=1; j<=aNbEdges; j++) {
    const TopTools_ListOfShape& aLF=aMapEdgeFaces(j);
    aNbFaces=aLF.Extent();
    if(aNbFaces>2) {
      const TopoDS_Shape& aE=aMapEdgeFaces.FindKey(j);
      aMapMultiConnectEdges.Add(aE);
    }
  }
  //
  //Gets shells without taking in account of multiconnexity.
  Standard_Boolean isGetShells = Standard_True;

  while(isGetShells && Lface.Length()) {
    TopTools_SequenceOfShape aTmpSeqShells;
    Standard_Boolean bGetShells;

    bGetShells=GetShells(Lface, 
			 aMapMultiConnectEdges, 
			 aTmpSeqShells,
			 aMapFaceShells,
			 aErrFaces);
    if(bGetShells) {
      done = Standard_True;
    }
    
    isGetShells = !aTmpSeqShells.IsEmpty();
    if(isGetShells) {
      aSeqShells.Append(aTmpSeqShells);
    }
  } // while(...)
  //
  //
  Standard_Boolean aIsDone = Standard_False;
  Standard_Integer aLfaceLength, aErrFacesLength;
  
  aLfaceLength=Lface.Length();
  aNumMultShell=aSeqShells.Length();

  if(aLfaceLength  && aNumMultShell) { 
    //Crating shells in the case of compsolid
    aIsDone = AddMultiConexityFaces(Lface,
				    aMapMultiConnectEdges,
				    aSeqShells,
				    aMapFaceShells,
				    aMapEdgeFaces,
				    aErrFaces);
  }
  //
  aNumMultShell = aSeqShells.Length();
  aErrFacesLength=aErrFaces.Length();
  //
  if (aErrFacesLength)  {
    BRep_Builder B;
    TopoDS_Compound aCompShells;
    
    B.MakeCompound  (aCmpErrFaces);
    B.MakeCompound(aCompShells);

    for(j =1; j <= aErrFacesLength; j++){
      B.Add(aCmpErrFaces, aErrFaces.Value(j));
    }

    if(aNumMultShell) {
      
      if(aNumMultShell == 1) {
        aShellsOut = aSeqShells.Value(1);
        B.Add(aCompShells, aSeqShells.Value(1));

        for(j=1; j <= aErrFacesLength; j++) {
          TopoDS_Shell aSh;
          B.MakeShell(aSh);
          B.Add(aSh, aErrFaces.Value(j));
	  B.Add(aCompShells, aSh);
        }
        aShellsOut = aCompShells;
      }
      
      else {
        for(i=1; i <= aNumMultShell; i++) {
          B.Add(aCompShells, aSeqShells.Value(i));
	}

        for(j=1; j<= aErrFacesLength; j++) {
          TopoDS_Shell aSh;
          B.MakeShell(aSh);
          B.Add(aSh,aErrFaces.Value(j));
	  B.Add(aCompShells, aSh);
        }
        aShellsOut = aCompShells;
      }
    } //if(aNumMultShell)

    done = Standard_True;
    return done;
  } // if (aErrFacesLength) 
  //
  //
  if(aNumMultShell>1) {
    TopTools_SequenceOfShape OpenShells;
    
    for(i=1; i <= aSeqShells.Length(); i++) {
      TopoDS_Shape aShell = aSeqShells.Value(i);
      if(!BRep_Tool::IsClosed(aShell)) {
        OpenShells.Append(aShell);
        aSeqShells.Remove(i--);
      }
    }

    j=OpenShells.Length();
    if(j>1) {
      // Attempt of creation closed shell from open shells 
      // with taking into account multiconnexity.
      //
      CreateClosedShell(OpenShells, aMapMultiConnectEdges, aMapEdgeFaces);
      aSeqShells.Append(OpenShells);
    }
  } //if(aNumMultShell>1)
  //
  //
  j=Lface.Length();
  if(j) {
    for(i=1; i <= j; i++) {
      BRep_Builder aB;
      TopoDS_Shell OneShell;
      aB.MakeShell(OneShell);
      aB.Add(OneShell, Lface.Value(i));
      aSeqShells.Append(OneShell);
    }
  }
  //
  //
  aNumMultShell = aSeqShells.Length();
  if(!done) {
    done = (aNumMultShell>1 || aIsDone);
  }

  BRep_Builder B;
  TopoDS_Compound aCompShells;
  B.MakeCompound(aCompShells);
  for(i=1; i <= aNumMultShell; i++){
    B.Add(aCompShells, aSeqShells.Value(i));  
  }
  aShellsOut = aCompShells;
  //
  return done;
}

//=======================================================================
// function : GetShells
// purpose  : 
//=======================================================================
Standard_Boolean GetShells(TopTools_SequenceOfShape& Lface,
			   const TopTools_MapOfShape& aMapMultiConnectEdges,
			   TopTools_SequenceOfShape& aSeqShells,
			   TopTools_DataMapOfShapeShape& aMapFaceShells,
			   TopTools_SequenceOfShape& ErrFaces) 
{
  Standard_Boolean done = Standard_False;
  Standard_Integer i, j;

  j=Lface.Length();
  if(!j) {
    return done;
  }

  Standard_Boolean isMultiConnex;
  TopoDS_Shell nshell;
  TopTools_MapOfShape dire, reve;
  BRep_Builder B;
  TopTools_SequenceOfShape aSeqUnconnectFaces;

  B.MakeShell(nshell);
  isMultiConnex = !aMapMultiConnectEdges.IsEmpty();
  i=1; 
  j=1;
  //
  for(; i<=Lface.Length(); i++)  {
    TopTools_MapOfShape dtemp, rtemp;
    Standard_Integer nbbe=0, nbe = 0;
    
    TopoDS_Face aF = TopoDS::Face(Lface.Value(i));

    TopExp_Explorer anExpe(aF, TopAbs_EDGE);
    for(; anExpe.More(); anExpe.Next()) {
      const TopoDS_Edge& aE = TopoDS::Edge(anExpe.Current());
      
      if(isMultiConnex && aMapMultiConnectEdges.Contains(aE)){
        continue;
      }
      
      if (BRep_Tool::Degenerated (aE)) {
	continue;
      }

      if (BRep_Tool::IsClosed(aE, aF)) {
	continue;
      }

      TopAbs_Orientation anEOr;
      anEOr=aE.Orientation();

      Standard_Boolean bDireContains, bReveContains;

      bDireContains=dire.Contains(aE);
      bReveContains=reve.Contains(aE);

      if((anEOr == TopAbs_FORWARD  && bDireContains) || 
	 (anEOr == TopAbs_REVERSED && bReveContains)) {
	nbbe++;
      }
      else if((anEOr == TopAbs_FORWARD  && bReveContains) ||
	      (anEOr == TopAbs_REVERSED && bDireContains)) {   
	nbe++;
      }
      
      if(bDireContains) {
	dire.Remove(aE);
      }
      else if(bReveContains) {
	reve.Remove(aE);
      }
      else {
	if(anEOr == TopAbs_FORWARD) {
	  dtemp.Add(aE);
	}
	if(anEOr == TopAbs_REVERSED) {
	  rtemp.Add(aE);
	}
      }
    } // for(; expe.More(); expe.Next())
    //
    //
    if(!nbbe && !nbe && dtemp.IsEmpty() && rtemp.IsEmpty()) {
      continue;
    }
    //
    if( nbe != 0 && nbbe != 0) {
      ErrFaces.Append(aF);
      Lface.Remove(i);
      j++;
      continue;
    }
    //
    if((nbe != 0 || nbbe != 0) || j == 1) {
      TopTools_MapIteratorOfMapOfShape ite;
      if(nbbe != 0) {
	aF.Reverse();
	
	ite.Initialize(dtemp);
	for(; ite.More(); ite.Next()) {
	  reve.Add(ite.Key());
	}
	
	ite.Initialize(rtemp);
	for(; ite.More(); ite.Next()){
	  dire.Add(ite.Key());
	}
	done = Standard_True;
      }
      else {
	ite.Initialize(dtemp);
	for(; ite.More(); ite.Next()) {
	  dire.Add(ite.Key());
	}
	
	ite.Initialize(rtemp);
	for(; ite.More(); ite.Next()){
	  reve.Add(ite.Key());
	}
      }

      j++;
      B.Add(nshell, aF);
      aMapFaceShells.Bind(aF, nshell);
      Lface.Remove(i);
      if(isMultiConnex && BRep_Tool::IsClosed(nshell)) {
        aSeqShells.Append(nshell);
        TopoDS_Shell nshellnext;
        B.MakeShell(nshellnext);
        nshell = nshellnext;
        j=1;
      }
      i=0;
    } // if((nbe != 0 || nbbe != 0) || j == 1)
    //
    //
    if(Lface.Length() && i == Lface.Length() && j <=2) {
      TopoDS_Iterator aItf(nshell,Standard_False);
      if(aItf.More()){
        aSeqUnconnectFaces.Append(aItf.Value());
      }
      TopoDS_Shell nshellnext;
      B.MakeShell(nshellnext);
      nshell = nshellnext;
      i=0;
      j=1;
    }
  }//for(; i<=Lface.Length(); i++) 

  Standard_Boolean isContains = Standard_False;
  j=aSeqShells.Length();
  for(i=1 ; i <= j; i++){
    isContains = nshell.IsSame(aSeqShells.Value(i));
    if (isContains) {
      break;
    }
  }

  if(!isContains) {
    Standard_Integer numFace =0;
    TopoDS_Shape aFace;
    
    TopoDS_Iterator aItf(nshell, Standard_False) ;
    for(; aItf.More(); aItf.Next()) {
      aFace = aItf.Value();
      numFace++;
    }
    
    if(numFace >1) {
      aSeqShells.Append(nshell);
    }
    else if(numFace == 1) {
      Lface.Append(aFace);
    }
  }
  
  for(i=1; i<= aSeqUnconnectFaces.Length(); i++){
    Lface.Append(aSeqUnconnectFaces);
  }
  return done;
}

//=======================================================================
// function : AddMultiConexityFaces
// purpose  : 
//=======================================================================
Standard_Boolean AddMultiConexityFaces(TopTools_SequenceOfShape& Lface,
				       const TopTools_MapOfShape& aMapMultiConnectEdges,
				       TopTools_SequenceOfShape& SeqShells,
				       const TopTools_DataMapOfShapeShape& aMapFaceShells,
				       const TopTools_IndexedDataMapOfShapeListOfShape& aMapEdgeFaces,
				       TopTools_SequenceOfShape& ErrFaces)
{
  Standard_Boolean done = Standard_False;
  BRep_Builder aB;
  Standard_Integer i1;

  for(i1 = 1 ; i1<=Lface.Length(); )  {
    TopTools_MapOfShape dire, reve;
    TopTools_IndexedMapOfShape MapOtherShells;
    Standard_Integer aNbOtherShells;

    const TopoDS_Face& aFace = TopoDS::Face(Lface.Value(i1));
    //
    //Finds shells containg multishared edges from this face
    TopExp_Explorer aExpEdges(aFace, TopAbs_EDGE);
    for(; aExpEdges.More(); aExpEdges.Next()) {
      const TopoDS_Shape& aE = aExpEdges.Current();
      
      if(!aMapMultiConnectEdges.Contains(aE)) {
	continue;
      }

      if( aE.Orientation() == TopAbs_FORWARD) {
	dire.Add(aE);
      }
      else {
	reve.Add(aE);
      }

      const TopTools_ListOfShape& aLF = aMapEdgeFaces.FindFromKey(aE);
      TopTools_ListIteratorOfListOfShape aItl(aLF);
      for(; aItl.More(); aItl.Next()) {
        const TopoDS_Shape& aF = aItl.Value();
        
	if(aF.IsSame(aFace)) {
	  continue;
	}

        TopoDS_Shape aOthershell;
        if(aMapFaceShells.IsBound(aF)) {
          aOthershell = aMapFaceShells.Find(aF);
          if(!MapOtherShells.Contains(aOthershell)) {
            MapOtherShells.Add(aOthershell);
	  }
        }
      }
    }//for(; aExpEdges.More(); aExpEdges.Next())
    //
    //
    aNbOtherShells=MapOtherShells.Extent();
    //
    if(!aNbOtherShells) {
      i1++;
      continue;
    }
    
    else {
      //Adds face to open shells containg the same multishared edges.
      //For nonmanifold mode creation ine shell from face and shells 
      // containing the same multishared edges.
      done = Standard_True;
      
      Standard_Integer j, k;
      
      TColStd_SequenceOfInteger SeqOtherShells;
      for(j =1; j <= aNbOtherShells; j++) {
        Standard_Integer index=0;
        for(k =1; k <= SeqShells.Length() && !index; k++) {
          if(SeqShells.Value(k) == MapOtherShells.FindKey(j)){
            index = k;
	  }
	}
        SeqOtherShells.Append(index);
      }

      aNbOtherShells= SeqOtherShells.Length();

      for(j =1; j <= aNbOtherShells; j++) {
        Standard_Integer nbdir =0,nbrev =0;
        TopTools_MapOfShape mapEdges;
	
	k = SeqOtherShells.Value(j);
	const TopoDS_Shape& aShk=SeqShells.Value(k);
	
	TopExp_Explorer aExpF(aShk, TopAbs_FACE);
        for(; aExpF.More(); aExpF.Next()) {
	  const TopoDS_Shape& aFC=aExpF.Current();
	  
	  TopExp_Explorer aExpE(aFC,TopAbs_EDGE);
          for(; aExpE.More(); aExpE.Next()) {
            
	    const TopoDS_Shape& aEC = aExpE.Current();
            if(!mapEdges.Contains(aEC)){
              mapEdges.Add(aEC);
	    }
            else {
	      mapEdges.Remove(aEC);
	    }

          }// for(; aExpE.More(); aExpE.Next())
        }// for(; aExpF.More(); aExpF.Next()) {
	//
	//
	TopTools_MapIteratorOfMapOfShape aIte(mapEdges);
        for(;aIte.More(); aIte.Next()) {
          const TopoDS_Shape& aEC = aIte.Key();
	  TopAbs_Orientation anOrEC=aEC.Orientation();
	  
	  Standard_Boolean bDireContains, bReveContains;

	  bDireContains=dire.Contains(aEC);
	  bReveContains=reve.Contains(aEC);

          if((anOrEC == TopAbs_FORWARD  && bDireContains) || 
	     (anOrEC == TopAbs_REVERSED && bReveContains)) {
	    nbrev++;
	  }
          else if((anOrEC == TopAbs_FORWARD  && bReveContains)||
		  (anOrEC == TopAbs_REVERSED && bDireContains)) {
	    nbdir++;
	  }
        }// for(;aIte.More(); aIte.Next())

        if(nbdir && nbrev) {
          ErrFaces.Append(aFace);
	}

        else if(nbdir || nbrev) { 
	  // for manifold mode face containing multiconnexity 
	  // edges will be added in the each shell
	  // containing the same edges. ???
	  
	  TopoDS_Shape aShell;
	  aShell = SeqShells.Value(k);
	  if (!nbrev) {
	    aB.Add(aShell, aFace);
	    SeqShells.ChangeValue(k) = aShell;
	  }
        }// else if(nbdir || nbrev)
      }// for(j =1; j <= aNbOtherShells; j++)
      //
      dire.Clear();
      reve.Clear();
      Lface.Remove(i1);
    }
  }
  return done;
}
//=======================================================================
// function : CreateClosedShell
// purpose  : 
//=======================================================================
void CreateClosedShell(TopTools_SequenceOfShape& OpenShells,
		       const TopTools_MapOfShape& aMapMultiConnectEdges,
		       const TopTools_IndexedDataMapOfShapeListOfShape& aMapEdgeFaces)
{
  TopTools_MapOfShape amapFaces;
  
  TopTools_MapIteratorOfMapOfShape aItEdg(aMapMultiConnectEdges);
  for(; aItEdg.More(); aItEdg.Next()) {
    const TopTools_ListOfShape& aLF = aMapEdgeFaces.FindFromKey(aItEdg.Key());
    TopTools_ListIteratorOfListOfShape aItF(aLF);
    for(; aItF.More(); aItF.Next()) {
      amapFaces.Add(aItF.Value());
    }
  }
  //
  // Creating new shells if some open shells contain the same edges.
  Standard_Integer i, j;
  Standard_Boolean isClosedShell; 

  for(i=1; i <= OpenShells.Length(); i++)  {
    TopTools_MapOfShape dire, reve;

    isClosedShell = Standard_False;
    const TopoDS_Shape& anOpenShelli=OpenShells.Value(i);
    TopExp_Explorer aExpF(anOpenShelli, TopAbs_FACE);
    
    for(; aExpF.More(); aExpF.Next()) {
      const TopoDS_Shape& aFace = aExpF.Current();
      
      if(!amapFaces.Contains(aFace)) {
	continue;
      }

      TopExp_Explorer aExpEdges(aFace, TopAbs_EDGE);
      for(; aExpEdges.More(); aExpEdges.Next()) {
        const TopoDS_Shape& aE = aExpEdges.Current();
        
	if(!aMapMultiConnectEdges.Contains(aE)) {
	  continue;
	}
	
	TopAbs_Orientation anOrE;
	anOrE=aE.Orientation();
	
        if(anOrE == TopAbs_FORWARD) {
	  dire.Add(aE);
	}
        else if(anOrE == TopAbs_REVERSED) {
	  reve.Add(aE);
	}
      }
    }// for(; aExpF.More(); aExpF.Next())
    //
    // 
    for(j=i+1; j<=OpenShells.Length(); j++)  {
      Standard_Integer nbedge =0;
      
      const TopoDS_Shape& anOpenShellj=OpenShells.Value(j);

      TopExp_Explorer aExpF2(anOpenShellj, TopAbs_FACE);
      for(; aExpF2.More() && !nbedge; aExpF2.Next()) {

	const TopoDS_Shape& aFace2 = aExpF2.Current();
        
	if(!amapFaces.Contains(aFace2)) {
	  continue;
	}

        TopExp_Explorer aExpEdges2(aFace2, TopAbs_EDGE);
        for(; aExpEdges2.More()&& !nbedge; aExpEdges2.Next()) {
          const TopoDS_Shape& aE2 = aExpEdges2.Current();

          if(!aMapMultiConnectEdges.Contains(aE2)) {
	    continue;
	  }

	  Standard_Boolean bDireContains, bReveContains;

	  bDireContains=dire.Contains(aE2);
	  bReveContains=reve.Contains(aE2);

          if(!bDireContains && !bReveContains) {
	    continue;
	  }

          isClosedShell = Standard_True;
          nbedge++;
        }
      }// for(; aExpF2.More() && !nbedge; aExpF2.Next())
      
      if(!isClosedShell){
	continue;
      }

      BRep_Builder aB;
      TopoDS_Shape aShell = OpenShells.Value(i);

      TopExp_Explorer aExpF21(anOpenShellj, TopAbs_FACE);
      for(; aExpF21.More(); aExpF21.Next()) {
        const TopoDS_Shape& aFace = aExpF21.Current();
        //if(isReversed) {
        //  aFace.Reverse();
	//}
        aB.Add(aShell, aFace);
      }
      
      OpenShells.ChangeValue(i) = aShell;
      OpenShells.Remove(j--);
    }// for(j=i+1 ; j<=OpenShells.Length();j++ )
  }//for(i=1; i <= OpenShells.Length(); i++)
}