summaryrefslogtreecommitdiff
path: root/src/GeomFill/GeomFill_Pipe.cxx
blob: 9cf899d7aedd2e451dba6ae7358a0ed6cbf3f911 (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
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
// File:	GeomFill_Pipe.cxx
// Created:	Wed Apr 13 14:23:52 1994
// Author:	Eric BONNARDEL
//		<ebo@fuegox>
// Modified 22/09/1997 by PMN : Refonte du a l'introduction de F(t) dans
//             le cas des 2 lignes guides
// Modified:	Mon Jan 18 11:06:46 1999
// Author:	Joelle CHAUVET
//		<jct@sgi64>
//		dans Init(Path, Nsections) : 
//              les parametres des sections doivent etre strict. croissants
//		dans Init(Path, FirstSect, LastSect) :
//		il faut placer les 2 sections au debut de la trajectoire

#include <stdio.h>

#include <GeomFill_Pipe.ixx>

#include <GeomFill_Line.hxx>
#include <GeomFill_SweepSectionGenerator.hxx>
#include <GeomFill_AppSweep.hxx>
#include <GeomFill_Profiler.hxx>
#include <GeomFill_CircularBlendFunc.hxx>
#include <GeomFill_UniformSection.hxx>
#include <GeomFill_Fixed.hxx>
#include <GeomFill_Frenet.hxx>
#include <GeomFill_CorrectedFrenet.hxx>
#include <GeomFill_ConstantBiNormal.hxx>
#include <GeomFill_CurveAndTrihedron.hxx>
#include <GeomFill_SectionPlacement.hxx>
#include <GeomFill_Sweep.hxx>
#include <GeomFill_NSections.hxx>

#include <GeomFill_GuideTrihedronAC.hxx>

#include <GeomFill_GuideTrihedronPlan.hxx>
#include <GeomFill_LocationGuide.hxx>


#include <Geom_BSplineCurve.hxx>
#include <Geom_Circle.hxx>
#include <Geom_Line.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_BSplineSurface.hxx>
#include <Geom_ToroidalSurface.hxx>
#include <Geom_CylindricalSurface.hxx>
#include <Geom_SphericalSurface.hxx>
#include <Geom_Plane.hxx>
#include <Geom_ConicalSurface.hxx>
#include <Geom_RectangularTrimmedSurface.hxx>
#include <Geom_SurfaceOfLinearExtrusion.hxx>

#include <GeomAbs_SurfaceType.hxx>
#include <GeomAdaptor_Curve.hxx>
#include <GeomAdaptor_HCurve.hxx>
#include <GeomLProp_CLProps.hxx>

#include <Approx_SweepApproximation.hxx>

#include <gp_Torus.hxx>
#include <gp_Ax2.hxx>
#include <gp_Ax3.hxx>

#include <Precision.hxx>
#include <ElCLib.hxx>

#include <GeomLib.hxx>

#include <GeomFill_Darboux.hxx>
#include <Adaptor3d_CurveOnSurface.hxx>
#include <Adaptor3d_HCurveOnSurface.hxx>
#include <Geom2dAdaptor_HCurve.hxx>
#include <GeomAdaptor_HSurface.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TColGeom_SequenceOfCurve.hxx>

#ifdef DEB
static Standard_Boolean Affich = Standard_False;
static Standard_Integer NbSections = 0;
#endif

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

static Standard_Boolean CheckSense(const TColGeom_SequenceOfCurve& Seq1,
				         TColGeom_SequenceOfCurve& Seq2)
{
  // initialisation
  Standard_Boolean no_sing = Standard_True;
  Seq2.Clear();

  Handle(Geom_Curve) C1 = Seq1.Value(1);
  Standard_Real f = C1->FirstParameter(), l = C1->LastParameter();
  Standard_Integer iP, NP = 21;
  TColgp_Array1OfPnt Tab(1,NP);
  Standard_Real u = f, h = Abs(f-l) / 20.;
  for (iP=1;iP<=NP;iP++) {
    C1->D0(u,Tab(iP));
    u += h;
    if ((u-f)*(u-l)>0.0) u = l;
  }
  gp_Ax2 AxeRef, Axe;
  gp_Pnt Pos;
  Standard_Boolean sing;
  GeomLib::AxeOfInertia(Tab,AxeRef,sing);

  // si la section est une droite, ca ne marche pas
  if (sing) no_sing = Standard_False;

  Pos = AxeRef.Location();
  Standard_Real alpha1, alpha2, alpha3;
  gp_Pnt P1, P2;
  u = (f+l-h)/2 - h;
  C1->D0(u,P1);
  u += h;
  C1->D0(u,P2);
  alpha1 = gp_Vec(Pos,P1).AngleWithRef(gp_Vec(Pos,P2),AxeRef.Direction());
  P1 = P2;
  u += h;
  C1->D0(u,P2);
  alpha2 = gp_Vec(Pos,P1).AngleWithRef(gp_Vec(Pos,P2),AxeRef.Direction());
  P1 = P2;
  u += h;
  C1->D0(u,P2);
  alpha3 = gp_Vec(Pos,P1).AngleWithRef(gp_Vec(Pos,P2),AxeRef.Direction());
  Seq2.Append(C1);

  for (Standard_Integer iseq=2; iseq<=Seq1.Length(); iseq++) {
    // discretisation de C2
    Handle(Geom_Curve) C2 = Seq1.Value(iseq);
    f = C2->FirstParameter();
    l = C2->LastParameter();
    u = f;
    for (iP=1;iP<=NP;iP++) {
      C2->D0(u,Tab(iP));
      u += h;
      if ((u-f)*(u-l)>0.0) u = l;
    }
    GeomLib::AxeOfInertia(Tab,Axe,sing);  

    // si la section est une droite, ca ne marche pas
    if (sing) no_sing = Standard_False;

    Pos = Axe.Location();
    Standard_Real beta1, beta2, beta3;
    u = (f+l-h)/2 - h;
    C2->D0(u,P1);
    u += h;
    C2->D0(u,P2);
    beta1 = gp_Vec(Pos,P1).AngleWithRef(gp_Vec(Pos,P2),AxeRef.Direction());
    P1 = P2;
    u += h;
    C2->D0(u,P2);
    beta2 = gp_Vec(Pos,P1).AngleWithRef(gp_Vec(Pos,P2),AxeRef.Direction());
    P1 = P2;
    u += h;
    C2->D0(u,P2);
    beta3 = gp_Vec(Pos,P1).AngleWithRef(gp_Vec(Pos,P2),AxeRef.Direction());
    
    // meme sens ?
    Standard_Boolean ok = Standard_True, 
                pasnul1 = ( Abs(alpha1) > Precision::Confusion() )
		          && ( Abs(beta1) > Precision::Confusion() ),
                pasnul2 = ( Abs(alpha2) > Precision::Confusion() )
		          && ( Abs(beta2) > Precision::Confusion() ),
                pasnul3 = ( Abs(alpha3) > Precision::Confusion() )
		          && ( Abs(beta3) > Precision::Confusion() );
    if (pasnul1 && pasnul2 && pasnul3) {
      if (alpha1*beta1>0.0) 
	ok = (alpha2*beta2>0.0) || (alpha3*beta3>0.0);
      else
	ok = (alpha2*beta2>0.0) && (alpha3*beta3>0.0);
    }
    else if (pasnul1 && pasnul2 && !pasnul3) 
      ok = (alpha1*beta1>0.0) || (alpha2*beta2>0.0);
    else if (pasnul1 && !pasnul2 && pasnul3) 
      ok = (alpha1*beta1>0.0) || (alpha3*beta3>0.0);
    else if (!pasnul1 && pasnul2 && pasnul3) 
      ok = (alpha2*beta2>0.0) || (alpha3*beta3>0.0);
    else if (pasnul1) 
	ok = (alpha1*beta1>0.0);
    else if (pasnul2) 
	ok = (alpha2*beta2>0.0);
    else if (pasnul3) 
	ok = (alpha3*beta3>0.0);
    
    if (no_sing && !ok) {
      C2 ->Reverse();
    }
    Seq2.Append(C2);
  }

  return no_sing;
}

//=======================================================================
//function : GeomFill_Pipe
//purpose  : constructor with no parameters. 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe() : myExchUV(Standard_False),myKPart(Standard_False)
{
  Init();
}


//=======================================================================
//function : GeomFill_Pipe
//purpose  : 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Geom_Curve)& Path, 
                             const Standard_Real Radius) 
     : myExchUV(Standard_False),myKPart(Standard_False)
{
  Init();
  Init(Path, Radius);
}

//=======================================================================
//function : GeomFill_Pipe
//purpose  : 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Geom_Curve)& Path,
                             const Handle(Geom_Curve)& FirstSect,
                             const GeomFill_Trihedron Option) 
     : myExchUV(Standard_False),myKPart(Standard_False)
{
  Init();
  Init(Path, FirstSect, Option);
}

//=======================================================================
//function : GeomFill_Pipe
//purpose  : 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Geom2d_Curve)& Path,
                             const Handle(Geom_Surface)& Support,
                             const Handle(Geom_Curve)& FirstSect) 
     : myExchUV(Standard_False),myKPart(Standard_False)
{
  Init();
  Init(Path, Support, FirstSect);
}

//=======================================================================
//function : GeomFill_Pipe
//purpose  : 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Geom_Curve)& Path,
                             const Handle(Geom_Curve)& FirstSect,
                             const Handle(Geom_Curve)& LastSect)
     : myExchUV(Standard_False),myKPart(Standard_False)
{
  Init();
  Init(Path, FirstSect, LastSect);
}


//=======================================================================
//function : GeomFill_Pipe
//purpose  : 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Geom_Curve)& Path,
                             const TColGeom_SequenceOfCurve& NSections)
     : myExchUV(Standard_False),myKPart(Standard_False)
{
  Init();
  Init(Path, NSections);
}

//=======================================================================
//function : GeomFill_Pipe
//purpose  : 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Geom_Curve)& Path,
                             const Handle(Geom_Curve)& Curve1,
                             const gp_Dir& Direction)
     : myExchUV(Standard_False), myKPart(Standard_False)
{
 Init(Path, Curve1, Direction); 
}
//=======================================================================
//function : GeomFill_Pipe
//purpose  : 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Geom_Curve)& Path,
                             const Handle(Geom_Curve)& Curve1,
                             const Handle(Geom_Curve)& Curve2,
                             const Standard_Real       Radius)
     : myExchUV(Standard_False),myKPart(Standard_False)
{
  Init();
  Handle(GeomAdaptor_HCurve) AdpPath = 
    new GeomAdaptor_HCurve( Path);
  Handle(GeomAdaptor_HCurve) AdpCurve1 = 
    new GeomAdaptor_HCurve( Curve1);
  Handle(GeomAdaptor_HCurve) AdpCurve2 = 
    new GeomAdaptor_HCurve( Curve2);

  Init(AdpPath, AdpCurve1, AdpCurve2, Radius);
}


//=======================================================================
//function : GeomFill_Pipe
//purpose  : 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Adaptor3d_HCurve)& Path, 
                             const Handle(Adaptor3d_HCurve)& Curve1,
                             const Handle(Adaptor3d_HCurve)& Curve2,
                             const Standard_Real Radius)
     : myExchUV(Standard_False),myKPart(Standard_False)
{
  Init();
  Init(Path,Curve1,Curve2,Radius);
}


//=======================================================================


//=======================================================================
//function : GeomFill_Pipe
//purpose  : pipe avec courbe guide 
//=======================================================================

GeomFill_Pipe::GeomFill_Pipe(const Handle(Geom_Curve)& Path,
                             const Handle(Adaptor3d_HCurve)& Guide,
			     const Handle(Geom_Curve)& FirstSect,
			     const Standard_Boolean byACR,
			     const Standard_Boolean rotat)
     : myExchUV(Standard_False),myKPart(Standard_False)
// Path : trajectoire
// Guide : courbe guide
// FirstSect : section
// NbPt : nb de points pour le calcul du triedre
{
  Init();
  Init(Path, Guide, FirstSect, byACR, rotat);
}



//=======================================================================
//function : Init
//purpose  : pipe avec courbe guide 
//=======================================================================

void GeomFill_Pipe::Init(const Handle(Geom_Curve)& Path,
			 const Handle(Adaptor3d_HCurve)& Guide,
			 const Handle(Geom_Curve)& FirstSect,
			 const Standard_Boolean byACR,
			 const Standard_Boolean rotat)
// Path : trajectoire
// Guide : courbe guide
// FirstSect : section
// rotat : vrai si on veu la rotation false sinon
// triedre : AC pour absc. curv. ou P pour plan ortho
{
  Standard_Real angle;
  myAdpPath = new (GeomAdaptor_HCurve) 
    (Handle(Geom_Curve)::DownCast(Path->Copy()));

  Handle (GeomFill_TrihedronWithGuide) TLaw;
  
  // loi de triedre
  if (byACR) {
    TLaw = new (GeomFill_GuideTrihedronAC)(Guide);
    TLaw->SetCurve(myAdpPath);
  }
  else {
    TLaw =  new (GeomFill_GuideTrihedronPlan)(Guide);
    TLaw->SetCurve(myAdpPath);      
  }  
  

// loi de positionnement
  Handle(GeomFill_LocationGuide) TheLoc = 
    new (GeomFill_LocationGuide) (TLaw);
  TheLoc->SetCurve(myAdpPath);

  GeomFill_SectionPlacement Place(TheLoc, FirstSect);
  Place.Perform(Precision::Confusion());

// loi de section
  mySec = new (GeomFill_UniformSection) (Place.Section(Standard_False),
					 myAdpPath->FirstParameter(), 
					 myAdpPath->LastParameter());

#ifdef DRAW
  if  (Affich) {
    char* Temp = "TheSect" ;
    DrawTrSurf::Set(Temp, FirstSect );
//    DrawTrSurf::Set("TheSect", FirstSect );
  }
#endif

 if (rotat)  TheLoc->Set(mySec,rotat, 
			 myAdpPath->FirstParameter(),
			 myAdpPath->LastParameter(),
			 0., angle);
 myLoc = TheLoc;
}


//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void GeomFill_Pipe::Init()
{
  myType = 0;
  myError  = 0;
  myRadius = 0.;
  myKPart = Standard_True;
  myPolynomial = Standard_False;
  myAdpPath.Nullify();
  myAdpFirstSect.Nullify();
  myAdpLastSect.Nullify();
  myLoc.Nullify();
  mySec.Nullify(); 
}
//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void GeomFill_Pipe::Init(const Handle(Geom_Curve)& Path,
                         const Standard_Real       Radius)
{
// Ancienne methode
  myType   = 1;
  myError  = 0;
  myRadius = Radius;

// Nouvelle methode
  myAdpPath = new (GeomAdaptor_HCurve) (Path);
  Handle(Geom_Circle) C = new (Geom_Circle) (gp::XOY(), Radius);
  C->Rotate(gp::OZ(),PI/2.);
  
  mySec = new (GeomFill_UniformSection) (C, Path->FirstParameter(), 
					    Path->LastParameter());
  Handle (GeomFill_CorrectedFrenet)TLaw = new (GeomFill_CorrectedFrenet) ();
  myLoc = new (GeomFill_CurveAndTrihedron) (TLaw);
  myLoc->SetCurve(myAdpPath);

#ifdef DRAW
  if  (Affich) {
    char* Temp = "TheSect" ;
    DrawTrSurf::Set(Temp, C);   
//    DrawTrSurf::Set("TheSect", C);   
  }
#endif

}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void GeomFill_Pipe::Init(const Handle(Geom_Curve)& Path,
			 const Handle(Geom_Curve)& FirstSect,
			 const GeomFill_Trihedron Option)
{
  Handle(Geom_Curve) Sect;
  Handle(GeomFill_TrihedronLaw) TLaw;
  myAdpPath = new (GeomAdaptor_HCurve) 
    (Handle(Geom_Curve)::DownCast(Path->Copy()));
  Standard_Real param =  Path->FirstParameter();

// Construction de la loi de triedre
  switch (Option) {
  case GeomFill_IsCorrectedFrenet :
    {
      TLaw = new (GeomFill_CorrectedFrenet) ();
      break;
    }

  case GeomFill_IsDarboux :
#if DEB
    {
      cout << "Option Darboux: non realisable" << endl; 
    }
#endif
  case GeomFill_IsFrenet :
    {
      TLaw = new (GeomFill_Frenet) ();
      break; 
    }

  case GeomFill_IsFixed :
    {     
      Standard_Real Eps = 1.e-9;
      gp_Vec V1(0,0,1), V2(0,1,0);
      gp_Dir D;
      GeomLProp_CLProps CP(Path, param, 2, Eps);
      if (CP.IsTangentDefined()) {
	CP.Tangent(D);
	V1.SetXYZ(D.XYZ());
	V1.Normalize();
	if (CP.Curvature() > Eps) {
	  CP.Normal(D);
	  V2.SetXYZ(D.XYZ());
	  V2.Normalize();
	}
	else {
	  gp_Pnt P0(0., 0., 0.);
	  gp_Ax2 Axe(P0, D);
	  D = Axe.XDirection ();
	  V2.SetXYZ(D.XYZ());
	  V2.Normalize();
	}
      }
      TLaw = new (GeomFill_Fixed) (V1, V2);
      break;
    }

  case GeomFill_IsConstantNormal :
    {     
      TLaw = new (GeomFill_Frenet) ();
      myLoc = new (GeomFill_CurveAndTrihedron) (TLaw);
      myLoc->SetCurve(myAdpPath);
      GeomFill_SectionPlacement Place(myLoc, FirstSect);
      Place.Perform(Precision::Confusion());
      Standard_Real ponsec = Place.ParameterOnSection();
      
      Standard_Real Eps = 1.e-9;
      gp_Vec V(0,1,0);
      gp_Dir D;
      GeomLProp_CLProps CP(FirstSect, ponsec, 2, Eps);
      if (CP.IsTangentDefined()) {
        CP.Tangent(D);
        if (CP.Curvature() > Eps) {
          CP.Normal(D);
          V.SetXYZ(D.XYZ());
          V.Normalize();
        }
        else {
          gp_Pnt P0(0., 0., 0.);
          gp_Ax2 Axe(P0, D);
          D = Axe.XDirection ();
          V.SetXYZ(D.XYZ());
          V.Normalize();
        }
      }
      TLaw = new (GeomFill_ConstantBiNormal) (V);
      break;
    }

  default :
    {
      Standard_ConstructionError::Raise
		("GeomFill::Init : Unknown Option");
    }
  }
 
  if (!TLaw.IsNull()) {
    myLoc = new (GeomFill_CurveAndTrihedron) (TLaw);
    myLoc->SetCurve(myAdpPath);
    GeomFill_SectionPlacement Place(myLoc, FirstSect);
    Place.Perform(Precision::Confusion());
    param =  Place.ParameterOnPath();
    Sect = Place.Section(Standard_False);

#ifdef DRAW
    if  (Affich) {
      char* Temp = "TheSect" ;
      DrawTrSurf::Set(Temp,Sect);
//      DrawTrSurf::Set("TheSect",Sect);
      
    }
#endif
    mySec = new (GeomFill_UniformSection) (Sect, 
					   Path->FirstParameter(), 
					   Path->LastParameter());
  }
}

//=======================================================================
//function : Init
//purpose  : sweep using Darboux's trihedron
//=======================================================================

void GeomFill_Pipe::Init(const Handle(Geom2d_Curve)& Path,
                         const Handle(Geom_Surface)& Support,
                         const Handle(Geom_Curve)& FirstSect)
{
  Handle(Geom_Curve) Sect;
  Handle(GeomFill_TrihedronLaw) TLaw = new (GeomFill_Darboux)();
  myAdpPath = 
    new Adaptor3d_HCurveOnSurface(Adaptor3d_CurveOnSurface(
		       new Geom2dAdaptor_HCurve(Path), 
		       new GeomAdaptor_HSurface(Support)));
 
  myLoc = new (GeomFill_CurveAndTrihedron) (TLaw);
  myLoc->SetCurve(myAdpPath);
  GeomFill_SectionPlacement Place(myLoc, FirstSect);
  Place.Perform(myAdpPath, Precision::Confusion());
  Sect = Place.Section(Standard_False);
  
#ifdef DRAW
  if  (Affich) {
    char* temp = "TheSect" ;
    DrawTrSurf::Set(temp,Sect);
//    DrawTrSurf::Set("TheSect",Sect);
    
  }
#endif
  mySec = new (GeomFill_UniformSection) (Sect, 
					 myAdpPath->FirstParameter(), 
					 myAdpPath->LastParameter());  
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void GeomFill_Pipe::Init(const Handle(Geom_Curve)& Path, 
			 const Handle(Geom_Curve)& FirstSect,
			 const gp_Dir& Direction)
{
  Init();

  Handle(Geom_Curve) Sect;
  myAdpPath = new (GeomAdaptor_HCurve) 
    (Handle(Geom_Curve)::DownCast(Path->Copy()));
  gp_Vec V;
  V.SetXYZ(Direction.XYZ());
  Handle (GeomFill_ConstantBiNormal) TLaw = 
    new (GeomFill_ConstantBiNormal) (V);

  myLoc = new (GeomFill_CurveAndTrihedron) (TLaw);
  myLoc->SetCurve(myAdpPath);
  GeomFill_SectionPlacement Place(myLoc, FirstSect);
  Place.Perform(Precision::Confusion());
  Sect = Place.Section(Standard_False);

#ifdef DRAW
  if  (Affich) {
    char* temp = "TheSect" ;
    DrawTrSurf::Set(temp,Sect);    
//    DrawTrSurf::Set("TheSect",Sect);    
  }
#endif
  mySec = new (GeomFill_UniformSection) (Sect, 
					 Path->FirstParameter(), 
					 Path->LastParameter());
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void GeomFill_Pipe::Init(const Handle(Geom_Curve)& Path, 
			 const TColGeom_SequenceOfCurve& NSections)
{
  myType      = 3;
  myError  = 0;
  myRadius    = 0;

  Handle(GeomFill_TrihedronLaw) TLaw;
  TLaw = new (GeomFill_CorrectedFrenet) ();
  myAdpPath = new (GeomAdaptor_HCurve) 
    (Handle(Geom_Curve)::DownCast(Path->Copy()));
  if (!TLaw.IsNull()) {
    myLoc = new (GeomFill_CurveAndTrihedron) (TLaw);
    myLoc->SetCurve(myAdpPath);
    TColGeom_SequenceOfCurve SeqC;
    TColStd_SequenceOfReal SeqP;
    SeqC.Clear();
    SeqP.Clear();
    Standard_Integer i ;
    for ( i = 1; i<=NSections.Length(); i++) {
      GeomFill_SectionPlacement Place(myLoc, NSections(i));
      Place.Perform(Precision::Confusion());
      SeqP.Append(Place.ParameterOnPath());
      SeqC.Append(Place.Section(Standard_False));
    }

    // verification des orientations
    TColGeom_SequenceOfCurve NewSeq;
    if (CheckSense(SeqC,NewSeq)) SeqC = NewSeq;

    // verification des parametres
    Standard_Boolean play_again = Standard_True;
    while (play_again) {
      play_again = Standard_False;
      for (i = 1; i<=NSections.Length(); i++) {
	for (Standard_Integer j = i; j<=NSections.Length(); j++) {
	  if (SeqP.Value(i)>SeqP.Value(j)) {
	    SeqP.Exchange(i,j);
	    SeqC.Exchange(i,j);
	    play_again = Standard_True;
	  }
	}
      }
    }
    for ( i = 1; i<NSections.Length(); i++) {
      if ( Abs(SeqP.Value(i+1)-SeqP.Value(i)) < Precision::PConfusion()) {
       Standard_ConstructionError::Raise
	 ("GeomFill_Pipe::Init with NSections : invalid parameters");     
      }
    }
    
    // creation de la NSections
    Standard_Real first = Path->FirstParameter(),
                  last = Path->LastParameter();
    Standard_Real deb,fin;
    deb = SeqC.First()->FirstParameter();
    fin = SeqC.First()->LastParameter();
    mySec = new (GeomFill_NSections) (SeqC,SeqP,deb,fin,first,last);
  }
}


//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void GeomFill_Pipe::Init(const Handle(Geom_Curve)& Path, 
			 const Handle(Geom_Curve)& FirstSect,
			 const Handle(Geom_Curve)& LastSect)
{
  myType      = 3;
  myError  = 0;
  myRadius    = 0;
  Standard_Real first = Path->FirstParameter(),
                last = Path->LastParameter();
  Handle(GeomFill_TrihedronLaw) TLaw;
  TLaw = new (GeomFill_CorrectedFrenet) ();
  myAdpPath = new (GeomAdaptor_HCurve) 
    (Handle(Geom_Curve)::DownCast(Path->Copy()));

  if (!TLaw.IsNull()) {
    myLoc = new (GeomFill_CurveAndTrihedron) (TLaw);
    myLoc->SetCurve(myAdpPath);
    TColGeom_SequenceOfCurve SeqC;
    TColStd_SequenceOfReal SeqP;
    SeqC.Clear();
    SeqP.Clear();
    // sequence des sections
    GeomFill_SectionPlacement Pl1(myLoc, FirstSect);
    Pl1.Perform(first,Precision::Confusion());
    SeqC.Append(Pl1.Section(Standard_False));
    GeomFill_SectionPlacement Pl2(myLoc, LastSect);
    Pl2.Perform(first,Precision::Confusion());
    SeqC.Append(Pl2.Section(Standard_False));
    // sequence des parametres associes
    SeqP.Append(first);
    SeqP.Append(last);

    // verification de l'orientation
    TColGeom_SequenceOfCurve NewSeq;
    if (CheckSense(SeqC,NewSeq)) SeqC = NewSeq;

    // creation de la NSections
    Standard_Real deb,fin;
    deb =  SeqC.First()->FirstParameter();
    fin =  SeqC.First()->LastParameter();
    mySec = new (GeomFill_NSections) (SeqC,SeqP,deb,fin,first,last);
  }
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void GeomFill_Pipe::Init(const Handle(Adaptor3d_HCurve)& Path, 
			 const Handle(Adaptor3d_HCurve)& Curve1,
			 const Handle(Adaptor3d_HCurve)& Curve2,
			 const Standard_Real           Radius)
{
  myType         = 4;
  myError        = 0;
  myRadius       = Radius;
  myAdpPath      = Path;
  myAdpFirstSect = Curve1;
  myAdpLastSect  = Curve2;
}

//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

void GeomFill_Pipe::Perform(const Standard_Boolean WithParameters, 
			    const Standard_Boolean Polynomial) 
{

  if ( (! myLoc.IsNull()) && (! mySec.IsNull()) ) {
    Perform(1.e-4, Polynomial);
    return;
  }

  myPolynomial = Polynomial;
  // on traite la cas tuyau sur arete Type = 4
  if ( myPolynomial) {
    ApproxSurf(WithParameters);
    return;
  }   
  if ( !KPartT4() ) ApproxSurf(WithParameters);

}

//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================
void GeomFill_Pipe::Perform(const Standard_Real Tol,
			    const Standard_Boolean Polynomial,
			    const GeomAbs_Shape Conti,
			    const Standard_Integer DegMax,
			    const Standard_Integer NbMaxSegment)
{
 GeomAbs_Shape TheConti;
 switch (Conti) {
 case GeomAbs_C0:
   {
     TheConti = GeomAbs_C0;
   } 
   break; 
 case GeomAbs_G1:
 case GeomAbs_C1:
   {
     TheConti = GeomAbs_C1;
   } 
   break;
 case GeomAbs_G2:
 case GeomAbs_C2:
   {
     TheConti = GeomAbs_C2;
   } 
   break;
 default:
   TheConti = GeomAbs_C2;// On ne sait pas faire mieux !
 }
 Handle (Approx_SweepFunction) Func;
 Func.Nullify();

 if (myType == 4) { 
   if (!KPartT4()) {
     Func = new (GeomFill_CircularBlendFunc)(myAdpPath,  
					     myAdpFirstSect,
					     myAdpLastSect,
					     myRadius,
					     Polynomial);

     Approx_SweepApproximation App(Func);
     App.Perform(myAdpPath->FirstParameter(),
		 myAdpPath->LastParameter(),
		 Tol, Tol, 0., 0.01,
		 TheConti, DegMax, NbMaxSegment);
#if DEB
   cout << "Tuyau : ";
   App.Dump(cout);
   cout << endl;
#endif
     if (App.IsDone()) {
       mySurface = new Geom_BSplineSurface(App.SurfPoles(),
					   App.SurfWeights(),
					   App.SurfUKnots(),
					   App.SurfVKnots(),
					   App.SurfUMults(),
					   App.SurfVMults(),
					   App.UDegree(),
					   App.VDegree());
       myError = App.MaxErrorOnSurf();
     }
     else {
       Standard_ConstructionError::Raise
	 ("GeomFill_Pipe::Perform : Cannot make a surface");     
     }
   }
 }
 else if ( (! myLoc.IsNull()) && (! mySec.IsNull()) ) {
   GeomFill_Sweep Sweep(myLoc, myKPart);
   Sweep.SetTolerance(Tol);
   Sweep.Build(mySec, GeomFill_Location, TheConti, DegMax, NbMaxSegment);   
   if (Sweep.IsDone()) {
      mySurface = Sweep.Surface();
      myError = Sweep.ErrorOnSurface();
   }
   else {
     Standard_ConstructionError::Raise
	 ("GeomFill_Pipe::Perform : Cannot make a surface");     
     }     
   }
 else {
   Perform(Standard_True, Polynomial);
 }
}    
   
   
//=======================================================================
//function : KPartT4
//purpose  : Pour gerer les cas particulier de type 4
//=======================================================================
Standard_Boolean GeomFill_Pipe::KPartT4()
{
 Standard_Boolean Ok = Standard_False;
 // -------    Cas du Cylindre  --------------------------
 if (myAdpPath->GetType() == GeomAbs_Line &&
     myAdpFirstSect->GetType() == GeomAbs_Line &&
     myAdpLastSect ->GetType() == GeomAbs_Line   ) {
     // try to generate a cylinder.
     gp_Ax1 A0 = myAdpPath     ->Line().Position();
     gp_Ax1 A1 = myAdpFirstSect->Line().Position();
     gp_Ax1 A2 = myAdpLastSect ->Line().Position();
     // direction must be the same.
     gp_Dir D0 = A0.Direction();
     gp_Dir D1 = A1.Direction();
     gp_Dir D2 = A2.Direction();
     if (!D0.IsEqual(D1,Precision::Angular()) ||
	 !D1.IsEqual(D2,Precision::Angular())   ) {
       return Ok;
     }

     // the length of the line must be te same
     Standard_Real L0 = 
       myAdpPath->LastParameter() - myAdpPath->FirstParameter();
     Standard_Real L1 = 
       myAdpFirstSect->LastParameter() - myAdpFirstSect->FirstParameter();
     Standard_Real L2 =
       myAdpLastSect->LastParameter() - myAdpLastSect->FirstParameter();
     if (Abs(L1-L0) > Precision::Confusion() ||
	 Abs(L2-L0) > Precision::Confusion()   ) {
       return Ok;
      }

     // the first points must be normal to the path.
     gp_Pnt P0 = myAdpPath     ->Value(myAdpPath     ->FirstParameter());
     gp_Pnt P1 = myAdpFirstSect->Value(myAdpFirstSect->FirstParameter());
     gp_Pnt P2 = myAdpLastSect ->Value(myAdpLastSect ->FirstParameter());
     gp_Dir V1(gp_Vec(P0,P1));
     gp_Dir V2(gp_Vec(P0,P2));
     if (Abs(V1.Dot(D0)) > Precision::Confusion() ||
	 Abs(V2.Dot(D0)) > Precision::Confusion()   )  return Ok;

     // the result is a cylindrical surface.
     gp_Dir X(V1), Y(V2), ZRef;
     ZRef = X.Crossed(Y);

     gp_Ax3 Axis( A0.Location(), D0, X);
     if ( ZRef.Dot(D0) < 0.)
       Axis.YReverse();

     // rotate the surface to set the iso U = 0 not in the result.
     Axis.Rotate(gp_Ax1(P0,ZRef),-PI/2.);
      
     mySurface = new Geom_CylindricalSurface( Axis, myRadius);
     Standard_Real Alpha = V1.AngleWithRef(V2,ZRef);
     mySurface = 
       new Geom_RectangularTrimmedSurface(mySurface,
					  PI/2. , 
					  PI/2. + Alpha,
					  myAdpPath->FirstParameter(),
					  myAdpPath->LastParameter());
     Ok = Standard_True; //C'est bien un cylindre
   }
 // -----------    Cas du tore  ----------------------------------
 else if (myAdpPath->GetType()      == GeomAbs_Circle &&
	  myAdpFirstSect->GetType() == GeomAbs_Circle &&
	  myAdpLastSect->GetType()  == GeomAbs_Circle   ) {
   // try to generate a toroidal surface.    
   // les 3 cercles doivent avoir meme angle d'ouverture
   Standard_Real Alp0 =
     myAdpPath->FirstParameter() - myAdpPath->LastParameter();
   Standard_Real Alp1 =
     myAdpFirstSect->FirstParameter() - myAdpFirstSect->LastParameter();
   Standard_Real Alp2 =
     myAdpLastSect->FirstParameter() - myAdpLastSect->LastParameter();

   if (Abs(Alp0-Alp1) > Precision::Angular() ||
       Abs(Alp0-Alp2) > Precision::Angular()   ) return Ok;

   gp_Ax2 A0 = myAdpPath     ->Circle().Position();
   gp_Ax2 A1 = myAdpFirstSect->Circle().Position();
   gp_Ax2 A2 = myAdpLastSect ->Circle().Position();
   gp_Dir D0 = A0.Direction();
   gp_Dir D1 = A1.Direction();
   gp_Dir D2 = A2.Direction();
   gp_Pnt P0 = myAdpPath     ->Value(myAdpPath     ->FirstParameter());
   gp_Pnt P1 = myAdpFirstSect->Value(myAdpFirstSect->FirstParameter());
   gp_Pnt P2 = myAdpLastSect ->Value(myAdpLastSect ->FirstParameter());

   // les 3 directions doivent etre egales.
   if (!D0.IsEqual(D1,Precision::Angular()) ||
       !D1.IsEqual(D2,Precision::Angular())   )  return Ok;

   // les 3 ax1 doivent etre confondus.
   gp_Lin L(A0.Axis());
   if (!L.Contains(A1.Location(),Precision::Confusion()) ||
       !L.Contains(A2.Location(),Precision::Confusion())   )  return Ok;

   // les 3 premiers points doivent etre dans la meme section.
   gp_Dir V1(gp_Vec(P0,P1));
   gp_Dir V2(gp_Vec(P0,P2));
   gp_Circ Ci = myAdpPath->Circle();
   gp_Vec YRef = 
     ElCLib::CircleDN(myAdpPath->FirstParameter(),A0, Ci.Radius(), 1);
   if (Abs(V1.Dot(YRef)) > Precision::Confusion() ||
       Abs(V2.Dot(YRef)) > Precision::Confusion()   )  return Ok;

   // OK it`s a Toroidal Surface !!  OUF !!
   gp_Torus T(A0,Ci.Radius(),myRadius);
   gp_Vec XRef(A0.Location(),P0);
   // au maximum on fait un tore d`ouverture en V = PI
   Standard_Real VV1 = V1.AngleWithRef(XRef,YRef);
   Standard_Real VV2 = V2.AngleWithRef(XRef,YRef);
   Standard_Real deltaV = V2.AngleWithRef(V1,YRef);
   if (deltaV < 0.) {
     T.VReverse();
     VV1 = -VV1;
     VV2 = 2*PI + VV1 - deltaV;
   }
   mySurface = new Geom_RectangularTrimmedSurface
     (new Geom_ToroidalSurface(T),
      myAdpPath->FirstParameter(),myAdpPath->LastParameter(),VV1,VV2);
   myExchUV = Standard_True;
   Ok = Standard_True;
 }

 return Ok;
}

//=======================================================================
//function : ApproxSurf
//purpose  : 
//=======================================================================
void GeomFill_Pipe::ApproxSurf(const Standard_Boolean WithParameters) {

  // Traitment of general case. 
  // generate a sequence of the section by <SweepSectionGenerator> 
  // and approximate this sequence. 
  
  if (myType != 4) Standard_ConstructionError::Raise("GeomFill_Pipe");
  GeomFill_SweepSectionGenerator Section(myAdpPath, myAdpFirstSect,
					   myAdpLastSect,myRadius);

  Section.Perform(myPolynomial);
  
#ifdef DEB
  if ( Affich) {
    Standard_Integer NbPoles,NbKnots,Degree,NbPoles2d;
    Section.GetShape(NbPoles,NbKnots,Degree,NbPoles2d);

    TColgp_Array1OfPnt      Poles  (1,NbPoles);
    TColgp_Array1OfPnt2d    Poles2d(1,NbPoles);
    TColStd_Array1OfReal    Weights(1,NbPoles);
    TColStd_Array1OfInteger Mults  (1,NbKnots);
    TColStd_Array1OfReal    Knots  (1,NbKnots);
    Section.Knots(Knots);
    Section.Mults(Mults);

    for (Standard_Integer i = 1; i <= Section.NbSections(); i++) {
      NbSections++;
      Section.Section(i,Poles,Poles2d,Weights);
      Handle(Geom_BSplineCurve) BS = 
	new Geom_BSplineCurve(Poles,Weights,Knots,Mults,Degree);
#ifdef DRAW
      char name[256];
      sprintf(name,"SECT_%d",NbSections);
      DrawTrSurf::Set(name,BS);
#endif
    }
  }
#endif

  Handle(GeomFill_Line) Line = new GeomFill_Line(Section.NbSections());
  Standard_Integer NbIt = 0;
  Standard_Real T3d =  Precision::Approximation();
  Standard_Real T2d =  Precision::PApproximation();
  GeomFill_AppSweep App( 4, 8, T3d, T2d, NbIt, WithParameters);
  
  App.Perform( Line, Section, 30);

  if ( !App.IsDone()) {
#ifdef DEB    
    // on affiche les sections sous debug
    Standard_Integer NbPoles,NbKnots,Degree,NbPoles2d;
    Section.GetShape(NbPoles,NbKnots,Degree,NbPoles2d);

    TColgp_Array1OfPnt      Poles  (1,NbPoles);
    TColgp_Array1OfPnt2d    Poles2d(1,NbPoles);
    TColStd_Array1OfReal    Weights(1,NbPoles);
    TColStd_Array1OfInteger Mults  (1,NbKnots);
    TColStd_Array1OfReal    Knots  (1,NbKnots);
    Section.Knots(Knots);
    Section.Mults(Mults);

    for (Standard_Integer i = 1; i <= Section.NbSections(); i++) {
      Section.Section(i,Poles,Poles2d,Weights);
      Handle(Geom_BSplineCurve) BS = 
	new Geom_BSplineCurve(Poles,Weights,Knots,Mults,Degree);
#ifdef DRAW
      char name[256];
      sprintf(name,"sect_%d",i);
      DrawTrSurf::Set(name,BS);
#endif
    }
#endif
    StdFail_NotDone::Raise("Pipe : App not done");
  }
  else {
    Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, 
    NbVKnots;
    App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
    
    mySurface = new Geom_BSplineSurface(App.SurfPoles(),
					App.SurfWeights(),
					App.SurfUKnots(),
					App.SurfVKnots(),
					App.SurfUMults(),
					App.SurfVMults(),
					App.UDegree(),
					App.VDegree());
    Standard_Real t2d;
    App.TolReached(myError, t2d);
  }
}