summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepBuild/TopOpeBRepBuild_CorrectFace2d.cxx
blob: 650cc72be954425eb2b3bb2b0b754d69aaea0ad0 (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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
// File:	TopOpeBRepBuild_CorrectFace2d.cxx
// Created:	Wed Jan 26 10:04:31 2000
// Author:	Peter KURNEV
//		<pkv@irinox.nnov.matra-dtv.fr>


#include <TopOpeBRepBuild_CorrectFace2d.ixx>

#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Edge.hxx>

#include <TopExp.hxx>

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

#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeShape.hxx>

#include <Geom2d_TrimmedCurve.hxx>
#include <Geom_Surface.hxx>
#include <Geom2d_Curve.hxx>

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

#include <Bnd_Box2d.hxx>
#include <BndLib_Add2dCurve.hxx>

#include <gp_Pnt.hxx>
#include <Precision.hxx>
#include <BRepAdaptor_Surface.hxx>


//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::TopOpeBRepBuild_CorrectFace2d
// purpose: 
//=======================================================================
  TopOpeBRepBuild_CorrectFace2d::TopOpeBRepBuild_CorrectFace2d()
{
  myIsDone=Standard_False;
  myErrorStatus=1;
  
}
//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::TopOpeBRepBuild_CorrectFace2d
// purpose: 
//=======================================================================
  TopOpeBRepBuild_CorrectFace2d::TopOpeBRepBuild_CorrectFace2d(const TopoDS_Face& aFace,
							     const TopTools_IndexedMapOfOrientedShape& anAvoidMap,
							     TopTools_IndexedDataMapOfShapeShape& aMap)
{
  myFace=aFace;
  myAvoidMap=anAvoidMap;
  myIsDone=Standard_False;
  myErrorStatus=1;
  myMap=(Standard_Address) &aMap;
  
}
//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::SetMapOfTrans2dInfo
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::SetMapOfTrans2dInfo (TopTools_IndexedDataMapOfShapeShape& aMap)
{ 
  myMap=(Standard_Address) &aMap;
}

//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::MapOfTrans2dInfo
// purpose: 
//=======================================================================
  TopTools_IndexedDataMapOfShapeShape& TopOpeBRepBuild_CorrectFace2d::MapOfTrans2dInfo () 
{
  return *(TopTools_IndexedDataMapOfShapeShape*) myMap;
}
//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::Face
// purpose: 
//=======================================================================
  const TopoDS_Face& TopOpeBRepBuild_CorrectFace2d::Face() const
{
  return myFace;
}

//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::IsDone
// purpose: 
//=======================================================================
  Standard_Boolean TopOpeBRepBuild_CorrectFace2d::IsDone() const
{
  return myIsDone;
}
//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::ErrorStatus
// purpose: 
//=======================================================================
  Standard_Integer TopOpeBRepBuild_CorrectFace2d::ErrorStatus() const
{
  return myErrorStatus;
}
//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::CorrectedFace
// purpose: 
//=======================================================================
  const TopoDS_Face& TopOpeBRepBuild_CorrectFace2d::CorrectedFace() const
{
  return myCorrectedFace;
}
//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::BuildCopyData
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::BuildCopyData(const TopoDS_Face& aFace,
						   const TopTools_IndexedMapOfOrientedShape& anAvoidMap,
						   TopoDS_Face& aCopyFace,
						   TopTools_IndexedMapOfOrientedShape& aCopyAvoidMap,
						   const Standard_Boolean aNeedToUsePMap) 
{
  TopTools_IndexedDataMapOfShapeShape EdMap;

  BRep_Builder BB;

  TopTools_IndexedDataMapOfShapeShape* pMap= 
      (TopTools_IndexedDataMapOfShapeShape*) myMap;
  //
  // 1. Copy myFace =>> myCopyFace
  TopoDS_Shape aLocalShape = aFace.EmptyCopied();
  aCopyFace=TopoDS::Face(aLocalShape);
  //  aCopyFace=TopoDS::Face(aFace.EmptyCopied());

  TopExp_Explorer anExpWires(aFace, TopAbs_WIRE);
  for (; anExpWires.More(); anExpWires.Next()) {
    const TopoDS_Wire& W=TopoDS::Wire(anExpWires.Current());
    
    aLocalShape = W.EmptyCopied();
    TopoDS_Wire aWire = TopoDS::Wire(aLocalShape);
    //    TopoDS_Wire aWire = TopoDS::Wire(W.EmptyCopied());
    
    TopExp_Explorer anExpEdges(W, TopAbs_EDGE);
    for (; anExpEdges.More(); anExpEdges.Next()) {
      const TopoDS_Edge& E = TopoDS::Edge(anExpEdges.Current());
    
      if (EdMap.Contains(E)) {
	TopoDS_Shape anEdge = EdMap.ChangeFromKey(E);
	anEdge.Orientation(E.Orientation());
	BB.Add (aWire, anEdge); 
	continue;
      }

      if (aNeedToUsePMap) {
	if (pMap->Contains(E)) {
	  TopoDS_Shape& anEdge=pMap->ChangeFromKey(E);
	  anEdge.Orientation(E.Orientation());
	  EdMap.Add(E, anEdge);
	  BB.Add (aWire, anEdge);
	  continue;
	}
      }

      // add edges
      aLocalShape = E.EmptyCopied();
      TopoDS_Shape anEdge = TopoDS::Edge(aLocalShape);
      //      TopoDS_Shape anEdge = TopoDS::Edge(E.EmptyCopied());
      
      EdMap.Add(E, anEdge);

      TopExp_Explorer anExpVertices(E, TopAbs_VERTEX);
      for (; anExpVertices.More(); anExpVertices.Next()) {
	const TopoDS_Shape& aV=anExpVertices.Current();
	BB.Add(anEdge, aV);
      }

      anEdge.Orientation(E.Orientation());
      BB.Add (aWire, anEdge);
    }
    // Add wires
    aWire.Orientation(W.Orientation());
    EdMap.Add(W, aWire);

    BB.Add (aCopyFace, aWire);
  }

  //
  // 2. Copy myAvoidMap =>> myCopyAvoidMap
  Standard_Integer i, aNb;
  aNb=anAvoidMap.Extent();

  for (i=1; i<=aNb; i++) {
    const TopoDS_Shape& aSh=anAvoidMap(i);
    
    if (EdMap.Contains (aSh)) {
      TopoDS_Shape& aCopyShape=EdMap.ChangeFromKey(aSh);
      aCopyShape.Orientation(aSh.Orientation());
      aCopyAvoidMap.Add(aCopyShape);
    }
  }
  
  //
  // 3. Inversed EdMap
  if (aNeedToUsePMap) {
    aNb=EdMap.Extent();
    myEdMapInversed.Clear();
    for (i=1; i<=aNb; i++) {
      const TopoDS_Shape& aSh    =EdMap.FindKey(i);
      const TopoDS_Shape& aShCopy=EdMap.FindFromIndex(i);
      myEdMapInversed.Add (aShCopy, aSh);
    }
  }
}
//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::CheckFace
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::CheckFace() 
{
  //
  // I. Is the surface periodic
  TopLoc_Location aLocF;
  Handle(Geom_Surface) Surf = BRep_Tool::Surface(myCopyFace, aLocF);
  if (!(Surf->IsUPeriodic() || Surf->IsVPeriodic())) {
    myIsDone=Standard_True;
    myErrorStatus=4;
    return;
  }

  //modified by NIZHNY-MZV  Mon Apr 24 11:29:56 2000
  //don't treat torus surfaces
  if(Surf->IsUPeriodic() && Surf->IsVPeriodic()) {
    myIsDone=Standard_True;
    myErrorStatus=4;
    return;
  }

  // 
  // II. Has each wire at least one "licit" edge
  TopTools_IndexedMapOfOrientedShape aVoidWires;
  Standard_Integer i, aNbVoidWires, anEdgeExists=0, priz;
  TopExp_Explorer aFExp,aWExp;
  aFExp.Init (myCopyFace, TopAbs_WIRE);
  for (; aFExp.More(); aFExp.Next()) {
    const TopoDS_Shape& aWire=aFExp.Current();
    if (myCopyAvoidMap.Contains(aWire)) {
      anEdgeExists=1;
      continue;
    }

    anEdgeExists=0;  
    aWExp.Init(aWire, TopAbs_EDGE);
    for (; aWExp.More(); aWExp.Next()) {
      TopoDS_Shape anEdge=aWExp.Current();
      if (myCopyAvoidMap.Contains(anEdge)) {
	anEdgeExists=1;
	break;
      }
    }

    if (!anEdgeExists) {
      // This is the wire without any "Right" information
      aVoidWires.Add(aWire);
    }
  }

  // For Void Wires . 
  // We assume the first edge from the wire is non-movable edge
  // PKV 24-Feb-2000
  aNbVoidWires=aVoidWires.Extent();
  for (i=1; i<=aNbVoidWires; i++) {
    TopTools_IndexedMapOfShape aEM;
    TopExp::MapShapes(aVoidWires(i), TopAbs_EDGE, aEM);
    if (aEM.Extent())
      myCopyAvoidMap.Add (aEM(1));
  }

  // III. Check all wires to know  whether they are closed or not 
  aFExp.Init (myCopyFace, TopAbs_WIRE);
  for (; aFExp.More(); aFExp.Next()) {
    myCurrentWire = TopoDS::Wire(aFExp.Current());
    priz=MakeRightWire ();
    if (priz) {
      // This myFace contais a wire (myCurrentWire) that is not closed.
      myIsDone=Standard_True;
      myErrorStatus=3; 
      return;
    }
  }
  
  //
  // VI. Check connectability wires in 2d
  TopoDS_Face aCopyFace;
  TopTools_IndexedMapOfOrientedShape aCopyAvoidMap;
  
  // Coping data 
  BuildCopyData(myCopyFace, myCopyAvoidMap, aCopyFace, aCopyAvoidMap, Standard_False);
  
  aFExp.Init (aCopyFace, TopAbs_WIRE);
  for (; aFExp.More(); aFExp.Next()) {
    myCurrentWire = TopoDS::Wire(aFExp.Current());
    if (!aCopyAvoidMap.Contains(myCurrentWire)) {
      priz=ConnectWire (aCopyFace, aCopyAvoidMap, Standard_True);
      if (priz) {
	myIsDone=Standard_True;
	myErrorStatus=6; 
	return;
      }
    }
  }

  // Face seems to be OK
}
//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::ConnectWire
// purpose: 
//=======================================================================
  Standard_Integer TopOpeBRepBuild_CorrectFace2d::ConnectWire (TopoDS_Face& aCopyFace,
							      const TopTools_IndexedMapOfOrientedShape& aCopyAvoidMap,
							      const Standard_Boolean aTryFlag)
{
  BRep_Builder BB;
  //
  // 1.Make right order
  Standard_Integer priz;
  priz=MakeRightWire ();
  if (priz) return priz;
  //
  // 2. Define the First Edge on the Wire from aCopyAvoidMap
  Standard_Integer i, aNbEdges=0, aNbAvoidEdgesOnWire;
  Standard_Real aDMax=0, aR;

  TopExp_Explorer aWExp; 
  TopoDS_Edge  aFEdge;
  TopoDS_Shape aFirstEdge;
  TopTools_IndexedMapOfOrientedShape anAvoidMap;
  
  aWExp.Init(myCurrentWire, TopAbs_EDGE);
  for (; aWExp.More(); aWExp.Next(), aNbEdges++) {
    TopoDS_Shape anEdge=aWExp.Current(); 
    if (aCopyAvoidMap.Contains(anEdge))
      anAvoidMap.Add(anEdge);
  }

  if (aNbEdges==1)                    // nothing to do with this wire.
    return 0; 

  aNbAvoidEdgesOnWire=anAvoidMap.Extent();
  if (aNbAvoidEdgesOnWire==aNbEdges) // nothing to do. all edges on wire are good.
    return 0; 

  // if  at least one  non-degenerated edge exists 
  // among anAvoidMap set it as aFirstEdge
  priz=0;
  for (i=1; i<=aNbAvoidEdgesOnWire; i++) {
    const TopoDS_Edge& anEdge=TopoDS::Edge(anAvoidMap(i));
    if (!BRep_Tool::Degenerated(anEdge)) {
      aFirstEdge=anEdge;
      priz=1;
      break;
    }
  }

  
  if (!priz) {
    // All of anAvoidMap edges are degenerated
    // So take the edge with max dist. between 
    //First and Last 2d points as the First edge
    //if(aNbAvoidEdgesOnWire != 1)
     // return 1; //in case of several degenerated edges we cannot connect wire by right way
    for (i=1; i<=aNbAvoidEdgesOnWire; i++) {
      gp_Pnt2d PF, PL;
      const TopoDS_Edge& aDegenEdge=TopoDS::Edge(anAvoidMap(i));
      GetP2dFL(aCopyFace, aDegenEdge, PF, PL);
      aR=PF.Distance(PL);
      if ((aR - aDMax) > 1e-7) {
	aDMax=aR;
	aFirstEdge=anAvoidMap(i);
      }
    }
  }

  
  
  //
  // 3. Build HeadList (from myOrderedWireList), where aFirstEdge will be the first
  TopTools_ListOfShape HeadList;
  MakeHeadList(aFirstEdge, HeadList);
  //
  // 4. Check HeadList to provide toward point-arrow direction
//modified by NIZNHY-PKV Mon Apr 24 14:43:20 2000 f
  //CheckList (HeadList);
  //modified by NIZNHY-PKV Tue Apr 25 12:08:29 2000CheckList (aCopyFace, HeadList);
//modified by NIZNHY-PKV Mon Apr 24 14:43:24 2000t
  //
  // 5. Connect Edges
  TopTools_IndexedMapOfShape anEdMap;
  gp_Pnt2d PF, PL, PA, PB, PA1, PB1;
  Handle(Geom2d_Curve) aTrCF, aTrCR, aTrC;
  Standard_Real aD, aDTolerance=Precision::Confusion();

  TopTools_SequenceOfShape aSeqEdges;
  
  TopTools_ListIteratorOfListOfShape anIt;
  anIt.Initialize(HeadList);
  for (; anIt.More(); anIt.Next()) 
    aSeqEdges.Append(anIt.Value());
  aNbEdges=aSeqEdges.Length();

  //
  // First Edge
  TopoDS_Edge aFstEdge=TopoDS::Edge(aSeqEdges(1));
  GetP2dFL(aCopyFace, aFstEdge, PA, PB);
  PA1=PA; 
  PB1=PB;

  for (i=2; i<=aNbEdges; i++) {
    TopoDS_Edge anEdge= TopoDS::Edge(aSeqEdges(i));
    GetP2dFL(aCopyFace, anEdge, PF, PL);
    
    aD=PF.Distance(PB);
    if (aD<aDTolerance)  {
      PA=PF; PB=PL;
      continue; // nothing to do with the edge cose it already connected
    }
    
    // tralslation's  vector
    gp_Vec2d aTrV(PF, PB);

    //Begin modified by NIZHNY-MZV  Mon Mar 27 16:04:04 2000
    //translation vector must be equal to 2PI*n or it is wrong wire
    Standard_Real U = aTrV.X();
    Standard_Real V = aTrV.Y();

    BRepAdaptor_Surface BAS(aCopyFace);
    Standard_Boolean UP = BAS.IsUPeriodic();
    Standard_Boolean VP = BAS.IsVPeriodic();

    Standard_Boolean nonPU = (fabs(U) < 1e-7) ? Standard_True : Standard_False;
    Standard_Boolean nonPV = (fabs(V) < 1e-7) ? Standard_True : Standard_False;

    if(!nonPU && UP) {
      Standard_Real dU = fmod(fabs(U), 2*PI);
      nonPU = (dU > 1e-7 && (2*PI - dU > 1e-7)) ? Standard_True : Standard_False;
    }
    
    if(!nonPV && VP) {
      Standard_Real dV = fmod(fabs(V), 2*PI);
      nonPV = (dV > 1e-7 && (2*PI - dV > 1e-7)) ? Standard_True : Standard_False;
    }

//    printf("(fmod(fabs(U), 2*PI) =%lf\n", (fmod(fabs(U), 2*PI)));
//    printf(" (fmod(fabs(V), 2*PI) > 1e-7)=%lf\n", (fmod(fabs(V), 2*PI)));
    
    if(nonPU && nonPV && !BRep_Tool::Degenerated(anEdge))
      return 1;
    //End modified by NIZHNY-MZV  Mon Mar 27 16:04:11 2000

    if (BRep_Tool::IsClosed(anEdge, aCopyFace)) {
      // a. Closed edge <--->
      if (anEdMap.Contains(anEdge)) continue;
      anEdMap.Add(anEdge); 
      
      TopAbs_Orientation anOri = anEdge.Orientation();
      
      TopoDS_Edge anEF, anER;
      if (anOri==TopAbs_FORWARD) {
	anEF=anEdge;
	TopoDS_Shape aLocalShape = anEdge.Reversed();
	anER=TopoDS::Edge(aLocalShape);
	//	anER=TopoDS::Edge(anEdge.Reversed());
      }
      
      else {
	anER=anEdge;
	TopoDS_Shape aLocalShape = anEdge.Reversed();
	anEF=TopoDS::Edge(aLocalShape);
	//	anEF=TopoDS::Edge(anEdge.Reversed());
      }
      
      TranslateCurve2d (anEF, aCopyFace, aTrV, aTrCF);
      TranslateCurve2d (anER, aCopyFace, aTrV, aTrCR);

      if (aTryFlag) // Use Builder in a trying case
	BB.UpdateEdge(anEdge, aTrCF, aTrCR, aCopyFace, myFaceTolerance);

      else          // Use "False-Builder" otherwise
	UpdateEdge(anEdge, aTrCF, aTrCR, aCopyFace, myFaceTolerance);

    }
    
    else {
      // b. Usual Edge
      TranslateCurve2d (anEdge, aCopyFace, aTrV, aTrC);
      
      if (aTryFlag) 
	BB.UpdateEdge(anEdge, aTrC, aCopyFace, myFaceTolerance);
      else 
	UpdateEdge(anEdge, aTrC, aCopyFace, myFaceTolerance);
    }
    
    GetP2dFL(aCopyFace, anEdge, PF, PL);
    
    PA=PF;  
    PB=PL;

    ////////////////////////////////////////////
    // In case of a trying we check the first 
    // and last 2d point of the contour  
    if (aTryFlag) {
      if (i==aNbEdges) {
	aD=PA1.Distance(PB);
	if (aD>aDTolerance)
	  return 1;
      }
    }
    ////////////////////////////////////////////
    
  } //end of for (i=2; i<=aNbEdges; i++)
  
  return 0;
}


//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::Perform
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::Perform() 
{
  /////////////
  // 0. 
  myCopyAvoidMap.Clear();
  BuildCopyData(myFace, myAvoidMap, myCopyFace, myCopyAvoidMap, Standard_True);
  /////////////

  myCorrectedFace=myCopyFace;
  myCorrectedFace.Orientation(myCopyFace.Orientation());
  myFaceTolerance=BRep_Tool::Tolerance(myCopyFace);
  //
  // 1. Check the input face first
  CheckFace ();
  if (myIsDone)
    return ;

  //
  // 2. Make all wires connected 
  Standard_Integer  priz;
  TopExp_Explorer aFExp;

  aFExp.Init (myCopyFace, TopAbs_WIRE);
  for (; aFExp.More(); aFExp.Next()) {
    myCurrentWire = TopoDS::Wire(aFExp.Current());
    if (!myCopyAvoidMap.Contains(myCurrentWire)) {  
      priz=ConnectWire (myCopyFace, myCopyAvoidMap, Standard_False);
      if (priz) {
	// This myFace contais a wire (myCurrentWire) that is not closed.
	myIsDone=Standard_False;
	myErrorStatus=3; 
	return;
      }
    }
  }
  // 
  // 3. Define Outer Wire
  TopoDS_Wire anOuterWire;
  priz=OuterWire (anOuterWire);
  if (priz) {
    myIsDone=Standard_False;
    myErrorStatus=5; // can't find outer wire 
    return;
  }
  //
  // 4. Moving the anOuterWire and other wires in 2d space
  MoveWires2d(anOuterWire);

  myIsDone=Standard_True;
  myErrorStatus=0; 
}

//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::MakeRightWire
// purpose: 
//=======================================================================
  Standard_Integer TopOpeBRepBuild_CorrectFace2d::MakeRightWire ()
{
  Standard_Integer aNbEdgesReally=0;
  myOrderedWireList.Clear();
 
  //1. Real amount of the edges on aWire
  TopExp_Explorer aWExp;
  aWExp.Init(myCurrentWire, TopAbs_EDGE);
  for (; aWExp.More(); aWExp.Next()) aNbEdgesReally++;
  
  //2. We'll use TopOpeBRepBuild_Tools::Path
  TopTools_ListOfShape aL;
  TopOpeBRepBuild_Tools2d::Path (myCurrentWire, aL);
  if (aL.Extent()!=aNbEdgesReally) {
    myErrorStatus=4;
    return 1;
  }

  //Begin modified by NIZNHY-PKV Tue Apr 25 12:04:45 2000
  //from path we obtained list in reverse order, so to have right wire
  //we need to reverse it
  TopTools_ListOfShape aFL;
  TopTools_ListIteratorOfListOfShape lit(aL);
  for(; lit.More(); lit.Next())
    aFL.Prepend(lit.Value());

  myOrderedWireList=aFL;
  //End modified by NIZNHY-PKV Tue Apr 25 12:06:45 2000
  return 0;
}

//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::GetP2dFL
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::GetP2dFL (const TopoDS_Face& aF, const TopoDS_Edge& anEdge, 
					       gp_Pnt2d& P2dF, gp_Pnt2d& P2dL)
{
  Standard_Real aFirst, aLast;
  const Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (anEdge, aF, aFirst, aLast);
  C2d->D0 (aFirst, P2dF);
  C2d->D0 (aLast , P2dL);
  if (anEdge.Orientation()==TopAbs_REVERSED) {
    gp_Pnt2d P2dTmp;
    P2dTmp=P2dF; P2dF=P2dL; P2dL=P2dTmp;
  }
} 


//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::MakeHeadList
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::MakeHeadList(const TopoDS_Shape& aFirstEdge,
						  TopTools_ListOfShape& HeadList) const
{
  TopoDS_Shape aFE = aFirstEdge;
  TopTools_ListOfShape aTailList;
  TopTools_ListIteratorOfListOfShape anIt;
  Standard_Integer aFlag=0;

  anIt.Initialize(myOrderedWireList);
  for (; anIt.More(); anIt.Next()) {
    const TopoDS_Shape& anEdge=anIt.Value();
    //modified by NIZHNY-MZV  Mon Mar 27 11:40:00 2000
    if(aFE.IsNull() && !BRep_Tool::Degenerated(TopoDS::Edge(anEdge)))
      aFE = anEdge;
    if (anEdge==aFE) aFlag=1; //turn the switch ON
    if (aFlag) HeadList.Append(anEdge);
  }
  
  anIt.Initialize(myOrderedWireList);
  for (; anIt.More(); anIt.Next()) {
    const TopoDS_Shape& anEdge=anIt.Value();
    if (anEdge==aFE) break;
    aTailList.Append(anEdge);
  }
  HeadList.Append(aTailList);
}
//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::CheckList
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::CheckList (const TopoDS_Face& aFace,
						 TopTools_ListOfShape&  HeadList)
{
  TopAbs_Orientation r1;
  Standard_Real aDTolerance=Precision::Confusion();
  TopTools_SequenceOfShape aSeq;
  TopTools_ListIteratorOfListOfShape anIt(HeadList);
  for (; anIt.More(); anIt.Next()) {
    aSeq.Append(anIt.Value());
  }

  r1=aSeq(1).Orientation();
  Standard_Integer i, aNb=aSeq.Length();
//modified by NIZNHY-PKV Mon Apr 24 14:43:57 2000f
  Standard_Boolean aFirstCheck=Standard_False;
  if (r1==TopAbs_REVERSED ) {
    // when the orientation of the first edge is Revesed, 
    // and when everything in 2d is Ok :
    // if at least one Forward edge  exists in the List 
    // we consider that no necessity to reverse 
    // the list (aFirstCheck=Standard_True) .
    
    Standard_Real aD;
    gp_Pnt2d PF, PL, PA, PB;
    
    
    TopoDS_Edge aFstEdge=TopoDS::Edge(aSeq(1));
    GetP2dFL(aFace, aFstEdge, PA, PB);
    for (i=2; i<=aNb; i++) {
      TopoDS_Edge anEdge= TopoDS::Edge(aSeq(i));
      GetP2dFL(aFace, anEdge, PF, PL);
      
      aD=PF.Distance(PB);
      if (aD<aDTolerance)  {
	PA=PF; PB=PL;
	if (anEdge.Orientation()==TopAbs_FORWARD) {
	  aFirstCheck=Standard_True;
	  break;
	}
      }
    }
  }
//modified by NIZNHY-PKV Mon Apr 24 14:43:59 2000t

  r1=aSeq(1).Orientation();

  TopoDS_Vertex aV1R, aV2F;
  
  TopoDS_Edge aFirstEdge=TopoDS::Edge(aSeq(1));
  TopExp_Explorer anExp;
  anExp.Init (aFirstEdge, TopAbs_VERTEX);
  for (; anExp.More(); anExp.Next()) {
    const TopoDS_Shape& aV1=anExp.Current();
    if (aV1.Orientation()==TopAbs_REVERSED){
      aV1R=TopoDS::Vertex(aV1);
      break;
    }
  }

  TopoDS_Edge aSecondEdge=TopoDS::Edge(aSeq(2));
  anExp.Init (aSecondEdge, TopAbs_VERTEX);
  for (; anExp.More(); anExp.Next()) {
    const TopoDS_Shape& aV1=anExp.Current();
    if (aV1.Orientation()==TopAbs_FORWARD){
      aV2F=TopoDS::Vertex(aV1);
      break;
    }
  }
  
  gp_Pnt P1, P2;
  P1=BRep_Tool::Pnt(aV1R);
  P2=BRep_Tool::Pnt(aV2F);

  
  //modified by NIZNHY-PKV Mon Apr 24 13:24:06 2000 f
  Standard_Real dist=P1.Distance(P2);
  if (
      (!(dist < aDTolerance) && r1==TopAbs_FORWARD)||
      //(r1==TopAbs_REVERSED)) { 
     (!aFirstCheck && r1==TopAbs_REVERSED)
      ) {
    //modified by NIZNHY-PKV Mon Apr 24 14:28:06 2000 t
    // We have to reverse the order in list 
    aSeq.Append(aFirstEdge);
    HeadList.Clear();
    aNb=aSeq.Length();
    for (i=aNb; i>1; i--) { 
      HeadList.Append(aSeq(i));
    }
  }
}

//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::TranslateCurve2d
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::TranslateCurve2d(const TopoDS_Edge& anE,
						      const TopoDS_Face& aFace, 
						      const gp_Vec2d& aTrV, 
						      Handle(Geom2d_Curve)& aC2DOut)
{
  Standard_Real aFirst, aLast;
  Handle(Geom2d_Curve) C2d=BRep_Tool::CurveOnSurface (anE, aFace, aFirst, aLast);
  Handle(Geom2d_Curve) aTrC;
  aTrC = Handle(Geom2d_Curve)::DownCast(C2d->Copy());
  Handle(Geom2d_TrimmedCurve) newC2D = new Geom2d_TrimmedCurve(aTrC, aFirst, aLast);
  newC2D -> Translate(aTrV);
  aC2DOut=newC2D;
}
//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::OuterWire
// purpose: 
//=======================================================================
  Standard_Integer TopOpeBRepBuild_CorrectFace2d::OuterWire(TopoDS_Wire& anOuterWire) const
{
  TopLoc_Location Loc;
  Handle(Geom_Surface) Surf = BRep_Tool::Surface(TopoDS::Face(myCorrectedFace), Loc);

  TopExp_Explorer ex(myCorrectedFace, TopAbs_WIRE);
  for (; ex.More(); ex.Next()) {
    const TopoDS_Wire& aWire=TopoDS::Wire(ex.Current());

    TopoDS_Face newFace;
    BRep_Builder BB;
    BB.MakeFace(newFace, Surf, Loc, myFaceTolerance);
    BB.Add(newFace, aWire);
    
    BRepTopAdaptor_FClass2d aClass2d(newFace, myFaceTolerance);
    TopAbs_State aState=aClass2d.PerformInfinitePoint();
    if (aState==TopAbs_OUT) {
      anOuterWire=aWire;
      return 0;
    }
  }
  return 1; // such wire is not found
}

//=======================================================================
// function :TopOpeBRepBuild_CorrectFace2d::BndBoxWire
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::BndBoxWire(const TopoDS_Wire& aWire,
						Bnd_Box2d& B2d) const
{
  Bnd_Box2d aB2d;
  TopExp_Explorer aWEx (aWire, TopAbs_EDGE);
  for (; aWEx.More(); aWEx.Next()) {
    const TopoDS_Edge& anEdge = TopoDS::Edge(aWEx.Current());
    Standard_Real aTolE = BRep_Tool::Tolerance(anEdge); 
    BRepAdaptor_Curve2d aBAC2d (anEdge,myCorrectedFace);
    BndLib_Add2dCurve::Add(aBAC2d, aTolE, aB2d);
  }
  B2d=aB2d;
}

//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::MoveWire2d
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::MoveWire2d (TopoDS_Wire& aWire, 
						 const gp_Vec2d& aTrV)
{
  if (aTrV.Magnitude()<Precision::Confusion()) 
    return;
  
  Standard_Integer i, aNbEdges;
  TopTools_SequenceOfShape aSeqEdges;
  TopTools_IndexedMapOfShape anEdMap;
  Handle(Geom2d_Curve) aTrCF, aTrCR, aTrC;
  
  TopExp_Explorer aWExp;

  aWExp.Init(aWire, TopAbs_EDGE);
  for (; aWExp.More(); aWExp.Next()) {
    aSeqEdges.Append(aWExp.Current());
  }
  aNbEdges=aSeqEdges.Length();

  //
  // First Edge
  for (i=1; i<=aNbEdges; i++) {
    TopoDS_Edge anEdge= TopoDS::Edge(aSeqEdges(i));
    if (BRep_Tool::IsClosed(anEdge, myCorrectedFace)) {
      // a. Closed edge <--->
      if (anEdMap.Contains(anEdge)) continue;
      anEdMap.Add(anEdge); 
      
      TopAbs_Orientation anOri = anEdge.Orientation();
      
      TopoDS_Edge anEF, anER;

      if (anOri==TopAbs_FORWARD) {
	anEF=anEdge;
	TopoDS_Shape aLocalShape = anEdge.Reversed();
	anER=TopoDS::Edge(aLocalShape);
	//	anER=TopoDS::Edge(anEdge.Reversed());
      }
      
      else {
	anER=anEdge;
	TopoDS_Shape aLocalShape = anEdge.Reversed();
	anEF=TopoDS::Edge(aLocalShape);
	//	anEF=TopoDS::Edge(anEdge.Reversed());
      }
      
      TranslateCurve2d (anEF, myCorrectedFace, aTrV, aTrCF);
      TranslateCurve2d (anER, myCorrectedFace, aTrV, aTrCR);
      UpdateEdge(anEdge, aTrCF, aTrCR, myCorrectedFace, myFaceTolerance);
    }
    
    else {
      // b. Usual Edge
      TranslateCurve2d (anEdge, myCorrectedFace, aTrV, aTrC);
      UpdateEdge(anEdge, aTrC, myCorrectedFace, myFaceTolerance);
    }
  } //end of for (i=1; i<=aNbEdges; i++)
}
//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::MoveWires2d
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::MoveWires2d (TopoDS_Wire& anOuterWire)
{ 
  Bnd_Box2d B2d, B2dOuterWire;
  Standard_Real OuterU1,OuterV1,OuterU2,OuterV2, x, a, TwoPI;
  Standard_Integer b, k;
  gp_Vec2d aTrV;

  TwoPI=2.*PI;

  BndBoxWire(anOuterWire, B2dOuterWire);
  B2dOuterWire.Get(OuterU1,OuterV1,OuterU2,OuterV2);
  
  a=.5*(OuterU1+OuterU2);
  b=Standard_Integer(-a/TwoPI);
  k=(a<0.)? 1 : 0;
  x=(b+k)*TwoPI;
  //
  // 1. Move the Outer Wire to [0, 2*PI]
  aTrV.SetCoord (x, 0.);
  MoveWire2d (anOuterWire, aTrV);
 
  BndBoxWire(anOuterWire, B2dOuterWire);
  B2dOuterWire.Get(OuterU1,OuterV1,OuterU2,OuterV2);
  //
  // 2. Move all other wires into bounding box of the Outer Wire
  TopExp_Explorer aFExp;
  aFExp.Init (myCorrectedFace, TopAbs_WIRE);
  for (; aFExp.More(); aFExp.Next()) {
    myCurrentWire = TopoDS::Wire(aFExp.Current());
    if (myCurrentWire!=anOuterWire) {
      BndBoxWire(myCurrentWire, B2d);
      Standard_Real u1,v1,u2,v2;
      B2d.Get(u1,v1,u2,v2);
      
      if (B2d.IsOut(B2dOuterWire)) {
	//printf(" Need to Move\n" );
	a=u1-OuterU1;
	b=Standard_Integer(-a/TwoPI);
	k= (a<0.) ? 1 : 0;
	x=(b+k)*TwoPI;
	aTrV.SetCoord (x, 0.);
	MoveWire2d (myCurrentWire, aTrV);
      }
    }
  }
}
//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::UpdateEdge
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::UpdateEdge (const TopoDS_Edge& ECopy,
						 const Handle(Geom2d_Curve)& C,
						 const TopoDS_Face& F,
						 const Standard_Real Tol) 
{
  BRep_Builder BB;
  
  TopTools_IndexedDataMapOfShapeShape* pMap= 
    (TopTools_IndexedDataMapOfShapeShape*) myMap;
  
  // E is the Original Edge from Original Face.
  if (myEdMapInversed.Contains(ECopy)) {
    const TopoDS_Shape& E=myEdMapInversed.FindFromKey(ECopy);

    if (!pMap->Contains(E)) { 
      TopExp_Explorer anExp;
      
      TopoDS_Shape anEdge=E.EmptyCopied();
      
      anExp.Init(E, TopAbs_VERTEX);
      for (; anExp.More(); anExp.Next()) {
	const TopoDS_Shape& aV=anExp.Current();
	BB.Add(anEdge, aV);
      }
      
      BB.UpdateEdge(TopoDS::Edge(anEdge), C, F, Tol);
      pMap->Add(E, anEdge);
      //printf("pMap->Extent()=%d\n", pMap->Extent());
    }
    
    else {
      TopoDS_Shape& anEdge=pMap->ChangeFromKey(E);
      BB.UpdateEdge(TopoDS::Edge(anEdge), C, F, Tol); 
    }
  }

  BB.UpdateEdge (ECopy, C, F, Tol);
}


//=======================================================================
// function : TopOpeBRepBuild_CorrectFace2d::UpdateEdge
// purpose: 
//=======================================================================
  void TopOpeBRepBuild_CorrectFace2d::UpdateEdge (const TopoDS_Edge& ECopy,
						 const Handle(Geom2d_Curve)& C1,
						 const Handle(Geom2d_Curve)& C2,
						 const TopoDS_Face& F,
						 const Standard_Real Tol) 
{
  BRep_Builder BB;
  
  TopTools_IndexedDataMapOfShapeShape* pMap= 
    (TopTools_IndexedDataMapOfShapeShape*) myMap;
  
  // E is the Original Edge from Original Face.
  if (myEdMapInversed.Contains(ECopy)) {
    const TopoDS_Shape& E=myEdMapInversed.FindFromKey(ECopy);
    
    if (!pMap->Contains(E)) {
      TopoDS_Shape anEdge=E.EmptyCopied();
      TopExp_Explorer anExp;
      anExp.Init(E, TopAbs_VERTEX);
      for (; anExp.More(); anExp.Next()) {
	const TopoDS_Shape& aV=anExp.Current();
	BB.Add(anEdge, aV);
      }
      BB.UpdateEdge(TopoDS::Edge(anEdge), C1, C2, F, Tol);
      pMap->Add(E, anEdge);
      //printf("pMap->Extent()=%d\n", pMap->Extent());
    }
    
    else {
      TopoDS_Shape& anEdge=pMap->ChangeFromKey(E);
      BB.UpdateEdge(TopoDS::Edge(anEdge), C1, C2, F, Tol);
    }
  }
  
  BB.UpdateEdge (ECopy, C1, C2, F, Tol);
}

//
// Description for ErrorStatus
// ErrorStatus=0 : All is done;
// ErrorStatus=1 : Nothing is done;
// ErrorStatus=2 : The wire doesn't contain any licit edges and the wire is not closed;
// ErrorStatus=3 : The face contains a wire that is not connectable
// ErrorStatus=4 : The base surface is not periodic;
// ErrorStatus=5 : an outer wir can not be found;
// ErrorStatus=6 : Wrong  connectability of a wire in 2d