summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepDS/TopOpeBRepDS_BuildTool.cxx
blob: 828a6bc62703b0e2f6c2abf5b6737b6cb8e708e5 (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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
// File:	TopOpeBRepDS_BuildTool.cxx
// Created:	Thu Jun 17 18:30:52 1993
// Author:	Jean Yves LEBEY
//		<jyl@zerox>

#include <TopOpeBRepDS_BuildTool.ixx>

#include <TopoDS.hxx>
#include <TopExp_Explorer.hxx>
#include <TopAbs.hxx>
#include <BRep_Tool.hxx>
#include <TopOpeBRepTool_GeomTool.hxx>
#include <TopOpeBRepTool_ShapeTool.hxx>
#include <TopOpeBRepTool_OutCurveType.hxx>
#include <TopOpeBRepDS_SurfaceCurveInterference.hxx>
#include <Precision.hxx>
#include <Geom_Surface.hxx>
#include <GeomAdaptor_Curve.hxx>
#include <Geom_BSplineCurve.hxx>
#include <Geom_Plane.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <GeomAPI_ProjectPointOnSurf.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <ElCLib.hxx>
#include <gp.hxx>
#include <gp_Vec2d.hxx>
#include <TopExp.hxx>
#include <TopLoc_Location.hxx>
#include <TopOpeBRepTool_ShapeTool.hxx>
#include <Geom_SphericalSurface.hxx>
#include <Standard_ProgramError.hxx>
#include <Standard_NotImplemented.hxx>
#include <gp_Pnt.hxx>
#include <gp_Pnt2d.hxx>
#include <TopOpeBRepDS_Dumper.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <Geom2d_Conic.hxx>
#include <Geom2d_Circle.hxx>
#include <Geom2d_Line.hxx>
#include <Geom2d_Ellipse.hxx>
#include <Geom2d_Parabola.hxx>
#include <Geom2d_Hyperbola.hxx>
#include <Geom2d_BezierCurve.hxx>
#include <Geom2d_BSplineCurve.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <Geom2d_OffsetCurve.hxx>
#include <Geom_Conic.hxx>
#include <Geom_Circle.hxx>
#include <Geom_Line.hxx>
#include <Geom_Ellipse.hxx>
#include <Geom_Parabola.hxx>
#include <Geom_Hyperbola.hxx>
#include <Geom_BezierCurve.hxx>
#include <Geom_BSplineCurve.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_OffsetCurve.hxx>
// includes especially needed by the static Project function
#include <BRep_Tool.hxx>
#include <GeomAdaptor_Curve.hxx>
#include <Extrema_ExtPC.hxx>
#include <gp_Pnt.hxx>
#include <Extrema_POnCurv.hxx>
#include <Geom_Curve.hxx>
#include <Geom_BSplineCurve.hxx>
#include <TCollection_AsciiString.hxx>
#include <Geom2d_TrimmedCurve.hxx>

#ifdef DEB
Standard_EXPORT Standard_Boolean TopOpeBRepDS_GettraceDSNC();
Standard_EXPORT Standard_Boolean TopOpeBRepDS_GettraceBUTO();
Standard_EXPORT Standard_Boolean TopOpeBRepDS_GettraceTRPE();
Standard_EXPORT Standard_Boolean TopOpeBRepDS_GettraceSANTRAN();
static void DUMPCURVES(const Handle(Geom_Curve)& C3D,const TopOpeBRepDS_Curve& C)
{
  if ( !C3D.IsNull() ) {
    Standard_Real dp1 = C3D->FirstParameter();
    Standard_Real dp2 = C3D->LastParameter();
    cout<<"TopOpeBRepDS_BuildTool : C3D = "<<dp1<<","<<dp2<<endl;
  }
  if ( !C.Curve1().IsNull() ) {
    Standard_Real dp1 = C.Curve1()->FirstParameter();
    Standard_Real dp2 = C.Curve1()->LastParameter();
    cout<<"TopOpeBRepDS_BuildTool : PC1 = "<<dp1<<","<<dp2<<endl;
  }
  if ( !C.Curve2().IsNull() ) {
    Standard_Real dp1 = C.Curve2()->FirstParameter();
    Standard_Real dp2 = C.Curve2()->LastParameter();
    cout<<"TopOpeBRepDS_BuildTool : PC2 = "<<dp1<<","<<dp2<<endl;
  }
}
#endif

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

//-----------------------------------------------------------------------
//function : PrintSurface
//purpose  : print the name of a surface on a stream
// a mettre dans TopOpeBRepDS_Dumper.cxx NYI
//-----------------------------------------------------------------------

#ifdef DEB
static Standard_OStream& PrintSurface(const TopoDS_Face& F, Standard_OStream& s)
{
  BRepAdaptor_Surface STA_Surface(F);
  GeomAbs_SurfaceType t =  STA_Surface.GetType();
  switch(t) {
  case GeomAbs_Plane               : s<<"PLANE";               break;
  case GeomAbs_Cylinder            : s<<"CYLINDER";            break;
  case GeomAbs_Cone                : s<<"CONE";                break;
  case GeomAbs_Sphere              : s<<"SPHERE";              break;
  case GeomAbs_Torus               : s<<"TORUS";               break;
  case GeomAbs_BezierSurface       : s<<"BEZIERSURFACE";       break;
  case GeomAbs_BSplineSurface      : s<<"BSPLINESURFACE";      break;
  case GeomAbs_SurfaceOfRevolution : s<<"SURFACEOFREVOLUTION"; break;
  case GeomAbs_SurfaceOfExtrusion  : s<<"SURFACEOFEXTRUSION";  break;
  case GeomAbs_OtherSurface : default :  s<<"OTHERSURFACE";        break;
  }
  return s;                                                    
}
#endif

Standard_EXPORT Handle(Geom2d_Curve) BASISCURVE2D(const Handle(Geom2d_Curve)& C);

//-----------------------------------------------------------------------
//function : GetOrigin
//purpose  : a mettre dans TopOpeBRepDS_Dumper.cxx NYI
//-----------------------------------------------------------------------

#ifdef DEB
static Standard_Boolean GetOrigin(const Handle(Geom2d_Curve)& PCIN, gp_Pnt2d& o)
{
  if (PCIN.IsNull()) return Standard_False;
  Handle(Geom2d_Curve) PC = ::BASISCURVE2D(PCIN);
  if (PC.IsNull()) return Standard_False;

  Handle(Standard_Type) T = PC->DynamicType();
  if      ((T==STANDARD_TYPE(Geom2d_Circle)) ||
	   (T==STANDARD_TYPE(Geom2d_Ellipse))  ||
	   (T==STANDARD_TYPE(Geom2d_Parabola)) ||
	   (T==STANDARD_TYPE(Geom2d_Hyperbola))) {
    o = Handle(Geom2d_Conic)::DownCast(PC)->Location();
    return Standard_True;
  }
  else if (T==STANDARD_TYPE(Geom2d_Line)) {
    o = Handle(Geom2d_Line)::DownCast(PC)->Location();
    return Standard_True;
  }
  else  {
    return Standard_False;
  }
  return Standard_False;
}
#endif

//-----------------------------------------------------------------------
//function : GetOrigin
//purpose  : a mettre dans TopOpeBRepDS_Dumper.cxx NYI
//-----------------------------------------------------------------------

#ifdef DEB
static Standard_Boolean GetOrigin(const Handle(Geom_Curve)& CIN, gp_Pnt& o)
{
  if (CIN.IsNull()) return Standard_False;
  Handle(Geom_Curve) C = TopOpeBRepTool_ShapeTool::BASISCURVE(CIN);
  if (C.IsNull()) return Standard_False;

  Handle(Standard_Type) T = C->DynamicType();
  if      ((T==STANDARD_TYPE(Geom_Circle)) ||
	   (T==STANDARD_TYPE(Geom_Ellipse))  ||
	   (T==STANDARD_TYPE(Geom_Parabola)) ||
	   (T==STANDARD_TYPE(Geom_Hyperbola))) {
    o = Handle(Geom_Conic)::DownCast(C)->Location();
    return Standard_True;
  }
  else if (T==STANDARD_TYPE(Geom_Line)) {
    o = Handle(Geom_Line)::DownCast(C)->Position().Location();
    return Standard_True;
  }
  else  {
    return Standard_False;
  }
  return Standard_False;
}
#endif

Standard_Boolean FUN_UisoLineOnSphe
(const TopoDS_Shape& F,
 const Handle(Geom2d_Curve)& PC)
{  
  if (PC.IsNull()) return Standard_False;

  Handle(Geom_Surface) SSS = TopOpeBRepTool_ShapeTool::BASISSURFACE(TopoDS::Face(F));
  Handle(Geom2d_Curve) LLL = ::BASISCURVE2D(PC);
  Handle(Standard_Type) TS = SSS->DynamicType();
  Handle(Standard_Type) T2 = LLL->DynamicType();
  Standard_Boolean issphere = (TS == STANDARD_TYPE(Geom_SphericalSurface)); 
  Standard_Boolean isline2d = (T2 == STANDARD_TYPE(Geom2d_Line));
  Standard_Boolean isisoU = Standard_False;
  if (issphere && isline2d) {
    Handle(Geom2d_Line) L = Handle(Geom2d_Line)::DownCast(LLL);
    const gp_Dir2d& d = L->Direction();
    isisoU = (Abs(d.X()) < Precision::Parametric(Precision::Confusion()));
  }
  return isisoU;
}

//=======================================================================
//function : TopOpeBRepDS_BuildTool
//purpose  : 
//=======================================================================

TopOpeBRepDS_BuildTool::TopOpeBRepDS_BuildTool():
myCurveTool(TopOpeBRepTool_APPROX),
myOverWrite(Standard_True),
myTranslate(Standard_True)
{
}

//=======================================================================
//function : TopOpeBRepDS_BuildTool
//purpose  : 
//=======================================================================

TopOpeBRepDS_BuildTool::TopOpeBRepDS_BuildTool
  (const TopOpeBRepTool_OutCurveType O) : 
myCurveTool(O),
myOverWrite(Standard_True),
myTranslate(Standard_True)
{
}

//=======================================================================
//function : TopOpeBRepDS_BuildTool
//purpose  : 
//=======================================================================

TopOpeBRepDS_BuildTool::TopOpeBRepDS_BuildTool
  (const TopOpeBRepTool_GeomTool& GT) : 
myCurveTool(GT),
myOverWrite(Standard_True),
myTranslate(Standard_True)
{
}

Standard_Boolean TopOpeBRepDS_BuildTool::OverWrite()const
{
  return myOverWrite;
}

void TopOpeBRepDS_BuildTool::OverWrite(const Standard_Boolean O)
{
  myOverWrite = O;
}

Standard_Boolean TopOpeBRepDS_BuildTool::Translate()const
{
  return myTranslate;
}

void TopOpeBRepDS_BuildTool::Translate(const Standard_Boolean T)
{
  myTranslate = T;
}

//=======================================================================
//function : GetGeomTool
//purpose  : 
//=======================================================================

const TopOpeBRepTool_GeomTool& TopOpeBRepDS_BuildTool::GetGeomTool() const 
{
  const TopOpeBRepTool_GeomTool& GT = myCurveTool.GetGeomTool();
  return GT;
}

//=======================================================================
//function : ChangeGeomTool
//purpose  : 
//=======================================================================

TopOpeBRepTool_GeomTool& TopOpeBRepDS_BuildTool::ChangeGeomTool()
{
  TopOpeBRepTool_GeomTool& GT = myCurveTool.ChangeGeomTool();
  return GT;
}

//=======================================================================
//function : MakeVertex
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeVertex(TopoDS_Shape& V, 
					 const TopOpeBRepDS_Point& P)const 
{
  myBuilder.MakeVertex(TopoDS::Vertex(V),P.Point(),P.Tolerance());
}

#ifdef DEB
//-----------------------------------------------------------------------
static Standard_OStream& DUMPPNT(const gp_Pnt& P, Standard_OStream& OS)
//-----------------------------------------------------------------------
{ OS<<P.X()<<" "<<P.Y()<<" "<<P.Z(); return OS; }
#endif

//=======================================================================
//function : MakeEdge
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeEdge(TopoDS_Shape& E, 
				       const TopOpeBRepDS_Curve& C)const 
{
  // Gestion des courbes nulles pour carreaux pointus
  // RLE 28-6-94

  if (C.Curve().IsNull()) {
    myBuilder.MakeEdge(TopoDS::Edge(E));
    myBuilder.Degenerated(TopoDS::Edge(E),Standard_True);
    return;
  }

  const Handle(Geom_Curve)& GC = C.Curve();
  myBuilder.MakeEdge(TopoDS::Edge(E),GC,C.Tolerance());

  Standard_Boolean addorigin = Standard_False;
  Standard_Boolean setrange  = Standard_False;

  if (addorigin) {
    if ( GC->IsClosed() ) {
      // in case of a closed curve, insert in E a vertex located at the origin
      // of the curve C.
      TopoDS_Vertex V;
      Standard_Real first = GC->FirstParameter();
      gp_Pnt P = GC->Value(first);
      myBuilder.MakeVertex(V,P,C.Tolerance());
      myBuilder.Add(E,V);
      V.Reverse();
      myBuilder.Add(E,V);
      
      // If the curve is a degree 1 bspline set the range to 1 .. NbPoles
      Handle(Geom_BSplineCurve) BSC = Handle(Geom_BSplineCurve)::DownCast(GC);
      if (!BSC.IsNull()) {
	if (BSC->Degree() == 1) {
	  myBuilder.Range(TopoDS::Edge(E),1,BSC->NbPoles());
#ifdef DEB
	  if (TopOpeBRepDS_GettraceBUTO()) {
	    cout<<endl<<"TopOpeBRepDS_BuildTool : ";
	    cout<<"new range of "<< 1 <<" "<<BSC->NbPoles()<<endl;
	  }
#endif
	}
      }
      
#ifdef DEB
      if (TopOpeBRepDS_GettraceBUTO()) {
	cout<<"TopOpeBRepDS_BuildTool : ";
	cout<<"vertices on parameter "<<first<<endl;
	cout<<" point is "; DUMPPNT(P,cout); cout<<endl;
      }
#endif
    }
  }

  if (setrange) {
    Standard_Real first,last;
    Standard_Boolean rangedef = C.Range(first,last);
    if (rangedef) {
      Range(E,first,last);
#ifdef DEB
      if (TopOpeBRepDS_GettraceBUTO()) {
	cout<<"TopOpeBRepDS_BuildTool : ";
	cout<<"set edge range : "<<first<<" "<<last<<endl;
      }
#endif
    }
  }
}

//=======================================================================
//function : MakeEdge
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeEdge
(TopoDS_Shape& E, 
 const TopOpeBRepDS_Curve& C,
 const TopOpeBRepDS_DataStructure& BDS) const
{
  // Gestion des courbes nulles pour carreaux pointus
  // RLE 28-6-94

  TopoDS_Edge& EE = TopoDS::Edge(E);

  if (C.Curve().IsNull()) {
    myBuilder.MakeEdge(EE);
    myBuilder.Degenerated(EE,Standard_True);
    
    // Creation d'une arete avec PCurve connectee a la BDS Curve 
    // JYL 22-09-94
    Handle(TopOpeBRepDS_Interference) I = C.GetSCI1();
    Handle(TopOpeBRepDS_SurfaceCurveInterference) SCI;
    SCI=Handle(TopOpeBRepDS_SurfaceCurveInterference)::DownCast(I);
    Standard_Integer iS = SCI->Support();
    const TopOpeBRepDS_Surface& DSS = BDS.Surface(iS);
    const Handle(Geom_Surface)& GS  = DSS.Surface();
    const Handle(Geom2d_Curve)& PC  = SCI->PCurve();
    myBuilder.UpdateEdge(EE,PC,GS,TopLoc_Location(),DSS.Tolerance());
    return;
  }
  else {
    const Handle(Geom_Curve)& GC = C.Curve();
    myBuilder.MakeEdge(EE,GC,C.Tolerance());
  }

}

//=======================================================================
//function : MakeEdge
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeEdge
(TopoDS_Shape& E, 
 const Handle(Geom_Curve)& C,
 const Standard_Real Tol)const 
{
  myBuilder.MakeEdge(TopoDS::Edge(E),C,Tol);
}


//=======================================================================
//function : MakeEdge
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeEdge(TopoDS_Shape& E) const
{
  myBuilder.MakeEdge(TopoDS::Edge(E));
}


//=======================================================================
//function : MakeWire
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeWire(TopoDS_Shape& W)const 
{
  myBuilder.MakeWire(TopoDS::Wire(W));
}


//=======================================================================
//function : MakeFace
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeFace(TopoDS_Shape& F, 
				       const TopOpeBRepDS_Surface& S)const 
{
  myBuilder.MakeFace(TopoDS::Face(F),S.Surface(),S.Tolerance());
}


//=======================================================================
//function : MakeShell
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeShell(TopoDS_Shape& Sh)const 
{
  myBuilder.MakeShell(TopoDS::Shell(Sh));
}


//=======================================================================
//function : MakeSolid
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::MakeSolid(TopoDS_Shape& S)const 
{
  myBuilder.MakeSolid(TopoDS::Solid(S));
}


//=======================================================================
//function : CopyEdge
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::CopyEdge(const TopoDS_Shape& Ein, 
				       TopoDS_Shape& Eou)const 
{

  // Splendide evolution de BRep_Curve3D::BRep_Curve3D(Geom_Curve,Location)
  // apres modification de la primitive Sphere pour parametrisation de 
  // l'arete meridienne en -pi/2,+pi/2.
  // Ein est l'arete de couture complete d'une sphere complete
  // BRep_Tool::Range(Ein) --> -pi/2,+pi/2
  // BRep_Tool::Range(Ein.EmptyCopied()) --> 0,2pi
  // NYI reflexion sur la notion de Range d'une arete et de la geometrie
  // NYI sous jacente dans le cas ou, par construction, les vertex d'une
  // NYI arete on des valeurs de parametre HORS des bornes [first,last] de la
  // NYI courbe 3D support de l'arete (cas de l'arete de couture d'une sphere)
  // On redefinit desormais le range de l'arete Eou, a la place de se 
  // contenter du simplissime Eou = Ein.EmptyCopied();
  // merci les amis : correction bug PRO2586

  Standard_Real f,l;
  TopoDS_Edge E1 = TopoDS::Edge(Ein); 
  BRep_Tool::Range(E1,f,l);
  Eou = Ein.EmptyCopied();
  TopoDS_Edge E2 = TopoDS::Edge(Eou); 
  myBuilder.Range(E2,f,l);
}


//=======================================================================
//function : GetOrientedEdgeVertices
//purpose  : 
//=======================================================================

void TopOpeBRepDS_BuildTool::GetOrientedEdgeVertices
(TopoDS_Edge& E,
 TopoDS_Vertex& Vmin, TopoDS_Vertex& Vmax,
 Standard_Real& Parmin, Standard_Real& Parmax) const
{
  if ( E.Orientation() == TopAbs_FORWARD) 
    TopExp::Vertices(E,Vmin,Vmax);
  else
    TopExp::Vertices(E,Vmax,Vmin);
  if ( !Vmin.IsNull() && !Vmax.IsNull()) {  
    Parmin = BRep_Tool::Parameter(Vmin,E);
    Parmax = BRep_Tool::Parameter(Vmax,E);
  }
}

//=======================================================================
//function : UpdateEdgeCurveTol
//purpose  : 
//=======================================================================

void TopOpeBRepDS_BuildTool::UpdateEdgeCurveTol
//(const TopoDS_Face& F1,const TopoDS_Face& F2,
(const TopoDS_Face& ,const TopoDS_Face& ,
 TopoDS_Edge& E, const Handle(Geom_Curve)& C3Dnew,
// const Standard_Real tol3d,
 const Standard_Real ,
// const Standard_Real tol2d1,
 const Standard_Real ,
// const Standard_Real tol2d2,
 const Standard_Real ,
 Standard_Real& newtol,
 Standard_Real& newparmin,
 Standard_Real& newparmax) const

{
  if (C3Dnew.IsNull()) return;
  BRep_Builder BB;

  // newtol = max des tolerances atteintes en 3d
// JMB le 06 Juillet 1999
// les valeurs tol3d et tol2d1,tol2d2 proviennent des approx. Dans la version 2.0 de CasCade, 
// elles n'etaient pas calculees et on renvoyait systematiquement les valeurs initiales (a savoir)
// 1.E-7. Dans la version 2.1 de CasCade, ces valeurs sont desormais calculees selon un calcul
// d'erreur dans les Approx. Malheureusement, il apparait que ce calcul d'erreur renvoit dans la
// plupart des cas de tres grosses valeurs (parfois de l'ordre de 1.E-1). Ce qui amenait la topologie
// a coder des tolerances enormes dans les pieces resultats rendant celles-ci inexpoitables.
// De plus on essayait de rafiner la tolerance en appelant les UResolution sur les surfaces support.
// sur des surfaces tres particulieres, ce UREsolution n'a plus aucun sens et peut amener a des valeurs
// abberantes.
// On decide donc de laisser la tolerance de l'edge telle qu'elle est afin d'avoir un comportement similaire
// a 2.0. Jusqu'a present on a constate que des problemes avec la methode de calcul d'erreur des approx.


  newtol = 1.E-7;
//  Standard_Real r1,r2;
//  r1 = TopOpeBRepTool_ShapeTool::Resolution3d(F1,tol2d1);
//  r2 = TopOpeBRepTool_ShapeTool::Resolution3d(F2,tol2d2);
//  newtol=tol3d;
//  if (r1>newtol) newtol=r1;
//  if (r2>newtol) newtol=r2;
//#ifdef DEB
//  if (TopOpeBRepDS_GettraceDSNC()) cout<<"newtol = "<<newtol<<endl;
//#endif

//  newtol *= 1.5;

  TopoDS_Vertex Vmin,Vmax;
  Standard_Real parmin = 0,parmax = 0;
  GetOrientedEdgeVertices(E,Vmin,Vmax,parmin,parmax);
  
  Standard_Real tolmin=BRep_Tool::Tolerance(Vmin);
  if(newtol>tolmin) tolmin=newtol;
  Standard_Real tolmax=BRep_Tool::Tolerance(Vmax);
  if(newtol>tolmax) tolmax=newtol;


//  newparmin=C3Dnew->FirstParameter(); // -merge 04-07-97
//  newparmax=C3Dnew->LastParameter(); // -merge 04-07-97

 // +merge 04-07-97
  Handle(Geom_TrimmedCurve) GTC = Handle(Geom_TrimmedCurve)::DownCast(C3Dnew);
  if(GTC.IsNull()) {
    Handle(Geom_BSplineCurve) GBSC = Handle(Geom_BSplineCurve)::DownCast(C3Dnew);
    if(GBSC.IsNull()) {
      newparmin = parmin;
      newparmax = parmax;
    } else {
      newparmin=C3Dnew->FirstParameter();
      newparmax=C3Dnew->LastParameter();
    } 
  } else {
    newparmin=C3Dnew->FirstParameter();
    newparmax=C3Dnew->LastParameter();
  } // +merge 04-07-97

  if (Vmin.Orientation() == TopAbs_FORWARD) { 
    BB.UpdateVertex(Vmin,newparmin,E,tolmin);
    BB.UpdateVertex(Vmax,newparmax,E,tolmax);
  }
  else {
    BB.UpdateVertex(Vmin,newparmax,E,tolmin);
    BB.UpdateVertex(Vmax,newparmin,E,tolmax);
  }
  
//  DSBT.Curve3D(E,C3Dnew,newtol);  // -merge 04-07-97
  Curve3D(E,C3Dnew,newtol);
  
  // projection des vertex INTERNAL de E pour parametrage
  // sur la nouvelle courbe C3Dnew de l'arete E
  TopExp_Explorer exi(E,TopAbs_VERTEX);
  for (;exi.More(); exi.Next() ) {
    const TopoDS_Vertex& vi = TopoDS::Vertex(exi.Current());
    if ( vi.Orientation() != TopAbs_INTERNAL ) continue;
    gp_Pnt P = BRep_Tool::Pnt(vi);
    Standard_Real tolvi=TopOpeBRepTool_ShapeTool::Tolerance(vi);
    GeomAPI_ProjectPointOnCurve dm(P,C3Dnew,newparmin,newparmax);
    Standard_Boolean dmdone = dm.Extrema().IsDone();
    if ( dmdone ) {
      if ( dm.NbPoints() ) {
	Standard_Real newpar = dm.LowerDistanceParameter();
	BB.UpdateVertex(vi,newpar,E,tolvi);
      }
    }
  } // INTERNAL vertex
}

//=======================================================================
//function : ApproxCurves
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::ApproxCurves
(const TopOpeBRepDS_Curve& C,
 TopoDS_Edge& E,
 Standard_Integer& inewC,
 const Handle(TopOpeBRepDS_HDataStructure)& HDS) const
{
  TopOpeBRepDS_Curve newC1;
  inewC = HDS->MakeCurve(C,newC1);
  TopOpeBRepDS_Curve& newC = HDS->ChangeCurve(inewC);

#ifdef DEB
//  Standard_Boolean tDSNC = TopOpeBRepDS_GettraceDSNC();
  Standard_Boolean tBUTO = TopOpeBRepDS_GettraceBUTO();
#endif
  
  // C1 curves have been approximated by BSplines of degree 1 :
  // compute new geometry on curves.

  const TopoDS_Face& F1 = TopoDS::Face(newC.Shape1());
  const TopoDS_Face& F2 = TopoDS::Face(newC.Shape2());
  
  const Handle(Geom_Curve)& C3D = C.Curve();
  const Handle(Geom2d_Curve)& PC1 = C.Curve1();
  const Handle(Geom2d_Curve)& PC2 = C.Curve2();

  // Vmin,Vmax = bounding vertices of edge <E>
  // and their parameters parmin,parmax .

  TopoDS_Vertex Vmin,Vmax;
  Standard_Real parmin = 0,parmax = 0;
  GetOrientedEdgeVertices(E,Vmin,Vmax,parmin,parmax);

#ifdef DEB
  if (tBUTO) cout<<"Recompute1 min,max = "<<parmin<<","<<parmax<<endl;
  if (tBUTO) DUMPCURVES(C3D,C);
#endif
  
  Handle(Geom_Curve)   C3Dnew;
  Handle(Geom2d_Curve) PC1new;
  Handle(Geom2d_Curve) PC2new;
  Standard_Real tolreached3d,tolreached2d;
  
  Standard_Boolean approxMade = myCurveTool.MakeCurves(parmin,parmax,
			 C3D,PC1,PC2,F1,F2,
			 C3Dnew,PC1new,PC2new,
			 tolreached3d,tolreached2d);

  Standard_Real newtol,newparmin,newparmax;
  // MSV Nov 12, 2001: if approx failed than leave old curves of degree 1
  if (!approxMade) {
#ifdef DEB
    cout<<"TopOpeBRepDS_BuildTool::ApproxCurves : approx failed, leave curves of degree 1"
      <<endl;
#endif
    newtol = BRep_Tool::Tolerance(E);
    newparmin = parmin;
    newparmax = parmax;
    C3Dnew = C3D;
    PC1new = PC1;
    PC2new = PC2;
  }
  else {
    UpdateEdgeCurveTol
      (F1,F2,E,C3Dnew,tolreached3d,tolreached2d,tolreached2d,
       newtol,newparmin,newparmax);
  }

  if (!C3Dnew.IsNull()) {
    newC.DefineCurve(C3Dnew,newtol,Standard_False);
    newC.SetRange(newparmin,newparmax);
  }

  if (!PC1new.IsNull()) newC.Curve1(PC1new);
  if (!PC2new.IsNull()) newC.Curve2(PC2new);
}


//=======================================================================
//function : ComputePCurves
//purpose  : 
//=======================================================================
Standard_Boolean FUN_getUV
(const Handle(Geom_Surface) surf,
 const Handle(Geom_Curve) C3D,
 const Standard_Real par3d,
 Standard_Real& u0,
 Standard_Real& v0)
{
  gp_Pnt P3d; C3D->D0(par3d,P3d);
  GeomAPI_ProjectPointOnSurf pons(P3d,surf);
  if (pons.NbPoints() < 1) return Standard_False;
  pons.LowerDistanceParameters(u0,v0);
  return Standard_True;
}

Standard_Boolean FUN_reversePC
(Handle(Geom2d_Curve) PCnew,
 const TopoDS_Face& F,
 const gp_Pnt& P3DC3D,
 const Standard_Real par2d,
 const Standard_Real tol)
{
  gp_Pnt2d P2D; PCnew->D0(par2d,P2D);
  BRepAdaptor_Surface BAS(F,Standard_False);
  gp_Pnt P3D = BAS.Value(P2D.X(),P2D.Y());
  Standard_Boolean PCreversed = Standard_False;
  Standard_Boolean sam = P3D.IsEqual(P3DC3D,tol);
  PCreversed = !sam;

#ifdef DEB
#ifdef DRAW
  Standard_Boolean tBUTO = TopOpeBRepDS_GettraceBUTO();
  if (tBUTO) {FUN_draw(P3DC3D); FUN_draw(P3D);}
#endif
#endif

  if ( PCreversed ) {
    Handle(Geom2d_Curve) PC = ::BASISCURVE2D(PCnew);
    if (!PC.IsNull()) {
      Handle(Geom2d_Line) L = Handle(Geom2d_Line)::DownCast(PC);
      gp_Dir2d d = L->Direction();
      d.Reverse();
      L->SetDirection(d);
    }
  }
  return PCreversed;
}
Standard_Boolean FUN_makeUisoLineOnSphe
(const TopoDS_Face& F, // with geometry the spherical surface
 const Handle(Geom_Curve) C3D, 
 Handle(Geom2d_Curve) PCnew,
 const Standard_Real tol3d)
{
  // p3df,p3dl : C3d first and last parameters
  Standard_Real p3df = C3D->FirstParameter();
  Standard_Real p3dl = C3D->LastParameter();

  // u0,v0 : C3d(par3d) UV parameters
  Standard_Real deltainf = 0.243234, deltasup = 0.543345;
  Standard_Real par3dinf = (1-deltainf)*p3df + deltainf*p3dl;
  Standard_Real par3dsup = (1-deltasup)*p3df + deltasup*p3dl;
  Standard_Real uinf,vinf,usup,vsup;
  Handle(Geom_Surface) surf = BRep_Tool::Surface(F);
  if (!FUN_getUV(surf,C3D,par3dinf,uinf,vinf)) return Standard_False;
  if (!FUN_getUV(surf,C3D,par3dsup,usup,vsup)) return Standard_False;
  Standard_Real tol = Precision::Parametric(tol3d);
  if (Abs(uinf-usup) > tol) return Standard_False;
#ifdef DEB
//  Standard_Real deltav = vsup-vinf;
#endif

  Standard_Boolean isvgrowing = (vsup - vinf > -tol);
  gp_Dir2d vdir;
  if (isvgrowing) vdir = gp_Dir2d(0,1);
  else            vdir = gp_Dir2d(0,-1);
  
  gp_Pnt2d origin(uinf,vinf);
  origin.Translate(gp_Vec2d(vdir).Scaled(p3df-par3dinf));
  Handle(Geom2d_Curve) PC = ::BASISCURVE2D(PCnew);
  if (!PC.IsNull()) {  
    Handle(Geom2d_Line) L = Handle(Geom2d_Line)::DownCast(PC); 
    L->SetLin2d(gp_Lin2d(origin,vdir));
#ifdef DEB
#ifdef DRAW
  Standard_Boolean trc = TopOpeBRepDS_GettraceBUTO();
    if (trc) {
      FUN_draw(gp_Pnt2d(uinf,vinf));
      FUN_draw(gp_Pnt2d(usup,vsup));
      FUN_draw(L,p3dl-p3df);
    }
#endif
#endif
  } // (!PC.IsNull())
  
  return Standard_True;
}

void TopOpeBRepDS_BuildTool::ComputePCurves
(const TopOpeBRepDS_Curve& C,
 TopoDS_Edge& E,
 TopOpeBRepDS_Curve& newC,
 const Standard_Boolean comppc1,
 const Standard_Boolean comppc2,
 const Standard_Boolean compc3d) const
{
#ifdef DEB
//  Standard_Boolean tDSNC = TopOpeBRepDS_GettraceDSNC();
  Standard_Boolean tBUTO = TopOpeBRepDS_GettraceBUTO();
  Standard_Boolean tTRPE = TopOpeBRepDS_GettraceTRPE();
#endif
  
  const TopoDS_Face& F1 = TopoDS::Face(newC.Shape1());
  const TopoDS_Face& F2 = TopoDS::Face(newC.Shape2());
  
  const Handle(Geom_Curve)& C3D = C.Curve();
#ifdef DEB
//  const Handle(Geom2d_Curve)& PC1 = C.Curve1();
//  const Handle(Geom2d_Curve)& PC2 = C.Curve2();
#endif

  // get bounding vertices Vmin,Vmax supported by the new edge <E>
  // and their corresponding parameters parmin,parmax .

  TopoDS_Vertex Vmin,Vmax;Standard_Real parmin,parmax;
  GetOrientedEdgeVertices(E,Vmin,Vmax,parmin,parmax);

#ifdef DEB
  if (tBUTO) cout<<"Recompute2 min,max = "<<parmin<<","<<parmax<<endl;
  if (tBUTO) DUMPCURVES(C3D,C);
#endif

  Handle(Geom2d_Curve) PC1new;
  Handle(Geom2d_Curve) PC2new;
  
  if(C3D.IsNull()) {
    Standard_Real tolreached2d1 = Precision::Confusion() , tolreached2d2 = Precision::Confusion(), r1, r2, tol=0;
    if (comppc1) PC1new = myCurveTool.MakePCurveOnFace(F1,C3D,tolreached2d1);
    if (comppc2) PC2new = myCurveTool.MakePCurveOnFace(F2,C3D,tolreached2d2);
    
    r1 = TopOpeBRepTool_ShapeTool::Resolution3d(F1,tolreached2d1);
    r2 = TopOpeBRepTool_ShapeTool::Resolution3d(F2,tolreached2d2);
    tol = Max(tol,r1);
    tol = Max(tol,r2);
    newC.Tolerance(tol);
    
    if (!PC1new.IsNull()) newC.Curve1(PC1new);
    if (!PC2new.IsNull()) newC.Curve2(PC2new);

    return;
  }
  
  Handle(Geom_Curve) C3Dnew = C3D;
  
  if ( C3D->IsPeriodic() ) {
    // ellipse on cone : periodize parmin,parmax
    Standard_Real period = C3D->LastParameter() - C3D->FirstParameter();
    Standard_Real f,l;
    if (Vmin.Orientation() == TopAbs_FORWARD) { f = parmin; l = parmax; }
    else {                                      f = parmax; l = parmin; }
    parmin = f; parmax = l;
    ElCLib::AdjustPeriodic(f,f+period,Precision::PConfusion(),parmin,parmax);
    if (compc3d) C3Dnew = new Geom_TrimmedCurve(C3D,parmin,parmax);
    
#ifdef DEB
    if (tBUTO||tTRPE) cout<<"Recompute2 : parmin,parmax "<<f<<","<<l<<endl;
    if (tBUTO||tTRPE) cout<<"                   --> parmin,parmax "<<parmin<<","<<parmax<<endl;
#endif
  }
  
  Standard_Real tolreached3d = C.Tolerance();
  Standard_Real tolreached2d1 = C.Tolerance();
  Standard_Real tolreached2d2 = C.Tolerance();
  
  if (comppc1) PC1new = myCurveTool.MakePCurveOnFace(F1,C3Dnew,tolreached2d1);
  if (comppc2) PC2new = myCurveTool.MakePCurveOnFace(F2,C3Dnew,tolreached2d2);
  
  Standard_Real newtol,newparmin,newparmax;
  UpdateEdgeCurveTol(F1,F2,E,C3Dnew,tolreached3d,tolreached2d1,tolreached2d2,
		     newtol,newparmin,newparmax);
  
    // xpu : suite merge : 07-07-97
    // xpu : 17-06-97
    // Rmq : C1.Curve<i>() ne sert plus qu'a determiner si la courbe
    //       est une isos de la sphere
    // NYI : enlever FUN_reversePC
    Standard_Boolean UisoLineOnSphe1 = Standard_False;
    UisoLineOnSphe1 = ::FUN_UisoLineOnSphe(F1,PC1new);
    if (UisoLineOnSphe1) ::FUN_makeUisoLineOnSphe(F1,C3Dnew,PC1new,newtol);

    Standard_Boolean UisoLineOnSphe2 = Standard_False;
    UisoLineOnSphe2 = ::FUN_UisoLineOnSphe(F2,PC2new);
    if (UisoLineOnSphe2) ::FUN_makeUisoLineOnSphe(F2,C3Dnew,PC2new,newtol);
    // xpu : 17-06-97
    // xpu : suite merge : 07-07-97

  if (!C3Dnew.IsNull()) {
    newC.Curve(C3Dnew,newtol);
    newC.SetRange(newparmin, newparmax);
  }
  if (!PC1new.IsNull()) newC.Curve1(PC1new);
  if (!PC2new.IsNull()) newC.Curve2(PC2new);
  
#ifdef DEB
  if (tBUTO) DUMPCURVES(C3Dnew,newC);
#endif
}

//=======================================================================
//function : PutPCurves
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::PutPCurves
(const TopOpeBRepDS_Curve& newC,
 TopoDS_Edge& E,
 const Standard_Boolean comppc1,
 const Standard_Boolean comppc2) const
{

  TopoDS_Face& F1 = *((TopoDS_Face*)(void*)&(TopoDS::Face(newC.Shape1())));
  Handle(Geom2d_Curve) PC1 = newC.Curve1();
  if (!PC1.IsNull() && comppc1) {
    PCurve(F1,E,PC1);
  }
  
  TopoDS_Face& F2 = *((TopoDS_Face*)(void*)&(TopoDS::Face(newC.Shape2())));
  Handle(Geom2d_Curve) PC2 = newC.Curve2();
  if (!PC2.IsNull() && comppc2) {
    PCurve(F2,E,PC2);
  }
  
}

//=======================================================================
//function : RecomputeCurves
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::RecomputeCurves
(const TopOpeBRepDS_Curve& C,
// const TopoDS_Edge& oldE,
 const TopoDS_Edge& ,
 TopoDS_Edge& E,
 Standard_Integer& inewC,
 const Handle(TopOpeBRepDS_HDataStructure)& HDS) const
{
  const TopOpeBRepTool_GeomTool& GT = myCurveTool.GetGeomTool();
  const Standard_Boolean compc3d = GT.CompC3D();
  const Standard_Boolean comppc1 = GT.CompPC1();
  const Standard_Boolean comppc2 = GT.CompPC2();
  const Standard_Boolean comppc = comppc1 || comppc2;
  const Standard_Boolean iswalk = C.IsWalk();
  const Standard_Boolean approx = Approximation();
  
  const Handle(Geom_Curve)& C3D = C.Curve();
  if (comppc1 && C.Shape1().IsNull()) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 2");
  if (comppc2 && C.Shape2().IsNull()) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 3");
  TopoDS_Vertex Vmin,Vmax; TopExp::Vertices(E,Vmin,Vmax);
  if ( Vmin.IsNull() ) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 4");
  if ( Vmax.IsNull() ) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 5");
  
  if (iswalk && approx) {
    if (compc3d && C3D.IsNull()) Standard_ProgramError::Raise
      ("TopOpeBRepDS_BuildTool::RecomputeCurve 1");
    ApproxCurves(C, E, inewC, HDS);
    TopOpeBRepDS_Curve& newC = HDS->ChangeCurve(inewC);
    PutPCurves(newC, E, comppc1, comppc2);
  }
//  else if (iswalk && interpol) {
//    InterpolCurves(C, E, inewC, comppc1, comppc2, HDS);
//    TopOpeBRepDS_Curve& newC = HDS->ChangeCurve(inewC);
//    PutPCurves(newC, E, comppc1, comppc2);
//  }

  else {
    if (comppc) {
      TopOpeBRepDS_Curve newC1;
      inewC = HDS->MakeCurve(C,newC1);
      TopOpeBRepDS_Curve& newC = HDS->ChangeCurve(inewC);
      if(iswalk && !approx) {
	if (compc3d && C3D.IsNull()) Standard_ProgramError::Raise
	  ("TopOpeBRepDS_BuildTool::RecomputeCurve 1");
	newC.Curve1(C.Curve1());
	newC.Curve2(C.Curve2());
      }
      else
	ComputePCurves(C, E, newC, comppc1, comppc2, compc3d);	
      PutPCurves(newC, E, comppc1, comppc2);
    }
  }
}

//=======================================================================
//function : CopyFace
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::CopyFace(const TopoDS_Shape& Fin, 
				       TopoDS_Shape& Fou)const 
{
  Fou = Fin.EmptyCopied();
}


//=======================================================================
//function : AddEdgeVertex
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::AddEdgeVertex(const TopoDS_Shape& Ein, 
					    TopoDS_Shape& Eou, 
					    const TopoDS_Shape& V)const 
{
  myBuilder.Add(Eou,V);
  TopoDS_Edge e1 = TopoDS::Edge(Ein);
  TopoDS_Edge e2 = TopoDS::Edge(Eou);
  TopoDS_Vertex v1 = TopoDS::Vertex(V);
  myBuilder.Transfert(e1,e2,v1,v1);
}


//=======================================================================
//function : AddEdgeVertex
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::AddEdgeVertex(TopoDS_Shape& E, 
					    const TopoDS_Shape& V)const 
{
  myBuilder.Add(E,V);
}


//=======================================================================
//function : AddWireEdge
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::AddWireEdge(TopoDS_Shape& W, 
					  const TopoDS_Shape& E)const 
{
  myBuilder.Add(W,E);
}


//=======================================================================
//function : AddFaceWire
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::AddFaceWire(TopoDS_Shape& F, 
					  const TopoDS_Shape& W)const 
{
  myBuilder.Add(F,W);
}


//=======================================================================
//function : AddShellFace
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::AddShellFace(TopoDS_Shape& Sh, 
					   const TopoDS_Shape& F)const 
{
  myBuilder.Add(Sh,F);
}


//=======================================================================
//function : AddSolidShell
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::AddSolidShell(TopoDS_Shape& S, 
					    const TopoDS_Shape& Sh)const 
{
  myBuilder.Add(S,Sh);
}


//=======================================================================
//function : Parameter
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::Parameter(const TopoDS_Shape& E, 
					const TopoDS_Shape& V, 
					const Standard_Real P)const 
{
  const TopoDS_Edge&   e = TopoDS::Edge(E);
  const TopoDS_Vertex& v = TopoDS::Vertex(V);
  Standard_Real p = P;

  // 13/07/95 : 
  TopLoc_Location loc; Standard_Real f,l;
  Handle(Geom_Curve) C = BRep_Tool::Curve(e,loc,f,l);
  if ( !C.IsNull() && C->IsPeriodic()) {
    Standard_Real per = C->Period();
#ifdef DEB
    TopAbs_Orientation oV;
#else
    TopAbs_Orientation oV=TopAbs_FORWARD;
#endif
    TopExp_Explorer exV(e,TopAbs_VERTEX);
    for (; exV.More(); exV.Next()) {
      const TopoDS_Vertex& vofe = TopoDS::Vertex(exV.Current());
      if ( vofe.IsSame(v) ) {
	oV = vofe.Orientation();
	break;
      }
    }
    if ( exV.More() ) {
      if ( oV == TopAbs_REVERSED ) {
	if ( p < f ) {
	  Standard_Real pp = ElCLib::InPeriod(p,f,f+per);
#ifdef DEB
	  if (TopOpeBRepDS_GettraceBUTO() ) {
	    cout<<"BuildTool Parameter : "<<p<<" --> "<<pp<<endl;
	  }
#endif
	  p = pp;
	}
      }
    }
  }

  myBuilder.UpdateVertex(v,p,e,
			 0);   // NYI : Tol on new vertex ?? 
}

//=======================================================================
//function : Range
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::Range(const TopoDS_Shape& E, 
				    const Standard_Real first,
				    const Standard_Real last)const 
{
  myBuilder.Range(TopoDS::Edge(E),first,last);
}


//=======================================================================
//function : UpdateEdge
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::UpdateEdge(const TopoDS_Shape& Ein,
					 TopoDS_Shape& Eou)const
{
  TopLoc_Location loc;
  Standard_Real f1,l1;
  Standard_Real f2,l2;
  Handle(Geom_Curve) Cin = BRep_Tool::Curve(TopoDS::Edge(Ein),loc,f1,l1);
  Handle(Geom_Curve) Cou = BRep_Tool::Curve(TopoDS::Edge(Eou),loc,f2,l2);
  if (Cin.IsNull() || Cou.IsNull()) return;
  
  if ( Cou->IsPeriodic() ) {
    Standard_Real f2n = f2, l2n = l2;
    if ( l2n <= f2n ) {
      ElCLib::AdjustPeriodic(f1,l1,Precision::PConfusion(),f2n,l2n);
      Range(Eou,f2n,l2n);
#ifdef DEB
      if (TopOpeBRepDS_GettraceBUTO() || TopOpeBRepDS_GettraceTRPE()) {
	cout<<endl;
	cout<<"UpdateEdge f1,l1   "<<f1<<" "<<l1<<endl;
	cout<<"UpdateEdge f2,l2   "<<f2<<" "<<l2<<" "<<endl;
	cout<<"UpdateEdge f2n,l2n "<<f2n<<" "<<l2n<<endl;
	TopExp_Explorer ex;
	for (ex.Init(Eou,TopAbs_VERTEX); ex.More(); ex.Next()) {
	  TopoDS_Vertex v = TopoDS::Vertex(ex.Current());
	  Standard_Real par = BRep_Tool::Parameter(v,TopoDS::Edge(Eou));
	  TopAbs_Orientation o = v.Orientation();
	  cout<<"BuildTool vertex ";TopAbs::Print(o,cout); cout<<" "<<par<<endl;
	}
      }
#endif
    }
  }
}

//=======================================================================
//function : Project
//purpose  : project a vertex on a curve
//=======================================================================

static Standard_Boolean Project(const Handle(Geom_Curve)& C,
				const TopoDS_Vertex& V,
				Standard_Real& p)
{
  gp_Pnt P = BRep_Tool::Pnt(V);
  Standard_Real tol = BRep_Tool::Tolerance(V);
  GeomAdaptor_Curve GAC(C);
  Extrema_ExtPC extrema(P,GAC);
  if (extrema.IsDone()) {
    Standard_Integer i,n = extrema.NbExt();
    for (i = 1; i <= n; i++) {
      if (extrema.IsMin(i)) {
	Extrema_POnCurv EPOC = extrema.Point(i);
	if (P.Distance(EPOC.Value()) <= tol) {
	  p = EPOC.Parameter();
	  return Standard_True;
	}
      }
    }
  }
  return Standard_False;
}


//=======================================================================
//function : Parameter
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::Parameter(const TopOpeBRepDS_Curve& C,
					TopoDS_Shape& E,
					TopoDS_Shape& V)const 
{
  Standard_Real newparam = 0;
  Project(C.Curve(),TopoDS::Vertex(V),newparam);
  Parameter(E,V,newparam);
}


//=======================================================================
//function : Curve3D
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::Curve3D
(TopoDS_Shape& E, 
 const Handle(Geom_Curve)& C,
 const Standard_Real Tol)const 
{
  myBuilder.UpdateEdge(TopoDS::Edge(E),
		       C,
		       Tol);
}


//=======================================================================
//function : TranslateOnPeriodic
//purpose  : 
//=======================================================================

void TopOpeBRepDS_BuildTool::TranslateOnPeriodic
  (TopoDS_Shape& F,
   TopoDS_Shape& E,
   Handle(Geom2d_Curve)& PC) const
{
  // get range C3Df,C3Dl of 3d curve C3D of E
  TopLoc_Location L; 
  Standard_Real C3Df,C3Dl;
//  Handle(Geom_Curve) C3D = BRep_Tool::Curve(TopoDS::Edge(E),L,C3Df,C3Dl);
  Handle(Geom_Curve) C3D = BRep_Tool::Curve(TopoDS::Edge(E),C3Df,C3Dl); // 13-07-97: xpu
 
  Standard_Real first = C3Df, last = C3Dl;
  if (C3D->IsPeriodic()) {
    if ( last < first ) last += Abs(first - last);
  }

  // jyl-xpu : 13-06-97 : 
  // if <PC> is U isoline on sphere, a special parametrization
  // is to provide, we compute <PC> (which is a line) bounds 
  // with C3D bounds.
  Standard_Boolean UisoLineOnSphe = FUN_UisoLineOnSphe(F,PC);
  Standard_Boolean newv = Standard_True;

  Standard_Real du,dv;

  gp_Pnt2d ptest;
  Standard_Real t =(first+last)*.5;
  PC->D0(t,ptest);
  Standard_Real u1 = ptest.X(), u2 = u1;
  Standard_Real v1 = ptest.Y(), v2 = v1;

  if (newv) {
    if (UisoLineOnSphe) {
      Handle(Geom_Curve) c3d = BRep_Tool::Curve(TopoDS::Edge(E),C3Df,C3Dl);
      GeomAdaptor_Curve GC(c3d); gp_Pnt p3dtest = GC.Value(t);
      Handle(Geom_Surface) surf = BRep_Tool::Surface(TopoDS::Face(F));
      GeomAPI_ProjectPointOnSurf pons(p3dtest,surf);
      if (!(pons.NbPoints() < 1))
	pons.LowerDistanceParameters(u2,v2);
    } else TopOpeBRepTool_ShapeTool::AdjustOnPeriodic(F,u2,v2);
  }
  if (!newv) TopOpeBRepTool_ShapeTool::AdjustOnPeriodic(F,u2,v2);
  du = u2 - u1, dv = v2 - v1;    

  if ( du != 0. || dv != 0.) {
#ifdef DEB
    if (TopOpeBRepDS_GettraceBUTO() || TopOpeBRepDS_GettraceTRPE()) {
      cout<<endl;
      cout<<"TranslateOnPeriodic :  Curve range "<<C3Df<<" "<<C3Dl<<endl;
      Standard_Real PCf,PCl;
      BRep_Tool::Range(TopoDS::Edge(E),TopoDS::Face(F),PCf,PCl);
      cout<<"TranslateOnPeriodic : PCurve range "<<PCf<<" "<<PCl<<endl;
      cout<<"TranslateOnPeriodic :  translation "<<du<<" "<<dv<<endl;
    }
#endif
    // translate curve PC of du,dv
    Handle(Geom2d_Curve) PCT = Handle(Geom2d_Curve)::DownCast(PC->Copy());
    PCT->Translate(gp_Vec2d(du,dv));
    PC = PCT;
  }
}


// RLE - IAB 16 june 94
// should be provided by the BRep_Builder

Standard_EXPORT void TopOpeBRepDS_SetThePCurve(const BRep_Builder& B,
					       TopoDS_Edge& E,
					       const TopoDS_Face& F,
					       const TopAbs_Orientation O,
					       const Handle(Geom2d_Curve)& C)
{
  // check if there is already a pcurve on non planar faces
  Standard_Real f,l;
  Handle(Geom2d_Curve) OC;
  TopLoc_Location SL;
  Handle(Geom_Plane) GP = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(F,SL));
  if (GP.IsNull())
    OC = BRep_Tool::CurveOnSurface(E,F,f,l);

  if (OC.IsNull()) 
    B.UpdateEdge(E,C,F,Precision::Confusion());
  else {
    Standard_Boolean degen = BRep_Tool::Degenerated(E);
    if(!degen){
      if (O == TopAbs_REVERSED) 
	B.UpdateEdge(E,OC,C,F,Precision::Confusion());
      else 
	B.UpdateEdge(E,C,OC,F,Precision::Confusion());
    }
  }
}

#ifdef DEB
//------------------------------------------------------------------------
static void DUMPPCURVE(const TopoDS_Edge& EE, 
		       const TopoDS_Face& FF,
		       const Handle(Geom2d_Curve)& PC)
//------------------------------------------------------------------------
{
  TopLoc_Location L; Standard_Real Cf,Cl;
  Handle(Geom_Curve) C = BRep_Tool::Curve(EE,L,Cf,Cl);
  if (C->IsPeriodic()) {
    cout<<endl;
      cout<<"DUMPPCURVE  : ";cout<<" face "; ::PrintSurface(FF,cout); cout<<endl;
      cout<<"PCurve type : ";TopOpeBRepDS_Dumper::PrintType(PC,cout); cout<<endl;
    gp_Pnt oC; 
    if (::GetOrigin(C,oC)) 
      cout<<"(E)Curve origin : "<<oC.X()<<" "<<oC.Y()<<" "<<oC.Z()<<endl;
      cout<<"(E)Curve range  : Cf,Cl = "<<Cf<<" "<<Cl<<endl;
    
    Standard_Real PCf,PCl;
    PCf = PC->FirstParameter(); PCl = PC->LastParameter();
    gp_Pnt2d oPC; 
    if (::GetOrigin(PC,oPC))
      cout<<"(PC)PCurve origin : "<<oPC.X()<<" "<<oPC.Y()<<endl;
      cout<<"(PC)PCurve range  : PCf,PCl = "<<PCf<<" "<<PCl<<endl;
  }
}
#endif

//=======================================================================
//function : PCurve
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::PCurve(TopoDS_Shape& F, 
				     TopoDS_Shape& E, 
				     const Handle(Geom2d_Curve)& PC)const 
{
  if ( ! PC.IsNull() ) {
    TopoDS_Face FF = TopoDS::Face(F);
    TopoDS_Edge EE = TopoDS::Edge(E);
    Handle(Geom2d_Curve) PCT = PC;

#ifdef DEB
#ifdef DRAW
    Standard_Boolean trc = Standard_False;
    if (trc) FUN_draw(FF);
    if (trc) FUN_draw(EE);
    if (trc) FUN_draw(PC,0.);
#endif
    if (TopOpeBRepDS_GettraceTRPE()) DUMPPCURVE(EE,FF,PCT);
#endif
    
    // pour iab, ajout de Translate
    Standard_Boolean tran = myTranslate;
#ifdef DEB
    if ( TopOpeBRepDS_GettraceSANTRAN()) {
      tran = Standard_False; 
      cout<<"SANS translation de pcurve"<<endl; 
    }
#endif

    // xpu : 13-06-97 : 
    // recompute twice the pcurve boundaries if OverWrite
    // if the pcurve <PC> is U isoline on sphere -> to avoid.
    Standard_Boolean UisoLineOnSphe = FUN_UisoLineOnSphe(F,PC);
    Standard_Boolean overwrite = UisoLineOnSphe? Standard_False:myOverWrite;
    // xpu : 13-06-97

    if (tran)      
      TranslateOnPeriodic(F,E,PCT);  

    if (overwrite) 
      myBuilder.UpdateEdge(EE,PCT,FF,0);
    else           
      TopOpeBRepDS_SetThePCurve(myBuilder,EE,FF,E.Orientation(),PCT);

    // parametrage sur la nouvelle courbe 2d
    TopExp_Explorer exi(E,TopAbs_VERTEX);
    for (;exi.More(); exi.Next() ) {
      const TopoDS_Vertex& vi = TopoDS::Vertex(exi.Current());
      if ( vi.Orientation() != TopAbs_INTERNAL ) continue;
      Standard_Real tolvi = TopOpeBRepTool_ShapeTool::Tolerance(vi);
      // NYI tester l'existence d'au moins
      // NYI un parametrage de vi sur EE (en 3d ou en 2d) 
      // NYI --> a faire dans BRep_Tool
      Standard_Real newpar = BRep_Tool::Parameter(vi,EE);
      myBuilder.UpdateVertex(vi,newpar,EE,FF,tolvi);
    } // INTERNAL vertex
    
#ifdef DEB
    if (TopOpeBRepDS_GettraceTRPE()) DUMPPCURVE(EE,FF,PCT);
#endif
  }
}

//=======================================================================
//function : PCurve
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::PCurve(TopoDS_Shape& F, 
				     TopoDS_Shape& E, 
				     const TopOpeBRepDS_Curve& CDS,
				     const Handle(Geom2d_Curve)& PC)const
{
  if ( ! PC.IsNull() ) {
    TopoDS_Face FF = TopoDS::Face(F);
    TopoDS_Edge EE = TopoDS::Edge(E);

    Handle(Geom2d_Curve) PCT = PC;
    Standard_Real    CDSmin,CDSmax;
    Standard_Boolean rangedef = CDS.Range(CDSmin,CDSmax);

    
    TopLoc_Location L; Standard_Real Cf,Cl;
    Handle(Geom_Curve) C = BRep_Tool::Curve(EE,L,Cf,Cl);

#ifdef DEB
    if (TopOpeBRepDS_GettraceTRPE()) DUMPPCURVE(EE,FF,PCT);
#endif
      
    if (!C.IsNull()){
      Standard_Boolean deca = (Abs(Cf - CDSmin) > Precision::PConfusion());
      Handle(Geom2d_Line) line2d = Handle(Geom2d_Line)::DownCast(PCT);
      Standard_Boolean isline2d = !line2d.IsNull();
      Standard_Boolean tran=(rangedef && deca && C->IsPeriodic() && isline2d);
      if (tran) {
	TopLoc_Location Loc;
	const Handle(Geom_Surface) Surf = BRep_Tool::Surface(FF,Loc);
	Standard_Boolean isUperio = Surf->IsUPeriodic();
	Standard_Boolean isVperio = Surf->IsVPeriodic();
	gp_Dir2d dir2d = line2d->Direction();
	Standard_Real delta;
	if (isUperio && dir2d.IsParallel(gp::DX2d(),Precision::Angular())) {
	  delta = (CDSmin - Cf) * dir2d.X();
	  PCT->Translate(gp_Vec2d(delta,0.));
	}
	else if(isVperio && dir2d.IsParallel(gp::DY2d(),Precision::Angular())){
	  delta = (CDSmin - Cf) * dir2d.Y();
	  PCT->Translate(gp_Vec2d(0.,delta));
	}
      }
    }
    
    TopOpeBRepDS_SetThePCurve(myBuilder,EE,FF,E.Orientation(),PCT);
    
#ifdef DEB
    if (TopOpeBRepDS_GettraceTRPE()) DUMPPCURVE(EE,FF,PCT);
#endif
  }
}


//=======================================================================
//function : Orientation
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::Orientation(TopoDS_Shape& S, 
					  const TopAbs_Orientation O)const 
{
  S.Orientation(O);
}


//=======================================================================
//function : Orientation
//purpose  : 
//=======================================================================

TopAbs_Orientation  TopOpeBRepDS_BuildTool::Orientation
  (const TopoDS_Shape& S) const
{
  return S.Orientation();
}

//=======================================================================
//function : Closed
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::Closed(TopoDS_Shape& S, 
				     const Standard_Boolean B)const 
{
  S.Closed(B);
}


//=======================================================================
//function : Approximation
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRepDS_BuildTool::Approximation() const
{
  return myCurveTool.GetGeomTool().TypeC3D() != TopOpeBRepTool_BSPLINE1;
}

void TopOpeBRepDS_BuildTool::UpdateSurface(const TopoDS_Shape& F,const Handle(Geom_Surface)& SU) const
{
  BRep_Builder BB;
  TopLoc_Location L;
  Standard_Real tol = BRep_Tool::Tolerance(TopoDS::Face(F));
  BB.UpdateFace(TopoDS::Face(F),SU,L,tol);
}

void TopOpeBRepDS_BuildTool::UpdateSurface(const TopoDS_Shape& E,const TopoDS_Shape& oldF,
					   const TopoDS_Shape& newF) const
{
  BRep_Builder BB;
  Standard_Real f,l;
  const Handle(Geom2d_Curve)& PC = BRep_Tool::CurveOnSurface(TopoDS::Edge(E),TopoDS::Face(oldF),f,l);
  Standard_Real tol = BRep_Tool::Tolerance(TopoDS::Face(oldF));
  BB.UpdateEdge(TopoDS::Edge(E),PC,TopoDS::Face(newF),tol);
}


/* // - merge 04-07-97
//=======================================================================
//function : RecomputeCurve
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::RecomputeCurve
(const TopOpeBRepDS_Curve& C1,
 TopoDS_Shape& E,
 TopOpeBRepDS_Curve& C2 ) const
{
  // - C1 curves have been approximated by BSplines of degree 1 :
  // or
  // - C1.Curve() is non projectable on at least one of the original
  // intersecting faces.

  const TopOpeBRepTool_GeomTool& GT = myCurveTool.GetGeomTool();
  Standard_Boolean compc3d = GT.CompC3D();
  Standard_Boolean comppc1 = GT.CompPC1();
  Standard_Boolean comppc2 = GT.CompPC2();
  
  const Handle(Geom_Curve)& C3D = C1.Curve();
  if (compc3d && C3D.IsNull()) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 1");
  if (comppc1 && C2.Shape1().IsNull()) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 2");
  if (comppc2 && C2.Shape2().IsNull()) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 3");
  TopoDS_Vertex Vmin,Vmax; TopExp::Vertices(TopoDS::Edge(E),Vmin,Vmax);
  if ( Vmin.IsNull() ) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 4");
  if ( Vmax.IsNull() ) Standard_ProgramError::Raise
    ("TopOpeBRepDS_BuildTool::RecomputeCurve 5");

  Standard_Boolean kbspl1 = Standard_False;
  Handle(Geom_BSplineCurve) BS = Handle(Geom_BSplineCurve)::DownCast(C3D);
  if (!BS.IsNull()) kbspl1 = (BS->Degree() == 1);
  if (kbspl1) RecomputeBSpline1Curve(C1,E,C2);
  else        RecomputeCurveOnCone(C1,E,C2);
}

//=======================================================================
//function : RecomputeBSpline1Curve
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::RecomputeBSpline1Curve
(const TopOpeBRepDS_Curve& C1,
 TopoDS_Shape& EE,
 TopOpeBRepDS_Curve& C2) const
{
#ifdef DEB
  Standard_Boolean tDSNC = TopOpeBRepDS_GettraceDSNC();
  Standard_Boolean tBUTO = TopOpeBRepDS_GettraceBUTO();
#endif
  
  // C1 curves have been approximated by BSplines of degree 1 :
  // compute new geometry on curves.

  TopoDS_Edge& E = TopoDS::Edge(EE);

  const TopOpeBRepTool_GeomTool& GT = myCurveTool.GetGeomTool();
  TopOpeBRepTool_OutCurveType typec3d = GT.TypeC3D();
  Standard_Boolean compc3d =            GT.CompC3D();
  Standard_Boolean comppc1 =            GT.CompPC1();
  Standard_Boolean comppc2 =            GT.CompPC2();

  const TopoDS_Face& F1 = TopoDS::Face(C2.Shape1());
  const TopoDS_Face& F2 = TopoDS::Face(C2.Shape2());
  
  const Handle(Geom_Curve)&   C3D = C1.Curve();
  const Handle(Geom2d_Curve)& PC1 = C1.Curve1();
  const Handle(Geom2d_Curve)& PC2 = C1.Curve2();

  // Vmin,Vmax = bounding vertices of edge <E>
  // and their parameters parmin,parmax .

  TopoDS_Vertex Vmin,Vmax; Standard_Real parmin,parmax;
  ::GetOrientedEdgeVertices(E,Vmin,Vmax,parmin,parmax);

#ifdef DEB
  if (tBUTO) cout<<"Recompute1 min,max = "<<parmin<<","<<parmax<<endl;
  if (tBUTO) DUMPCURVES(C3D,C1);
#endif
  
  Handle(Geom_Curve)   C3Dnew;
  Handle(Geom2d_Curve) PC1new;
  Handle(Geom2d_Curve) PC2new;
  Standard_Real tolreached3d,tolreached2d;

  if ( typec3d == TopOpeBRepTool_BSPLINE1 ) {
    if ( compc3d ) {
      C3Dnew = Handle(Geom_BSplineCurve)::DownCast(C3D->Copy());
      (Handle(Geom_BSplineCurve)::DownCast(C3Dnew))->Segment(parmin,parmax);
    }
    if ( comppc1 && (!PC1.IsNull()) ) {
      PC1new = Handle(Geom2d_BSplineCurve)::DownCast(PC1->Copy());
      (Handle(Geom2d_BSplineCurve)::DownCast(PC1new))->Segment(parmin,parmax);
    }
    if ( comppc2 && (!PC2.IsNull()) ) {
      PC2new = Handle(Geom2d_BSplineCurve)::DownCast(PC2->Copy());
      (Handle(Geom2d_BSplineCurve)::DownCast(PC2new))->Segment(parmin,parmax);
    }
  }

  else if ( typec3d == TopOpeBRepTool_APPROX ) {
    if (!comppc1 || !comppc2) Standard_NotImplemented::Raise("DSBuildToolAPPROX");
    myCurveTool.MakeCurves(parmin,parmax,
			   C3D,PC1,PC2,F1,F2,
			   C3Dnew,PC1new,PC2new,
			   tolreached3d,tolreached2d);
  }

  else if ( typec3d == TopOpeBRepTool_INTERPOL ) {
    Standard_NotImplemented::Raise("DSBuildToolINTERPOL");
  }

  Standard_Real newtol,newparmin,newparmax;
  ::FUN_updateEDGECURVETOL
    (*this,F1,F2,E,C3Dnew,tolreached3d,tolreached2d,tolreached2d,
     newtol,newparmin,newparmax);
  
  if (!C3Dnew.IsNull()) {
    C2.DefineCurve(C3Dnew,newtol,Standard_False);
    C2.SetRange(newparmin,newparmax);
  }
  if (!PC1new.IsNull()) C2.Curve1(PC1new);
  if (!PC2new.IsNull()) C2.Curve2(PC2new);
}


//=======================================================================
//function : RecomputeCurveOnCone
//purpose  : 
//=======================================================================

void  TopOpeBRepDS_BuildTool::RecomputeCurveOnCone
  (const TopOpeBRepDS_Curve& C1,
   TopoDS_Shape&             EE,
   TopOpeBRepDS_Curve&       C2 ) const
{
#ifdef DEB
  Standard_Boolean tDSNC = TopOpeBRepDS_GettraceDSNC();
  Standard_Boolean tBUTO = TopOpeBRepDS_GettraceBUTO();
  Standard_Boolean tTRPE = TopOpeBRepDS_GettraceTRPE();
#endif
  
  // C1 Pcurves have not been computed because C1 Curve is not projectable
  // on one at least of the intersecting faces giving C1 Curve.
  // (see TopOpeBRepTool_CurveTool::IsProjectable())

  TopoDS_Edge& E = TopoDS::Edge(EE);
  
  const TopOpeBRepTool_GeomTool& GT = myCurveTool.GetGeomTool();
  TopOpeBRepTool_OutCurveType typec3d = GT.TypeC3D();
  Standard_Boolean compc3d =            GT.CompC3D();
  Standard_Boolean comppc1 =            GT.CompPC1();
  Standard_Boolean comppc2 =            GT.CompPC2();
  
  const TopoDS_Face& F1 = TopoDS::Face(C2.Shape1());
  const TopoDS_Face& F2 = TopoDS::Face(C2.Shape2());
  
  const Handle(Geom_Curve)&   C3D = C1.Curve();
  const Handle(Geom2d_Curve)& PC1 = C1.Curve1();
  const Handle(Geom2d_Curve)& PC2 = C1.Curve2();

  // get bounding vertices Vmin,Vmax supported by the new edge <E>
  // and their corresponding parameters parmin,parmax .

  TopoDS_Vertex Vmin,Vmax; Standard_Real parmin,parmax;
  ::GetOrientedEdgeVertices(E,Vmin,Vmax,parmin,parmax);

#ifdef DEB
  if (tBUTO) {cout<<"Recompute2 min,max = "<<parmin<<","<<parmax<<endl;
	      DUMPCURVES(C3D,C1);}
#endif

  if ( C3D->IsPeriodic() ) {
    // ellipse on cone : periodize parmin,parmax
    Standard_Real period = C3D->LastParameter() - C3D->FirstParameter();
    Standard_Real f,l;
    if (Vmin.Orientation() == TopAbs_FORWARD) { f = parmin; l = parmax; }
    else {                                      f = parmax; l = parmin; }
    parmin = f; parmax = l;
    ElCLib::AdjustPeriodic(f,f+period,Precision::PConfusion(),parmin,parmax);
#ifdef DEB
    if (tBUTO||tTRPE) cout<<"Recompute2 : parmin,parmax "<<f<<","<<l<<endl;
    if (tBUTO||tTRPE) cout<<"                   --> parmin,parmax "<<parmin<<","<<parmax<<endl;
#endif
  }
  
  Handle(Geom_TrimmedCurve) C3Dnew;
  Handle(Geom2d_Curve) PC1new;
  Handle(Geom2d_Curve) PC2new;
  Standard_Real tolreached3d = C1.Tolerance();
  Standard_Real tolreached2d1 = C1.Tolerance();
  Standard_Real tolreached2d2 = C1.Tolerance();
  if (compc3d) C3Dnew = new Geom_TrimmedCurve(C3D,parmin,parmax);
  if (comppc1) PC1new = myCurveTool.MakePCurveOnFace(F1,C3Dnew,tolreached2d1);
  if (comppc2) PC2new = myCurveTool.MakePCurveOnFace(F2,C3Dnew,tolreached2d2);

#ifdef DRAW
  if (tBUTO) {FUN_draw(F1); FUN_draw(F2); FUN_draw(E);}
#endif

  Standard_Real newtol,newparmin,newparmax;
  FUN_updateEDGECURVETOL
  (*this,F1,F2,E,C3Dnew,tolreached3d,tolreached2d1,tolreached2d2,
   newtol,newparmin,newparmax);

//   jyl : 16-06-97
//  Standard_Real fac = 0.3798123578771;
//  Standard_Real tol = newtol;
//  Standard_Real par3d = (1-fac)*newparmin + (fac)*newparmax;
//  Standard_Real par2d = par3d - newparmin;
//
//  gp_Pnt P3DC3D;       C3D->D0(par3d,P3DC3D);
//
//  Standard_Boolean UisoLineOnSphe1 = Standard_False;
//  UisoLineOnSphe1 = ::FUN_UisoLineOnSphe(F1,PC1new);
//  if (UisoLineOnSphe1) {
//    Standard_Real isrev1 = 
//      ::FUN_reversePC(PC1new,F1,P3DC3D,par2d,tol);
//    
//#ifdef DEB
//    if (tBUTO && isrev1) cout<<"on retourne PC1"<<endl;
//#endif
//
//  }
//
//  Standard_Boolean UisoLineOnSphe2 = Standard_False;
//  UisoLineOnSphe2 = ::FUN_UisoLineOnSphe(F2,PC2new);
//  if (UisoLineOnSphe2) {
//    Standard_Real isrev2 = 
//     ::FUN_reversePC(PC2new,F2,P3DC3D,par2d,tol);
//
//#ifdef DEB
//    if (tBUTO && isrev2) cout<<"on retourne PC2"<<endl;
//#endif
//  }

  // xpu : 17-06-97
  // Rmq : C1.Curve<i>() ne sert plus qu'a determiner si la courbe
  //       est une isos de la sphere
  // NYI : enlever FUN_reversePC
  Standard_Boolean UisoLineOnSphe1 = Standard_False;
  UisoLineOnSphe1 = ::FUN_UisoLineOnSphe(F1,PC1new);
  if (UisoLineOnSphe1) {
    ::FUN_makeUisoLineOnSphe(F1,C3Dnew,PC1new,newtol);
  }
  Standard_Boolean UisoLineOnSphe2 = Standard_False;
  UisoLineOnSphe2 = ::FUN_UisoLineOnSphe(F2,PC2new);
  if (UisoLineOnSphe2) {
    ::FUN_makeUisoLineOnSphe(F2,C3Dnew,PC2new,newtol);
  } // xpu : 17-06-97

  if (!C3Dnew.IsNull()) C2.Curve(C3Dnew,newtol);
  if (!PC1new.IsNull()) C2.Curve1(PC1new);
  if (!PC2new.IsNull()) C2.Curve2(PC2new);
  
#ifdef DEB
  if (tBUTO) DUMPCURVES(C3Dnew,C2);
#endif
}*/ // - merge 04-07-97