summaryrefslogtreecommitdiff
path: root/src/BRepFill/BRepFill.cxx
blob: 0800066ba3393e511507c5dafdc516bdf454ef19 (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
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
// File:	BRepFill.cxx
// Created:	Thu Mar  3 11:18:14 1994
// Author:	Bruno DUMORTIER
//		<dub@fuegox>
// Modified:	Mon Jan 12 10:50:10 1998
// Author:	Joelle CHAUVET
//		<jct@sgi64>
//              gestion automatique de l'origine et de l'orientation
//              avec la methode Organize
// Modified:	Mon Feb 23 09:28:46 1998
// Author:	Joelle CHAUVET
//		<jct@sgi64>
//              methode Organize avec option de projection pour les wires fermes
//              nouvelle methode SameNumber avec option de report des decoupes
//              + utilitaires ComputeACR et InsertACR
//              + traitement du cas derniere section ponctuelle
// Modified:	Thu Apr 30 15:24:17 1998
// Author:	Joelle CHAUVET
//		<jct@sgi64>
//              separation sections fermees / sections ouvertes + debug 
//              Organize devient ComputeOrigin et SearchOrigin 
// Modified:	Tue Jul 21 16:48:35 1998
// Author:	Joelle CHAUVET
//		<jct@sgi64>
//              cas limite pour Pnext d'ou vrillage (BUC60281) 
// Modified:	Thu Jul 23 11:38:36 1998
// Author:	Joelle CHAUVET
//		<jct@sgi64>
//              calcul de l'angle de la rotation dans SearchOrigin 
// Modified:	Fri Jul 31 15:14:19 1998
// Author:	Joelle CHAUVET
//		<jct@sgi64>
//              IntersectOnWire + MapVLV
// Modified:	Mon Oct 12 09:42:33 1998
// Author:	Joelle CHAUVET
//		<jct@sgi64>
//              numero des aretes dans EdgesFromVertex (CTS21570) 

#include <BRepFill.ixx>

#include <BRepLib.hxx>
#include <BRepLib_FindSurface.hxx>
#include <BRepLib_MakeFace.hxx>
#include <BRepLib_MakeEdge.hxx>
#include <BRepLib_MakeVertex.hxx>
#include <BRepLib_MakeWire.hxx>
#include <BRepExtrema_ExtPC.hxx>
#include <BRepExtrema_DistShapeShape.hxx>
#include <BRep_Tool.hxx>
#include <BRepTools_WireExplorer.hxx>

#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Vertex.hxx>
#include <BRep_Builder.hxx>
#include <TopLoc_Location.hxx>
#include <TopExp_Explorer.hxx>
#include <gp_Vec.hxx>
#include <gp_Lin.hxx>
#include <gp_Pln.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Dir.hxx>
#include <gp_Dir2d.hxx>
#include <gp_Circ.hxx>
#include <gp_Elips.hxx>
#include <Geom_Curve.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_Surface.hxx>
#include <Geom_Plane.hxx>
#include <Geom2d_Line.hxx>
#include <GeomFill_Generator.hxx>
#include <GeomAdaptor_Curve.hxx>
#include <BRepLProp.hxx>
#include <BRepGProp.hxx>
#include <GProp_GProps.hxx>
#include <GProp_PrincipalProps.hxx>
#include <GCPnts_AbscissaPoint.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_DataMapOfShapeListOfShape.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_Array1OfShape.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <TopTools_HSequenceOfShape.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <TopTools_IndexedMapOfShape.hxx>

#include <BRep_Tool.hxx>
#include <TopoDS.hxx>
#include <TopExp.hxx>
#include <Precision.hxx>

#include <TColStd_Array1OfInteger.hxx>
#include <Standard_NoSuchObject.hxx>


static void MakeWire(const TopTools_Array1OfShape& Edges,
		     const Standard_Integer rangdeb,
		     const Standard_Boolean forward,
		     TopoDS_Wire& newwire)
{
  BRep_Builder BW;
  Standard_Integer rang, nbEdges = Edges.Length();
  BW.MakeWire(newwire);
  if (forward) {
    for (rang=rangdeb;rang<=nbEdges;rang++) {
      BW.Add(newwire,TopoDS::Edge(Edges(rang)));
    }
    for (rang=1;rang<rangdeb;rang++) {
      BW.Add(newwire,TopoDS::Edge(Edges(rang)));
    }
  }

  else {
    TopoDS_Edge E;
    for (rang=rangdeb;rang>=1;rang--) {
      E = TopoDS::Edge(Edges(rang));
      BW.Add(newwire,E.Reversed());
    }
    for (rang=nbEdges;rang>rangdeb;rang--) {
      E = TopoDS::Edge(Edges(rang));
      BW.Add(newwire, E.Reversed());
    }
  }
  newwire.Orientation(TopAbs_FORWARD);
}

static void CutEdge(const TopoDS_Edge&    CurrentEdge,
		    const Standard_Real&  Param,
		    TopoDS_Edge& E1,
		    TopoDS_Edge& E2,
		    const TopoDS_Vertex& VRef)
{
  BRep_Builder B; 
  Standard_Real first,last;
  Handle(Geom_Curve) C = BRep_Tool::Curve(CurrentEdge,first,last);
  TopoDS_Vertex Vf, Vl, Vi;
  B.MakeVertex(Vi, C->Value(Param), Precision::Confusion());
  TopExp::Vertices(CurrentEdge, Vf, Vl);
  if (VRef.IsSame(Vf)) {
    E1 = BRepLib_MakeEdge(C,Vf,Vi, first,Param);
    E2 = BRepLib_MakeEdge(C,Vi,Vl, Param,last);
  }
  else {
    E2 = BRepLib_MakeEdge(C,Vf,Vi, first,Param);
    E1 = BRepLib_MakeEdge(C,Vi,Vl, Param,last);    
  }   
}


static void TrimEdge (const TopoDS_Edge&              CurrentEdge,
		      const TColStd_SequenceOfReal&   CutValues,
		      const Standard_Real   t0, const Standard_Real   t1,
		      const Standard_Boolean          SeqOrder,
		      TopTools_SequenceOfShape& S)

{
  S.Clear();
  Standard_Integer j, ndec=CutValues.Length();
  Standard_Real first,last,m0,m1;
  Handle(Geom_Curve) C = BRep_Tool::Curve(CurrentEdge,first,last);

  TopoDS_Vertex Vf,Vl,Vbid,V0,V1;
  TopAbs_Orientation CurrentOrient = CurrentEdge.Orientation();
  TopExp::Vertices(CurrentEdge,Vf,Vl);
  Vbid.Nullify();

  if (SeqOrder) {
    // de first vers last
    m0 = first;
    V0 = Vf;
    for (j=1; j<=ndec; j++) {
      // morceau d'edge  
      m1 = (CutValues.Value(j)-t0)*(last-first)/(t1-t0)+first;
      TopoDS_Edge CutE = (TopoDS_Edge) BRepLib_MakeEdge(C,V0,Vbid,m0,m1);
      CutE.Orientation(CurrentOrient);
      S.Append(CutE);
      m0 = m1;
      V0 = TopExp::LastVertex(CutE);
      if (j==ndec) {
	// dernier morceau
	TopoDS_Edge LastE = (TopoDS_Edge) BRepLib_MakeEdge(C,V0,Vl,m0,last);
	LastE.Orientation(CurrentOrient);
	S.Append(LastE);
      }
    }
  }
  else {
    // de last vers first
    m1 = last;
    V1 = Vl;
    for (j=ndec; j>=1; j--) {
      // morceau d'edge  
      m0 = (CutValues.Value(j)-t0)*(last-first)/(t1-t0)+first;
      TopoDS_Edge CutE = (TopoDS_Edge) BRepLib_MakeEdge(C,Vbid,V1,m0,m1);
      CutE.Orientation(CurrentOrient);
      S.Append(CutE);
      m1 = m0;
      V1 = TopExp::FirstVertex(CutE);
      if (j==1) {
	// dernier morceau
	TopoDS_Edge LastE = (TopoDS_Edge) BRepLib_MakeEdge(C,Vf,V1,first,m1);
	LastE.Orientation(CurrentOrient);
	S.Append(LastE);
      }
    }
  }
}


//=======================================================================
//function : Face
//purpose  : 
//=======================================================================

TopoDS_Face BRepFill::Face(const TopoDS_Edge& Edge1, 
			   const TopoDS_Edge& Edge2 )
{
  TopoDS_Face Face;

  BRep_Builder B;
// Class BRep_Tool without fields and without Constructor :
//  BRep_Tool BT;

  TopLoc_Location L,L1,L2;
  Standard_Real f1,f2,l1,l2, Tol;

//  Handle(Geom_Curve) C1 = BT.Curve(Edge1,L1,f1,l1);
  Handle(Geom_Curve) C1 = BRep_Tool::Curve(Edge1,L1,f1,l1);
//  Handle(Geom_Curve) C2 = BT.Curve(Edge2,L2,f2,l2);
  Handle(Geom_Curve) C2 = BRep_Tool::Curve(Edge2,L2,f2,l2);

  // compute the location
  Standard_Boolean SameLoc = Standard_False;
  if (L1 == L2) {
    L = L1;
    L1 = L2 = TopLoc_Location();
    SameLoc = Standard_True;
  }

  // transform and trim the curves

  TopoDS_Vertex V1f,V1l,V2f,V2l;
  
  // on cree un new Handle
  if (Abs(f1 - C1->FirstParameter()) > Precision::PConfusion() ||
      Abs(l1 - C1->LastParameter())  > Precision::PConfusion()   ) {
    C1 = new Geom_TrimmedCurve(C1,f1,l1);
  }
  else {
    C1 = Handle(Geom_Curve)::DownCast(C1->Copy());
  }
  // eventuellement on bouge la courbe
  if ( !SameLoc) {
    C1->Transform(L1.Transformation());
  }
  // on la met dans le bon sens et on prend ses vertex
  if (Edge1.Orientation() == TopAbs_REVERSED) {
    TopExp::Vertices(Edge1,V1l,V1f);
    C1->Reverse();
  }
  else {
    TopExp::Vertices(Edge1,V1f,V1l);
  }

  // on cree un new Handle
  if (Abs(f2 - C2->FirstParameter()) > Precision::PConfusion() ||
      Abs(l2 - C2->LastParameter())  > Precision::PConfusion()   ) {
    C2 = new Geom_TrimmedCurve(C2,f2,l2);
  }
  else {
    C2 = Handle(Geom_Curve)::DownCast(C2->Copy());
  }
  // eventuellement on bouge la courbe
  if ( !SameLoc) {
    C2->Transform(L2.Transformation());
  }
  // on la met dans le bon sens et on prend ses vertex
  if (Edge2.Orientation() == TopAbs_REVERSED) {
    TopExp::Vertices(Edge2,V2l,V2f);
    C2->Reverse();
  }
  else {
    TopExp::Vertices(Edge2,V2f,V2l);
  }

  // Sont-ce des edges fermes
  Standard_Boolean Closed = V1f.IsSame(V1l) && V2f.IsSame(V2l);


  GeomFill_Generator Generator;
  Generator.AddCurve( C1);
  Generator.AddCurve( C2);
  Generator.Perform( Precision::PConfusion());

  Handle(Geom_Surface) Surf = Generator.Surface();
  Handle(Geom_Curve) Iso;

  B.MakeFace(Face,Surf,Precision::Confusion());

  // make the missing edges
  Surf->Bounds(f1,l1,f2,l2);

  TopoDS_Edge Edge3, Edge4;

  Iso = Surf->UIso(f1);
//  Tol = Max(BT.Tolerance(V1f), BT.Tolerance(V2f));
  Tol = Max(BRep_Tool::Tolerance(V1f), BRep_Tool::Tolerance(V2f));
  if (Iso->Value(f2).Distance(Iso->Value(l2)) > Tol) {
    B.MakeEdge(Edge3,Iso,Precision::Confusion());
  }
  else {
    B.MakeEdge(Edge3);
    B.Degenerated(Edge3, Standard_True);
  }
  V1f.Orientation(TopAbs_FORWARD);
  B.Add(Edge3,V1f);
  V2f.Orientation(TopAbs_REVERSED);
  B.Add(Edge3,V2f);
  B.Range(Edge3,f2,l2);

  if (Closed) {
    Edge4 = Edge3;
  }
  else {
    Iso = Surf->UIso(l1);
//    Tol = Max(BT.Tolerance(V1l), BT.Tolerance(V2l));
    Tol = Max(BRep_Tool::Tolerance(V1l), BRep_Tool::Tolerance(V2l));
    if (Iso->Value(l2).Distance(Iso->Value(f2)) > Tol) {
      B.MakeEdge(Edge4,Iso,Precision::Confusion());
    }
    else {
      B.MakeEdge(Edge4);
      B.Degenerated(Edge4, Standard_True);
    }
    V1l.Orientation(TopAbs_FORWARD);
    B.Add(Edge4,V1l);
    V2l.Orientation(TopAbs_REVERSED);
    B.Add(Edge4,V2l);
    B.Range(Edge4,f2,l2);
  }

  // make the wire

  TopoDS_Wire W;
  B.MakeWire(W);

  Edge3.Reverse();
  B.Add(W,Edge1);
  B.Add(W,Edge4);
  B.Add(W,Edge2.Reversed());
  B.Add(W,Edge3);

  B.Add(Face,W);

  // set the pcurves

  Standard_Real T = Precision::Confusion();

  if ( Edge1.Orientation() == TopAbs_REVERSED ) {
    B.UpdateEdge(Edge1,new Geom2d_Line(gp_Pnt2d(0,f2),gp_Dir2d(-1,0)),Face,T);
    B.Range(Edge1,Face,-l1,-f1);
  }
  else {
    B.UpdateEdge(Edge1,new Geom2d_Line(gp_Pnt2d(0,f2),gp_Dir2d(1,0)),Face,T);
    B.Range(Edge1,Face,f1,l1);
  }

  if ( Edge2.Orientation() == TopAbs_REVERSED ) {
    B.UpdateEdge(Edge2,new Geom2d_Line(gp_Pnt2d(0,l2),gp_Dir2d(-1,0)),Face,T);
    B.Range(Edge2,Face,-l1,-f1);
  }
  else {
    B.UpdateEdge(Edge2,new Geom2d_Line(gp_Pnt2d(0,l2),gp_Dir2d(1,0)),Face,T);
    B.Range(Edge2,Face,f1,l1);
  }

  if ( Closed) {
    B.UpdateEdge(Edge3,
		 new Geom2d_Line(gp_Pnt2d(l1,0),gp_Dir2d(0,1)),
		 new Geom2d_Line(gp_Pnt2d(f1,0),gp_Dir2d(0,1)),Face,T);
  }
  else {
    B.UpdateEdge(Edge3,new Geom2d_Line(gp_Pnt2d(f1,0),gp_Dir2d(0,1)),Face,T);
    B.UpdateEdge(Edge4,new Geom2d_Line(gp_Pnt2d(l1,0),gp_Dir2d(0,1)),Face,T);
  }

  // Set the non parameter flag;
  B.SameParameter(Edge1,Standard_False);
  B.SameParameter(Edge2,Standard_False);
  B.SameParameter(Edge3,Standard_False);
  B.SameParameter(Edge4,Standard_False);
  B.SameRange(Edge1,Standard_False);
  B.SameRange(Edge2,Standard_False);
  B.SameRange(Edge3,Standard_False);
  B.SameRange(Edge4,Standard_False);
  
  BRepLib::SameParameter(Face);

  if ( SameLoc) Face.Move(L);
  return Face;
}


//=======================================================================
//function : Shell
//purpose  : 
//=======================================================================

TopoDS_Shell BRepFill::Shell(const TopoDS_Wire& Wire1, 
			     const TopoDS_Wire& Wire2 )
{
  TopoDS_Shell Shell;
  TopoDS_Face  Face;
  TopoDS_Shape S1, S2;
  TopoDS_Edge  Edge1, Edge2, Edge3, Edge4, Couture;

  BRep_Builder B;
// Class BRep_Tool without fields and without Constructor :
//  BRep_Tool BT;
  B.MakeShell(Shell);

  TopExp_Explorer ex1;
  TopExp_Explorer ex2;

  Standard_Boolean Closed = Wire1.Closed() && Wire2.Closed();
  
  Standard_Boolean thefirst = Standard_True;

  ex1.Init(Wire1,TopAbs_EDGE);
  ex2.Init(Wire2,TopAbs_EDGE);

  while ( ex1.More() && ex2.More() ) { 

    Edge1 = TopoDS::Edge(ex1.Current());
    Edge2 = TopoDS::Edge(ex2.Current());

    Standard_Boolean Periodic = Edge1.Closed() && Edge2.Closed();
    
    ex1.Next();
    ex2.Next();
    
    TopLoc_Location L,L1,L2;
    Standard_Real f1,l1,f2,l2,Tol;
    
    Handle(Geom_Curve) C1 = BRep_Tool::Curve(Edge1,L1,f1,l1);
    Handle(Geom_Curve) C2 = BRep_Tool::Curve(Edge2,L2,f2,l2);
    
    // compute the location
    Standard_Boolean SameLoc = Standard_False;
    if (L1 == L2) {
      L = L1;
      L1 = L2 = TopLoc_Location();
      SameLoc = Standard_True;
    }

    // transform and trim the curves

    TopoDS_Vertex V1f,V1l,V2f,V2l;
    

    if (Abs(f1 - C1->FirstParameter()) > Precision::PConfusion() ||
	Abs(l1 - C1->LastParameter())  > Precision::PConfusion()   ) {
      C1 = new Geom_TrimmedCurve(C1,f1,l1);
    }
    else {
      C1 = Handle(Geom_Curve)::DownCast(C1->Copy());
    }
    if ( !SameLoc) {
      C1->Transform(L1.Transformation());
    }
    if (Edge1.Orientation() == TopAbs_REVERSED) {
      TopExp::Vertices(Edge1,V1l,V1f);
      C1->Reverse();
    }
    else
      TopExp::Vertices(Edge1,V1f,V1l);
    
    if (Abs(f2 - C2->FirstParameter()) > Precision::PConfusion() ||
	Abs(l2 - C2->LastParameter())  > Precision::PConfusion()   ) {
      C2 = new Geom_TrimmedCurve(C2,f2,l2);
    }
    else {
      C2 = Handle(Geom_Curve)::DownCast(C2->Copy());
    }
    if ( !SameLoc) {
      C2->Transform(L2.Transformation());
    }
    if (Edge2.Orientation() == TopAbs_REVERSED) {
      TopExp::Vertices(Edge2,V2l,V2f);
	C2->Reverse();
      }
    else
      TopExp::Vertices(Edge2,V2f,V2l);
    
    GeomFill_Generator Generator;
    Generator.AddCurve( C1);
    Generator.AddCurve( C2);
    Generator.Perform( Precision::PConfusion());
    
    Handle(Geom_Surface) Surf = Generator.Surface();
    Handle(Geom_Curve) Iso;
    
    B.MakeFace(Face,Surf,Precision::Confusion());
    
    // make the missing edges
    Surf->Bounds(f1,l1,f2,l2);
        
    if ( thefirst) {
      Iso = Surf->UIso(f1);
//      Tol = Max(BT.Tolerance(V1f), BT.Tolerance(V2f));
      Tol = Max(BRep_Tool::Tolerance(V1f), BRep_Tool::Tolerance(V2f));
      if (Iso->Value(f2).Distance(Iso->Value(l2)) > Tol) {
	B.MakeEdge(Edge3,Iso,Precision::Confusion());
      }
      else {
	B.MakeEdge(Edge3);
	B.Degenerated(Edge3, Standard_True);
      }
      V1f.Orientation(TopAbs_FORWARD);
      B.Add(Edge3,V1f);
      V2f.Orientation(TopAbs_REVERSED);
      B.Add(Edge3,V2f);
      B.Range(Edge3,f2,l2);
      if ( Closed) {
	Couture = Edge3;
      }
      Edge3.Reverse();
      thefirst = Standard_False;
    }
    else {
      Edge3 = Edge4;
      Edge3.Reverse();
    }
    
    if ( Closed && !ex1.More() && !ex2.More() ) {
      Edge4 = Couture;
    }
    else {
      Iso = Surf->UIso(l1);
//      Tol = Max(BT.Tolerance(V1l), BT.Tolerance(V2l));
      Tol = Max(BRep_Tool::Tolerance(V1l), BRep_Tool::Tolerance(V2l));
      if (Iso->Value(l2).Distance(Iso->Value(f2)) > Tol) {
	B.MakeEdge(Edge4,Iso,Precision::Confusion());
      }
      else {
	B.MakeEdge(Edge4);
	B.Degenerated(Edge4, Standard_True);
      }
      V1l.Orientation(TopAbs_FORWARD);
      B.Add(Edge4,V1l);
      V2l.Orientation(TopAbs_REVERSED);
      B.Add(Edge4,V2l);
      B.Range(Edge4,f2,l2);
    }

    // make the wire
    
    TopoDS_Wire W;
    B.MakeWire(W);
    
    B.Add(W,Edge1);
    B.Add(W,Edge4);
    B.Add(W,Edge2.Reversed());
    B.Add(W,Edge3);
    
    B.Add(Face,W);
    
    if ( SameLoc) Face.Move( L);

    B.Add(Shell,Face);

    // set the pcurves
    
    Standard_Real T = Precision::Confusion();

    if ( Edge1.Orientation() == TopAbs_REVERSED ) {
      B.UpdateEdge(Edge1,new Geom2d_Line(gp_Pnt2d(0,f2),gp_Dir2d(-1,0)),
		   Face,T);
      B.Range(Edge1,Face,-l1,-f1);
    }
    else {
      B.UpdateEdge(Edge1,new Geom2d_Line(gp_Pnt2d(0,f2),gp_Dir2d(1,0)),
		   Face,T);
      B.Range(Edge1,Face,f1,l1);
    }
    
    if ( Edge2.Orientation() == TopAbs_REVERSED ) {
      B.UpdateEdge(Edge2,new Geom2d_Line(gp_Pnt2d(0,l2),gp_Dir2d(-1,0)),
		   Face,T);
      B.Range(Edge2,Face,-l1,-f1);
    }
    else {
      B.UpdateEdge(Edge2,new Geom2d_Line(gp_Pnt2d(0,l2),gp_Dir2d(1,0)),
		   Face,T);
      B.Range(Edge2,Face,f1,l1);
    }

    if ( Periodic) {
      B.UpdateEdge(Edge3,
		   new Geom2d_Line(gp_Pnt2d(l1,0),gp_Dir2d(0,1)),
		   new Geom2d_Line(gp_Pnt2d(f1,0),gp_Dir2d(0,1)),
		   Face,T);
    }
    else {
      B.UpdateEdge(Edge3,new Geom2d_Line(gp_Pnt2d(f1,0),gp_Dir2d(0,1)),Face,T);
      B.UpdateEdge(Edge4,new Geom2d_Line(gp_Pnt2d(l1,0),gp_Dir2d(0,1)),Face,T);
    }
    
    // Set the non parameter flag;
    B.SameParameter(Edge1,Standard_False);
    B.SameParameter(Edge2,Standard_False);
    B.SameParameter(Edge3,Standard_False);
    B.SameParameter(Edge4,Standard_False);
    B.SameRange(Edge1,Standard_False);
    B.SameRange(Edge2,Standard_False);
    B.SameRange(Edge3,Standard_False);
    B.SameRange(Edge4,Standard_False);
  }
  
  BRepLib::SameParameter(Shell);
  return Shell;
}

//=======================================================================
//function : Axe
//purpose  : 
//=======================================================================

void BRepFill::Axe (const TopoDS_Shape&       Spine,
		    const TopoDS_Wire&        Profile,
		          gp_Ax3&             AxeProf,
		          Standard_Boolean&   ProfOnSpine,
		    const Standard_Real       Tol)
{  
  gp_Pnt Loc,Loc1,Loc2;
  gp_Vec Tang,Tang1,Tang2,Normal;

  Handle(Geom_Surface) S;
  TopLoc_Location      L;
  
  TopoDS_Face aFace;

  // normale au Spine.
  if (Spine.ShapeType() == TopAbs_FACE) {
    aFace = TopoDS::Face(Spine);
    S = BRep_Tool::Surface(TopoDS::Face(Spine), L);  
    if ( !S->IsKind(STANDARD_TYPE(Geom_Plane))) {
      BRepLib_FindSurface FS(TopoDS::Face(Spine), -1, Standard_True);
      if ( FS.Found()) {
	S = FS.Surface();
	L = FS.Location();
      }
      else {
	Standard_NoSuchObject::Raise 
	  ("BRepFill_Evolved : The Face is not planar");
      }
    }
  }
  else if (Spine.ShapeType() == TopAbs_WIRE) {  
    aFace = BRepLib_MakeFace(TopoDS::Wire(Spine),Standard_True);
    S = BRep_Tool::Surface(aFace, L);
  }
  
  if (S.IsNull()) Standard_DomainError::Raise("BRepFill_Evolved::Axe");
    
  if (!L.IsIdentity())
    S = Handle(Geom_Surface)::DownCast(S->Transformed(L.Transformation()));
  
  Normal = Handle(Geom_Plane)::DownCast(S)->Pln().Axis().Direction();

  // Recherche du vertex du profil  le plus proche du spine.
  Standard_Real     DistMin = Precision::Infinite();
  Standard_Real     Dist;
//  Standard_Real     Tol2 = Tol*Tol;
  Standard_Real     Tol2 = 1.e-10;
  TopExp_Explorer   PE, SE;
  BRepExtrema_ExtPC BE;   
  Standard_Real     Par =0.,f,l;	
//  Standard_Real     D1,D2;
  gp_Pnt            P1,P2;

  // On cherche d'abord si il y a contact Vertex Vertex.
  Standard_Boolean IsOnVertex = Standard_False;
  SE.Init(aFace.Oriented(TopAbs_FORWARD),TopAbs_VERTEX);
//  modified by NIZHNY-EAP Wed Feb 23 12:31:52 2000 ___BEGIN___
//  for (;SE.More() && !IsOnVertex ; SE.Next()) {
  for (;SE.More(); SE.Next()) {
    P1 = BRep_Tool::Pnt(TopoDS::Vertex(SE.Current()));

    PE.Init(Profile,TopAbs_VERTEX);
    for ( ; PE.More(); PE.Next()) {
      P2 = BRep_Tool::Pnt(TopoDS::Vertex(PE.Current()));
      Standard_Real DistP1P2 = P1.SquareDistance(P2);
      IsOnVertex = (DistP1P2 <= Tol2);
      if (IsOnVertex) break;
    }
    // otherwise SE.Next() is done and VonF is wrong
    if (IsOnVertex) break;
//  modified by NIZHNY-EAP Wed Jan 26 09:08:36 2000 ___END___
  }

  if (IsOnVertex) {
    // try to find on which edge which shared this vertex,
    // the profile must be considered.
    // E1, E2 : those two edges.
    TopTools_IndexedDataMapOfShapeListOfShape Map;
    TopExp::MapShapesAndAncestors(aFace.Oriented(TopAbs_FORWARD),
				  TopAbs_VERTEX, 
				  TopAbs_EDGE,
				  Map);

    const TopoDS_Vertex&        VonF = TopoDS::Vertex(SE.Current());
    const TopTools_ListOfShape& List = Map.FindFromKey(VonF);
    const TopoDS_Edge&          E1   = TopoDS::Edge(List.First());
    const TopoDS_Edge&          E2   = TopoDS::Edge(List. Last());

    Handle(Geom_Curve) CE1 = BRep_Tool::Curve(E1,L,f,l);
    Standard_Real Par1 = BRep_Tool::Parameter(VonF,E1,aFace);
    CE1->D1(Par1,Loc1,Tang1);
    if (!L.IsIdentity()) {
      Tang1.Transform(L.Transformation());
      Loc1.Transform(L.Transformation());
    }
    if (E1.Orientation() == TopAbs_REVERSED) Tang1.Reverse();

    Handle(Geom_Curve) CE2 = BRep_Tool::Curve(E2,L,f,l);
    Standard_Real Par2 = BRep_Tool::Parameter(VonF,E2,aFace);
    CE2->D1(Par2,Loc2,Tang2);
    if (!L.IsIdentity()) {
      Tang2.Transform(L.Transformation());
      Loc2.Transform(L.Transformation());
    }
    if (E2.Orientation() == TopAbs_REVERSED) Tang2.Reverse();

//  modified by NIZHNY-EAP Wed Feb  2 15:38:41 2000 ___BEGIN___
    Tang1.Normalize();
    Tang2.Normalize();
    Standard_Real sca1=0., sca2=0.;
    TopoDS_Vertex V1, V2;
    TopoDS_Edge E;
    for (PE.Init(Profile,TopAbs_EDGE); PE.More(); PE.Next()) {
      E = TopoDS::Edge(PE.Current());
      TopExp::Vertices(E, V1, V2);
      P1 = BRep_Tool::Pnt(V1);
      P2 = BRep_Tool::Pnt(V2);
      gp_Vec vec(P1,P2);
      sca1 += Abs(Tang1.Dot(vec));
      sca2 += Abs(Tang2.Dot(vec));
    } 
//  modified by NIZHNY-EAP Wed Feb  2 15:38:44 2000 ___END___

    if ( Abs(sca1) < Abs(sca2)) {
      Loc  = Loc1;
      Tang = Tang1;
    }
    else {
      Loc  = Loc2;
      Tang = Tang2;
    }
    DistMin = 0.;
  }
  else {
    SE.Init(aFace.Oriented(TopAbs_FORWARD),TopAbs_EDGE);
    for ( ; SE.More(); SE.Next()) {
      const TopoDS_Edge& E = TopoDS::Edge(SE.Current());
      BE.Initialize(E);
      for (PE.Init(Profile,TopAbs_VERTEX) ; PE.More(); PE.Next()) {
	Dist = Precision::Infinite();
	const TopoDS_Vertex&  V = TopoDS::Vertex(PE.Current());
	BE.Perform(V);
	if (BE.IsDone()) {
	  // extrema.
	  for (Standard_Integer i = 1; i <= BE.NbExt(); i++) {
	    if (BE.IsMin(i)) { 
	      Dist = sqrt (BE.SquareDistance(i));
	      Par  = BE.Parameter(i);
	      break;
	    }
	  }
	}
	// sauvegarde minimum.
	if (Dist < DistMin) {
	  DistMin = Dist;
	  BRepAdaptor_Curve BAC(E);
	  BAC.D1 (Par,Loc,Tang);
	  if (E.Orientation() == TopAbs_REVERSED) Tang.Reverse();
	}
      }
    }
  }

  ProfOnSpine = (DistMin < Tol);
  //Construction AxeProf;
  gp_Ax3 A3 (Loc,Normal,Tang);
  AxeProf = A3;
  
}

//=======================================================================
//function : SearchOrigin
//purpose  : Decoupe et oriente un wire ferme. 
//=======================================================================

void BRepFill::SearchOrigin(TopoDS_Wire & W,
			    const gp_Pnt& P,
			    const gp_Vec& Dir,
			    const Standard_Real Tol)
{
  if (!W.Closed()) 
    Standard_NoSuchObject::
      Raise("BRepFill::SearchOrigin : the wire must be closed");


  Standard_Boolean NewVertex = Standard_False;
  Standard_Real theparam = 1.e101, angle;
  TopoDS_Vertex V ;
  TopoDS_Edge E, Eref, E1 , E2;
  BRep_Builder B;
// Class BRep_Tool without fields and without Constructor :
//  BRep_Tool BT;

  W.Orientation(TopAbs_FORWARD); //pour ne pas composer les orientations

  // Calcul la distance
  B.MakeVertex(V, P, Tol);  
  BRepExtrema_DistShapeShape DSS(V, W);
  if (DSS.IsDone()) {
    Standard_Integer isol = 1;
    Standard_Real dss = P.Distance(DSS.PointOnShape2(isol));
    for (Standard_Integer iss=2; iss<=DSS.NbSolution(); iss++) 
      if (dss > P.Distance(DSS.PointOnShape2(iss))) {
	dss = P.Distance(DSS.PointOnShape2(iss));
	isol = iss;
      }
    TopoDS_Shape supp = DSS.SupportOnShape2(isol);
    if (DSS.SupportTypeShape2(isol)==BRepExtrema_IsVertex) {
      V = TopoDS::Vertex(supp);
    }
    else {
      TopoDS_Vertex Vf, Vl;
      Standard_Real d, dist;
      E = TopoDS::Edge(supp);
      TopExp::Vertices(E, Vf, Vl);
//      dist = P.Distance(BT.Pnt(Vf));
      dist = P.Distance(BRep_Tool::Pnt(Vf));
      if (dist < Tol) {
	V = Vl;
      }
//      d = P.Distance(BT.Pnt(Vl));
      d = P.Distance(BRep_Tool::Pnt(Vl));
      if ((d<Tol) && (d<dist)) {
	V = Vf;
	dist = d;
      }
      NewVertex = (dist > Tol);
      if (NewVertex) {
	DSS.ParOnEdgeS2(isol, theparam);
      }
    }
  } 
#if DEB
  else {
    cout << "BRepFill::SearchOrigine : Echec Distance" << endl;
  }
#endif

  Standard_Integer ii, rangdeb=0, NbEdges=0;
  Standard_Boolean forward;
  BRepTools_WireExplorer exp;

  // Calcul le nombre d'edges
  for(exp.Init(W); exp.More(); exp.Next()) NbEdges++;
  if (NewVertex) {
    NbEdges++;
    Eref = E;
  }

  // Construit la Table et calcul rangdeb
  TopTools_Array1OfShape Edges(1, NbEdges);
  for(exp.Init(W), ii=1; exp.More(); exp.Next(), ii++) {
    E = exp.Current();
    if (NewVertex && E.IsSame(Eref)) {
      TopoDS_Edge E1, E2;
      CutEdge(E, theparam, E1, E2, exp.CurrentVertex());
      Edges(ii) = E1;
      ii++;
      Edges(ii) = E2;
      rangdeb = ii;
    }
    else {
      Edges(ii) = E;
    }
    if (!NewVertex && V.IsSame(exp.CurrentVertex())) {
      rangdeb = ii;
    }
  }
  if (rangdeb == 0) rangdeb = NbEdges;

  // Calcul du sens de parcourt
  E = TopoDS::Edge(Edges(rangdeb));
  if (!NewVertex) {
//    theparam = BT.Parameter(V, E);
    theparam = BRep_Tool::Parameter(V, E);
  }
  BRepAdaptor_Curve AC(E);
  gp_Pnt Pe;
  gp_Vec Ve;
  AC.D1(theparam, Pe, Ve);
  if (E.Orientation()==TopAbs_REVERSED) {
    Ve *= -1;
  }
  angle = Ve.Angle(Dir);
  if (angle > PI) angle = 2*PI - angle;
  forward = (angle <= PI/2);

  // Reconstruction
  MakeWire( Edges, rangdeb, forward, W);
  W.Closed(Standard_True);
}



//=======================================================================
//function : ComputeACR
//purpose  : 
//=======================================================================

void BRepFill::ComputeACR(const TopoDS_Wire& wire,
			  TColStd_Array1OfReal& ACR)
{
  // calcul des abscisses curvilignes reduites et de la longueur du wire
  BRepTools_WireExplorer anExp;
  Standard_Integer nbEdges=0, i;

  // longueurs cumulees
  ACR.Init(0);
  for(anExp.Init(wire); anExp.More(); anExp.Next()) {
    nbEdges++;
    TopoDS_Edge Ecur = TopoDS::Edge(anExp.Current());
    ACR(nbEdges) = ACR(nbEdges-1);
    if (!BRep_Tool::Degenerated(Ecur)) {
      BRepAdaptor_Curve anEcur(Ecur);
      ACR(nbEdges) += GCPnts_AbscissaPoint::Length(anEcur);
    }
  }

  // longueur totale du wire
  ACR(0) = ACR(nbEdges);

  // abscisses curvilignes reduites
  if (ACR(0)>Precision::Confusion()) {
    for (i=1; i<=nbEdges; i++) {
      ACR(i) /= ACR(0);
    }
  }
  else {
    // wire ponctuel
    ACR(nbEdges) = 1;
  }

}

//=======================================================================
//function : InsertACR
//purpose  : 
//=======================================================================

TopoDS_Wire BRepFill::InsertACR(const TopoDS_Wire& wire,
				const TColStd_Array1OfReal& ACRcuts,
				const Standard_Real prec)
{
  // calcul des ACR du wire a decouper
  BRepTools_WireExplorer anExp;
  Standard_Integer nbEdges=0;
  for(anExp.Init(wire); anExp.More(); anExp.Next()) {
    nbEdges++;
  }
  TColStd_Array1OfReal ACRwire(0,nbEdges);
  ComputeACR(wire, ACRwire);

  Standard_Integer i, j, nmax=ACRcuts.Length();
  TColStd_Array1OfReal paradec(1,nmax);
  BRepLib_MakeWire MW;

  Standard_Real t0,t1=0;
  nbEdges=0;

  // traitement edge par edge
  for(anExp.Init(wire); anExp.More(); anExp.Next()) {
    nbEdges++;
    t0 = t1;
    t1 = ACRwire(nbEdges);

    // parametres de decoupe sur cette edge
    Standard_Integer ndec=0;
    for (i=1; i<=ACRcuts.Length(); i++ ) {
      if (t0+prec<ACRcuts(i) && ACRcuts(i)<t1-prec) {
	ndec++;
	paradec(ndec) = ACRcuts(i);
      }
    }

    TopoDS_Edge E = anExp.Current();
    TopoDS_Vertex V = anExp.CurrentVertex();

    if (ndec==0 || BRep_Tool::Degenerated(E)) {
      // on copie l'edge
      MW.Add(E);
    }
    else {
      // il faut couper l'edge
      // en respectant le sens de parcours du wire
      Standard_Boolean SO = (V.IsSame(TopExp::FirstVertex(E)));
      TopTools_SequenceOfShape SE;
      SE.Clear();
      TColStd_SequenceOfReal SR;
      SR.Clear();
      // le wire est toujours FORWARD
      // il faut modifier le parametre de decoupe si l'edge est REVERSED
      if (E.Orientation() == TopAbs_FORWARD) {
	for (j=1; j<=ndec; j++) SR.Append(paradec(j));
      }
      else {
	for (j=1; j<=ndec; j++) SR.Append(t0+t1-paradec(ndec+1-j));
      }
      TrimEdge(E,SR,t0,t1,SO,SE);
      for (j=1; j<=SE.Length(); j++) {
	MW.Add(TopoDS::Edge(SE.Value(j)));
      }
    }
  }

  // resultat
  TopAbs_Orientation Orien = wire.Orientation();
  TopoDS_Shape aLocalShape = MW.Wire();
  aLocalShape.Orientation(Orien);
  TopoDS_Wire wres = TopoDS::Wire(aLocalShape);
//  TopoDS_Wire wres = TopoDS::Wire(MW.Wire().Oriented(Orien));
  return wres;
}