summaryrefslogtreecommitdiff
path: root/src/BRepAlgo/BRepAlgo_DSAccess.cxx
blob: 07aacfee6e734b9ba3dbb99a9d7c5940fc37c469 (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
// File:	BRepAlgo_DSAccess.cxx
// Created:	Wed Aug 13 16:05:15 1997
// Author:	Prestataire Mary FABIEN
//		<fbi@langdox.paris1.matra-dtv.fr>


#include <BRepAlgo_DSAccess.ixx>
#include <BRepAlgo_EdgeConnector.hxx>

#include <TColStd_ListOfInteger.hxx>
#include <TColStd_MapOfInteger.hxx>
#include <TColStd_IndexedMapOfInteger.hxx>
#include <TColStd_MapIteratorOfMapOfInteger.hxx>
#include <TColStd_SetIteratorOfSetOfInteger.hxx>

#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TColStd_ListIteratorOfListOfInteger.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Wire.hxx>
#include <TopOpeBRepDS_PointIterator.hxx>
#include <TopOpeBRepDS_BuildTool.hxx>
#include <TopOpeBRepDS_CheckStatus.hxx>
#include <TopOpeBRepDS_Check.hxx>
#include <TopOpeBRepDS_ListOfInterference.hxx>
#include <TopOpeBRepDS_Interference.hxx>
#include <TopOpeBRepDS_InterferenceIterator.hxx>
#include <TopOpeBRepDS_ListIteratorOfListOfInterference.hxx>
#include <TopOpeBRepDS_ShapeShapeInterference.hxx>
#include <TopOpeBRepDS_HDataStructure.hxx>
#include <TopOpeBRepDS_CurveExplorer.hxx>
#include <TopOpeBRepDS_CurveIterator.hxx>
#include <TopOpeBRepDS_Filter.hxx>
#include <TopOpeBRepDS_Reducer.hxx>
#include <TopOpeBRepTool_GeomTool.hxx>
#include <TopOpeBRepBuild_HBuilder.hxx>
#include <TopOpeBRepBuild_WireEdgeSet.hxx>
#include <TopOpeBRepBuild_FaceBuilder.hxx>
#include <TopOpeBRep_DSFiller.hxx>

#ifdef DRAW
//#include <TestTopOpe.hxx>
#endif

//=======================================================================
//function : Create
//purpose  : 
//=======================================================================

BRepAlgo_DSAccess::BRepAlgo_DSAccess() {
  Init();
}

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

void BRepAlgo_DSAccess::Init()
{
  if(myHDS.IsNull()) 
    myHDS = new TopOpeBRepDS_HDataStructure();
  else
    myHDS->ChangeDS().Init();
  myRecomputeBuilderIsDone = Standard_False;
  myGetSectionIsDone = Standard_False;
  myListOfCompoundOfEdgeConnected.Clear();
  myEC = new BRepAlgo_EdgeConnector();
  myHB.Nullify();
  
  // init of the builder
  Standard_Real tol3dAPPROX = 1e-7;
  Standard_Real tol2dAPPROX = 1e-7;
  // set tolerance values used by the APPROX process
  TopOpeBRepTool_GeomTool GT;
  GT.Define(TopOpeBRepTool_APPROX);
  GT.SetTolerances(tol3dAPPROX,tol2dAPPROX);
  TopOpeBRepDS_BuildTool BT(GT);
  myHB = new TopOpeBRepBuild_HBuilder(BT);
  myHB->ChangeBuilder().ChangeClassify(Standard_False);

  myState1 = TopAbs_UNKNOWN;
  myState2 = TopAbs_UNKNOWN;

}


// Filling of the DS

//=======================================================================
//function : Load
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::Load(const TopoDS_Shape& S)
{
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  myS1 = S;
  DS.AddShape(S, 1);
}

//=======================================================================
//function : Load
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::Load(TopoDS_Shape& S1, 
			     TopoDS_Shape& S2)
{
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  
  if ( S1.Orientation() == TopAbs_REVERSED ) {
    S1.Orientation(TopAbs_FORWARD);
  }
  if ( S2.Orientation() == TopAbs_REVERSED ) {
    S2.Orientation(TopAbs_FORWARD);
  }
  
  DS.AddShape(S1,1);
  DS.AddShape(S2,2);

  TopOpeBRepTool_ShapeExplorer ex1,ex2;
  for (ex1.Init(S1,TopAbs_SOLID); ex1.More(); ex1.Next()) {
    const TopoDS_Shape& so1 = ex1.Current();
    for (ex2.Init(S2,TopAbs_SOLID); ex2.More(); ex2.Next()) {
      const TopoDS_Shape& so2 = ex2.Current();
      DS.FillShapesSameDomain(so1,so2);
    }
  }
  
  myS1 = S1;
  myS2 = S2;

#ifdef DRAW
//  TestTopOpe::CurrentDS(myHDS);
//  TestTopOpe::Shapes(myS1,myS2);
#endif
}

//=======================================================================
//function : Intersect
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::Intersect()
{
  myRecomputeBuilderIsDone = Standard_False;
  
  if(!myS1.IsNull() && !myS2.IsNull())
    myDSFiller.Insert(myS1, myS2, myHDS);
}

//=======================================================================
//function : Intersect
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::Intersect
(const TopoDS_Shape& S1,
 const TopoDS_Shape& S2)
{
  myRecomputeBuilderIsDone = Standard_False;
  
  if(S1.IsNull() || S2.IsNull()) {
    return;
  }
  
  Standard_Boolean orientFORWARD = Standard_False;
  TopExp_Explorer exp;
  if(S1.ShapeType() != TopAbs_FACE) {
    exp.Init(S1, TopAbs_FACE);
    if(!exp.More())
      return;
  }
  if(S2.ShapeType() != TopAbs_FACE) {
    exp.Init(S2, TopAbs_FACE);
    if(!exp.More())
      return;
  }
  myDSFiller.Insert(S1, S2, myHDS, orientFORWARD);
}

//=======================================================================
//function : SameDomain
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::SameDomain
(const TopoDS_Shape& S1,
 const TopoDS_Shape& S2)
{
  myRecomputeBuilderIsDone = Standard_False;
  
  if(S1.IsNull() || S2.IsNull())
    return;

  TopExp_Explorer exp1, exp2;
    exp1.Init(S1, TopAbs_FACE);
    if(!exp1.More())
      return;
    exp2.Init(S2, TopAbs_FACE);
    if(!exp2.More())
      return;
  
  myDSFiller.Insert2d(S1, S2, myHDS);
}


// Construction of Sections

#define FindKeep Standard_False

//=======================================================================
//function : GetSectionEdgeSet
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepAlgo_DSAccess::GetSectionEdgeSet
(const TopoDS_Shape& S1,
 const TopoDS_Shape& S2)
{
  GetSectionEdgeSet();

  // Check if S1 and S2 contain faces
  TopExp_Explorer exp1, exp2;
  exp1.Init(S1, TopAbs_FACE);
  if(!exp1.More())
    return myEmptyListOfShape;
  exp2.Init(S2, TopAbs_FACE);
  if(!exp2.More())
    return myEmptyListOfShape;
  
  for(exp1.Init(S1, TopAbs_FACE); exp1.More(); exp1.Next()) {
    if(!myHDS->HasShape(exp1.Current(), FindKeep))
      return myEmptyListOfShape;
  }
  for(exp2.Init(S2, TopAbs_FACE); exp2.More(); exp2.Next())
    if(!myHDS->HasShape(exp2.Current(), FindKeep))
      return myEmptyListOfShape;
  
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  TopOpeBRepBuild_Builder& Builder = myHB->ChangeBuilder();
  
  // The purpose is to find interferences associated with faces,
  // edges that come from their Geometry (= Edge || Curve)
  TopTools_ListOfShape LE;
  LE.Clear();
  TopExp_Explorer exp;
  for(exp1.Init(S1, TopAbs_FACE); exp1.More(); exp1.Next()) {
    const TopoDS_Shape& F1 = exp1.Current();
    
    TopOpeBRepDS_ListOfInterference& lof = DS.ChangeShapeInterferences(F1);
    TopOpeBRepDS_InterferenceIterator li(lof);
    li.SupportKind(TopOpeBRepDS_FACE);
    for(exp2.Init(S2, TopAbs_FACE); exp2.More(); exp2.Next()) {
      const TopoDS_Shape& F2 = exp2.Current();
      Standard_Integer si = DS.Shape(F2, FindKeep);
      li.Support(si);
      
      for(; li.More(); li.Next()) {
	const TopOpeBRepDS_Interference& CurrInt = li.Value();
	TopOpeBRepDS_Kind gk = CurrInt.GeometryType();
	Standard_Integer gi = CurrInt.Geometry();
	const TopoDS_Shape& geosha = DS.Shape(gi, FindKeep);
	if(gk == TopOpeBRepDS_CURVE) {
	  const TopTools_ListOfShape& lEdge = myHB->NewEdges(gi);
	  LE.Append(lEdge.First());
	} else {
	  const TopTools_ListOfShape& lEdge = Builder.Splits(geosha, TopAbs_ON);
	  TopTools_ListIteratorOfListOfShape it(lEdge);
	  for(; it.More(); it.Next()) {
	    const TopoDS_Shape& CurrEdge = it.Value();
	    Standard_Integer ipv1, ipv2;
	    TopOpeBRepDS_Kind pvk1, pvk2;
	    PntVtxOnSectEdge(CurrEdge, ipv1, pvk1, ipv2, pvk2);
	    if(pvk1 != TopOpeBRepDS_VERTEX) {
	      ipv1 = 0;
	      if(pvk2 != TopOpeBRepDS_VERTEX) continue;
	    } else {
	      if(pvk2 != TopOpeBRepDS_VERTEX)
		ipv2 = 0;
	    }
	    for(exp.Init(F1, TopAbs_VERTEX); exp.More(); exp.Next()) {
	      Standard_Integer iVert = DS.Shape(exp.Current());
	      if(iVert) {
		if((iVert == ipv1) || (iVert == ipv2)) {
		  LE.Append(CurrEdge);
		  break;
		}
	      }
	    }
	  }
	}
      }
    }
  }

  // find all groups of connected Edges associated to LE
  TopTools_ListIteratorOfListOfShape ILE;
  myCurrentList.Clear();
  TopTools_MapOfShape ME;
  ME.Clear();
  TopTools_ListIteratorOfListOfShape ILC;
  TopExp_Explorer ECE;
  ILE.Initialize(LE);
  for(;ILE.More();ILE.Next()) {
    const TopoDS_Shape& E = ILE.Value();
    ILC.Initialize(myListOfCompoundOfEdgeConnected);
    for(;ILC.More();ILC.Next()) {
      const TopoDS_Shape& Com = ILC.Value();
      ECE.Init(Com, TopAbs_EDGE);
      for(;ECE.More();ECE.Next()) {
	if(ECE.Current().IsSame(E)) {
	  if(!ME.Contains(Com)) {
	    myCurrentList.Append(Com);
	    ME.Add(Com);
	    break;
	  }
	}
      }
    }
  }
  
  return myCurrentList;
}

//=======================================================================
//function : GetSectionEdgeSet
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepAlgo_DSAccess::GetSectionEdgeSet()
{
  if(!myRecomputeBuilderIsDone) {
    // it is possible to call the method many times consecutively
    myHDS->AddAncestors(myS1);
    // start of lpa modification
    if (!myS1.IsSame(myS2) && !myS2.IsNull()) {
      myHDS->AddAncestors(myS2);
      myHB->Perform(myHDS,myS1,myS2);
    }
    else {
      myHB->Perform(myHDS);
    }
    // end of modif lpa
    myRecomputeBuilderIsDone = Standard_True;
    myGetSectionIsDone = Standard_False;
  } 
  if(myGetSectionIsDone)
    return myListOfCompoundOfEdgeConnected;
  myGetSectionIsDone = Standard_True;
  
  myListOfCompoundOfEdgeConnected.Clear();
  
  // EdgeConnector
  Handle(BRepAlgo_EdgeConnector) EC = myEC;
  EC->ClearStartElement();
  TopTools_MapOfShape ME;
  ME.Clear();
  myHB->InitSection();
  for(; myHB->MoreSection(); myHB->NextSection()) {
    const TopoDS_Edge& ES = TopoDS::Edge(myHB->CurrentSection());
    if(ME.Contains(ES)) continue;
    ME.Add(ES);
    EC->AddStart(ES);
  }
  TopTools_ListOfShape& LW = EC->MakeBlock();
  
  // the wires are tranformed into compounds.
  myCompoundWireMap.Clear();
  BRep_Builder BB;
  TopTools_ListIteratorOfListOfShape ILW(LW);
  TopExp_Explorer Explor;
  for(;ILW.More();ILW.Next()) {
      TopoDS_Compound Compound;
//POP
      BB.MakeCompound(Compound);
//      BB.MakeCompound(TopoDS::Compound(Compound));
      Explor.Init(ILW.Value(), TopAbs_EDGE);
      for(;Explor.More(); Explor.Next()) {
	BB.Add(Compound, Explor.Current());
      }
      myListOfCompoundOfEdgeConnected.Append(Compound);
      myCompoundWireMap.Bind(Compound,ILW.Value());
    }
  return myListOfCompoundOfEdgeConnected;
}

//=======================================================================
//function : IsWire
//purpose  : 
//=======================================================================

Standard_Boolean BRepAlgo_DSAccess::IsWire(const TopoDS_Shape& S)
{
  Standard_Boolean b = Standard_False;
  if(myEC->IsDone()) {
    if (myCompoundWireMap.IsBound(S))
      b = myEC->IsWire(myCompoundWireMap(S));
  }
  return b;
}

//=======================================================================
//function : Wire
//purpose  : 
//=======================================================================

const TopoDS_Shape& BRepAlgo_DSAccess::Wire(const TopoDS_Shape& S)
{
  if(!IsWire(S)) {
    myWire.Nullify();
  }
  else {
    BRep_Builder BB;
    BB.MakeWire(myWire);
    TopExp_Explorer Explor(S, TopAbs_EDGE);
    for(;Explor.More(); Explor.Next()) BB.Add(myWire, Explor.Current());
  }
  return myWire;
}

//=======================================================================
//function : SectionVertex
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepAlgo_DSAccess::SectionVertex
(const TopoDS_Shape& F,
 const TopoDS_Shape& E)
{
  TopTools_ListOfShape Result;
  Result.Clear();
  if(F.ShapeType() != TopAbs_FACE) return myEmptyListOfShape;
  if(E.ShapeType() != TopAbs_EDGE) return myEmptyListOfShape;
  Standard_Integer iF = myHDS->Shape(F), iE = myHDS->Shape(E);
  if((iF == 0) || (iE == 0)) return myEmptyListOfShape;

  const TopOpeBRepDS_DataStructure& DS = myHDS->DS();
  const TopOpeBRepDS_ListOfInterference& LI = 
    DS.ShapeInterferences(E, Standard_False);
  TopOpeBRepDS_InterferenceIterator II(LI);
  Standard_Integer goodIndex = 0;
  TopOpeBRepDS_Kind goodKind;
  for(;II.More();II.Next()) {
    Handle(TopOpeBRepDS_Interference)& I = II.Value();
    const TopOpeBRepDS_Transition& T = I->Transition();
    if((T.ONAfter() == TopAbs_FACE) &&
       (T.IndexAfter()  == iF)) {
      goodKind  = I->GeometryType();
      goodIndex = I->Geometry();
      if(goodKind == TopOpeBRepDS_VERTEX)
	Result.Append(myHDS->Shape(goodIndex));
      else 
	if (goodKind == TopOpeBRepDS_POINT)
	  Result.Append(myHB->NewVertex(goodIndex));
    }
  }
  myListOfVertex = Result;
  return myListOfVertex;
}


// Editing of the DS

//=======================================================================
//function : SuppressEdgeSet
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::SuppressEdgeSet
(const TopoDS_Shape& C)
{
  // It is checked if C really is a Coumpound of connected Edges

  myHB->InitExtendedSectionDS();
//  myGetSectionIsDone = Standard_False;

  TopTools_ListIteratorOfListOfShape LLS(myListOfCompoundOfEdgeConnected);
  for(;LLS.More(); LLS.Next())
    if(C == LLS.Value())
      break;
  if(!LLS.More())
    return;
  
  // Cleaning
  TopoDS_Shape Empty;
  Empty.Nullify();
  Suppress(C, Empty);
  myListOfCompoundOfEdgeConnected.Remove(LLS);
}

//=======================================================================
//function : ChangeEdgeSet
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::ChangeEdgeSet
(const TopoDS_Shape& Old, const TopoDS_Shape& New)
{
  // It is checked if Old is a Coumpound of connected Edges

  myHB->InitExtendedSectionDS();

  TopTools_ListIteratorOfListOfShape LLS(myListOfCompoundOfEdgeConnected);
  for(;LLS.More(); LLS.Next())
    if(Old == LLS.Value())
      break;
  if(!LLS.More())
    return;

  // The compound of Edges to be rotated is constructed
  BRep_Builder B;
  Standard_Boolean Trouve;
  Standard_Integer iC;
  TopoDS_Compound C;
  TopoDS_Edge E;
  B.MakeCompound(C);
  TColStd_SetOfInteger RPoint; //The points to be controlled 
  
 TopOpeBRepDS_ListIteratorOfListOfInterference iter;
  TopExp_Explorer exp(Old, TopAbs_EDGE);
  TopExp_Explorer exp2;
  for(; exp.More(); exp.Next()) {
    const TopoDS_Shape& Edge = exp.Current(); 
    for(exp2.Init(New, TopAbs_EDGE), Trouve=Standard_False;
	exp2.More() && (!Trouve); exp2.Next()) {
      E = TopoDS::Edge(exp2.Current());
      Trouve = E.IsSame(Edge);
    }

    if (!Trouve) B.Add(C, Edge); // Edge to be removed
    else if (!E.IsEqual(Edge)) {
      // It is necessary to change Interferences => take the complement
      iC = myHB->GetDSCurveFromSectEdge(Edge);
      if (!iC) {
#if DEB
	cout << "Warning DSAccess:Modifs d'une Edge non implemente" << endl;
#endif
      }
      else {
	// Complement on the interferences Curve/Face
	Standard_Integer iF;
	Handle(TopOpeBRepDS_Interference) interf;

	iF = myHB->GetDSFaceFromDSCurve(iC, 1);	
	TopOpeBRepDS_ListOfInterference& list1 = 
	   myHDS->ChangeDS().ChangeShapeInterferences(iF);
	for(iter.Initialize(list1); iter.More(); iter.Next()) {
	  interf = iter.Value();
	  if (interf->Geometry() == iC)
	    interf->Transition(interf->Transition().Complement());
	}
	iF = myHB->GetDSFaceFromDSCurve(iC, 2);	
	TopOpeBRepDS_ListOfInterference& list2 = 
	  myHDS->ChangeDS().ChangeShapeInterferences(iF);
	for(iter.Initialize(list2); iter.More(); iter.Next()) {
	  interf = iter.Value();
	  if (interf->Geometry() == iC)
	    interf->Transition(interf->Transition().Complement());
	}
	// The associated points are recorded
	Standard_Integer ipv1, ipv2;
	//Standard_Boolean bid; // skl
	TopOpeBRepDS_Kind k1, k2;	
	PntVtxOnCurve(iC, ipv1, k1, ipv2, k2);
	if (ipv1 != 0) /*bid = */RPoint.Add(ipv1); // skl
	if (ipv2 != 0) /*bid = */RPoint.Add(ipv2); // skl
      }
    }
  }


  // Netoyage
  Suppress(C, New);

  // Is it necessary to invert the Interferences "Edge on Fa"
  if (!RPoint.IsEmpty()) {
    const TopOpeBRepDS_DataStructure & DS = myHDS->DS();
    Standard_Integer iP,iE, nbShape = DS.NbShapes();
    Handle(TopOpeBRepDS_Interference) interf;
    for (iE=1; iE<=nbShape; iE++) {
      if (DS.Shape(iE,0).ShapeType() == TopAbs_EDGE) { 
	const TopOpeBRepDS_ListOfInterference& List = 
	  myHDS->DS().ShapeInterferences(iE);
	for(iter.Initialize(List); iter.More(); iter.Next()) {
	  interf = iter.Value();
	  if (interf->GeometryType() == TopOpeBRepDS_POINT) {
	    iP = interf->Geometry();
	    if (RPoint.Contains(iP))
	      interf->Transition(interf->Transition().Complement());
	  }
	}
      }
    }
  }

  // The old is replaced by new
  LLS.Value() = New;
}


//=======================================================================
//function : Remove
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::Suppress(const TopoDS_Shape& C,
				 const TopoDS_Shape& Keep)
{
 TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
 TopOpeBRepBuild_Builder& Builder = myHB->ChangeBuilder();
 Standard_Integer i, iC = 0, iF1, iF2,iE1, iE2;
// TopOpeBRepDS_ListIteratorOfListOfInterference;
 TColStd_ListIteratorOfListOfInteger it1, it2;

  //A group of points to be kept is constructed
 mySetOfKeepPoint.Clear();
 if (!Keep.IsNull()) {
   //Standard_Boolean B; // skl
   Standard_Integer ipv1, ipv2;
   TopOpeBRepDS_Kind k1, k2;
   TopExp_Explorer exp(Keep, TopAbs_EDGE);
   for(; exp.More(); exp.Next()) {
     const TopoDS_Shape& SectEdge = exp.Current();
     iC = myHB->GetDSCurveFromSectEdge(SectEdge);
     if(!iC) 
       PntVtxOnSectEdge(SectEdge, ipv1, k1, ipv2, k2);
     else 
       PntVtxOnCurve(iC, ipv1, k1, ipv2, k2);
     if (ipv1 != 0) /*B = */mySetOfKeepPoint.Add(ipv1); // skl
     if (ipv2 != 0) /*B = */mySetOfKeepPoint.Add(ipv2); // skl
   }
 }

  // The curves, which generated the the Edges, are found
  // during the parsing the Edges which come from Edge are found
  // (= MapOfInteger : ESE)
  
  // En premier, les interferences de support 1d.
  TopExp_Explorer exp(C, TopAbs_EDGE);
  for(; exp.More(); exp.Next()) {
    const TopoDS_Shape& SectEdge = exp.Current();
    iC = myHB->GetDSCurveFromSectEdge(SectEdge);
    if(!iC) {
      // the Edges that come from Edge are processed
      // the interferences connected with Edges are processed :
      iE1 = myHB->GetDSEdgeFromSectEdge(SectEdge, 1);
      iE2 = myHB->GetDSEdgeFromSectEdge(SectEdge, 2);
      
      RemoveEdgeInterferences(iE1,iE2,SectEdge);
      
      TColStd_ListOfInteger& loi11 = myHB->GetDSFaceFromDSEdge(iE1, 1);
      TColStd_ListOfInteger& loi12 = myHB->GetDSFaceFromDSEdge(iE1, 2);
      for(it1.Initialize(loi11); it1.More(); it1.Next()) {
	iF1 = it1.Value();
	for(it2.Initialize(loi12); it2.More(); it2.Next()) {
	  iF2 = it2.Value();
	  // similar to the case of SectEdges coming from curve.
	  RemoveEdgeInterferences(iF1,iF2,SectEdge);
	}
      }
      TColStd_ListOfInteger& loi21 = myHB->GetDSFaceFromDSEdge(iE2, 1);
      TColStd_ListOfInteger& loi22 = myHB->GetDSFaceFromDSEdge(iE2, 2);
      for(it1.Initialize(loi21); it1.More(); it1.Next()) {
	iF1 = it1.Value();
	for(it2.Initialize(loi22); it2.More(); it2.Next()) {
	  iF2 = it2.Value();
	  // similar to the case of  SectEdges coming from curve.
	  RemoveEdgeInterferences(iF1,iF2,SectEdge);
	}
      }
      continue;
    }    
    // The Edges that come from Curve are processed
    iF1 = myHB->GetDSFaceFromDSCurve(iC, 1);
    iF2 = myHB->GetDSFaceFromDSCurve(iC, 2);
    
    RemoveEdgeInterferences(iF1, iF2, iC);
    DS.ChangeKeepCurve(iC, FindKeep);
  }
  
  // Secondly, the interferences of 2D support.
  exp.Init(C, TopAbs_EDGE);
  for(; exp.More(); exp.Next()) {
    const TopoDS_Shape& SectEdge = exp.Current();
    iC = myHB->GetDSCurveFromSectEdge(SectEdge);
    if(!iC) {
      iE1 = myHB->GetDSEdgeFromSectEdge(SectEdge, 1);
      iE2 = myHB->GetDSEdgeFromSectEdge(SectEdge, 2);
      TColStd_ListOfInteger& loi11 = myHB->GetDSFaceFromDSEdge(iE1, 1);
      TColStd_ListOfInteger& loi12 = myHB->GetDSFaceFromDSEdge(iE1, 2);
      for(it1.Initialize(loi11); it1.More(); it1.Next()) {
	iF1 = it1.Value();
	for(it2.Initialize(loi12); it2.More(); it2.Next()) {
	  iF2 = it2.Value();
	  if(iF1 == iF2)
	    continue;
	  RemoveFaceInterferences(iF1, iF2, iE1, iE2);
	}
      }
      TColStd_ListOfInteger& loi21 = myHB->GetDSFaceFromDSEdge(iE2, 1);
      TColStd_ListOfInteger& loi22 = myHB->GetDSFaceFromDSEdge(iE2, 2);
      for(it1.Initialize(loi21); it1.More(); it1.Next()) {
	iF1 = it1.Value();
	for(it2.Initialize(loi22); it2.More(); it2.Next()) {
	  iF2 = it2.Value();
	  if(iF1 == iF2)
	    continue;
	  RemoveFaceInterferences(iF1, iF2, iE1, iE2);
	}
      }
    }
    else {
      iF1 = myHB->GetDSFaceFromDSCurve(iC, 1);
      iF2 = myHB->GetDSFaceFromDSCurve(iC, 2);
      
      RemoveFaceInterferences(iF1, iF2, iC);
    }
  }
  
  // Thirdly, RemoveSameDomain is done for the faces that contain all Edges of C,
  // and are SameDomain and without Geometry.
  
  RemoveFaceSameDomain(C);
  
  // Fourthly, the faces, that were not concerned, are removed
  Standard_Integer NbSh = DS.NbShapes();
  for(i = 1; i <= NbSh; i++) {
    const TopoDS_Shape& Face = DS.Shape(i);
    if(Face.IsNull())
      continue;
    if((Face.ShapeType() != TopAbs_FACE) || DS.HasGeometry(Face) ||
       (myHDS->HasSameDomain(Face)))
      continue;
    for(exp.Init(Face, TopAbs_EDGE); exp.More(); exp.Next()){
      const TopoDS_Shape& Edge = exp.Current();
      if(DS.HasShape(Edge))
	break;
    }
    if(exp.More())
      continue;
    DS.ChangeKeepShape(Face, Standard_False);
  }
  
  // Builder.myKPMAPf1f2 is reconstructed
  Builder.FindIsKPart(); 
  
  //  The Edges of section are removed from Builder.mySplitON
  exp.Init(C, TopAbs_EDGE);
  for(; exp.More(); exp.Next()) {
    const TopoDS_Shape& SectE= exp.Current();
    TopTools_ListOfShape& losob = Builder.ChangeSplit(SectE, TopAbs_ON);
    losob.Clear();
  }
}

//=======================================================================
//function : SuppressSectionVertex
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::SuppressSectionVertex
(const TopoDS_Vertex& /*V*/)
{
  if(!myRecomputeBuilderIsDone)
    return;
}


// Reconstruction of Shapes

//=======================================================================
//function : Merge
//purpose  : 
//=======================================================================

const TopoDS_Shape& BRepAlgo_DSAccess::Merge
(const TopAbs_State state1,
 const TopAbs_State state2)
{
  if((state1 != TopAbs_IN) &&
     (state1 != TopAbs_OUT))
    return myEmptyShape;
  if((state2 != TopAbs_IN) &&
     (state2 != TopAbs_OUT))
    return myEmptyShape;
  // if GetSectionEdgeSet has already been called, nothing is done 
  // in GetSectionEdgeSet.
  if(myState1 != TopAbs_UNKNOWN)
    if(myState1 != state1 || myState2 != state2)
      myGetSectionIsDone = Standard_False;
  myState1 = state1;
  myState2 = state2;
  GetSectionEdgeSet();
  
  myHB->Clear();
  myHB->MergeShapes(myS1,state1,myS2,state2);
  const TopTools_ListOfShape& L1 = myHB->Merged(myS1,state1);
  
  BRep_Builder BB;
  myResultShape.Nullify();
  BB.MakeCompound(TopoDS::Compound(myResultShape));
  TopTools_ListIteratorOfListOfShape it(L1);
  for(;it.More(); it.Next()) {
    BB.Add(myResultShape, it.Value());
  }
  return myResultShape;
}

//=======================================================================
//function : Merge
//purpose  : 
//=======================================================================

const TopoDS_Shape& BRepAlgo_DSAccess::Merge
(const TopAbs_State state1)
{
  if((state1 != TopAbs_IN) &&
     (state1 != TopAbs_OUT))
    return myEmptyShape;
  GetSectionEdgeSet();

  myHB->Clear();
  myHB->MergeSolid(myS1,state1);
  const TopTools_ListOfShape& L1 = myHB->Merged(myS1,state1);
  
  BRep_Builder BB;
  myResultShape.Nullify();
  BB.MakeCompound(TopoDS::Compound(myResultShape));
  TopTools_ListIteratorOfListOfShape it(L1);
  for(;it.More(); it.Next()) {
    BB.Add(myResultShape, it.Value());
  }
  return myResultShape;
}

//=======================================================================
//function : Propagate
//purpose  : 
//=======================================================================

const TopoDS_Shape& BRepAlgo_DSAccess::Propagate
(const TopAbs_State what,
 const TopoDS_Shape& /*fromShape*/,
 const TopoDS_Shape& /*LoadShape*/)
{
  if((what != TopAbs_IN) &&
     (what != TopAbs_OUT))
    return myEmptyShape;
  if(!myRecomputeBuilderIsDone)
    return myEmptyShape;

//  myHB->MergeShapes(myS1,t1,myS2,t2);

  //POP pour NT;
  static TopoDS_Shape bid;
  return bid;
}

//=======================================================================
//function : PropagateFromSection
//purpose  : 
//=======================================================================

const TopoDS_Shape& BRepAlgo_DSAccess::PropagateFromSection
(const TopoDS_Shape& SectionShape)
{
  GetSectionEdgeSet();
  TopTools_ListIteratorOfListOfShape ils(myListOfCompoundOfEdgeConnected);
  TopExp_Explorer exp;
  for(; ils.More(); ils.Next()) {
    const TopoDS_Shape& SetEdgSet = ils.Value();
    exp.Init(SetEdgSet, TopAbs_EDGE);
    for(; exp.More(); exp.Next()) {
      if(SectionShape.IsSame(exp.Current()))
	return SetEdgSet;
    }
  }
  return myEmptyShape;
}

//=======================================================================
//function : Modified
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepAlgo_DSAccess::Modified (const TopoDS_Shape& Shape) 
{
  myModified.Clear() ;

//  Handle(TopOpeBRepBuild_HBuilder)& HBuilder = myDSA.myHB ;
  TopTools_ListIteratorOfListOfShape Iterator ;
  
  if (myHB->IsSplit (Shape, TopAbs_OUT)) {
    for (Iterator.Initialize (myHB->Splits (Shape, TopAbs_OUT)) ;
	 Iterator.More() ;
	 Iterator.Next()) {
      myModified.Append (Iterator.Value()) ;
    }
  }
  if (myHB->IsSplit (Shape, TopAbs_IN)) {
    for (Iterator.Initialize (myHB->Splits (Shape, TopAbs_IN)) ;
	 Iterator.More() ;
	 Iterator.Next()) {
      myModified.Append (Iterator.Value()) ;
    }
  }
  if (myHB->IsSplit (Shape, TopAbs_ON)) {
    for (Iterator.Initialize (myHB->Splits (Shape, TopAbs_ON)) ;
	 Iterator.More() ;
	 Iterator.Next()) {
      myModified.Append (Iterator.Value()) ;
    }
  }

  if (myHB->IsMerged (Shape, TopAbs_OUT)) {
    for (Iterator.Initialize (myHB->Merged (Shape, TopAbs_OUT)) ;
	 Iterator.More() ;
	 Iterator.Next()) {
      myModified.Append (Iterator.Value()) ;
    }
  }
  if (myHB->IsMerged(Shape, TopAbs_IN)) {
    for (Iterator.Initialize (myHB->Merged (Shape, TopAbs_IN)) ;
	 Iterator.More() ;
	 Iterator.Next()) {
      myModified.Append (Iterator.Value()) ;
    }
  }
  if (myHB->IsMerged(Shape, TopAbs_ON)) {
    for (Iterator.Initialize (myHB->Merged (Shape, TopAbs_ON)) ;
	 Iterator.More() ;
	 Iterator.Next()) {
      myModified.Append (Iterator.Value()) ;
    }
  }

  return myModified ;
}



//=======================================================================
//function : Check
//purpose  : 
//=======================================================================

BRepAlgo_CheckStatus BRepAlgo_DSAccess::Check()
{
//  TopOpeBRepDS_Check Ck(HDS);
  // to be precised : in Ck, there is a possibility to know 
  // exactly the n*n of shapes/points/curves/surfaces, 
  // which are not correct in the DS.
//  Standard_Boolean IsOK = Ck.ChkIntgSamDom() ;
//  IsOK = IsOK && Ck.OneVertexOnPnt();
//  IsOK = IsOK && Ck.ChkIntg();
//  if(IsOK)
//    return TopOpeBRepDS_OK;
  return BRepAlgo_NOK;
}

//=======================================================================
//function : RemoveEdgeInterferences
//purpose  : case of SectEdge coming from Edge(s)
//       
//     if iE1 and iE2 are Edges :
//     Remove interferences of DSEdge(= iE1 or iE2) of
//     geometry a vertex of SectEdge, and if there is nothing else, 
//     make unkeep on DSEdge 
//     if iE1 or iE2 == 0, no interference on Edges in the DS 
//     NYI : management of SameDomain
//       
//     if iE1 and iE2 are Faces :
//     for each of faces F1 and F2, explode into Edges
//	   for each Edge :
//	     remove the interferences of a SectEdge vertex
//	     on geometry. If there is no other interferences attached to 
//           these Edges, and if these Edges are not SameDomain,
//           make unKeepShape.
//=======================================================================

void BRepAlgo_DSAccess::RemoveEdgeInterferences
(const Standard_Integer iE1,
 const Standard_Integer iE2,
 const TopoDS_Shape& SectEdge)
{
  if(!iE1 || !iE2)
    return;

  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  TopOpeBRepDS_Kind kind1, kind2;
  TopExp_Explorer exp(SectEdge, TopAbs_VERTEX);
  Standard_Integer i = 1, ipv1, ipv2;
  
  // the Vertex/Points of SectEdge are retrieved
  PntVtxOnSectEdge(SectEdge, ipv1, kind1, ipv2, kind2);
  
  const TopoDS_Shape& Shape = DS.Shape(iE1, FindKeep);
  if(Shape.IsNull())
    return;
  if(Shape.ShapeType() == TopAbs_FACE) {
    Standard_Integer iF1 = iE1, iF2 = iE2;
    RemoveEdgeInterferencesFromFace(iF1, iF2, ipv1, kind1, ipv2, kind2);
    return;
  }
  else if(Shape.ShapeType() != TopAbs_EDGE)
    return;
  
  // the interferences are taken from the DS
  TopOpeBRepDS_ListIteratorOfListOfInterference lioloi;
  TopOpeBRepDS_Kind gk;
  Standard_Integer iCurrE1, iCurrE2, gi;
//  Standard_Boolean RemInterf;
  
  for(i = 1; i <= 2; i++) {
    iCurrE1 = ((i == 1) ? iE1 : iE2);
    iCurrE2 = ((i == 1) ? iE2 : iE1);
    const TopoDS_Shape& DSEdge = DS.Shape(iCurrE1, FindKeep);
    if(DSEdge.IsNull())
      continue;
    TopOpeBRepDS_ListOfInterference& loi = 
      DS.ChangeShapeInterferences(DSEdge);
    //    RemInterf = Standard_True;
    for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
      Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
      if (I.IsNull()) continue;
      if((I->SupportType() != TopOpeBRepDS_EDGE) ||
	 (I->Support() != iCurrE2)) {
	//	RemInterf = Standard_False;//debug ...
	continue;
      }
      gk = I->GeometryType();
      gi = I->Geometry();
      if(gk == kind1) {
	if(gi == ipv1) {
	  DS.RemoveShapeInterference(DSEdge, I);
	  if(!DS.HasGeometry(DSEdge)) {
	    //	    if(RemInterf || (!lioloi.More())) {
	    RemoveEdgeSameDomain(iCurrE1, iCurrE2); // NYI : SameDomain
	    DS.ChangeKeepShape(iCurrE1, FindKeep);
	    //	  } 
	  } 
	}
      }
      else if(gk == kind2) {
	if(gi == ipv2) {
	  DS.RemoveShapeInterference(DSEdge, I);
	  if(!DS.HasGeometry(DSEdge)) {
	    //	    if(RemInterf || (!lioloi.More())) {//debug
	    RemoveEdgeSameDomain(iCurrE1, iCurrE2); // NYI : SameDomain
	    DS.ChangeKeepShape(iCurrE1, FindKeep);
	    //	  } 
	  }
	}
      }
    }
  }
}

//=======================================================================
//function : RemoveEdgeInterferences
//purpose  : case of SectEdge coming from Curve
//       for each of faces F1 and F2, explode into Edges
//	   for each Edge :
//	     remove the interferences that have a vertex of SectEdge
//           as a geometry. If no other interferences are attached to  
//           these Edges, and if the Edges are not SameDomain,
//           make unKeepShape.
//=======================================================================

void BRepAlgo_DSAccess::RemoveEdgeInterferences
(const Standard_Integer iF1,
 const Standard_Integer iF2,
 const Standard_Integer iCurve)
{
  TopOpeBRepDS_Kind gk1, gk2;
  Standard_Integer gi1, gi2;
  
  PntVtxOnCurve(iCurve, gi1, gk1, gi2, gk2);

  if (!mySetOfKeepPoint.IsEmpty()) {
    if (mySetOfKeepPoint.Contains(gi1)) gi1 = 0;
    if (mySetOfKeepPoint.Contains(gi2)) gi2 = 0;   
  }
  
  if (gi1 || gi2)
    RemoveEdgeInterferencesFromFace(iF1, iF2, gi1, gk1, gi2, gk2);
}

//=======================================================================
//function : RemoveFaceInterferences
//purpose  : case of SectEdge coming from Edge(s)
//        Remove interferences between F1 and F2 concerning 
//        DSEdge (= E1 or E2) :
//		a) if DSEdge is not SameDomain -> the edge is Removed
//		b) if among other interferences of DSEdge of 
//                 GeomtryType == VERTEX, il n'en existe pas qui soient 
//                 avec une Edge de DSFace(= F1 ou F2)
//	  if DSFace has no more interferences and is not SameDomain,
//        make unkeep DSFace.
//=======================================================================

void BRepAlgo_DSAccess::RemoveFaceInterferences
(const Standard_Integer iF1,
 const Standard_Integer iF2,
 const Standard_Integer iE1,
 const Standard_Integer iE2)
{
  if(!iF1 || !iF2)
    return;
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  TopOpeBRepDS_ListIteratorOfListOfInterference lioloi;//, lioloei, liolofi;
  TopTools_ListIteratorOfListOfShape liolos;
  TopOpeBRepDS_Kind gk;
  TopExp_Explorer exp;
  Standard_Integer i, iCurrF1, iCurrF2, j, iCurrE1, /*iCurrE2,*/ gi; // skl
  Standard_Boolean RemInterf;
  
  for(i = 1; i <= 2; i++) {
    iCurrF1 = ((i == 1) ? iF1 : iF2);
    iCurrF2 = ((i == 1) ? iF2 : iF1);
    const TopoDS_Shape& DSFace = DS.Shape(iCurrF1);
    if(DSFace.IsNull())
      continue;
    const TopOpeBRepDS_ListOfInterference& loi = DS.ShapeInterferences(DSFace);
    for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
      Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
      if (I.IsNull()) continue;
      if((I->SupportType() != TopOpeBRepDS_FACE) ||
	 (I->Support() != iCurrF2)) {
	continue;
      }
      gk = I->GeometryType();
      gi = I->Geometry();
      if(gk != TopOpeBRepDS_EDGE) continue;
      for(j = 1; j <= 2; j++) {
	iCurrE1 = ((j == 1) ? iE1 : iE2);
	//iCurrE2 = ((j == 1) ? iE2 : iE1); // skl
	if(gi != iCurrE1) continue;
	// a) if DSEdge is not SameDomain -> the interference is Removed
	//    et DSEdge
	const TopoDS_Shape& DSEdge = DS.Shape(iCurrE1, FindKeep);
	if(DSEdge.IsNull())
	  continue;
	if(!myHDS->HasSameDomain(DSEdge)) {
	  if(!DS.HasGeometry(DSEdge)) {
	    DS.RemoveShapeInterference(DSFace, I);
	    DS.ChangeKeepShape(DSEdge, FindKeep);
	  } else {
	    // NYI : manage the case when the geometry of DSEdge 
	    // NYI : is not connected anyhow with two faces
	  }
	  if(!DS.HasGeometry(DSFace)) {
	    DS.ChangeKeepShape(DSFace, FindKeep);
	  }
	  continue;
	}
	// b) if no Edges of  SameDomain(DSEdge),
	//    belong to DSFace(= F1 or F2)
	//     -> the interference is removed
	const TopoDS_Shape& Edge = DS.Shape(iCurrE1, FindKeep);
	if(Edge.IsNull())
	  continue;
	const TopTools_ListOfShape& loe = DS.ShapeSameDomain(Edge);
	RemInterf = Standard_True;
	for(liolos.Initialize(loe); liolos.More(); liolos.Next()) {
	  const TopoDS_Shape& ESD = liolos.Value();
	  for(exp.Init(DSFace, TopAbs_EDGE); exp.More(); exp.Next()) {
	    if(ESD.IsSame(exp.Current())) {
	      RemInterf = Standard_False;
	      break;
	    }
	  }
	  if(!RemInterf) break;
	}
	if(RemInterf) {
	  //	  RemoveSameDomain(iCurrF1, iCurrF2);
	  
	  if(!DS.HasGeometry(DSFace)) {
	    if(!myHDS->HasSameDomain(DSFace))
	      DS.ChangeKeepShape(DSFace, FindKeep);
	  }
	}
	if(!DS.HasGeometry(DSFace) && !myHDS->HasSameDomain(DSFace))
	  DS.ChangeKeepShape(DSFace, FindKeep);
      }
    }
  }
}

//=======================================================================
//function : RemoveFaceInterferences
//purpose  : case of SectEdge from Curve
//           remove interferences of Geometry iCurve between F1 and F2.
//           if Face(= F1 or F2) has noother interference, and if Face
//           is not SameDomain, make unKeepShape Face.
//=======================================================================

void BRepAlgo_DSAccess::RemoveFaceInterferences
(const Standard_Integer iF1,
 const Standard_Integer iF2,
 const Standard_Integer iCurve)
{
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  TopOpeBRepDS_ListIteratorOfListOfInterference lioloi;
  TopOpeBRepDS_Kind gk;
  Standard_Integer i, iCurrF1, iCurrF2, gi;

  for(i = 1; i <= 2; i++) {
    iCurrF1 = ((i == 1) ? iF1 : iF2);
    iCurrF2 = ((i == 1) ? iF2 : iF1);
    const TopoDS_Shape& DSFace = DS.Shape(iCurrF1);
    const TopOpeBRepDS_ListOfInterference& loi = DS.ShapeInterferences(DSFace);
    for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
      Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
      if (I.IsNull()) continue;
      if((I->SupportType() != TopOpeBRepDS_FACE) ||
	 (I->Support() != iCurrF2)) {
	break;;
      }
    }
    for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
      Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
      if (I.IsNull()) continue;
      if((I->SupportType() != TopOpeBRepDS_FACE) ||
	 (I->Support() != iCurrF2)) {
	continue;
      }
      gk = I->GeometryType();
      gi = I->Geometry();
      if(gk != TopOpeBRepDS_CURVE) continue;
      if(gi != iCurve) continue;
      DS.RemoveShapeInterference(DSFace, I);
//      const TopoDS_Shape& interferenceface = DS.Shape(iCurrF2);
//      DS.RemoveShapeInterference(interferenceface, I);
      if(!DS.HasGeometry(DSFace)) {
	const TopTools_ListOfShape& los = DS.ShapeSameDomain(DSFace);
	if(los.IsEmpty())
	  DS.ChangeKeepShape(DSFace, FindKeep);
      }
//      if(!DS.HasGeometry(interferenceface)) {
//	const TopTools_ListOfShape& los = DS.ShapeSameDomain(interferenceface);
//	if(los.IsEmpty())
//	  DS.ChangeKeepShape(interferenceface, FindKeep);
//      }
    }
  }
}

//=======================================================================
//function : RemoveEdgeInterferencesFromFace
//purpose  : Remove interferences of Edges from iF1 or iF2
//           that have GeometryType kind1/kind2 and 
//           Geometry ipv1/ipv2.
//           if kind1/kind2 == TopAbs_VERTEX -> RemoveEdgeFromFace
//=======================================================================

void BRepAlgo_DSAccess::RemoveEdgeInterferencesFromFace
(const Standard_Integer iF1,
 const Standard_Integer iF2,
 const Standard_Integer ipv1,
 const TopOpeBRepDS_Kind kind1,
 const Standard_Integer ipv2,
 const TopOpeBRepDS_Kind kind2)
{
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  TopOpeBRepDS_ListIteratorOfListOfInterference lioloi;
  TopExp_Explorer exp, exp2;
  TopOpeBRepDS_Kind sk, gk;
  Standard_Integer i, iCurrF1, iCurrF2, iE = 0, si, gi;

  for(i = 1; i <= 2; i++) {
    iCurrF1 = ((i == 1) ? iF1 : iF2);
    iCurrF2 = ((i == 1) ? iF2 : iF1);
    const TopoDS_Shape& DSFace = DS.Shape(iCurrF1, FindKeep);
    if(DSFace.IsNull())
      continue;
    exp.Init(DSFace, TopAbs_EDGE);
    for(; exp.More(); exp.Next()) {
      const TopoDS_Shape& DSEdge = exp.Current();
      iE = DS.Shape(DSEdge, FindKeep);
      if(!iE) continue;
      const TopOpeBRepDS_ListOfInterference& loi =
	DS.ShapeInterferences(DSEdge);
      for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
	Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
	if (I.IsNull()) continue;
	sk = I->SupportType();
	si = I->Support();
	if((sk != TopOpeBRepDS_FACE) || (si != iCurrF2)) {
	  if(sk != TopOpeBRepDS_EDGE)
	    continue;
	  const TopoDS_Shape& DSFace2 = DS.Shape(iCurrF2, FindKeep);
	  exp2.Init(DSFace2, TopAbs_EDGE);
	  for(; exp2.More(); exp2.Next()) {
	    if(si == DS.Shape(exp2.Current(), FindKeep))
	      break;
	  }
	  if(!exp2.More())
	    continue;
	}
	gk = I->GeometryType();
	gi = I->Geometry();
	if(gk == kind1) {
	  if(gi == ipv1) {
	    DS.RemoveShapeInterference(DSEdge, I);
//	    if(!DS.HasGeometry(DSEdge)) {
//	      const TopTools_ListOfShape& los = DS.ShapeSameDomain(DSEdge);
//	      if(los.IsEmpty()) {
//		DS.ChangeKeepShape(iE, FindKeep);
//	      }
//	    }
	  }
	  else if(gk == kind2) {
	    if(gi == ipv2) {
	      DS.RemoveShapeInterference(DSEdge, I);
//	      if(!DS.HasGeometry(DSEdge)) {
//		const TopTools_ListOfShape& los = DS.ShapeSameDomain(DSEdge);
//		if(los.IsEmpty()) {
//		  DS.ChangeKeepShape(iE, FindKeep);
//		}
//	      }
	    }
	  }
	  else continue;
	}
      }
    }
    if(kind1 == TopOpeBRepDS_VERTEX)
      RemoveEdgeFromFace(iCurrF1,ipv1);
    if(kind2 == TopOpeBRepDS_VERTEX)
      RemoveEdgeFromFace(iCurrF1,ipv2);
  }
}

//=======================================================================
//function : RemoveEdgeFromFace
//purpose  : Remove from DS the Edges, which belong to iF
//           and have iV as vertex if they do not have Geometry and
//           are not SameDomain.
//=======================================================================

void BRepAlgo_DSAccess::RemoveEdgeFromFace
(const Standard_Integer iF,
 const Standard_Integer iV)
{
  if(!iF || !iV)
    return;
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  const TopoDS_Shape& DSFace = DS.Shape(iF, FindKeep);
  const TopoDS_Shape& Vertex = DS.Shape(iV, FindKeep);
  if(DSFace.IsNull() || Vertex.IsNull())
    return;
  TopExp_Explorer exp(DSFace, TopAbs_EDGE), exp2;
  for(; exp.More(); exp.Next()) {
    const TopoDS_Shape& Edge = exp.Current();
#ifdef DEB
//    Standard_Integer iEdge2 = DS.Shape(Edge, FindKeep);
//    Standard_Integer iEdge3 = DS.Shape(Edge);
#endif
                              
    if(!DS.HasShape(Edge)) 
      continue;
    exp2.Init(Edge, TopAbs_VERTEX);
    for(; exp2.More(); exp2.Next()) {
#ifdef DEB
//      Standard_Integer iEdge5 = DS.Shape(Vertex, FindKeep);
//      Standard_Integer iEdge4 = DS.Shape(Vertex);
//      Standard_Integer iEdge6 = DS.Shape(exp2.Current(), FindKeep);
//      Standard_Integer iEdge7 = DS.Shape(exp2.Current());
#endif
                                
      if(Vertex.IsSame(exp2.Current())) {
	if(!DS.HasGeometry(Edge)) {
	  const TopTools_ListOfShape& los = DS.ShapeSameDomain(Edge);
	  if(los.IsEmpty()) {
#ifdef DEB
//	    Standard_Integer iEdge = DS.Shape(Edge);
#endif
	    DS.ChangeKeepShape(Edge, FindKeep);
	  }
	}
      }
    }
  }
}

//=======================================================================
//function : PntVtxOnCurve
//purpose  : To find the points/vertices on curves
//=======================================================================

void BRepAlgo_DSAccess::PntVtxOnCurve
(const Standard_Integer iCurve,
 Standard_Integer& ipv1,
 TopOpeBRepDS_Kind& pvk1,
 Standard_Integer& ipv2,
 TopOpeBRepDS_Kind& pvk2)
{
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  
  const TopOpeBRepDS_Curve& C = DS.Curve(iCurve);
  TopOpeBRepDS_Kind pvk; 
  Standard_Integer ipv, iMother = C.Mother(), igoodC = iCurve, comp = 0;
  if(iMother) igoodC = iMother;
//#ifndef DEB
  TopOpeBRepDS_PointIterator PII = myHDS->CurvePoints(igoodC);
  TopOpeBRepDS_PointIterator& PIt = PII; // skl : I change "PI" to "PIt"
//#else
//  TopOpeBRepDS_PointIterator& PIt = myHDS->CurvePoints(igoodC);
//#endif
  for(;PIt.More(); PIt.Next()) {
    comp++;
    if(comp > 2)
      // Standard_Error ...
      return;
    ipv = PIt.Current();
    // a point or a vertex is removed from the DS
    if(PIt.IsPoint()) {
      pvk = TopOpeBRepDS_POINT;
      DS.ChangeKeepPoint(ipv, FindKeep);
    }
    else if(PIt.IsVertex()) {
      pvk = TopOpeBRepDS_VERTEX;
      DS.ChangeKeepShape(ipv, FindKeep);
    }
    else continue;
    ((comp == 1) ? ipv1 : ipv2) = ipv;
    ((comp == 1) ? pvk1 : pvk2) = pvk;
  }
}

//=======================================================================
//function : PntVtxOnSectEdge
//purpose  : Points/Vertexes  on  SectEdge are found
//=======================================================================

void BRepAlgo_DSAccess::PntVtxOnSectEdge
(const TopoDS_Shape& SectEdge,
 Standard_Integer& ipv1,
 TopOpeBRepDS_Kind& pvk1,
 Standard_Integer& ipv2,
 TopOpeBRepDS_Kind& pvk2)
{
//  myHB->ChangeBuilder();
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  TopOpeBRepDS_Kind kind = TopOpeBRepDS_POINT;
  TopExp_Explorer exp(SectEdge, TopAbs_VERTEX);
  Standard_Integer i = 1, ipv;
  
  for(; exp.More(); exp.Next(), i++) {    
    const TopoDS_Shape& DSVertex = exp.Current();
    ipv = myHB->GetDSPointFromNewVertex(DSVertex);
    if(!ipv) {
      ipv = DS.Shape(DSVertex, FindKeep);
      kind = TopOpeBRepDS_VERTEX;
      if(!ipv)
	// Standard_Error ...
	return;
    }
    
    if(i == 1) {
      ipv1 = ipv;
      pvk1 = kind;
    }    
    else if(i == 2) {
      ipv2 = ipv;
      pvk2 = kind;
    }
    else
      // Standard_Error ...
      return;
  }
}

//=======================================================================
//function : RemoveEdgeSameDomain
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::RemoveEdgeSameDomain
(const Standard_Integer /*iE1*/,
 const Standard_Integer /*iE2*/)
{
  return;
/*  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  const TopoDS_Shape& E1 = DS.Shape(iE1);
  const TopoDS_Shape& E2 = DS.Shape(iE2);
  TopAbs_ShapeEnum ts1, ts2;
  ts1 = E1.ShapeType();
  ts2 = E2.ShapeType();
  if((ts1 != TopAbs_EDGE) ||
     (ts2 != TopAbs_EDGE)) 
    return;
  TopTools_ListOfShape& lossd = DS.ChangeShapeSameDomain(E1);
  if(lossd.IsEmpty())
    return;
  Standard_Integer exte = lossd.Extent();
  if(exte == 1) {
    if(lossd.First().IsSame(E2))
      DS.UnfillShapesSameDomain(E1,E2);
    return;
  }*/
}

//=======================================================================
//function : RemoveFaceSameDomain
//purpose  : remove SameDomain information of glued faces 
//=======================================================================

void BRepAlgo_DSAccess::RemoveFaceSameDomain
(const TopoDS_Shape& C)
{
//  myHB->ChangeBuilder();
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();

//TColStd_ListIteratorOfListOfInteger it;
  TopExp_Explorer exp(C, TopAbs_EDGE);
  Standard_Integer  iE1, iE2, iE, /*NbF,*/ iF1, iF2, iCurrF1, iCurrF2,   iC =0; // skl
  iF1 = iF2 = iCurrF1 = iCurrF2 = 0;
  Standard_Boolean b;
  const TopoDS_Shape& SectEdge = exp.Current();
  
  for(; exp.More(); exp.Next()) {
    iC = myHB->GetDSCurveFromSectEdge(SectEdge);
    if(!iC && !SectEdge.IsNull())
      break;
//  const TopoDS_Shape& SectEdge = exp.Current();
  }
  if(!iC && !SectEdge.IsNull()) {
    iE1 = myHB->GetDSEdgeFromSectEdge(SectEdge, 1);
    iE2 = myHB->GetDSEdgeFromSectEdge(SectEdge, 2);
    if(iE1 && iE2) return;
    iE = (iE1 ? iE1 : iE2);
    if(!iE) return;
    
    TColStd_ListOfInteger& loi = FindGoodFace(iE, iF1, b);
    if(!b) return;
    if(exp.More())
      exp.Next();
    //NbF = loi.Extent(); // skl
    for(; exp.More(); exp.Next()) {
      // skl : I change "SectEdge" to "SectEdg"
      const TopoDS_Shape& SectEdg = exp.Current();
      iC = myHB->GetDSCurveFromSectEdge(SectEdg);
      if(!iC) {
	iE1 = myHB->GetDSEdgeFromSectEdge(SectEdg, 1);
	iE2 = myHB->GetDSEdgeFromSectEdge(SectEdg, 2);
	if(iE1 && iE2) return;
	iE = (iE1 ? iE1 : iE2);
	if(!iE) return;
	
	TColStd_ListOfInteger& loi2 = FindGoodFace(iE, iCurrF1, b);
	if(!b) return;
	if(!iCurrF1 || !iF1) return;
	if(iCurrF1 != iF1) {
	  if(loi2.Extent() == 1) iCurrF2 = loi2.First();
	  if(iCurrF2 == iF1) continue;
	  if(loi.Extent() == 1) iF2 = loi.First();

	  if(!iCurrF2 || !iF2) return;
	  if((iCurrF1 == iF2) ||
	     (iCurrF2 == iF2)) {
	    iF1 = iF2;
	    continue;
	  }
	  return;
	} 
      }
    }
    
    const TopoDS_Shape& FSD = DS.Shape(iF1);
    if(FSD.IsNull()) 
      return;
    TopTools_ListOfShape& ssd = DS.ChangeShapeSameDomain(FSD);
    TopTools_ListIteratorOfListOfShape itssd(ssd);
    TopExp_Explorer exp2;
    for(; itssd.More(); itssd.Next()) {
      exp2.Init(itssd.Value(), TopAbs_VERTEX);
      for(; exp2.More(); exp2.Next()) {
	const TopoDS_Shape& exp2Curr = exp2.Current();
	exp.Init(C, TopAbs_VERTEX);
	for(; exp.More(); exp.Next()) {
	  if(exp2Curr.IsSame(exp.Current()))
	    break;
	}
	if(exp.More())
	  break;
      }
      if(exp2.More())
	break;
    }
    
    if(exp2.More()) {
      const TopoDS_Shape& FSD2 = itssd.Value();
      Standard_Integer iFSD = DS.Shape(FSD), iFSD2 = DS.Shape(FSD2);
      RemoveFaceSameDomain(iFSD, iFSD2);      
//      DS.UnfillShapesSameDomain(FSD, FSD2);
    }
  }
}

//=======================================================================
//function : FindGoodFace
//purpose  : 
//=======================================================================

TColStd_ListOfInteger& BRepAlgo_DSAccess::FindGoodFace
(const Standard_Integer iE,
 Standard_Integer& iF1,
 Standard_Boolean& b)
{
//  myHB->ChangeBuilder();
  b = Standard_False;
  TColStd_ListOfInteger& loi = myHB->GetDSFaceFromDSEdge(iE, 1);
  if(loi.Extent() == 1) {
    iF1 = loi.First();
    b = Standard_True;
    TColStd_ListOfInteger& loi2 = myHB->GetDSFaceFromDSEdge(iE, 2);
    return loi2;
  }
  else {
    TColStd_ListOfInteger& loi2 = myHB->GetDSFaceFromDSEdge(iE, 2);
    if(loi2.Extent() == 1) {
      b = Standard_True;
      iF1 = loi2.First();
      return loi;
    }
  }
  b = Standard_False;
  return myEmptyListOfInteger;
}

//=======================================================================
//function : RemoveFaceSameDomain
//purpose  : 
//=======================================================================

void BRepAlgo_DSAccess::RemoveFaceSameDomain
(const Standard_Integer iF1,
 const Standard_Integer iF2)
{
  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
  const TopoDS_Shape& F1 = DS.Shape(iF1, FindKeep);
  const TopoDS_Shape& F2 = DS.Shape(iF2, FindKeep);
  if(F1.IsNull() || F2.IsNull())
    return;


  Standard_Integer iref1 = DS.SameDomainRef(F1),
  iref2 = DS.SameDomainRef(F2), istart, iend;
  if(iref1 == iF1)
    DS.SameDomainRef(F2,iF2);
  if(iref2 == iF1)
    DS.SameDomainRef(F1,iF1);
  DS.UnfillShapesSameDomain(F1,F2);

  if(iref1 != iref2)
    return;
  Standard_Boolean iF1iF2IsConnected = Standard_False;
  TColStd_IndexedMapOfInteger moi;
  moi.Clear();
  if(iref2 == iF2) {
    istart = iF2;
    iend = iF1;
  }
  else {
    istart = iF1;
    iend = iF2;
  }
  moi.Add(istart);
  Standard_Integer NbConnect = 0, icurr;
  while(moi.Extent() > NbConnect) {
    NbConnect++;
    icurr = moi.FindKey(NbConnect);
    DS.SameDomainRef(icurr, istart);
    const TopTools_ListOfShape& los = DS.ShapeSameDomain(icurr);
    if(los.IsEmpty()) {
      const TopoDS_Shape& SNSD = DS.Shape(icurr);
      DS.SameDomainRef(SNSD, 0);
    }
    TopTools_ListIteratorOfListOfShape li(los);
    for(; li.More(); li.Next()) {
      Standard_Integer iCurrShap = DS.Shape(li.Value(), FindKeep);
      if(!iCurrShap)
	return;
      if(iCurrShap == iend)
	iF1iF2IsConnected = Standard_True;
      moi.Add(iCurrShap);
    }
  }
  if(!iF1iF2IsConnected) {
    moi.Clear();
    moi.Add(iend);
    NbConnect = 0;
    while(moi.Extent() > NbConnect) {
      NbConnect++;
      icurr = moi.FindKey(NbConnect);
      DS.SameDomainRef(icurr, iend);
      const TopTools_ListOfShape& los = DS.ShapeSameDomain(icurr);
      if(los.IsEmpty()) {
	const TopoDS_Shape& SNSD = DS.Shape(icurr);
	DS.SameDomainRef(SNSD, 0);
      }
      TopTools_ListIteratorOfListOfShape li(los);
      for(; li.More(); li.Next()) {
	Standard_Integer iCurrShap = DS.Shape(li.Value(), FindKeep);
	if(!iCurrShap)
	  return;
	moi.Add(iCurrShap);
      }
    }
  }
}

//=======================================================================
//function : DS
//purpose  : 
//=======================================================================

const Handle(TopOpeBRepDS_HDataStructure)&
BRepAlgo_DSAccess::DS() const
{
  return myHDS;
}

//=======================================================================
//function : changeDS
//purpose  : 
//=======================================================================
Handle(TopOpeBRepDS_HDataStructure)&
BRepAlgo_DSAccess::ChangeDS()
{
  return myHDS;
}

//=======================================================================
//function : Builder
//purpose  : 
//=======================================================================

const Handle(TopOpeBRepBuild_HBuilder)& 
BRepAlgo_DSAccess::Builder() const
{
  return myHB;
}

//=======================================================================
//function : ChangeBuilder
//purpose  : 
//=======================================================================

Handle(TopOpeBRepBuild_HBuilder)& 
BRepAlgo_DSAccess::ChangeBuilder()
{
  return myHB;
}