summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepBuild/TopOpeBRepBuild_FaceBuilder.cxx
blob: 1e4338fe2d3a63a7d6c1bbb7e2786eaa5660fd60 (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
// File:	TopOpeBRepBuild_FaceBuilder.cxx
// Created:	Fri Jan  5 18:10:57 1996
// Author:	Jean Yves LEBEY
//		<jyl@meteox>

#include <TopOpeBRepBuild_FaceBuilder.ixx>

#include <gp_Pnt.hxx>
#include <gp_Pnt2d.hxx>
#include <TopAbs_Orientation.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopOpeBRepDS_BuildTool.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>
//#include <BRepAdaptor_Curve2d.hxx>
#include <TopExp.hxx>
#include <TopTools_Array1OfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_IndexedMapOfOrientedShape.hxx>
#include <TopTools_IndexedDataMapOfShapeShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_DataMapOfShapeListOfShape.hxx>
#include <TopTools_DataMapOfShapeListOfShape.hxx>
#include <TopOpeBRepBuild_WireEdgeClassifier.hxx>
#include <TopOpeBRepBuild_Loop.hxx>
#include <TopOpeBRepBuild_BlockBuilder.hxx>
#include <TopOpeBRepBuild_LoopSet.hxx>
#include <Standard_ProgramError.hxx>
#include <Precision.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopTools_DataMapOfShapeInteger.hxx>
#include <TopOpeBRepBuild_define.hxx>

#undef RM_HANGING
// MSV: RM_HANGING behaviour: when state of wire is UNCLOSEDW we do not
// remove the whole wire but remove the chains of hanging edges. This would
// produce a good result in some cases. But :-( it gives regressions on grid
// tests (1cto 021 W4,X4). Therefore I leaved this code not active.
#ifdef RM_HANGING
#include <TColStd_ListIteratorOfListOfInteger.hxx>
#endif

#ifdef DEB
Standard_IMPORT Standard_Boolean TopOpeBRepBuild_GettracePURGE();
Standard_IMPORT Standard_Boolean TopOpeBRepDS_GettraceSTRANGE();
#include <TopOpeBRepDS_DSX.hxx>
void debifb() {}
#endif

#ifdef DRAW
#include <TopOpeBRepTool_DRAW.hxx>
#endif

//=======================================================================
//function : TopOpeBRepBuild_FaceBuilder
//purpose  : 
//=======================================================================
TopOpeBRepBuild_FaceBuilder::TopOpeBRepBuild_FaceBuilder()
{
}

//=======================================================================
//function : TopOpeBRepBuild_FaceBuilder
//purpose  : 
//=======================================================================
TopOpeBRepBuild_FaceBuilder::TopOpeBRepBuild_FaceBuilder(TopOpeBRepBuild_WireEdgeSet& WES,const TopoDS_Shape& F,const Standard_Boolean ForceClass)
{
  InitFaceBuilder(WES,F,ForceClass);
}

//=======================================================================
//function : InitFaceBuilder
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::InitFaceBuilder(TopOpeBRepBuild_WireEdgeSet& WES,const TopoDS_Shape& F,const Standard_Boolean ForceClass) 
{
  myFace = TopoDS::Face(F);
#ifdef DEB
  Standard_Boolean deb = TopOpeBRepDS_GettraceSPSX(myFace);
  if (deb) debifb();
#endif
  MakeLoops(WES);
  TopOpeBRepBuild_BlockBuilder& BB = myBlockBuilder;
  TopOpeBRepBuild_WireEdgeClassifier WEC(F,BB);
  TopOpeBRepBuild_LoopSet& LS = myLoopSet;
  myFaceAreaBuilder.InitFaceAreaBuilder(LS,WEC,ForceClass);

  //  Standard_Boolean hascnxsta = WES.HasConnexStartElement();
  //  if ( hascnxsta ) {
  //    myMOSI.Clear();
  //    InitFace();
  //    for (;MoreFace();NextFace()) {
  //      InitWire();
  //      for (;MoreWire();NextWire()) {
  //	Standard_Boolean isold = IsOldWire();if (isold) continue;
  //	InitEdge();
  //	for(;MoreEdge();NextEdge()) {
  //	  const TopoDS_Shape& E = Edge();
  //	  Standard_Boolean issta = WES.IsStartElement(E);
  //	  Standard_Boolean nsta  = WES.NStartElement(E);Standard_Boolean iscnx = (nsta == 2);
  //	  if (!issta || !iscnx ) continue;
  //	  Standard_Boolean isb = myMOSI.IsBound(E);
  //	  if (!isb) myMOSI.Bind(E,0);
  //	  Standard_Integer nmosi = myMOSI(E);
  //	  nmosi++;
  //	  myMOSI(E) = nmosi;
  //	}
  //      }
  //    }
  //  } // hascnxsta
  
}

//---------------------------------------------------------------
void FUN_DetectVerticesOn1Edge(const TopoDS_Shape& W,TopTools_IndexedDataMapOfShapeShape& mapVon1E)
{
  // Fills the map <mapVon1edge>,with vertices of <W> of 3d connexity 1.
  TopTools_IndexedDataMapOfShapeListOfShape mapVedges;
  TopExp::MapShapesAndAncestors(W,TopAbs_VERTEX,TopAbs_EDGE,mapVedges);
  Standard_Integer nV = mapVedges.Extent();

  for (Standard_Integer i = 1;i <= nV;i++) {
    const TopoDS_Shape& V = mapVedges.FindKey(i);
    if (V.Orientation() == TopAbs_INTERNAL) continue;

    const TopTools_ListOfShape& loE = mapVedges.FindFromIndex(i);
    if (loE.Extent() < 2) {
      // Keeping INTERNAL or EXTERNAL edges
      const TopoDS_Shape& E = loE.First();
      TopAbs_Orientation oriE = E.Orientation();
      if ((oriE == TopAbs_INTERNAL) || (oriE == TopAbs_EXTERNAL)) continue;
      mapVon1E.Add(V,E);
    }
  }
} 

#define ISUNKNOWN -1
#define ISVERTEX  0
#define GCLOSEDW  1
#define UNCLOSEDW 2
#define CLOSEDW   10
Standard_Integer FUN_AnalyzemapVon1E(const TopTools_IndexedDataMapOfShapeShape& mapVon1E,
			TopTools_IndexedDataMapOfShapeShape& mapVV)
{
#ifdef DRAW
  Standard_Boolean trc = TopOpeBRepBuild_GettracePURGE();
  if (trc) cout<<endl<<"* DetectUnclosedWire :"<<endl;
#endif

  Standard_Integer res = ISUNKNOWN;

  Standard_Integer nV = mapVon1E.Extent();
  if      (nV == 0) {
    res = CLOSEDW;
  }
  else if (nV == 1) {
    const TopoDS_Shape& E = mapVon1E.FindFromIndex(1);
    Standard_Boolean Eclosed = E.Closed();
    Standard_Boolean dgE = BRep_Tool::Degenerated(TopoDS::Edge(E));
    if      (dgE)     res = ISVERTEX;
    else if (Eclosed) res = CLOSEDW;
    else              res = UNCLOSEDW;
  } 
  else {
    // Finding among all vertices,couple of vertices falling on same
    // geometry.
    // Filling up map <mapVV>,with (vi,vj),vi and vj are on same point.
    Standard_Real tol = Precision::Confusion();
    for (Standard_Integer i = 1;i <= nV;i++) {
      const TopoDS_Vertex& vi = TopoDS::Vertex(mapVon1E.FindKey(i));
      gp_Pnt pi = BRep_Tool::Pnt(vi);
      for (Standard_Integer j = i+1;j <= nV;j++) {
	const TopoDS_Vertex& vj = TopoDS::Vertex(mapVon1E.FindKey(j));
	gp_Pnt pj = BRep_Tool::Pnt(vj);
	Standard_Boolean same = pi.IsEqual(pj,tol);
	if (same) {
	  mapVV.Add(vi,vj);
	  mapVV.Add(vj,vi);
	  break;
	}
      } // j
    } // i
    Standard_Integer nVV = mapVV.Extent();
#ifdef RM_HANGING
    // MSV Oct 4, 2001: consider GCLOSEDW even if not all vertices from mapVon1E
    //                  hit into mapVV; reason is the left vertices may start
    //                  useless chains of hanging edges that can be removed to
    //                  achieve a closed wire.
    if (nVV > 0)   res = GCLOSEDW;
#else
    if (nVV == nV) res = GCLOSEDW;
#endif
    else           res = UNCLOSEDW;
  }
  return res;
} // FUN_AnalyzemapVon1E

#ifdef DRAW
void FUN_AnalyzemapVon1EDRAW(const Standard_Integer res,
			     const TopTools_IndexedDataMapOfShapeShape& mapVon1E,
			     const TopTools_IndexedDataMapOfShapeShape& mapVV,
			     const TopoDS_Shape& W,const Standard_Integer iiwi,
			     TopTools_IndexedDataMapOfShapeShape& mapVon1EdgeDRAW,
			     TopTools_IndexedDataMapOfShapeShape& mapVVsameGDRAW)
{
  Standard_Boolean trc = TopOpeBRepBuild_GettracePURGE();
  if (!trc) return;
  cout<<"wire "<<iiwi;
  if      (res == ISVERTEX) {
    cout<<" is vertex"<<endl;
  }
  else if (res == CLOSEDW) {
    cout<<" is closed"<<endl;
  }
  else if (res == GCLOSEDW) {
    cout<<" is Gclosed :"<<endl;
    TCollection_AsciiString aa("w_");FUN_tool_draw(aa,W,iiwi);
    Standard_Integer i ;
    for ( i = 1;i <= mapVV.Extent();i++) {
      Standard_Integer iV = mapVVsameGDRAW.Add(mapVV.FindKey(i),mapVV.FindFromIndex(i));
      cout<<" on vve_"<<iV; aa = "vve_";
      FUN_tool_draw(aa,mapVVsameGDRAW.FindKey(iV),iV);
    }
    for (i = 1;i <= mapVon1E.Extent();i++) {
      Standard_Integer iE = mapVon1EdgeDRAW.Add(mapVon1E.FindKey(i),mapVon1E.FindFromIndex(i));
      cout<<" on eed_"<<iE; aa = "eed_";
      FUN_tool_draw(aa,mapVon1EdgeDRAW.FindFromIndex(iE),iE);
    }
    cout<<endl;
  }
  else if (res == UNCLOSEDW) {
    cout<<" is unclosed "<<endl;
    TCollection_AsciiString aa("w_");FUN_tool_draw(aa,W,iiwi);
  }
  cout<<endl;
} // FUN_AnalyzemapVon1EDRAW
#endif

//=======================================================================
//function : DetectUnclosedWire
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::DetectUnclosedWire(TopTools_IndexedDataMapOfShapeShape& mapVVsameG,
			      TopTools_IndexedDataMapOfShapeShape& mapVon1Edge)
{
  // wire unclosed : has vertices of connexity == 1
  // exception : a wire of one closed edge with only one vertex describing
  //             a face or a degenerated edge.

  mapVVsameG.Clear();
  mapVon1Edge.Clear();

  // During the wire processing,
  // * IF THE WIRE IS G-CLOSED,we fill up the maps :
  //  - <mapVVsameG> with vertices falling on same geometry
  //  - <mapVon1Edge> with (key = vertex,item = edge),
  //    the vertex is connected to only one unclosed,undegenerated edge.
  // * Else,if it is unclosed,we delete it (or it`s hanging edges).

#ifdef DRAW
  TopTools_IndexedDataMapOfShapeShape mapVon1EdgeDRAW,mapVVsameGDRAW;
  Standard_Boolean trc = TopOpeBRepBuild_GettracePURGE();
  if (trc) cout<<endl<<"* DetectUnclosedWire :"<<endl<<endl;
#endif

  Standard_Integer iiwi = 0; // DEB

  InitFace();
  for (;MoreFace();NextFace()) {
   InitWire();
    for (;MoreWire();NextWire()) { 
      iiwi++;
      Standard_Boolean isold = IsOldWire();
#ifdef DRAW
      if ( trc && isold ) cout<<"wire "<<iiwi<<" is old wire => closed"<<endl;
#endif
      if (isold) continue;
      
      TopoDS_Compound cmp;BRep_Builder BB;BB.MakeCompound(cmp);
      InitEdge();
      for(;MoreEdge();NextEdge()) AddEdgeWire(Edge(),cmp);
      TopoDS_Shape W = cmp;

      // <mapVon1E> binds vertices of connexity 1 attached to one non-closed,non-degenerated edge.
      TopTools_IndexedDataMapOfShapeShape mapVon1E;
      FUN_DetectVerticesOn1Edge(W,mapVon1E);

      TopTools_IndexedDataMapOfShapeShape mapVV;
      Standard_Integer res = FUN_AnalyzemapVon1E(mapVon1E,mapVV);
#ifdef DRAW
      FUN_AnalyzemapVon1EDRAW(res,mapVon1E,mapVV,W,iiwi,mapVon1EdgeDRAW,mapVVsameGDRAW);
#endif

      if      (res == ISVERTEX) {
	continue;
      }
      else if (res == CLOSEDW) {
	continue;
      }
      else if (res == GCLOSEDW) {
	Standard_Integer i;
	for (i = 1;i <= mapVV.Extent();i++) {
          mapVVsameG.Add(mapVV.FindKey(i),mapVV.FindFromIndex(i));
	}
	for (i = 1;i <= mapVon1E.Extent();i++) {
           mapVon1Edge.Add(mapVon1E.FindKey(i),mapVon1E.FindFromIndex(i));
	}
      }
      else if (res == UNCLOSEDW) {
#ifdef RM_HANGING
        // MSV Oct 4, 2001: remove hanging edges
        TopTools_IndexedDataMapOfShapeListOfShape mapVE;
        TopExp::MapShapesAndAncestors (W, TopAbs_VERTEX, TopAbs_EDGE, mapVE);
        Standard_Integer nV = mapVon1E.Extent();
        for (Standard_Integer i = 1; i <= nV; i++)
        {
          TopoDS_Vertex V = TopoDS::Vertex (mapVon1E.FindKey(i));
          if (mapVV.Contains(V)) continue;  // V is in same geometry pair
          while (1)
          {
            const TopTools_ListOfShape &LE = mapVE.FindFromKey(V);

            // get not yet processed edge, count the number of such edges
            Standard_Integer nEdges = 0;
            TopoDS_Edge Edge;
            TColStd_ListOfInteger LOI;
            TopTools_ListIteratorOfListOfShape itE (LE);
            for (; itE.More() && nEdges <= 1; itE.Next())
            {
              const TopoDS_Edge &E = TopoDS::Edge (itE.Value());
              Standard_Integer I = myBlockBuilder.Element(E);
              if (!E.Closed() && myBlockBuilder.ElementIsValid(I))
              {
                TopoDS_Vertex Vf,Vl;
                TopExp::Vertices (E, Vf, Vl);
                LOI.Append(I);
                // consider not small edges only
                if (!Vf.IsSame(Vl))
                {
                  Edge = E;
                  nEdges++;
                }
              }
            }
            if (nEdges != 1) break;  // stop this chain

            // remove edges from Block Builder
            TColStd_ListIteratorOfListOfInteger itLOI (LOI);
            for (; itLOI.More(); itLOI.Next())
              myBlockBuilder.SetValid (itLOI.Value(), Standard_False);

            // get other vertex
            TopoDS_Vertex aV1, aV2, otherV;
            TopExp::Vertices (Edge, aV1, aV2);
            if (aV1.IsSame (V))
              otherV = aV2;
            else if (aV2.IsSame (V))
              otherV = aV1;
            if (otherV.IsNull()) break;
            V = otherV;
          }
        }
#else
	TopExp_Explorer ex;
	for (ex.Init(W,TopAbs_EDGE);ex.More();ex.Next()) {
//	for (TopExp_Explorer ex(W,TopAbs_EDGE);ex.More();ex.Next()) {
	  Standard_Integer I = myBlockBuilder.Element(ex.Current());
	  myBlockBuilder.SetValid(I,Standard_False);
	}
#endif
      }
    } // MoreWire
  } // MoreFace
} // DetectUnclosedWire

//=======================================================================
//function : CorrectGclosedWire
//purpose  :
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::CorrectGclosedWire(const TopTools_IndexedDataMapOfShapeShape& mapVVref,
			       const TopTools_IndexedDataMapOfShapeShape& mapVon1Edge)
{
  // prequesitory : edges described by <mapVon1Edge> are not closed,not degenerated
#ifdef DEB
  Standard_Boolean trc = TopOpeBRepDS_GettraceSTRANGE();
  if (TopOpeBRepBuild_GettracePURGE()) {
    cout<<endl<<"* CorrectGclosedWire :"<<endl<<endl;
    trc = Standard_True;
  }
#endif
  
  Standard_Integer nVV = mapVVref.Extent();
  for (Standard_Integer i = 1;i <= nVV;i++) {
    const TopoDS_Vertex& V = TopoDS::Vertex(mapVVref.FindKey(i));
    const TopoDS_Vertex& Vref = TopoDS::Vertex(mapVVref.FindFromIndex(i));
    
    if (V.IsSame(Vref)) continue;

    TopoDS_Edge E = TopoDS::Edge(mapVon1Edge.FindFromKey(V));
    Standard_Real paronE = BRep_Tool::Parameter(V,E);
    
    BRep_Builder BB;E.Free(Standard_True);
    BB.Remove(E,V);
    TopoDS_Shape aLocalShape = Vref.Oriented(V.Orientation());
    TopoDS_Vertex newVref = TopoDS::Vertex(aLocalShape);
//    TopoDS_Vertex newVref = TopoDS::Vertex(Vref.Oriented(V.Orientation()));
    BB.Add(E,newVref);
#ifdef DEB
    Standard_Integer iV = i,iVref = mapVVref.FindIndex(Vref),iE = mapVon1Edge.FindIndex(V);
    if (trc) cout << " replacing V "<<iV<<" with V "<<iVref<<" on edge "<<iE<<endl;
#endif
    TopOpeBRepDS_BuildTool BT;
    BT.Parameter(E,newVref,paronE);
  }
}

//=======================================================================
//function : DetectPseudoInternalEdge
//purpose  :
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::DetectPseudoInternalEdge(TopTools_IndexedMapOfShape& MapE)
{
  TopoDS_Compound cmp;BRep_Builder BB;BB.MakeCompound(cmp);
  InitFace();
  for (;MoreFace();NextFace()) {
    InitWire();
    for (;MoreWire();NextWire()) {
      Standard_Boolean isold = IsOldWire(); if (isold) continue;
      InitEdge();
      for(;MoreEdge();NextEdge()) AddEdgeWire(Edge(),cmp);
    } // MoreWire
  } // MoreFace

  TopTools_IndexedDataMapOfShapeListOfShape mapVOE;
  TopExp::MapShapesAndAncestors(cmp,TopAbs_VERTEX,TopAbs_EDGE,mapVOE);
  Standard_Integer nv = mapVOE.Extent();

  MapE.Clear();
  for (Standard_Integer i = 1; i <= nv; i++) {
    const TopTools_ListOfShape& le = mapVOE.FindFromIndex(i);
    Standard_Integer ne = le.Extent();
    if (ne == 2) {
      TopTools_ListIteratorOfListOfShape ile(le); const TopoDS_Shape& e1 = ile.Value();
      ile.Next();        const TopoDS_Shape& e2 = ile.Value();
      Standard_Boolean same = e1.IsSame(e2);
      TopAbs_Orientation o1 = e1.Orientation();
      TopAbs_Orientation o2 = e2.Orientation();
      Standard_Boolean o1co2 = (o1 == TopAbs::Complement(o2));

      if ( same && o1co2 ) {
	MapE.Add(e1);

	Standard_Integer ie1 = myBlockBuilder.Element(e1);
	myBlockBuilder.SetValid(ie1,Standard_False);

	Standard_Integer ie2 = myBlockBuilder.Element(e2);
	myBlockBuilder.SetValid(ie2,Standard_False);
      }
    }
  }

}

//=======================================================================
//function : Face
//purpose  : 
//=======================================================================
const TopoDS_Shape& TopOpeBRepBuild_FaceBuilder::Face() const 
{
  return myFace;
}

//=======================================================================
//function : InitFace
//purpose  : 
//=======================================================================
Standard_Integer TopOpeBRepBuild_FaceBuilder::InitFace()
{
  Standard_Integer n = myFaceAreaBuilder.InitArea();
  return n;
}

//=======================================================================
//function : MoreFace
//purpose  : 
//=======================================================================
Standard_Boolean TopOpeBRepBuild_FaceBuilder::MoreFace() const
{
  Standard_Boolean b = myFaceAreaBuilder.MoreArea();
  return b;
}

//=======================================================================
//function : NextFace
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::NextFace()
{
  myFaceAreaBuilder.NextArea();
}

//=======================================================================
//function : InitWire
//purpose  : 
//=======================================================================
Standard_Integer TopOpeBRepBuild_FaceBuilder::InitWire()
{
  Standard_Integer n = myFaceAreaBuilder.InitLoop();
  return n;
}

//=======================================================================
//function : MoreWire
//purpose  : 
//=======================================================================
Standard_Boolean TopOpeBRepBuild_FaceBuilder::MoreWire() const
{
  Standard_Boolean b = myFaceAreaBuilder.MoreLoop();
  return b;
}

//=======================================================================
//function : NextWire
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::NextWire()
{
  myFaceAreaBuilder.NextLoop();
}

//=======================================================================
//function : IsOldWire
//purpose  : 
//=======================================================================
Standard_Boolean TopOpeBRepBuild_FaceBuilder::IsOldWire() const
{
  const Handle(TopOpeBRepBuild_Loop)& L = myFaceAreaBuilder.Loop();
  Standard_Boolean b = L->IsShape();
  return b;
}

//=======================================================================
//function : OldWire
//purpose  : 
//=======================================================================
const TopoDS_Shape& TopOpeBRepBuild_FaceBuilder::OldWire() const
{
  const Handle(TopOpeBRepBuild_Loop)& L = myFaceAreaBuilder.Loop();
  return L->Shape();
}

//=======================================================================
//function : FindNextValidElement
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::FindNextValidElement()
{
  // prerequisites : myBlockIterator.Initialize
  myFaceAreaBuilder.Loop();
  Standard_Boolean found = Standard_False;

  while ( myBlockIterator.More()) {
    const Standard_Integer i = myBlockIterator.Value();
    found = myBlockBuilder.ElementIsValid(i);
    if (found) break;
    else myBlockIterator.Next();
  }
}

//=======================================================================
//function : InitEdge
//purpose  : 
//=======================================================================
Standard_Integer TopOpeBRepBuild_FaceBuilder::InitEdge()
{
  const Handle(TopOpeBRepBuild_Loop)& L = myFaceAreaBuilder.Loop();
  if ( L->IsShape() )
    Standard_DomainError::Raise("TopOpeBRepBuild_FaceBuilder:InitEdge");
  else {
    myBlockIterator = L->BlockIterator();
    myBlockIterator.Initialize();
    FindNextValidElement();
  }
  Standard_Integer n = myBlockIterator.Extent();
  return n;
}

//=======================================================================
//function : MoreEdge
//purpose  : 
//=======================================================================
Standard_Boolean TopOpeBRepBuild_FaceBuilder::MoreEdge() const
{
  Standard_Boolean b = myBlockIterator.More();
  return b;
}

//=======================================================================
//function : NextEdge
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::NextEdge()
{
  myBlockIterator.Next();
  FindNextValidElement();
}

//=======================================================================
//function : Edge
//purpose  : 
//=======================================================================
const TopoDS_Shape& TopOpeBRepBuild_FaceBuilder::Edge() const
{
  if (!myBlockIterator.More()) Standard_Failure::Raise("OutOfRange");

  const Standard_Integer i = myBlockIterator.Value();
  Standard_Boolean isvalid = myBlockBuilder.ElementIsValid(i);
  if (!isvalid) Standard_Failure::Raise("Edge not Valid");

  const TopoDS_Shape& E = myBlockBuilder.Element(i);
  return E;
}

//=======================================================================
//function : EdgeConnexity
//purpose  : 
//=======================================================================
Standard_Integer TopOpeBRepBuild_FaceBuilder::EdgeConnexity(const TopoDS_Shape& /*E*/) const
{
#ifdef DEB
  Standard_ProgramError::Raise("FaceBuilder::EdgeConnexity management disactivated");
#endif
  return 0;
//  Standard_Boolean inmosi = myMOSI.IsBound(E);
//  Standard_Integer nmosi = (inmosi) ? myMOSI.Find(E) : 0;
//  return nmosi;
}

//=======================================================================
//function : AddEdgeWire
//purpose  : 
//=======================================================================
Standard_Integer TopOpeBRepBuild_FaceBuilder::AddEdgeWire(const TopoDS_Shape& E,TopoDS_Shape& W) const
{
  Standard_Integer nadd = 0;
  BRep_Builder BB;
  BB.Add(W,E);nadd++;
//  Standard_Integer nmosi = EdgeConnexity(E);
//  Standard_Boolean addEC = (nmosi == 1);
//  if (addEC) {
//    TopAbs_Orientation oe = E.Orientation();
//    TopAbs_Orientation oc = TopAbs::Complement(oe);
//    TopoDS_Shape EC = E.Oriented(oc);
//    BB.Add(W,EC);nadd++;
//  }
  return nadd;
}

//=======================================================================
//function : MakeLoops
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_FaceBuilder::MakeLoops(TopOpeBRepBuild_ShapeSet& SS)
{
  TopOpeBRepBuild_BlockBuilder& BB = myBlockBuilder;
  TopOpeBRepBuild_ListOfLoop& LL = myLoopSet.ChangeListOfLoop();

  // Build blocks on elements of SS
  BB.MakeBlock(SS);

  // make list of loop (LL) of the LoopSet
  // - on shapes of the ShapeSet (SS)
  // - on blocks of the BlockBuilder (BB)

  // Add shapes of SS as shape loops
  LL.Clear();
  for(SS.InitShapes();SS.MoreShapes();SS.NextShape()) {
    const TopoDS_Shape& S = SS.Shape();
    Handle(TopOpeBRepBuild_Loop) ShapeLoop = new TopOpeBRepBuild_Loop(S);
    LL.Append(ShapeLoop);
  }
  
  // Add blocks of BB as block loops
  for (BB.InitBlock();BB.MoreBlock();BB.NextBlock()) {
    TopOpeBRepBuild_BlockIterator BI = BB.BlockIterator();
    Handle(TopOpeBRepBuild_Loop) BlockLoop = new TopOpeBRepBuild_Loop(BI);
    LL.Append(BlockLoop);
  }

}