summaryrefslogtreecommitdiff
path: root/src/SWDRAW/SWDRAW_ShapeUpgrade.cxx
blob: 2e6c02a1fb76a8a0e0b2f2787b215044992183cf (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
// File:	SWDRAW_ShapeUpgrade.cxx
// Created:	Tue Mar  9 15:49:27 1999
// Author:	data exchange team
//		<det@kinox.nnov.matra-dtv.fr>
//gka,rln 30.04.99 S4137: new commands for testing ShapeDivide added, some removed
//abv,pdn 05.05.99 S4174: new commands for testing ShapeDivide added, some removed
//pdn,gka 10.06.99 S4189: command DT_ShapeConvertRev added

#include <SWDRAW_ShapeUpgrade.ixx>

#include <DBRep.hxx>
#include <ShapeUpgrade.hxx>
#include <ShapeUpgrade_SplitCurve3dContinuity.hxx>
#include <ShapeUpgrade_SplitCurve2dContinuity.hxx>
#include <ShapeUpgrade_SplitSurfaceContinuity.hxx>
//#include <ShapeUpgrade_SupportModification.hxx>
#include <Draw_Interpretor.hxx>
#include <DrawTrSurf.hxx>
#include <Geom_Curve.hxx>
#include <Geom_Plane.hxx>
#include <Geom_Surface.hxx>
#include <Geom2d_Curve.hxx>
#include <TColGeom_HArray1OfCurve.hxx>
#include <TColGeom_HArray2OfSurface.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_HArray1OfReal.hxx>
#include <TColStd_HSequenceOfReal.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Compound.hxx>
#include <TopExp_Explorer.hxx>
#include <TColGeom_HArray2OfSurface.hxx>
#include <TColGeom2d_HArray1OfCurve.hxx>
#include <BRepBuilderAPI.hxx>
#include <SWDRAW.hxx>
#include <ShapeUpgrade_ShapeDivideArea.hxx>

#include <stdio.h> 
//#include <ShapeExtend_WireData.hxx>
//#include <ShapeAnalysis_Shell.hxx>
//#include <ShapeAnalysis_WireOrder.hxx>
//#include <ShapeAnalysis_Wire.hxx>
//#include <ShapeUpgrade_ShellSewing.hxx>
#include <Geom2d_Curve.hxx>
#include <Geom2d_OffsetCurve.hxx>
#include <Geom_OffsetCurve.hxx>
#include <ShapeUpgrade_SplitCurve3dContinuity.hxx>
#include <TopoDS_Iterator.hxx>
#include <BRep_Tool.hxx>
#include <ShapeExtend_CompositeSurface.hxx>
#include <Geom_RectangularTrimmedSurface.hxx>
#include <TColGeom_HArray2OfSurface.hxx>
#include <ShapeFix_ComposeShell.hxx>
#include <Precision.hxx>
#include <ShapeBuild_ReShape.hxx>
#include <BRepTools.hxx>
#include <ShapeFix.hxx>
#include <ShapeUpgrade_ShapeDivideContinuity.hxx>
#include <ShapeUpgrade_ShapeDivideAngle.hxx>
#include <ShapeUpgrade_ShapeConvertToBezier.hxx>
#include <ShapeCustom.hxx>
#include <ShapeUpgrade_ShapeDivideClosed.hxx>
#include <ShapeUpgrade_RemoveInternalWires.hxx>
// the plane (equation z=0) shared by PlaneDividedFaceContinuity and PlaneGridShell
//static Handle(Geom_Plane) ThePlane= new Geom_Plane(0,0,1,0);


//=======================================================================
//function : DT_ShapeDivide 
//purpose  : 
//=======================================================================

static Standard_Integer DT_ShapeDivide (Draw_Interpretor& di,
					Standard_Integer n, const char** a)
{
  // DT_ShapeDivide result Shape Tol
  // a[1]= result
  // a[2]= input Face/Surface
  // a[3] si n>3= Wire/Face
  // a[n-1]= Tolerance
   
  if (n<3) {
    di << "bad number of arguments" <<"\n";
    return 1;
  } 
  
  // try to read a shape:
  TopoDS_Shape inputShape=DBRep::Get(a[2]);
  if (inputShape.IsNull()) {
    di << "Unknown shape"<< "\n";
    return 1;
  }
  // a[2] is a shape. managing:
  // DT_ShapeDivide result Face Tol
  
  // giving a face is available only in the constructor:
  // we make the whole and quit.
  ShapeUpgrade_ShapeDivideContinuity tool(inputShape);
    
  // tolerance is optional
  if (n==4) {
    Standard_Real Tol=atof(a[3]);
    tool.SetTolerance(Tol);
  }
  
  //  theTool.SetGlobalCriterion(GeomAbs_C1);
  tool.Perform();
  TopoDS_Shape res = tool.Result();

  if ( tool.Status ( ShapeExtend_OK ) ) di << "Status: OK" << "\n";
  if ( tool.Status ( ShapeExtend_DONE1 ) ) di << "Status: DONE1" << "\n";
  if ( tool.Status ( ShapeExtend_DONE2 ) ) di << "Status: DONE2" << "\n";
  if ( tool.Status ( ShapeExtend_DONE3 ) ) di << "Status: DONE3" << "\n";
  if ( tool.Status ( ShapeExtend_DONE4 ) ) di << "Status: DONE4" << "\n";
  if ( tool.Status ( ShapeExtend_DONE5 ) ) di << "Status: DONE5" << "\n";
  if ( tool.Status ( ShapeExtend_DONE6 ) ) di << "Status: DONE6" << "\n";
  if ( tool.Status ( ShapeExtend_DONE7 ) ) di << "Status: DONE7" << "\n";
  if ( tool.Status ( ShapeExtend_DONE8 ) ) di << "Status: DONE8" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL1 ) ) di << "Status: FAIL1" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL2 ) ) di << "Status: FAIL2" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL3 ) ) di << "Status: FAIL3" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL4 ) ) di << "Status: FAIL4" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL5 ) ) di << "Status: FAIL5" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL6 ) ) di << "Status: FAIL6" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL7 ) ) di << "Status: FAIL7" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL8 ) ) di << "Status: FAIL8" << "\n";

  // fixes
  
  ShapeFix::SameParameter ( res, Standard_False );

  DBRep::Set(a[1],res);
  return 0;
}

static Standard_Integer DT_ShapeConvertRev (Draw_Interpretor& di,
					 Standard_Integer n, const char** a)
{
  if (n<5) {
    di << "bad number of arguments" <<"\n";
    return 1;
  } 
  
  // try to read a shape:
  TopoDS_Shape inputShape=DBRep::Get(a[2]);
  if (inputShape.IsNull()) {
    di << "Unknown shape"<< "\n";
    return 1;
  }
  
  Standard_Integer c2d = atoi(a[3]);
  Standard_Integer c3d = atoi(a[4]);
  TopoDS_Shape revsh = ShapeCustom::ConvertToRevolution (inputShape);
  if (revsh.IsNull()) { di<<"NO RESULT"<<"\n"; return 1; }
  else if (revsh == inputShape) { di<<"No modif"<<"\n";}
  else di<<"ConvertToRevolution -> Result : "<<"\n";
  
  ShapeUpgrade_ShapeConvertToBezier tool(revsh);
  tool.SetSurfaceConversion(Standard_True);
  if(c2d)
    tool.Set2dConversion(Standard_True);
  if(c3d) {
    tool.Set3dConversion(Standard_True);
    if(n > 5)
      tool.Set3dLineConversion(Standard_False);
    if(n > 6)
      tool.Set3dCircleConversion(Standard_False);
    if(n > 7)
      tool.Set3dConicConversion(Standard_False);
  }
  tool.Perform();
  TopoDS_Shape res = tool.Result();
  
  if ( tool.Status ( ShapeExtend_OK ) ) di << "Status: OK" << "\n";
  if ( tool.Status ( ShapeExtend_DONE1 ) ) di << "Status: DONE1" << "\n";
  if ( tool.Status ( ShapeExtend_DONE2 ) ) di << "Status: DONE2" << "\n";
  if ( tool.Status ( ShapeExtend_DONE3 ) ) di << "Status: DONE3" << "\n";
  if ( tool.Status ( ShapeExtend_DONE4 ) ) di << "Status: DONE4" << "\n";
  if ( tool.Status ( ShapeExtend_DONE5 ) ) di << "Status: DONE5" << "\n";
  if ( tool.Status ( ShapeExtend_DONE6 ) ) di << "Status: DONE6" << "\n";
  if ( tool.Status ( ShapeExtend_DONE7 ) ) di << "Status: DONE7" << "\n";
  if ( tool.Status ( ShapeExtend_DONE8 ) ) di << "Status: DONE8" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL1 ) ) di << "Status: FAIL1" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL2 ) ) di << "Status: FAIL2" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL3 ) ) di << "Status: FAIL3" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL4 ) ) di << "Status: FAIL4" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL5 ) ) di << "Status: FAIL5" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL6 ) ) di << "Status: FAIL6" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL7 ) ) di << "Status: FAIL7" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL8 ) ) di << "Status: FAIL8" << "\n";

  // fixes
  
  ShapeFix::SameParameter ( res, Standard_False );

  DBRep::Set(a[1],res);
  return 0;
}


/*
  if (!inputShape.IsNull()) {
    // a[2] is a shape. managing:
    // DT_ShapeDivide result Face Tol

    TopoDS_Face  inputFace = TopoDS::Face(inputShape);
    if (inputFace.IsNull()) {
      di << a[2] << " is not a face" << "\n";
      return 1;
    }

    // giving a face is available only in the constructor:
    // we make the whole and quit.
    ShapeUpgrade_ShapeDivideContinuity theTool(inputFace);
    
    // tolerance is optional
    if (n==4) {
      Standard_Real Tol=atof(a[n-1]);
      theTool.SetTolerance(Tol);
    }

    theTool.SetGlobalCriterion(GeomAbs_C1);
    theTool.Build();
    if (!theTool.IsDone()) {
      ShapeUpgrade_Error theError=theTool.Error();
      di << "Not done: error=";
      if (theError==ShapeUpgrade_Done) 
	di << "Done"<<"\n";
      else if (theError==ShapeUpgrade_NotDone) 
	di << "NotDone"<<"\n";
      else if (theError==ShapeUpgrade_EmptyShell) 
	di << "EmptyShell"<<"\n";
      else if (theError==ShapeUpgrade_InvalidCriterion) 
	di << "InvalidCriterion"<<"\n";
      else if (theError==ShapeUpgrade_InvalidGridSurface) 
	di << "InvalidGridSurface"<<"\n";
      else if (theError==ShapeUpgrade_DegeneratedEdge) 
	di << "DegeneratedEdge"<<"\n";
      else if (theError==ShapeUpgrade_NoSurface) 
	di << "NoSurface"<<"\n";
      else if (theError==ShapeUpgrade_NoTolerance) 
	di << "NoTolerance"<<"\n";
      return 1;
    }   
    TopoDS_Shell res = theTool.Shell();
    DBRep::Set(a[1],res);
    
    return 0;
  }
  else {
    // not a face: we can use the empty consturctor.
    ShapeUpgrade_ShapeDivideContinuity theTool;
    Standard_Real Tol=atof(a[n-1]);
    theTool.SetTolerance(Tol);
    theTool.SetGlobalCriterion(GeomAbs_C1);

    // try to read a surface:
    Handle(Geom_Surface) GS = DrawTrSurf::GetSurface(a[2]);
    if (! GS.IsNull()) {
      // a[2] is a surface. managing the configurations:
      // DT_ShapeDivide result Surface Tol
      // DT_ShapeDivide result Surface Face Tol
      // DT_ShapeDivide result Surface Wire Surf Tol
      
      theTool.SetSupport(GS);

      // try to read a Wire or a Face:
      if (n>=5) {
	TopoDS_Shape inputBoundary=DBRep::Get(a[3]);
	if (inputBoundary.IsNull()) {
	  di << "Invalid Boundary" << "\n";
	  return 1;
	}
	TopoDS_Wire WireBoundary = TopoDS::Wire(inputBoundary);
	if (!WireBoundary.IsNull()) {
	  // DT_ShapeDivide result Surface Wire Surf Tol
	  Handle(Geom_Surface) WireSupport = DrawTrSurf::GetSurface(a[4]);
	  if (WireSupport.IsNull()) {
	    di << "Invalid Surface supporting the Wire" << "\n";
	    return 1;
	  }
	  theTool.SetBoundary(WireBoundary, WireSupport);
	}
	else {
	  TopoDS_Face  FaceBoundary = TopoDS::Face(inputBoundary);
	  // DT_ShapeDivide result Surface Face Tol
	  theTool.SetBoundary(FaceBoundary);
	}
      }
    }
    else {
      // it must be a grid: managing the configurations:
      // DT_ShapeDivide result NbU NbV {Surf_u_v...} Tol
      // DT_ShapeDivide result NbU NbV {Surf_u_v...} Face Tol
      // DT_ShapeDivide result NbU NbV {Surf_u_v...} Wire Surf Tol
      if (n<6) {
	di << "bad number of arguments for grid input" <<"\n";
	return 1;
      }
      // number of surf:
      Standard_Integer NbU=atoi(a[2]);
      Standard_Integer NbV=atoi(a[3]);
      if (n < 4+NbU*NbV+1) {
	di << "bad number of arguments" <<"\n";
	return 1;
      }
      
      Handle(TColGeom_HArray2OfSurface) 
	TheGridSurf= new TColGeom_HArray2OfSurface(1,NbU,1,NbV);
      
      for (Standard_Integer iu=1; iu<=NbU; iu++) {
	for (Standard_Integer jv=1; jv<=NbV; jv++) {
	  Handle(Geom_Surface) GS = DrawTrSurf::GetSurface(a[4+(iu-1)*NbV+jv-1]);
	  TheGridSurf->SetValue(iu,jv,GS);
	}
      }
      theTool.SetSupport(TheGridSurf,Tol);   

      // try to read a Wire or a Face:
      if (n>=6+NbU*NbV) {
	TopoDS_Shape inputBoundary=DBRep::Get(a[4+NbU*NbV]);
	if (inputBoundary.IsNull()) {
	  di << "Invalid Boundary" << "\n";
	  return 1;
	}
	TopoDS_Wire  WireBoundary = TopoDS::Wire(inputBoundary);
	if (!WireBoundary.IsNull()) {
	  // DT_ShapeDivide result Surface Wire Surf Tol
	  Handle(Geom_Surface) WireSupport = DrawTrSurf::GetSurface(a[4+NbU*NbV+1]);
	  if (WireSupport.IsNull()) {
	    di << "Invalid Surface supporting the Wire" << "\n";
	    return 1;
	  }
	  theTool.SetBoundary(WireBoundary, WireSupport);
	}
	else {
	  TopoDS_Face  FaceBoundary = TopoDS::Face(inputBoundary);
	  // DT_ShapeDivide result Surface Face Tol
	  theTool.SetBoundary(FaceBoundary);
	}
      }     
    } 

    theTool.Build();
    if (!theTool.IsDone()) {
      ShapeUpgrade_Error theError=theTool.Error();
      di << "Not done: error=";
      if (theError==ShapeUpgrade_Done) 
	di << "Done"<<"\n";
      else if (theError==ShapeUpgrade_NotDone) 
	di << "NotDone"<<"\n";
      else if (theError==ShapeUpgrade_EmptyShell) 
	di << "EmptyShell"<<"\n";
      else if (theError==ShapeUpgrade_InvalidCriterion) 
	di << "InvalidCriterion"<<"\n";
      else if (theError==ShapeUpgrade_InvalidGridSurface) 
	di << "InvalidGridSurface"<<"\n";
      else if (theError==ShapeUpgrade_DegeneratedEdge) 
	di << "DegeneratedEdge"<<"\n";
      else if (theError==ShapeUpgrade_NoSurface) 
	di << "NoSurface"<<"\n";
      else if (theError==ShapeUpgrade_NoTolerance) 
	di << "NoTolerance"<<"\n";
      return 1;
    }   
    
    TopoDS_Shell res = theTool.Shell();
    DBRep::Set(a[1],res);
    
    return 0;
  }
}
*/
static Standard_Integer DT_ShapeConvert (Draw_Interpretor& di,
					 Standard_Integer n, const char** a)
{
  if (n<5) {
    di << "bad number of arguments" <<"\n";
    return 1;
  } 
  
  // try to read a shape:
  TopoDS_Shape inputShape=DBRep::Get(a[2]);
  if (inputShape.IsNull()) {
    di << "Unknown shape"<< "\n";
    return 1;
  }
  
  Standard_Integer c2d = atoi(a[3]);
  Standard_Integer c3d = atoi(a[4]);
  
  ShapeUpgrade_ShapeConvertToBezier tool(inputShape);
  tool.SetSurfaceConversion(Standard_True);
  if(c2d)
    tool.Set2dConversion(Standard_True);
  if(c3d)
    tool.Set3dConversion(Standard_True);
  tool.Perform();
  TopoDS_Shape res = tool.Result();
  
  if ( tool.Status ( ShapeExtend_OK ) ) di << "Status: OK" << "\n";
  if ( tool.Status ( ShapeExtend_DONE1 ) ) di << "Status: DONE1" << "\n";
  if ( tool.Status ( ShapeExtend_DONE2 ) ) di << "Status: DONE2" << "\n";
  if ( tool.Status ( ShapeExtend_DONE3 ) ) di << "Status: DONE3" << "\n";
  if ( tool.Status ( ShapeExtend_DONE4 ) ) di << "Status: DONE4" << "\n";
  if ( tool.Status ( ShapeExtend_DONE5 ) ) di << "Status: DONE5" << "\n";
  if ( tool.Status ( ShapeExtend_DONE6 ) ) di << "Status: DONE6" << "\n";
  if ( tool.Status ( ShapeExtend_DONE7 ) ) di << "Status: DONE7" << "\n";
  if ( tool.Status ( ShapeExtend_DONE8 ) ) di << "Status: DONE8" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL1 ) ) di << "Status: FAIL1" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL2 ) ) di << "Status: FAIL2" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL3 ) ) di << "Status: FAIL3" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL4 ) ) di << "Status: FAIL4" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL5 ) ) di << "Status: FAIL5" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL6 ) ) di << "Status: FAIL6" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL7 ) ) di << "Status: FAIL7" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL8 ) ) di << "Status: FAIL8" << "\n";

  // fixes
  
  ShapeFix::SameParameter ( res, Standard_False );

  DBRep::Set(a[1],res);
  return 0;
}
static Standard_Integer DT_SplitAngle(Draw_Interpretor& di,
				      Standard_Integer n, const char** a)
{
  if (n<3) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }
  
  TopoDS_Shape inputShape=DBRep::Get(a[2]);
  if (inputShape.IsNull()) {
    di << "Unknown shape"<< "\n";
    return 1;
  }
  
  Standard_Real maxangle = 95;
  if ( n >3 ) {
    maxangle = atof ( a[3] );
    if ( maxangle <1 ) maxangle = 1;
  }
  
  ShapeUpgrade_ShapeDivideAngle tool(maxangle*PI/180,inputShape);
  tool.Perform();
  TopoDS_Shape res = tool.Result();

  if ( tool.Status ( ShapeExtend_OK ) ) di << "Status: OK" << "\n";
  if ( tool.Status ( ShapeExtend_DONE1 ) ) di << "Status: DONE1" << "\n";
  if ( tool.Status ( ShapeExtend_DONE2 ) ) di << "Status: DONE2" << "\n";
  if ( tool.Status ( ShapeExtend_DONE3 ) ) di << "Status: DONE3" << "\n";
  if ( tool.Status ( ShapeExtend_DONE4 ) ) di << "Status: DONE4" << "\n";
  if ( tool.Status ( ShapeExtend_DONE5 ) ) di << "Status: DONE5" << "\n";
  if ( tool.Status ( ShapeExtend_DONE6 ) ) di << "Status: DONE6" << "\n";
  if ( tool.Status ( ShapeExtend_DONE7 ) ) di << "Status: DONE7" << "\n";
  if ( tool.Status ( ShapeExtend_DONE8 ) ) di << "Status: DONE8" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL1 ) ) di << "Status: FAIL1" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL2 ) ) di << "Status: FAIL2" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL3 ) ) di << "Status: FAIL3" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL4 ) ) di << "Status: FAIL4" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL5 ) ) di << "Status: FAIL5" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL6 ) ) di << "Status: FAIL6" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL7 ) ) di << "Status: FAIL7" << "\n";
  if ( tool.Status ( ShapeExtend_FAIL8 ) ) di << "Status: FAIL8" << "\n";

  // fixes
  
  ShapeFix::SameParameter ( res, Standard_False );

  DBRep::Set(a[1],res);
  return 0;
}
  
/*  
//=======================================================================
//function : DT_PlaneDividedFace 
//purpose  : Transfer into a plane with boundary divided
//           
//
//=======================================================================
static Standard_Integer DT_PlaneDividedFace (Draw_Interpretor& di,
				   Standard_Integer n, const char** a)

{
  // a[1]= result
  // a[2]= input Face
  // a[3]= Tolerance

  if (n !=4) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  Standard_Real      Tol=atof(a[3]);
  TopoDS_Shape inputShape=DBRep::Get(a[2]);
  TopoDS_Face  inputFace = TopoDS::Face(inputShape);
  if (inputFace.IsNull()) {
    di << a[2] << " is not a face" << "\n";
    return 1;
  }

  ShapeUpgrade_PlaneDividedFace theTool(ThePlane);
  theTool.Init(inputFace);
  //theTool.SetBoundaryCriterion(GeomAbs_C1);
  //theTool.SetTolerance(Tol);
  theTool.Build();
  if (!theTool.IsDone()) {
    di << "Not done" << "\n";
    return 1;
  }    

  TopoDS_Face res = theTool.Face();
  DBRep::Set(a[1],res);

  Standard_Real the2d3dFactor=theTool.Get2d3dFactor();
  di << "2d3dFactor="<<the2d3dFactor<< "\n";
  return 0;
}

//=======================================================================
//function : DT_PlaneGridShell 
//purpose  : Create a Plane Grid Shell from U and V knots
//           
//
//=======================================================================
static Standard_Integer DT_PlaneGridShell (Draw_Interpretor& di,
				   Standard_Integer n, const char** a)

{

  if (n < 4) return 1;
  // a[1]= result
  // a[2]= NbU >=2
  // a[3]= NbV >=2
  // a[4..]= {UKnots}
  // a[4+NbU...] = {VKnots}
  // a[4+NbU+NbV+1] = Tol

  // number of knots:
  Standard_Integer NbU=atoi(a[2]);
  Standard_Integer NbV=atoi(a[3]);
  if (n != 4+NbU+NbV+1) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  TColStd_Array1OfReal TheUKnots(1,NbU);
  TColStd_Array1OfReal TheVKnots(1,NbV);

  for (Standard_Integer ii=1; ii<=NbU; ii++) {
    TheUKnots(ii)=atof(a[4+ii-1]);
  }
  for (ii=1; ii<=NbV; ii++) {
    TheVKnots(ii)=atof(a[4+NbU+ii-1]);
  }

  Standard_Real Tol=atof(a[4+NbU+NbV]);

  ShapeUpgrade_PlaneGridShell TheGrid(ThePlane,TheUKnots,TheVKnots,Tol);

  TopoDS_Shell res = TheGrid.Shell();
  DBRep::Set(a[1],res);

  return 0;
}

//=======================================================================
//function : DT_PlaneFaceCommon 
//purpose  : Common between a plane Face and a Shell whose all Faces are 
//           laying in the same plane
//           
//
//=======================================================================
static Standard_Integer DT_PlaneFaceCommon (Draw_Interpretor& di,
				   Standard_Integer n, const char** a)

{
  // a[1]= result
  // a[2]= input Face
  // a[3]= input Shell

  if (n !=4) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  TopoDS_Shape inputShape= DBRep::Get(a[2]);
  TopoDS_Face  inputFace = TopoDS::Face(inputShape);
  if (inputFace.IsNull()) {
    di << a[2] << " is not a face" << "\n";
    return 1;
  }

  inputShape = DBRep::Get(a[3]);
  TopoDS_Shell inputShell = TopoDS::Shell(inputShape);
  if (inputShell.IsNull()) {
    di << a[3] << " is not a shell" << "\n";
    return 1;
  }

  ShapeUpgrade_PlaneFaceCommon theTool;
  theTool.Init(inputFace,inputShell);

  TopoDS_Shell res = theTool.Shell();
  DBRep::Set(a[1],res);

  return 0;
}*/

//=======================================================================
//function : DT_SplitCurve 
//purpose  :  Splits the curve with C1 criterion
//           
//
//=======================================================================
static Standard_Integer DT_SplitCurve (Draw_Interpretor& di,
				   Standard_Integer n, const char** a)

{
  // a[1]= input curve. This name is used with a suffix to name the output curves
  // a[2]= Tolerance

  if (n < 3) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  Standard_Real      Tol=atof(a[2]);
  Handle(Geom_Curve) GC = DrawTrSurf::GetCurve(a[1]);
  if ( GC.IsNull()) return 1;
  Standard_Integer Split = atoi(a[3]);
  Handle(ShapeUpgrade_SplitCurve3dContinuity) theTool = new ShapeUpgrade_SplitCurve3dContinuity;
  theTool->Init(GC);
  theTool->SetTolerance (Tol);
  theTool->SetCriterion (GeomAbs_C1);
  if(Split == 1) {
    Handle(TColStd_HSequenceOfReal) spval = new TColStd_HSequenceOfReal;
    for(Standard_Integer i = 1; i<=5; i++) spval->Append(i);
    theTool->SetSplitValues(spval);
  }
  theTool->Perform (Standard_True);
  Handle(TColGeom_HArray1OfCurve) theCurves= theTool->GetCurves();
  Standard_Integer NbC=theCurves->Length();
  for (Standard_Integer icurv=1; icurv<=NbC; icurv++) {
    char name[100];
    sprintf(name,"%s%s%d",a[1],"_",icurv);
    char* newname = name;
    DrawTrSurf::Set(newname, theCurves->Value(icurv));
    di.AppendElement(newname);
  }
  return 0;
}


//=======================================================================
//function : DT_SplitCurve2d 
//purpose  :  Splits the curve with C1 criterion
//           
//
//=======================================================================
static Standard_Integer DT_SplitCurve2d (Draw_Interpretor& di,
				   Standard_Integer n, const char** a)

{
  // a[1]= input 2d curve. This name is used with a suffix to name the output curves
  // a[2]= Tolerance

  if (n < 3) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  Standard_Real      Tol=atof(a[2]);
  Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[1]);
  if ( GC.IsNull()) return 1;
  Standard_Integer Split = atoi(a[3]);
  Handle(ShapeUpgrade_SplitCurve2dContinuity) theTool = new ShapeUpgrade_SplitCurve2dContinuity;
  theTool->Init(GC);
  theTool->SetTolerance (Tol);
  theTool->SetCriterion (GeomAbs_C1);
  if(Split == 1) {
    Handle(TColStd_HSequenceOfReal) spval = new TColStd_HSequenceOfReal;
    for(Standard_Integer i = 1; i<=5; i++) spval->Append(i);
    theTool->SetSplitValues(spval);
  }
  theTool->Perform (Standard_True);
  Handle(TColGeom2d_HArray1OfCurve) theCurves= theTool->GetCurves();
  Standard_Integer NbC=theCurves->Length();
  for (Standard_Integer icurv=1; icurv<=NbC; icurv++) {
    char name[100];
        sprintf(name,"%s%s%d",a[1],"_",icurv);
    char* newname = name;
    DrawTrSurf::Set(newname, theCurves->Value(icurv));
    di.AppendElement(newname);
  }
  return 0;
}


//=======================================================================
//function : DT_SplitSurface 
//purpose  :  Splits the surface with C1 criterion
//           
//
//=======================================================================
/*
static Standard_Integer DT_SplitWire (Draw_Interpretor& di,
				      Standard_Integer n, const char** a)
{

  if (n <3) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  TopoDS_Face source = TopoDS::Face(DBRep::Get(a[2]));
  if(source.IsNull()) {
    di <<"Shape is not face"<<"\n";
    return 1;
  }
  TopoDS_Iterator wi(source);
  if(!wi.More()) {
    di <<"Shape is face without wire"<<"\n";
    return 1;
  }
  
  TopoDS_Wire wire = TopoDS::Wire(wi.Value());
  Handle(ShapeUpgrade_WireDivideContinuity) tool = new ShapeUpgrade_WireDivideContinuity;
  tool->Init(wire,source);
  if(n >=4 ) {
    Standard_Real      Tol=atof(a[3]);
  }
  Handle(ShapeBuild_ReShape) context = new ShapeBuild_ReShape;
  tool->Perform(context);
  TopoDS_Wire result = tool->Wire();
  DBRep::Set(a[1],result);
  return 0;
}
*/
/*
static Standard_Integer DT_SplitFace (Draw_Interpretor& di,
				      Standard_Integer n, const char** a)
{

  if (n <3) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  TopoDS_Face source = TopoDS::Face(DBRep::Get(a[2]));
  if(source.IsNull()) {
    di <<"Shape is not face"<<"\n";
    return 1;
  } 
  Handle(ShapeUpgrade_ShapeDivideContinuity) tool = new ShapeUpgrade_FaceDivideContinuity;
  tool->Init(source);
  if(n >=4 ) {
    Standard_Real      Tol=atof(a[3]);
    tool->SetPrecision(Tol);
  }
  
  Handle(ShapeBuild_ReShape) context = new ShapeBuild_ReShape;
  tool->Perform(context);
  TopoDS_Shape result = tool->Result();
  
  
  if ( tool->Status ( ShapeExtend_OK ) ) di << "Status: OK" << "\n";
  if ( tool->Status ( ShapeExtend_DONE1 ) ) di << "Status: DONE1" << "\n";
  if ( tool->Status ( ShapeExtend_DONE2 ) ) di << "Status: DONE2" << "\n";
  if ( tool->Status ( ShapeExtend_DONE3 ) ) di << "Status: DONE3" << "\n";
  if ( tool->Status ( ShapeExtend_DONE4 ) ) di << "Status: DONE4" << "\n";
  if ( tool->Status ( ShapeExtend_DONE5 ) ) di << "Status: DONE5" << "\n";
  if ( tool->Status ( ShapeExtend_DONE6 ) ) di << "Status: DONE6" << "\n";
  if ( tool->Status ( ShapeExtend_DONE7 ) ) di << "Status: DONE7" << "\n";
  if ( tool->Status ( ShapeExtend_DONE8 ) ) di << "Status: DONE8" << "\n";
  if ( tool->Status ( ShapeExtend_FAIL1 ) ) di << "Status: FAIL1" << "\n";
  if ( tool->Status ( ShapeExtend_FAIL2 ) ) di << "Status: FAIL2" << "\n";
  if ( tool->Status ( ShapeExtend_FAIL3 ) ) di << "Status: FAIL3" << "\n";
  if ( tool->Status ( ShapeExtend_FAIL4 ) ) di << "Status: FAIL4" << "\n";
  if ( tool->Status ( ShapeExtend_FAIL5 ) ) di << "Status: FAIL5" << "\n";
  if ( tool->Status ( ShapeExtend_FAIL6 ) ) di << "Status: FAIL6" << "\n";
  if ( tool->Status ( ShapeExtend_FAIL7 ) ) di << "Status: FAIL7" << "\n";
  if ( tool->Status ( ShapeExtend_FAIL8 ) ) di << "Status: FAIL8" << "\n";

  // fixes
  
  ShapeFix::SameParameter ( result, Standard_False );
  
  DBRep::Set(a[1],result);
  return 0;
}
*/

static Standard_Integer DT_SplitSurface (Draw_Interpretor& di,
				   Standard_Integer n, const char** a)

{
  // a[1]= result (used with a suffix to name the output surfaces)
  // a[2]= input surface. 
  // a[3]= Tolerance

  // a[1]= result
  // a[2]= nbU
  // a[3]= nbV
  // a[3+1]..a[3+nbU*nbV] = Input Surfaces
  // a[4+nbU*nbV]= Tolerance

  if (n <4) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  Handle(ShapeUpgrade_SplitSurfaceContinuity) theTool = new ShapeUpgrade_SplitSurfaceContinuity;//S4137
  
  Standard_Real      Tol=atof(a[3]);
  Standard_Integer Split = atoi(a[4]);
  theTool->SetTolerance(Tol);
  theTool->SetCriterion(GeomAbs_C1);
  Handle(Geom_Surface) GS = DrawTrSurf::GetSurface(a[2]);
/* 
  if ( GS.IsNull()) {
    // Case of composite grid surface
    di << "composite surf" << "\n";
    Standard_Integer      nbU=atoi(a[2]);
    Standard_Integer      nbV=atoi(a[3]);
    if (nbU==0 || nbV==0) return 1;
    Handle(TColGeom_HArray2OfSurface) 
      theGrid= new TColGeom_HArray2OfSurface(1,nbU,1,nbV);
    for (Standard_Integer iu=1; iu<=nbU; iu++) {
      for (Standard_Integer iv=1; iv<=nbV; iv++) {
	Handle(Geom_Surface) GS = DrawTrSurf::GetSurface(a[3+(iu-1)*nbV+iv]);
	theGrid->SetValue(iu,iv,GS);
      }
    }
    di << "appel a SplitSurface::Init" << "\n";
    theTool->Init(theGrid);
  }
  else {*/
    // Case of single surface
  di << "single surf" << "\n";
  
  di << "appel a SplitSurface::Init" << "\n";
  theTool->Init(GS);
  if(Split ==1) {
    Handle(TColStd_HSequenceOfReal) spval = new TColStd_HSequenceOfReal;
    for(Standard_Integer i = 1; i<=5; i++) spval->Append(i);
    theTool->SetUSplitValues(spval);
    theTool->SetVSplitValues(spval);
  }

  di << "appel a SplitSurface::Build" << "\n";
  theTool->Build(Standard_True);

  di << "appel a SplitSurface::GlobalU/VKnots" << "\n";
  Handle(ShapeExtend_CompositeSurface) Grid = theTool->ResSurfaces();
  Handle(TColStd_HArray1OfReal) GlobalU=Grid->UJointValues();
  Handle(TColStd_HArray1OfReal) GlobalV=Grid->VJointValues();
  Standard_Integer nbGlU=GlobalU->Length();
  Standard_Integer nbGlV=GlobalV->Length();
  di << "nb GlobalU ; nb GlobalV="<<nbGlU<<" "<<nbGlV;
  for (Standard_Integer iu=1; iu<=nbGlU; iu++)
    di  <<" "<< GlobalU->Value(iu);
//  di <<"\n";
//  di << "nb GlobalV="<<nbGlV;
  for (Standard_Integer iv=1; iv<=nbGlV; iv++)
    di  <<" "<< GlobalV->Value(iv);
  di <<"\n";

di << "appel a Surfaces" << "\n";
  Handle(TColGeom_HArray2OfSurface) theSurfaces= Grid->Patches();

di << "transfert resultat" << "\n";
  Standard_Integer NbRow=theSurfaces->ColLength();
  Standard_Integer NbCol=theSurfaces->RowLength();
  for (Standard_Integer irow=1; irow<=NbRow; irow++) {
    for (Standard_Integer icol=1; icol<=NbCol; icol++) {
      char name[100];
      sprintf(name,"%s%s%d%s%d",a[1],"_",irow,"_",icol);
      char* newname = name;
      DrawTrSurf::Set(newname, theSurfaces->Value(irow, icol));
      di.AppendElement(newname);
    }
  }
  return 0;
}



//=======================================================================
//function : DT_SupportModification 
//purpose  : Changes the supports of a Shell
//           The given surface wil support all the faces of the new shell.
//           with the PCurves of the old ones.
//
//=======================================================================
/*static Standard_Integer DT_SupportModification (Draw_Interpretor& di,
				   Standard_Integer n, const char** a)

{
  // a[1]= result
  // a[2]= input Shell
  // a[3]= new Surface
  // a[4]= 2d3d Scale Factor

  if (n !=5) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  TopoDS_Shape inputShape= DBRep::Get(a[2]);
  TopoDS_Shell inputShell = TopoDS::Shell(inputShape);
  if (inputShell.IsNull()) {
    di << a[2] << " is not a shell" << "\n";
    return 1;
  }

  Handle(Geom_Surface) theSurf = DrawTrSurf::GetSurface(a[3]);
  if ( theSurf.IsNull()) {
    di << a[3] << " is not a surface" << "\n";
    return 1;
  }

  ShapeUpgrade_DataMapOfShapeSurface theMap;
  // Associates thesurf to each face of inputShell.
  TopExp_Explorer ExpSh;
  for (ExpSh.Init(inputShell,TopAbs_FACE); ExpSh.More(); ExpSh.Next()){
    TopoDS_Face theFace= TopoDS::Face(ExpSh.Current());
    theMap.Bind(theFace,theSurf);
  }

  Standard_Real the2d3dFactor=atof(a[4]);
  ShapeUpgrade_SupportModification theTool(inputShell,theMap,the2d3dFactor);

  TopoDS_Shell res = theTool.Shell();
  DBRep::Set(a[1],res);

  return 0;
}*/

//=======================================================================
//function : DT_Debug
//purpose  : activation of the debug mode
//
//=======================================================================
/*
static Standard_Integer DT_Debug (Draw_Interpretor& di,
				   Standard_Integer n, const char** a)

{
  // a[1]= 0/1

  if (n !=2) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }

  if (atoi(a[1])==1) {
    di << "Activation of debug messages"<<"\n";
    ShapeUpgrade::SetDebug(Standard_True);
  }
  else {
    di << "Desactivation of debug messages"<<"\n";
    ShapeUpgrade::SetDebug(Standard_False);
  }
  return 0;
}
*/

/*static Standard_Integer shellsolid
  (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc < 4) {
    di<<"Donner option + nom de SHAPE + nom de RESULTAT"<<"\n";
    di<<"Options : a all   (sewing sur tous les shells\n"
      <<  "          c check (sewing sur shells avec bad edges\n"
	<<"          b bad edges\n          f free edges"<<"\n";
    return 1 ;
  }
  Standard_CString arg1 = argv[1];
  Standard_CString arg2 = argv[2];
  Standard_CString arg3 = argv[3];

  TopoDS_Shape Shape = DBRep::Get(arg2);
  if (Shape.IsNull()) { di<<"Shape unknown : "<<arg2<<"\n"; return 1 ; }

  ShapeAnalysis_Shell STS;
  char opt = arg1[0];
  if      (opt == 'a') STS.LoadShells (Shape);
  else if (opt == 'f') STS.CheckOrientedShells (Shape,Standard_True);
  else if (opt == 'b' || opt == 'c') STS.CheckOrientedShells (Shape,Standard_False);

  else if (opt == 'o') {

//   Test WireOrder
    Handle(ShapeExtend_WireData) Frees = new ShapeExtend_WireData;//:sw  Frees.SetPrecision (BRepBuilderAPI::Precision());
    Standard_Integer nbe = 0;
    for (TopExp_Explorer edges(Shape,TopAbs_EDGE); edges.More(); edges.Next()) {
      Frees->Add (TopoDS::Edge(edges.Current()));  nbe ++;
    }
    ShapeAnalysis_WireOrder WO (Standard_True,BRepBuilderAPI::Precision());
    ShapeAnalysis_Wire saw;
    saw.Load ( Frees );
    saw.SetPrecision ( BRepBuilderAPI::Precision() );
    saw.CheckOrder ( WO );
    
    Standard_Integer stat = WO.Status();
    di<<"Wire Order Status = "<<stat<<" on "<<nbe<<" Edges"<<"\n";
    nbe = WO.NbEdges();
    for (Standard_Integer ie = 1; ie <= nbe; ie ++) {
      Standard_Integer io = WO.Ordered (ie);
      gp_XYZ st3d,en3d;
      WO.XYZ (io,st3d,en3d);
      di<<ie<<": Edge."<<io<<" Start:"<<st3d.X()<<" "<<st3d.Y()<<" "<<st3d.Z()
	<<"  End:"<<en3d.X()<<" "<<en3d.Y()<<" "<<en3d.Z()<<"\n";
    }
//    Chainage ?
    Standard_Real gap = BRepBuilderAPI::Precision();
    WO.SetChains (gap);
    Standard_Integer n1,n2,ic, nc = WO.NbChains();
    for (ic = 1; ic <= nc; ic ++) {
      WO.Chain(ic,n1,n2);
      di<<"Chain."<<ic<<" From "<<n1<<" To "<<n2<<"\n";
    }
  }

  else { di<<"Option non reconnue : "<<arg1<<"\n"; return 1; }

  TopoDS_Shape res;
  if (opt == 'a' || opt == 'c') {
    ShapeUpgrade_ShellSewing sew;
    res = sew.ApplySewing (Shape);
    if (res.IsEqual(Shape)) {
      res.Nullify();
      di<<"ShellSolid : no sewing done"<<"\n";
    }
  } else if (opt == 'b') {
    if (STS.HasBadEdges()) res = STS.BadEdges();
    else di<<"ShellSolid : no bad edge"<<"\n";
  } else if (opt == 'f') {
    if (STS.HasFreeEdges()) res = STS.FreeEdges();
    else di<<"ShellSolid : no free edge"<<"\n";
    if (STS.HasConnectedEdges()) di<<"ShellSolid : connected edges"<<"\n";
    else di<<"ShellSolid : NO connected edges"<<"\n";
  }

  if (!res.IsNull()) DBRep::Set (arg3,res);

  return 0; // Done
}*/
//---------------gka
//=======================================================================
//function : offset2dcurve
//purpose  : 
//
//=======================================================================
static Standard_Integer offset2dcurve
  (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc < 4) {
    di<<"result + curve + offset"<<"\n";
    
    return 1 /* Error */;    
  }
//  Standard_CString arg1 = argv[1];
//  Standard_CString arg2 = argv[2];
  Standard_Real Offset = atof(argv[3]);
  Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(argv[2]);
  if ( GC.IsNull()) return 1;
  Handle(Geom2d_OffsetCurve) offcrv = new Geom2d_OffsetCurve(GC,Offset);
  DrawTrSurf::Set(argv[1], offcrv);
  return 0;
}

//=======================================================================
//function : offsetcurve
//purpose  : 
//
//=======================================================================
static Standard_Integer offsetcurve
  (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc < 5) {
    di<<"result + curve + offset + Dir"<<"\n";
    
    return 1 /* Error */;    
  }
//  Standard_CString arg1 = argv[1];  
//  Standard_CString arg2 = argv[2];
  Standard_Real Offset = atof(argv[3]);
  Handle(Geom_Curve) GC = DrawTrSurf::GetCurve(argv[2]);
  if ( GC.IsNull()) return 1;
  gp_Pnt point;
  DrawTrSurf::GetPoint(argv[4],point);
  gp_Dir dir(point.XYZ()); 
  Handle(Geom_OffsetCurve) offcrv = new Geom_OffsetCurve(GC,Offset,dir);
  DrawTrSurf::Set(argv[1], offcrv);
  return 0;
}

//=======================================================================
//function : compose shell
//purpose  : 
//=======================================================================
static Standard_Integer splitface
  (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc < 5) {
    di << "Split face: splitface result face [u usplit1 usplit2...] [v vsplit1 vsplit2 ...]" << "\n";
    return 1;    
  }
  
  TopoDS_Shape aLocalShape = DBRep::Get(argv[2]) ;
  TopoDS_Face face = TopoDS::Face ( aLocalShape );
  if ( face.IsNull() ) {
    di << argv[2] << " is not Face" << "\n";
    return 1;
  }
  
  Handle(Geom_Surface) S = BRep_Tool::Surface ( face );
  Standard_Real Uf, Ul, Vf, Vl;
  BRepTools::UVBounds ( face, Uf, Ul, Vf, Vl );
  Standard_Real Umin, Umax, Vmin, Vmax;
  S->Bounds ( Umin, Umax, Vmin, Vmax );
  if ( Uf < Umin && ! S->IsUPeriodic() ) Uf = Umin;
  else if ( Uf > Umin ) {
    if ( Precision::IsInfinite(Umin) ) Uf -= 100;
    else Uf = Umin;
  }
  if ( Vf < Vmin && ! S->IsVPeriodic() ) Vf = Vmin;
  else if ( Vf > Vmin ) {
    if ( Precision::IsInfinite(Vmin) ) Vf -= 100;
    else Vf = Vmin;
  }
  if ( Ul > Umax && ! S->IsUPeriodic() ) Ul = Umax;
  else if ( Ul < Umax ) {
    if ( Precision::IsInfinite(Umax) ) Ul += 100;
    else Ul = Umax;
  }
  if ( Vl > Vmax && ! S->IsVPeriodic() ) Vl = Vmax;
  else if ( Vl < Vmax ) {
    if ( Precision::IsInfinite(Vmax) ) Vl += 100;
    else Vl = Vmax;
  }
  
  TColStd_SequenceOfReal uval;
  TColStd_SequenceOfReal vval;

  Standard_Boolean byV = Standard_False;
  Standard_Integer i; // svv Jan11 2000 : porting on DEC
  for ( i=3; i < argc; i++ ) {
    if ( argv[i][0] == 'u' ) byV = Standard_False;
    else if ( argv[i][0] == 'v' ) byV = Standard_True;
    else {
      Standard_Real val = atof ( argv[i] );
      TColStd_SequenceOfReal &vals = ( byV ? vval : uval );
      if ( vals.Length() >0 && val - vals.Last() < Precision::PConfusion() ) {
	di << "Values should be sorted in increasing order; skipped" << "\n";
	continue;
      }
      if ( ( byV && ( val < Vf+Precision::PConfusion() || 
		      val > Vl-Precision::PConfusion() ) ) ||
           (!byV && ( val < Uf+Precision::PConfusion() || 
		      val > Ul-Precision::PConfusion() ) ) ) {
	di << "Values should be inside range of surface; skipped" << "\n";
	continue; 
      }
      vals.Append ( val );
    }
  }
  if ( uval.Length() <1 && vval.Length() <1 ) {
    di << "No splitting defined" << "\n";
    return 1;
  }
  if ( uval.Length() >0 ) {
    di << "Splitting by U: ";
    for ( Standard_Integer j=1; j <= uval.Length(); j++ ) {
      //cout << ( i >j ? ", " : "" ) << uval(j);
      if (i >j) {
	di << ", ";
      } else {
	di << "";
      }
      di << uval(j);
    }
    di << "\n";
  }
  if ( vval.Length() >0 ) {
    di << "Splitting by V: ";
    for ( Standard_Integer j=1; j <= vval.Length(); j++ ) {
      //cout << ( j >1 ? ", " : "" ) << vval(j);
      if (j >1) {
	di << ", ";
      } else {
	di << "";
      }
      di << vval(j);
    }
    di << "\n";
  }
  
  Handle(TColGeom_HArray2OfSurface) AS = new TColGeom_HArray2OfSurface ( 1, uval.Length()+1, 
									 1, vval.Length()+1 );
  for ( i=0; i <= uval.Length(); i++ ) {
    Standard_Real umin = ( i ? uval(i) : Uf );
    Standard_Real umax = ( i < uval.Length() ? uval(i+1) : Ul );
    for ( Standard_Integer j=0; j <= vval.Length(); j++ ) {
      Standard_Real vmin = ( j ? vval(j) : Vf );
      Standard_Real vmax = ( j < vval.Length() ? vval(j+1) : Vl );
      Handle(Geom_RectangularTrimmedSurface) rect = 
	new Geom_RectangularTrimmedSurface ( S, umin, umax, vmin, vmax );
      AS->SetValue ( i+1, j+1, rect );
    }
  }

  Handle(ShapeExtend_CompositeSurface) Grid = new ShapeExtend_CompositeSurface;
  if ( ! Grid->Init ( AS ) ) di << "Grid badly connected!" << "\n";

  ShapeFix_ComposeShell SUCS;
  TopLoc_Location l;
  SUCS.Init ( Grid, l, face, Precision::Confusion() );
  Handle(ShapeBuild_ReShape) RS = new ShapeBuild_ReShape;
  SUCS.SetContext( RS );
  SUCS.Perform ();
  
  if ( SUCS.Status ( ShapeExtend_OK ) ) di << "Status: OK" << "\n";
  if ( SUCS.Status ( ShapeExtend_DONE1 ) ) di << "Status: DONE1" << "\n";
  if ( SUCS.Status ( ShapeExtend_DONE2 ) ) di << "Status: DONE2" << "\n";
  if ( SUCS.Status ( ShapeExtend_DONE3 ) ) di << "Status: DONE3" << "\n";
  if ( SUCS.Status ( ShapeExtend_DONE4 ) ) di << "Status: DONE4" << "\n";
  if ( SUCS.Status ( ShapeExtend_DONE5 ) ) di << "Status: DONE5" << "\n";
  if ( SUCS.Status ( ShapeExtend_DONE6 ) ) di << "Status: DONE6" << "\n";
  if ( SUCS.Status ( ShapeExtend_DONE7 ) ) di << "Status: DONE7" << "\n";
  if ( SUCS.Status ( ShapeExtend_DONE8 ) ) di << "Status: DONE8" << "\n";
  if ( SUCS.Status ( ShapeExtend_FAIL1 ) ) di << "Status: FAIL1" << "\n";
  if ( SUCS.Status ( ShapeExtend_FAIL2 ) ) di << "Status: FAIL2" << "\n";
  if ( SUCS.Status ( ShapeExtend_FAIL3 ) ) di << "Status: FAIL3" << "\n";
  if ( SUCS.Status ( ShapeExtend_FAIL4 ) ) di << "Status: FAIL4" << "\n";
  if ( SUCS.Status ( ShapeExtend_FAIL5 ) ) di << "Status: FAIL5" << "\n";
  if ( SUCS.Status ( ShapeExtend_FAIL6 ) ) di << "Status: FAIL6" << "\n";
  if ( SUCS.Status ( ShapeExtend_FAIL7 ) ) di << "Status: FAIL7" << "\n";
  if ( SUCS.Status ( ShapeExtend_FAIL8 ) ) di << "Status: FAIL8" << "\n";

  TopoDS_Shape sh = SUCS.Result();
  ShapeFix::SameParameter ( sh, Standard_False );
  DBRep::Set ( argv[1], sh );
  return 0;
}

static Standard_Integer converttobspline
  (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc<3) {
    di << "Use: " << argv[0] << " result shape [options=ero]\n";
    di << "where options is combination of letters indicating kinds of\n";
    di << "surfaces to be converted:\n";
    di << "e - extrusion\n";
    di << "r - revolution\n";
    di << "o - offset\n";
    di << "p - plane";
    return 1;
  }
  const char *options = ( argc > 3 ? argv[3] : "ero" );
  
  TopoDS_Shape inputShape=DBRep::Get(argv[2]);
  if (inputShape.IsNull()) {
    di << "Unknown shape"<< "\n";
    return 1;
  }
  TopoDS_Shape revsh = ShapeCustom::ConvertToRevolution (inputShape);
  TopoDS_Shape res = 
    ShapeCustom::ConvertToBSpline (revsh, strchr (options, 'e') != 0,
                                          strchr (options, 'r') != 0,
                                          strchr (options, 'o') != 0,
                                          strchr (options, 'p') != 0);
  ShapeFix::SameParameter ( res, Standard_False );
  DBRep::Set ( argv[1], res );
  return 0;
}


static Standard_Integer splitclosed (Draw_Interpretor& di, 
				     Standard_Integer argc, 
				     const char** argv)
{
  if (argc<3) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }
  
  TopoDS_Shape inputShape=DBRep::Get(argv[2]);
  if (inputShape.IsNull()) {
    di << "Unknown shape"<< "\n";
    return 1;
  }
  
  ShapeUpgrade_ShapeDivideClosed tool (inputShape);
  tool.Perform();
  TopoDS_Shape res = tool.Result();
  
  ShapeFix::SameParameter ( res, Standard_False );
  DBRep::Set ( argv[1], res );
  return 0;
}

static Standard_Integer splitarea (Draw_Interpretor& di, 
				     Standard_Integer argc, 
				     const char** argv)
{
  if (argc<4) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }
  
  TopoDS_Shape inputShape=DBRep::Get(argv[2]);
  if (inputShape.IsNull()) {
    di << "Unknown shape"<< "\n";
    return 1;
  }
  Standard_Real aMaxArea = atof(argv[3]);
  
    
  ShapeUpgrade_ShapeDivideArea tool (inputShape);
  if(argc >4) {
    Standard_Real prec = atof(argv[4]);
    tool.SetPrecision(prec);
  }
  tool.MaxArea() = aMaxArea;
  tool.Perform();
  TopoDS_Shape res = tool.Result();
  
  ShapeFix::SameParameter ( res, Standard_False );
  DBRep::Set ( argv[1], res );
  return 0;
}

static Standard_Integer removeinternalwires (Draw_Interpretor& di, 
                                             Standard_Integer argc, 
                                             const char** argv)
{
  if (argc<4) {
    di << "bad number of arguments" <<"\n";
    return 1;
  }
  Standard_Real aMinArea = atof(argv[2]);
  TopoDS_Shape inputShape=DBRep::Get(argv[3]);
  if (inputShape.IsNull()) {
    di << "Unknown shape"<< "\n";
    return 1;
  }
  Handle(ShapeUpgrade_RemoveInternalWires) aTool;
  TopTools_SequenceOfShape aSeqShapes;
  if(inputShape.ShapeType() < TopAbs_WIRE)
     aTool = new ShapeUpgrade_RemoveInternalWires(inputShape);
  else {
   di<<"Invalid type of first shape: should be FACE,SHELL,SOLID or COMPOUND"<<"\n";
   return 1;
  }
  
  Standard_Integer k = 4;
  Standard_Boolean isShape = Standard_True;
  Standard_Boolean aModeRemoveFaces =Standard_True;
  
 
  for( ; k < argc; k++) {
    if(isShape) {
      TopoDS_Shape aShape=DBRep::Get(argv[k]);
      isShape = !aShape.IsNull();
      if(isShape) {
        if(aShape.ShapeType() == TopAbs_FACE || aShape.ShapeType() == TopAbs_WIRE)
          aSeqShapes.Append(aShape);
      }
    }
    if(!isShape) 
      aModeRemoveFaces = (atoi(argv[k]) == 1);
  }
  
  aTool->MinArea() = aMinArea;
  aTool->RemoveFaceMode() = aModeRemoveFaces;
  if(aSeqShapes.Length())
    aTool->Perform(aSeqShapes);
  else
    aTool->Perform();
  if(aTool->Status(ShapeExtend_FAIL1))
     di<<"Initial shape has invalid type"<<"\n";
  else if(aTool->Status(ShapeExtend_FAIL2))
     di<<"Specified sub-shape is not belonged to whole shape"<<"\n";   
  if(aTool->Status(ShapeExtend_DONE1)) {
    const TopTools_SequenceOfShape& aRemovedWires =aTool->RemovedWires(); 
     di<<aRemovedWires.Length()<<" internal wires were removed"<<"\n";
    
  }
  if(aTool->Status(ShapeExtend_DONE2)) {
    const TopTools_SequenceOfShape& aRemovedFaces =aTool->RemovedFaces(); 
     di<<aRemovedFaces.Length()<<" small faces were removed"<<"\n";
    
  }   
  TopoDS_Shape res = aTool->GetResult();
  
  
  DBRep::Set ( argv[1], res );
  return 0;
}

//=======================================================================
//function : InitCommands
//purpose  : 
//=======================================================================

 void SWDRAW_ShapeUpgrade::InitCommands(Draw_Interpretor& theCommands) 
{
  static Standard_Integer initactor = 0;
  if (initactor) return;  initactor = 1;
  
  Standard_CString g = SWDRAW::GroupName(); // "Tests of DivideTool";
 
  theCommands.Add("DT_ShapeDivide",
		  "DT_ShapeDivide Result Shape Tol: Divides shape with C1 Criterion",
		  __FILE__,
		  DT_ShapeDivide,g);
  
  theCommands.Add("DT_SplitAngle",
		  "DT_SplitAngle Result Shape [MaxAngle=95]: Divides revolved surfaces on segments less MaxAngle deg",
		  __FILE__,
		  DT_SplitAngle,g);

  theCommands.Add("DT_ShapeConvert",
		  "DT_ShapeConvert Result Shape convert2d convert3d: Converts curves to beziers",
		  __FILE__,
		  DT_ShapeConvert,g);
  
  theCommands.Add("DT_ShapeConvertRev",
		  "DT_ShapeConvert Result Shape convert2d convert3d: Converts curves to beziers",
		  __FILE__,
		  DT_ShapeConvertRev,g);
/*  theCommands.Add("DT_PlaneDividedFace",
		  "DT_PlaneDividedFace Result Face Tol: Transfer into a plane with boundary divided",
		  __FILE__,
		  DT_PlaneDividedFace,g);

  theCommands.Add("DT_PlaneGridShell",
		  "DT_PlaneGridShell Result NbU NbV {UKnots} {VKnots} Tol : Create a plane grid Shell",
		  __FILE__,
		  DT_PlaneGridShell,g);

  theCommands.Add("DT_PlaneFaceCommon",
		  "DT_PlaneFaceCommon Result Face Shell: Common between a plane Face and a Shell",
		  __FILE__,
		  DT_PlaneFaceCommon,g);*/

  theCommands.Add("DT_SplitCurve2d",
		  "DT_SplitCurve2d Curve Tol: Splits the curve with C1 criterion",
		  __FILE__,
		  DT_SplitCurve2d,g);

  theCommands.Add("DT_SplitCurve",
		  "DT_SplitCurve Curve Tol: Splits the curve with C1 criterion",
		  __FILE__,
		  DT_SplitCurve,g);

  theCommands.Add("DT_SplitSurface",
		  "DT_SplitSurface Result Surface/GridSurf Tol: Splits the surface with C1 criterion",
		  __FILE__,
		  DT_SplitSurface,g);

  /*theCommands.Add("DT_SupportModification",
		  "DT_SupportModification Result Shell Surface 2d3dFactor: Surface will support all the faces",
		  __FILE__,
		  DT_SupportModification,g);*/

//  theCommands.Add("DT_SpltWire","DT_SpltWire Result Wire Tol",
//		  __FILE__,DT_SplitWire,g);
  
//  theCommands.Add("DT_SplitFace", "DT_SplitFace Result Face Tol",
//		  __FILE__, DT_SplitFace,g);
  
//  theCommands.Add("DT_Debug", "DT_Debug 0/1 : activation/desactivation of the debug messages",
//		  __FILE__, DT_Debug,g);
//  theCommands.Add ("shellsolid","option[a-b-c-f] shape result",
//		   __FILE__,shellsolid,g);
  theCommands.Add ("offset2dcurve","result curve offset",
		   __FILE__,offset2dcurve,g);
  
  theCommands.Add ("offsetcurve","result curve offset dir",
		   __FILE__,offsetcurve,g);

  theCommands.Add ("splitface","result face [u usplit1 usplit2...] [v vsplit1 vsplit2 ...]",
		   __FILE__,splitface,g);
  
  theCommands.Add ("DT_ToBspl","result shape [options=erop]",
		   __FILE__,converttobspline,g);
  theCommands.Add ("DT_ClosedSplit","result shape",
		   __FILE__,splitclosed,g);
  theCommands.Add ("DT_SplitByArea","result shape maxarea [preci]",
		   __FILE__,splitarea,g);
  
  theCommands.Add ("RemoveIntWires","result minarea wholeshape [faces or wires] [moderemoveface ]",
                   __FILE__,removeinternalwires,g);
}