0022792: Globally defined symbol PI conflicts with VTK definition (Intel compiler)
[occt.git] / src / BRepAlgo / BRepAlgo_DSAccess.cxx
1 // File:        BRepAlgo_DSAccess.cxx
2 // Created:     Wed Aug 13 16:05:15 1997
3 // Author:      Prestataire Mary FABIEN
4 //              <fbi@langdox.paris1.matra-dtv.fr>
5
6
7 #include <BRepAlgo_DSAccess.ixx>
8 #include <BRepAlgo_EdgeConnector.hxx>
9
10 #include <TColStd_ListOfInteger.hxx>
11 #include <TColStd_MapOfInteger.hxx>
12 #include <TColStd_IndexedMapOfInteger.hxx>
13 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
14 #include <TColStd_SetIteratorOfSetOfInteger.hxx>
15
16 #include <TopTools_ListOfShape.hxx>
17 #include <TopTools_ListIteratorOfListOfShape.hxx>
18 #include <TopTools_MapOfShape.hxx>
19 #include <TColStd_ListIteratorOfListOfInteger.hxx>
20 #include <TopoDS.hxx>
21 #include <TopoDS_Compound.hxx>
22 #include <TopoDS_Wire.hxx>
23 #include <TopOpeBRepDS_PointIterator.hxx>
24 #include <TopOpeBRepDS_BuildTool.hxx>
25 #include <TopOpeBRepDS_CheckStatus.hxx>
26 #include <TopOpeBRepDS_Check.hxx>
27 #include <TopOpeBRepDS_ListOfInterference.hxx>
28 #include <TopOpeBRepDS_Interference.hxx>
29 #include <TopOpeBRepDS_InterferenceIterator.hxx>
30 #include <TopOpeBRepDS_ListIteratorOfListOfInterference.hxx>
31 #include <TopOpeBRepDS_ShapeShapeInterference.hxx>
32 #include <TopOpeBRepDS_HDataStructure.hxx>
33 #include <TopOpeBRepDS_CurveExplorer.hxx>
34 #include <TopOpeBRepDS_CurveIterator.hxx>
35 #include <TopOpeBRepDS_Filter.hxx>
36 #include <TopOpeBRepDS_Reducer.hxx>
37 #include <TopOpeBRepTool_GeomTool.hxx>
38 #include <TopOpeBRepBuild_HBuilder.hxx>
39 #include <TopOpeBRepBuild_WireEdgeSet.hxx>
40 #include <TopOpeBRepBuild_FaceBuilder.hxx>
41 #include <TopOpeBRep_DSFiller.hxx>
42
43 #ifdef DRAW
44 //#include <TestTopOpe.hxx>
45 #endif
46
47 //=======================================================================
48 //function : Create
49 //purpose  : 
50 //=======================================================================
51
52 BRepAlgo_DSAccess::BRepAlgo_DSAccess() {
53   Init();
54 }
55
56 //=======================================================================
57 //function : Init
58 //purpose  : 
59 //=======================================================================
60
61 void BRepAlgo_DSAccess::Init()
62 {
63   if(myHDS.IsNull()) 
64     myHDS = new TopOpeBRepDS_HDataStructure();
65   else
66     myHDS->ChangeDS().Init();
67   myRecomputeBuilderIsDone = Standard_False;
68   myGetSectionIsDone = Standard_False;
69   myListOfCompoundOfEdgeConnected.Clear();
70   myEC = new BRepAlgo_EdgeConnector();
71   myHB.Nullify();
72   
73   // init of the builder
74   Standard_Real tol3dAPPROX = 1e-7;
75   Standard_Real tol2dAPPROX = 1e-7;
76   // set tolerance values used by the APPROX process
77   TopOpeBRepTool_GeomTool GT;
78   GT.Define(TopOpeBRepTool_APPROX);
79   GT.SetTolerances(tol3dAPPROX,tol2dAPPROX);
80   TopOpeBRepDS_BuildTool BT(GT);
81   myHB = new TopOpeBRepBuild_HBuilder(BT);
82   myHB->ChangeBuilder().ChangeClassify(Standard_False);
83
84   myState1 = TopAbs_UNKNOWN;
85   myState2 = TopAbs_UNKNOWN;
86
87 }
88
89
90 // Filling of the DS
91
92 //=======================================================================
93 //function : Load
94 //purpose  : 
95 //=======================================================================
96
97 void BRepAlgo_DSAccess::Load(const TopoDS_Shape& S)
98 {
99   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
100   myS1 = S;
101   DS.AddShape(S, 1);
102 }
103
104 //=======================================================================
105 //function : Load
106 //purpose  : 
107 //=======================================================================
108
109 void BRepAlgo_DSAccess::Load(TopoDS_Shape& S1, 
110                              TopoDS_Shape& S2)
111 {
112   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
113   
114   if ( S1.Orientation() == TopAbs_REVERSED ) {
115     S1.Orientation(TopAbs_FORWARD);
116   }
117   if ( S2.Orientation() == TopAbs_REVERSED ) {
118     S2.Orientation(TopAbs_FORWARD);
119   }
120   
121   DS.AddShape(S1,1);
122   DS.AddShape(S2,2);
123
124   TopOpeBRepTool_ShapeExplorer ex1,ex2;
125   for (ex1.Init(S1,TopAbs_SOLID); ex1.More(); ex1.Next()) {
126     const TopoDS_Shape& so1 = ex1.Current();
127     for (ex2.Init(S2,TopAbs_SOLID); ex2.More(); ex2.Next()) {
128       const TopoDS_Shape& so2 = ex2.Current();
129       DS.FillShapesSameDomain(so1,so2);
130     }
131   }
132   
133   myS1 = S1;
134   myS2 = S2;
135
136 #ifdef DRAW
137 //  TestTopOpe::CurrentDS(myHDS);
138 //  TestTopOpe::Shapes(myS1,myS2);
139 #endif
140 }
141
142 //=======================================================================
143 //function : Intersect
144 //purpose  : 
145 //=======================================================================
146
147 void BRepAlgo_DSAccess::Intersect()
148 {
149   myRecomputeBuilderIsDone = Standard_False;
150   
151   if(!myS1.IsNull() && !myS2.IsNull())
152     myDSFiller.Insert(myS1, myS2, myHDS);
153 }
154
155 //=======================================================================
156 //function : Intersect
157 //purpose  : 
158 //=======================================================================
159
160 void BRepAlgo_DSAccess::Intersect
161 (const TopoDS_Shape& S1,
162  const TopoDS_Shape& S2)
163 {
164   myRecomputeBuilderIsDone = Standard_False;
165   
166   if(S1.IsNull() || S2.IsNull()) {
167     return;
168   }
169   
170   Standard_Boolean orientFORWARD = Standard_False;
171   TopExp_Explorer exp;
172   if(S1.ShapeType() != TopAbs_FACE) {
173     exp.Init(S1, TopAbs_FACE);
174     if(!exp.More())
175       return;
176   }
177   if(S2.ShapeType() != TopAbs_FACE) {
178     exp.Init(S2, TopAbs_FACE);
179     if(!exp.More())
180       return;
181   }
182   myDSFiller.Insert(S1, S2, myHDS, orientFORWARD);
183 }
184
185 //=======================================================================
186 //function : SameDomain
187 //purpose  : 
188 //=======================================================================
189
190 void BRepAlgo_DSAccess::SameDomain
191 (const TopoDS_Shape& S1,
192  const TopoDS_Shape& S2)
193 {
194   myRecomputeBuilderIsDone = Standard_False;
195   
196   if(S1.IsNull() || S2.IsNull())
197     return;
198
199   TopExp_Explorer exp1, exp2;
200     exp1.Init(S1, TopAbs_FACE);
201     if(!exp1.More())
202       return;
203     exp2.Init(S2, TopAbs_FACE);
204     if(!exp2.More())
205       return;
206   
207   myDSFiller.Insert2d(S1, S2, myHDS);
208 }
209
210
211 // Construction of Sections
212
213 #define FindKeep Standard_False
214
215 //=======================================================================
216 //function : GetSectionEdgeSet
217 //purpose  : 
218 //=======================================================================
219
220 const TopTools_ListOfShape& BRepAlgo_DSAccess::GetSectionEdgeSet
221 (const TopoDS_Shape& S1,
222  const TopoDS_Shape& S2)
223 {
224   GetSectionEdgeSet();
225
226   // Check if S1 and S2 contain faces
227   TopExp_Explorer exp1, exp2;
228   exp1.Init(S1, TopAbs_FACE);
229   if(!exp1.More())
230     return myEmptyListOfShape;
231   exp2.Init(S2, TopAbs_FACE);
232   if(!exp2.More())
233     return myEmptyListOfShape;
234   
235   for(exp1.Init(S1, TopAbs_FACE); exp1.More(); exp1.Next()) {
236     if(!myHDS->HasShape(exp1.Current(), FindKeep))
237       return myEmptyListOfShape;
238   }
239   for(exp2.Init(S2, TopAbs_FACE); exp2.More(); exp2.Next())
240     if(!myHDS->HasShape(exp2.Current(), FindKeep))
241       return myEmptyListOfShape;
242   
243   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
244   TopOpeBRepBuild_Builder& Builder = myHB->ChangeBuilder();
245   
246   // The purpose is to find interferences associated with faces,
247   // edges that come from their Geometry (= Edge || Curve)
248   TopTools_ListOfShape LE;
249   LE.Clear();
250   TopExp_Explorer exp;
251   for(exp1.Init(S1, TopAbs_FACE); exp1.More(); exp1.Next()) {
252     const TopoDS_Shape& F1 = exp1.Current();
253     
254     TopOpeBRepDS_ListOfInterference& lof = DS.ChangeShapeInterferences(F1);
255     TopOpeBRepDS_InterferenceIterator li(lof);
256     li.SupportKind(TopOpeBRepDS_FACE);
257     for(exp2.Init(S2, TopAbs_FACE); exp2.More(); exp2.Next()) {
258       const TopoDS_Shape& F2 = exp2.Current();
259       Standard_Integer si = DS.Shape(F2, FindKeep);
260       li.Support(si);
261       
262       for(; li.More(); li.Next()) {
263         const TopOpeBRepDS_Interference& CurrInt = li.Value();
264         TopOpeBRepDS_Kind gk = CurrInt.GeometryType();
265         Standard_Integer gi = CurrInt.Geometry();
266         const TopoDS_Shape& geosha = DS.Shape(gi, FindKeep);
267         if(gk == TopOpeBRepDS_CURVE) {
268           const TopTools_ListOfShape& lEdge = myHB->NewEdges(gi);
269           LE.Append(lEdge.First());
270         } else {
271           const TopTools_ListOfShape& lEdge = Builder.Splits(geosha, TopAbs_ON);
272           TopTools_ListIteratorOfListOfShape it(lEdge);
273           for(; it.More(); it.Next()) {
274             const TopoDS_Shape& CurrEdge = it.Value();
275             Standard_Integer ipv1, ipv2;
276             TopOpeBRepDS_Kind pvk1, pvk2;
277             PntVtxOnSectEdge(CurrEdge, ipv1, pvk1, ipv2, pvk2);
278             if(pvk1 != TopOpeBRepDS_VERTEX) {
279               ipv1 = 0;
280               if(pvk2 != TopOpeBRepDS_VERTEX) continue;
281             } else {
282               if(pvk2 != TopOpeBRepDS_VERTEX)
283                 ipv2 = 0;
284             }
285             for(exp.Init(F1, TopAbs_VERTEX); exp.More(); exp.Next()) {
286               Standard_Integer iVert = DS.Shape(exp.Current());
287               if(iVert) {
288                 if((iVert == ipv1) || (iVert == ipv2)) {
289                   LE.Append(CurrEdge);
290                   break;
291                 }
292               }
293             }
294           }
295         }
296       }
297     }
298   }
299
300   // find all groups of connected Edges associated to LE
301   TopTools_ListIteratorOfListOfShape ILE;
302   myCurrentList.Clear();
303   TopTools_MapOfShape ME;
304   ME.Clear();
305   TopTools_ListIteratorOfListOfShape ILC;
306   TopExp_Explorer ECE;
307   ILE.Initialize(LE);
308   for(;ILE.More();ILE.Next()) {
309     const TopoDS_Shape& E = ILE.Value();
310     ILC.Initialize(myListOfCompoundOfEdgeConnected);
311     for(;ILC.More();ILC.Next()) {
312       const TopoDS_Shape& Com = ILC.Value();
313       ECE.Init(Com, TopAbs_EDGE);
314       for(;ECE.More();ECE.Next()) {
315         if(ECE.Current().IsSame(E)) {
316           if(!ME.Contains(Com)) {
317             myCurrentList.Append(Com);
318             ME.Add(Com);
319             break;
320           }
321         }
322       }
323     }
324   }
325   
326   return myCurrentList;
327 }
328
329 //=======================================================================
330 //function : GetSectionEdgeSet
331 //purpose  : 
332 //=======================================================================
333
334 const TopTools_ListOfShape& BRepAlgo_DSAccess::GetSectionEdgeSet()
335 {
336   if(!myRecomputeBuilderIsDone) {
337     // it is possible to call the method many times consecutively
338     myHDS->AddAncestors(myS1);
339     // start of lpa modification
340     if (!myS1.IsSame(myS2) && !myS2.IsNull()) {
341       myHDS->AddAncestors(myS2);
342       myHB->Perform(myHDS,myS1,myS2);
343     }
344     else {
345       myHB->Perform(myHDS);
346     }
347     // end of modif lpa
348     myRecomputeBuilderIsDone = Standard_True;
349     myGetSectionIsDone = Standard_False;
350   } 
351   if(myGetSectionIsDone)
352     return myListOfCompoundOfEdgeConnected;
353   myGetSectionIsDone = Standard_True;
354   
355   myListOfCompoundOfEdgeConnected.Clear();
356   
357   // EdgeConnector
358   Handle(BRepAlgo_EdgeConnector) EC = myEC;
359   EC->ClearStartElement();
360   TopTools_MapOfShape ME;
361   ME.Clear();
362   myHB->InitSection();
363   for(; myHB->MoreSection(); myHB->NextSection()) {
364     const TopoDS_Edge& ES = TopoDS::Edge(myHB->CurrentSection());
365     if(ME.Contains(ES)) continue;
366     ME.Add(ES);
367     EC->AddStart(ES);
368   }
369   TopTools_ListOfShape& LW = EC->MakeBlock();
370   
371   // the wires are tranformed into compounds.
372   myCompoundWireMap.Clear();
373   BRep_Builder BB;
374   TopTools_ListIteratorOfListOfShape ILW(LW);
375   TopExp_Explorer Explor;
376   for(;ILW.More();ILW.Next()) {
377       TopoDS_Compound Compound;
378 //POP
379       BB.MakeCompound(Compound);
380 //      BB.MakeCompound(TopoDS::Compound(Compound));
381       Explor.Init(ILW.Value(), TopAbs_EDGE);
382       for(;Explor.More(); Explor.Next()) {
383         BB.Add(Compound, Explor.Current());
384       }
385       myListOfCompoundOfEdgeConnected.Append(Compound);
386       myCompoundWireMap.Bind(Compound,ILW.Value());
387     }
388   return myListOfCompoundOfEdgeConnected;
389 }
390
391 //=======================================================================
392 //function : IsWire
393 //purpose  : 
394 //=======================================================================
395
396 Standard_Boolean BRepAlgo_DSAccess::IsWire(const TopoDS_Shape& S)
397 {
398   Standard_Boolean b = Standard_False;
399   if(myEC->IsDone()) {
400     if (myCompoundWireMap.IsBound(S))
401       b = myEC->IsWire(myCompoundWireMap(S));
402   }
403   return b;
404 }
405
406 //=======================================================================
407 //function : Wire
408 //purpose  : 
409 //=======================================================================
410
411 const TopoDS_Shape& BRepAlgo_DSAccess::Wire(const TopoDS_Shape& S)
412 {
413   if(!IsWire(S)) {
414     myWire.Nullify();
415   }
416   else {
417     BRep_Builder BB;
418     BB.MakeWire(myWire);
419     TopExp_Explorer Explor(S, TopAbs_EDGE);
420     for(;Explor.More(); Explor.Next()) BB.Add(myWire, Explor.Current());
421   }
422   return myWire;
423 }
424
425 //=======================================================================
426 //function : SectionVertex
427 //purpose  : 
428 //=======================================================================
429
430 const TopTools_ListOfShape& BRepAlgo_DSAccess::SectionVertex
431 (const TopoDS_Shape& F,
432  const TopoDS_Shape& E)
433 {
434   TopTools_ListOfShape Result;
435   Result.Clear();
436   if(F.ShapeType() != TopAbs_FACE) return myEmptyListOfShape;
437   if(E.ShapeType() != TopAbs_EDGE) return myEmptyListOfShape;
438   Standard_Integer iF = myHDS->Shape(F), iE = myHDS->Shape(E);
439   if((iF == 0) || (iE == 0)) return myEmptyListOfShape;
440
441   const TopOpeBRepDS_DataStructure& DS = myHDS->DS();
442   const TopOpeBRepDS_ListOfInterference& LI = 
443     DS.ShapeInterferences(E, Standard_False);
444   TopOpeBRepDS_InterferenceIterator II(LI);
445   Standard_Integer goodIndex = 0;
446   TopOpeBRepDS_Kind goodKind;
447   for(;II.More();II.Next()) {
448     Handle(TopOpeBRepDS_Interference)& I = II.Value();
449     const TopOpeBRepDS_Transition& T = I->Transition();
450     if((T.ONAfter() == TopAbs_FACE) &&
451        (T.IndexAfter()  == iF)) {
452       goodKind  = I->GeometryType();
453       goodIndex = I->Geometry();
454       if(goodKind == TopOpeBRepDS_VERTEX)
455         Result.Append(myHDS->Shape(goodIndex));
456       else 
457         if (goodKind == TopOpeBRepDS_POINT)
458           Result.Append(myHB->NewVertex(goodIndex));
459     }
460   }
461   myListOfVertex = Result;
462   return myListOfVertex;
463 }
464
465
466 // Editing of the DS
467
468 //=======================================================================
469 //function : SuppressEdgeSet
470 //purpose  : 
471 //=======================================================================
472
473 void BRepAlgo_DSAccess::SuppressEdgeSet
474 (const TopoDS_Shape& C)
475 {
476   // It is checked if C really is a Coumpound of connected Edges
477
478   myHB->InitExtendedSectionDS();
479 //  myGetSectionIsDone = Standard_False;
480
481   TopTools_ListIteratorOfListOfShape LLS(myListOfCompoundOfEdgeConnected);
482   for(;LLS.More(); LLS.Next())
483     if(C == LLS.Value())
484       break;
485   if(!LLS.More())
486     return;
487   
488   // Cleaning
489   TopoDS_Shape Empty;
490   Empty.Nullify();
491   Suppress(C, Empty);
492   myListOfCompoundOfEdgeConnected.Remove(LLS);
493 }
494
495 //=======================================================================
496 //function : ChangeEdgeSet
497 //purpose  : 
498 //=======================================================================
499
500 void BRepAlgo_DSAccess::ChangeEdgeSet
501 (const TopoDS_Shape& Old, const TopoDS_Shape& New)
502 {
503   // It is checked if Old is a Coumpound of connected Edges
504
505   myHB->InitExtendedSectionDS();
506
507   TopTools_ListIteratorOfListOfShape LLS(myListOfCompoundOfEdgeConnected);
508   for(;LLS.More(); LLS.Next())
509     if(Old == LLS.Value())
510       break;
511   if(!LLS.More())
512     return;
513
514   // The compound of Edges to be rotated is constructed
515   BRep_Builder B;
516   Standard_Boolean Trouve;
517   Standard_Integer iC;
518   TopoDS_Compound C;
519   TopoDS_Edge E;
520   B.MakeCompound(C);
521   TColStd_SetOfInteger RPoint; //The points to be controlled 
522   
523  TopOpeBRepDS_ListIteratorOfListOfInterference iter;
524   TopExp_Explorer exp(Old, TopAbs_EDGE);
525   TopExp_Explorer exp2;
526   for(; exp.More(); exp.Next()) {
527     const TopoDS_Shape& Edge = exp.Current(); 
528     for(exp2.Init(New, TopAbs_EDGE), Trouve=Standard_False;
529         exp2.More() && (!Trouve); exp2.Next()) {
530       E = TopoDS::Edge(exp2.Current());
531       Trouve = E.IsSame(Edge);
532     }
533
534     if (!Trouve) B.Add(C, Edge); // Edge to be removed
535     else if (!E.IsEqual(Edge)) {
536       // It is necessary to change Interferences => take the complement
537       iC = myHB->GetDSCurveFromSectEdge(Edge);
538       if (!iC) {
539 #if DEB
540         cout << "Warning DSAccess: Modifications of Edge are not implemented" << endl;
541 #endif
542       }
543       else {
544         // Complete the interferences Curve/Face
545         Standard_Integer iF;
546         Handle(TopOpeBRepDS_Interference) interf;
547
548         iF = myHB->GetDSFaceFromDSCurve(iC, 1); 
549         TopOpeBRepDS_ListOfInterference& list1 = 
550            myHDS->ChangeDS().ChangeShapeInterferences(iF);
551         for(iter.Initialize(list1); iter.More(); iter.Next()) {
552           interf = iter.Value();
553           if (interf->Geometry() == iC)
554             interf->Transition(interf->Transition().Complement());
555         }
556         iF = myHB->GetDSFaceFromDSCurve(iC, 2); 
557         TopOpeBRepDS_ListOfInterference& list2 = 
558           myHDS->ChangeDS().ChangeShapeInterferences(iF);
559         for(iter.Initialize(list2); iter.More(); iter.Next()) {
560           interf = iter.Value();
561           if (interf->Geometry() == iC)
562             interf->Transition(interf->Transition().Complement());
563         }
564         // The associated points are recorded
565         Standard_Integer ipv1, ipv2;
566         //Standard_Boolean bid; // skl
567         TopOpeBRepDS_Kind k1, k2;       
568         PntVtxOnCurve(iC, ipv1, k1, ipv2, k2);
569         if (ipv1 != 0) /*bid = */RPoint.Add(ipv1); // skl
570         if (ipv2 != 0) /*bid = */RPoint.Add(ipv2); // skl
571       }
572     }
573   }
574
575
576   // Cleaning
577   Suppress(C, New);
578
579   // Is it necessary to invert the Interferences "Edge on Fa"
580   if (!RPoint.IsEmpty()) {
581     const TopOpeBRepDS_DataStructure & DS = myHDS->DS();
582     Standard_Integer iP,iE, nbShape = DS.NbShapes();
583     Handle(TopOpeBRepDS_Interference) interf;
584     for (iE=1; iE<=nbShape; iE++) {
585       if (DS.Shape(iE,0).ShapeType() == TopAbs_EDGE) { 
586         const TopOpeBRepDS_ListOfInterference& List = 
587           myHDS->DS().ShapeInterferences(iE);
588         for(iter.Initialize(List); iter.More(); iter.Next()) {
589           interf = iter.Value();
590           if (interf->GeometryType() == TopOpeBRepDS_POINT) {
591             iP = interf->Geometry();
592             if (RPoint.Contains(iP))
593               interf->Transition(interf->Transition().Complement());
594           }
595         }
596       }
597     }
598   }
599
600   // The old is replaced by new
601   LLS.Value() = New;
602 }
603
604
605 //=======================================================================
606 //function : Remove
607 //purpose  : 
608 //=======================================================================
609
610 void BRepAlgo_DSAccess::Suppress(const TopoDS_Shape& C,
611                                  const TopoDS_Shape& Keep)
612 {
613  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
614  TopOpeBRepBuild_Builder& Builder = myHB->ChangeBuilder();
615  Standard_Integer i, iC = 0, iF1, iF2,iE1, iE2;
616 // TopOpeBRepDS_ListIteratorOfListOfInterference;
617  TColStd_ListIteratorOfListOfInteger it1, it2;
618
619   //A group of points to be kept is constructed
620  mySetOfKeepPoint.Clear();
621  if (!Keep.IsNull()) {
622    //Standard_Boolean B; // skl
623    Standard_Integer ipv1, ipv2;
624    TopOpeBRepDS_Kind k1, k2;
625    TopExp_Explorer exp(Keep, TopAbs_EDGE);
626    for(; exp.More(); exp.Next()) {
627      const TopoDS_Shape& SectEdge = exp.Current();
628      iC = myHB->GetDSCurveFromSectEdge(SectEdge);
629      if(!iC) 
630        PntVtxOnSectEdge(SectEdge, ipv1, k1, ipv2, k2);
631      else 
632        PntVtxOnCurve(iC, ipv1, k1, ipv2, k2);
633      if (ipv1 != 0) /*B = */mySetOfKeepPoint.Add(ipv1); // skl
634      if (ipv2 != 0) /*B = */mySetOfKeepPoint.Add(ipv2); // skl
635    }
636  }
637
638   // The curves, which generated the the Edges, are found
639   // during the parsing the Edges which come from Edge are found
640   // (= MapOfInteger : ESE)
641   
642   // First, the interferences of support 1d.
643   TopExp_Explorer exp(C, TopAbs_EDGE);
644   for(; exp.More(); exp.Next()) {
645     const TopoDS_Shape& SectEdge = exp.Current();
646     iC = myHB->GetDSCurveFromSectEdge(SectEdge);
647     if(!iC) {
648       // the Edges that come from Edge are processed
649       // the interferences connected with Edges are processed :
650       iE1 = myHB->GetDSEdgeFromSectEdge(SectEdge, 1);
651       iE2 = myHB->GetDSEdgeFromSectEdge(SectEdge, 2);
652       
653       RemoveEdgeInterferences(iE1,iE2,SectEdge);
654       
655       TColStd_ListOfInteger& loi11 = myHB->GetDSFaceFromDSEdge(iE1, 1);
656       TColStd_ListOfInteger& loi12 = myHB->GetDSFaceFromDSEdge(iE1, 2);
657       for(it1.Initialize(loi11); it1.More(); it1.Next()) {
658         iF1 = it1.Value();
659         for(it2.Initialize(loi12); it2.More(); it2.Next()) {
660           iF2 = it2.Value();
661           // similar to the case of SectEdges coming from curve.
662           RemoveEdgeInterferences(iF1,iF2,SectEdge);
663         }
664       }
665       TColStd_ListOfInteger& loi21 = myHB->GetDSFaceFromDSEdge(iE2, 1);
666       TColStd_ListOfInteger& loi22 = myHB->GetDSFaceFromDSEdge(iE2, 2);
667       for(it1.Initialize(loi21); it1.More(); it1.Next()) {
668         iF1 = it1.Value();
669         for(it2.Initialize(loi22); it2.More(); it2.Next()) {
670           iF2 = it2.Value();
671           // similar to the case of  SectEdges coming from curve.
672           RemoveEdgeInterferences(iF1,iF2,SectEdge);
673         }
674       }
675       continue;
676     }    
677     // The Edges that come from Curve are processed
678     iF1 = myHB->GetDSFaceFromDSCurve(iC, 1);
679     iF2 = myHB->GetDSFaceFromDSCurve(iC, 2);
680     
681     RemoveEdgeInterferences(iF1, iF2, iC);
682     DS.ChangeKeepCurve(iC, FindKeep);
683   }
684   
685   // Secondly, the interferences of 2D support.
686   exp.Init(C, TopAbs_EDGE);
687   for(; exp.More(); exp.Next()) {
688     const TopoDS_Shape& SectEdge = exp.Current();
689     iC = myHB->GetDSCurveFromSectEdge(SectEdge);
690     if(!iC) {
691       iE1 = myHB->GetDSEdgeFromSectEdge(SectEdge, 1);
692       iE2 = myHB->GetDSEdgeFromSectEdge(SectEdge, 2);
693       TColStd_ListOfInteger& loi11 = myHB->GetDSFaceFromDSEdge(iE1, 1);
694       TColStd_ListOfInteger& loi12 = myHB->GetDSFaceFromDSEdge(iE1, 2);
695       for(it1.Initialize(loi11); it1.More(); it1.Next()) {
696         iF1 = it1.Value();
697         for(it2.Initialize(loi12); it2.More(); it2.Next()) {
698           iF2 = it2.Value();
699           if(iF1 == iF2)
700             continue;
701           RemoveFaceInterferences(iF1, iF2, iE1, iE2);
702         }
703       }
704       TColStd_ListOfInteger& loi21 = myHB->GetDSFaceFromDSEdge(iE2, 1);
705       TColStd_ListOfInteger& loi22 = myHB->GetDSFaceFromDSEdge(iE2, 2);
706       for(it1.Initialize(loi21); it1.More(); it1.Next()) {
707         iF1 = it1.Value();
708         for(it2.Initialize(loi22); it2.More(); it2.Next()) {
709           iF2 = it2.Value();
710           if(iF1 == iF2)
711             continue;
712           RemoveFaceInterferences(iF1, iF2, iE1, iE2);
713         }
714       }
715     }
716     else {
717       iF1 = myHB->GetDSFaceFromDSCurve(iC, 1);
718       iF2 = myHB->GetDSFaceFromDSCurve(iC, 2);
719       
720       RemoveFaceInterferences(iF1, iF2, iC);
721     }
722   }
723   
724   // Thirdly, RemoveSameDomain is done for the faces that contain all Edges of C,
725   // and are SameDomain and without Geometry.
726   
727   RemoveFaceSameDomain(C);
728   
729   // Fourthly, the faces, that were not concerned, are removed
730   Standard_Integer NbSh = DS.NbShapes();
731   for(i = 1; i <= NbSh; i++) {
732     const TopoDS_Shape& Face = DS.Shape(i);
733     if(Face.IsNull())
734       continue;
735     if((Face.ShapeType() != TopAbs_FACE) || DS.HasGeometry(Face) ||
736        (myHDS->HasSameDomain(Face)))
737       continue;
738     for(exp.Init(Face, TopAbs_EDGE); exp.More(); exp.Next()){
739       const TopoDS_Shape& Edge = exp.Current();
740       if(DS.HasShape(Edge))
741         break;
742     }
743     if(exp.More())
744       continue;
745     DS.ChangeKeepShape(Face, Standard_False);
746   }
747   
748   // Builder.myKPMAPf1f2 is reconstructed
749   Builder.FindIsKPart(); 
750   
751   //  The Edges of section are removed from Builder.mySplitON
752   exp.Init(C, TopAbs_EDGE);
753   for(; exp.More(); exp.Next()) {
754     const TopoDS_Shape& SectE= exp.Current();
755     TopTools_ListOfShape& losob = Builder.ChangeSplit(SectE, TopAbs_ON);
756     losob.Clear();
757   }
758 }
759
760 //=======================================================================
761 //function : SuppressSectionVertex
762 //purpose  : 
763 //=======================================================================
764
765 void BRepAlgo_DSAccess::SuppressSectionVertex
766 (const TopoDS_Vertex& /*V*/)
767 {
768   if(!myRecomputeBuilderIsDone)
769     return;
770 }
771
772
773 // Reconstruction of Shapes
774
775 //=======================================================================
776 //function : Merge
777 //purpose  : 
778 //=======================================================================
779
780 const TopoDS_Shape& BRepAlgo_DSAccess::Merge
781 (const TopAbs_State state1,
782  const TopAbs_State state2)
783 {
784   if((state1 != TopAbs_IN) &&
785      (state1 != TopAbs_OUT))
786     return myEmptyShape;
787   if((state2 != TopAbs_IN) &&
788      (state2 != TopAbs_OUT))
789     return myEmptyShape;
790   // if GetSectionEdgeSet has already been called, nothing is done 
791   // in GetSectionEdgeSet.
792   if(myState1 != TopAbs_UNKNOWN)
793     if(myState1 != state1 || myState2 != state2)
794       myGetSectionIsDone = Standard_False;
795   myState1 = state1;
796   myState2 = state2;
797   GetSectionEdgeSet();
798   
799   myHB->Clear();
800   myHB->MergeShapes(myS1,state1,myS2,state2);
801   const TopTools_ListOfShape& L1 = myHB->Merged(myS1,state1);
802   
803   BRep_Builder BB;
804   myResultShape.Nullify();
805   BB.MakeCompound(TopoDS::Compound(myResultShape));
806   TopTools_ListIteratorOfListOfShape it(L1);
807   for(;it.More(); it.Next()) {
808     BB.Add(myResultShape, it.Value());
809   }
810   return myResultShape;
811 }
812
813 //=======================================================================
814 //function : Merge
815 //purpose  : 
816 //=======================================================================
817
818 const TopoDS_Shape& BRepAlgo_DSAccess::Merge
819 (const TopAbs_State state1)
820 {
821   if((state1 != TopAbs_IN) &&
822      (state1 != TopAbs_OUT))
823     return myEmptyShape;
824   GetSectionEdgeSet();
825
826   myHB->Clear();
827   myHB->MergeSolid(myS1,state1);
828   const TopTools_ListOfShape& L1 = myHB->Merged(myS1,state1);
829   
830   BRep_Builder BB;
831   myResultShape.Nullify();
832   BB.MakeCompound(TopoDS::Compound(myResultShape));
833   TopTools_ListIteratorOfListOfShape it(L1);
834   for(;it.More(); it.Next()) {
835     BB.Add(myResultShape, it.Value());
836   }
837   return myResultShape;
838 }
839
840 //=======================================================================
841 //function : Propagate
842 //purpose  : 
843 //=======================================================================
844
845 const TopoDS_Shape& BRepAlgo_DSAccess::Propagate
846 (const TopAbs_State what,
847  const TopoDS_Shape& /*fromShape*/,
848  const TopoDS_Shape& /*LoadShape*/)
849 {
850   if((what != TopAbs_IN) &&
851      (what != TopAbs_OUT))
852     return myEmptyShape;
853   if(!myRecomputeBuilderIsDone)
854     return myEmptyShape;
855
856 //  myHB->MergeShapes(myS1,t1,myS2,t2);
857
858   //POP for NT;
859   static TopoDS_Shape bid;
860   return bid;
861 }
862
863 //=======================================================================
864 //function : PropagateFromSection
865 //purpose  : 
866 //=======================================================================
867
868 const TopoDS_Shape& BRepAlgo_DSAccess::PropagateFromSection
869 (const TopoDS_Shape& SectionShape)
870 {
871   GetSectionEdgeSet();
872   TopTools_ListIteratorOfListOfShape ils(myListOfCompoundOfEdgeConnected);
873   TopExp_Explorer exp;
874   for(; ils.More(); ils.Next()) {
875     const TopoDS_Shape& SetEdgSet = ils.Value();
876     exp.Init(SetEdgSet, TopAbs_EDGE);
877     for(; exp.More(); exp.Next()) {
878       if(SectionShape.IsSame(exp.Current()))
879         return SetEdgSet;
880     }
881   }
882   return myEmptyShape;
883 }
884
885 //=======================================================================
886 //function : Modified
887 //purpose  : 
888 //=======================================================================
889
890 const TopTools_ListOfShape& BRepAlgo_DSAccess::Modified (const TopoDS_Shape& Shape) 
891 {
892   myModified.Clear() ;
893
894 //  Handle(TopOpeBRepBuild_HBuilder)& HBuilder = myDSA.myHB ;
895   TopTools_ListIteratorOfListOfShape Iterator ;
896   
897   if (myHB->IsSplit (Shape, TopAbs_OUT)) {
898     for (Iterator.Initialize (myHB->Splits (Shape, TopAbs_OUT)) ;
899          Iterator.More() ;
900          Iterator.Next()) {
901       myModified.Append (Iterator.Value()) ;
902     }
903   }
904   if (myHB->IsSplit (Shape, TopAbs_IN)) {
905     for (Iterator.Initialize (myHB->Splits (Shape, TopAbs_IN)) ;
906          Iterator.More() ;
907          Iterator.Next()) {
908       myModified.Append (Iterator.Value()) ;
909     }
910   }
911   if (myHB->IsSplit (Shape, TopAbs_ON)) {
912     for (Iterator.Initialize (myHB->Splits (Shape, TopAbs_ON)) ;
913          Iterator.More() ;
914          Iterator.Next()) {
915       myModified.Append (Iterator.Value()) ;
916     }
917   }
918
919   if (myHB->IsMerged (Shape, TopAbs_OUT)) {
920     for (Iterator.Initialize (myHB->Merged (Shape, TopAbs_OUT)) ;
921          Iterator.More() ;
922          Iterator.Next()) {
923       myModified.Append (Iterator.Value()) ;
924     }
925   }
926   if (myHB->IsMerged(Shape, TopAbs_IN)) {
927     for (Iterator.Initialize (myHB->Merged (Shape, TopAbs_IN)) ;
928          Iterator.More() ;
929          Iterator.Next()) {
930       myModified.Append (Iterator.Value()) ;
931     }
932   }
933   if (myHB->IsMerged(Shape, TopAbs_ON)) {
934     for (Iterator.Initialize (myHB->Merged (Shape, TopAbs_ON)) ;
935          Iterator.More() ;
936          Iterator.Next()) {
937       myModified.Append (Iterator.Value()) ;
938     }
939   }
940
941   return myModified ;
942 }
943
944
945
946 //=======================================================================
947 //function : Check
948 //purpose  : 
949 //=======================================================================
950
951 BRepAlgo_CheckStatus BRepAlgo_DSAccess::Check()
952 {
953 //  TopOpeBRepDS_Check Ck(HDS);
954 // to be precised : in Ck, there is a possibility to know 
955 // exactly the n*n of shapes/points/curves/surfaces, 
956 // which are not correct in the DS.
957 //  Standard_Boolean IsOK = Ck.ChkIntgSamDom() ;
958 //  IsOK = IsOK && Ck.OneVertexOnPnt();
959 //  IsOK = IsOK && Ck.ChkIntg();
960 //  if(IsOK)
961 //    return TopOpeBRepDS_OK;
962   return BRepAlgo_NOK;
963 }
964
965 //=======================================================================
966 //function : RemoveEdgeInterferences
967 //purpose  : case of SectEdge coming from Edge(s)
968 //       
969 //     if iE1 and iE2 are Edges :
970 //     Remove interferences of DSEdge(= iE1 or iE2) of
971 //     geometry a vertex of SectEdge, and if there is nothing else, 
972 //     make unkeep on DSEdge 
973 //     if iE1 or iE2 == 0, no interference on Edges in the DS 
974 //     NYI : management of SameDomain
975 //       
976 //     if iE1 and iE2 are Faces :
977 //     for each of faces F1 and F2, explode into Edges
978 //     for each Edge :
979 //           remove the interferences of a SectEdge vertex
980 //           on geometry. If there is no other interferences attached to 
981 //           these Edges, and if these Edges are not SameDomain,
982 //           make unKeepShape.
983 //=======================================================================
984
985 void BRepAlgo_DSAccess::RemoveEdgeInterferences
986 (const Standard_Integer iE1,
987  const Standard_Integer iE2,
988  const TopoDS_Shape& SectEdge)
989 {
990   if(!iE1 || !iE2)
991     return;
992
993   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
994   TopOpeBRepDS_Kind kind1, kind2;
995   TopExp_Explorer exp(SectEdge, TopAbs_VERTEX);
996   Standard_Integer i = 1, ipv1, ipv2;
997   
998   // the Vertex/Points of SectEdge are retrieved
999   PntVtxOnSectEdge(SectEdge, ipv1, kind1, ipv2, kind2);
1000   
1001   const TopoDS_Shape& Shape = DS.Shape(iE1, FindKeep);
1002   if(Shape.IsNull())
1003     return;
1004   if(Shape.ShapeType() == TopAbs_FACE) {
1005     Standard_Integer iF1 = iE1, iF2 = iE2;
1006     RemoveEdgeInterferencesFromFace(iF1, iF2, ipv1, kind1, ipv2, kind2);
1007     return;
1008   }
1009   else if(Shape.ShapeType() != TopAbs_EDGE)
1010     return;
1011   
1012   // the interferences are taken from the DS
1013   TopOpeBRepDS_ListIteratorOfListOfInterference lioloi;
1014   TopOpeBRepDS_Kind gk;
1015   Standard_Integer iCurrE1, iCurrE2, gi;
1016 //  Standard_Boolean RemInterf;
1017   
1018   for(i = 1; i <= 2; i++) {
1019     iCurrE1 = ((i == 1) ? iE1 : iE2);
1020     iCurrE2 = ((i == 1) ? iE2 : iE1);
1021     const TopoDS_Shape& DSEdge = DS.Shape(iCurrE1, FindKeep);
1022     if(DSEdge.IsNull())
1023       continue;
1024     TopOpeBRepDS_ListOfInterference& loi = 
1025       DS.ChangeShapeInterferences(DSEdge);
1026     //    RemInterf = Standard_True;
1027     for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
1028       Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
1029       if (I.IsNull()) continue;
1030       if((I->SupportType() != TopOpeBRepDS_EDGE) ||
1031          (I->Support() != iCurrE2)) {
1032         //      RemInterf = Standard_False;//debug ...
1033         continue;
1034       }
1035       gk = I->GeometryType();
1036       gi = I->Geometry();
1037       if(gk == kind1) {
1038         if(gi == ipv1) {
1039           DS.RemoveShapeInterference(DSEdge, I);
1040           if(!DS.HasGeometry(DSEdge)) {
1041             //      if(RemInterf || (!lioloi.More())) {
1042             RemoveEdgeSameDomain(iCurrE1, iCurrE2); // NYI : SameDomain
1043             DS.ChangeKeepShape(iCurrE1, FindKeep);
1044             //    } 
1045           } 
1046         }
1047       }
1048       else if(gk == kind2) {
1049         if(gi == ipv2) {
1050           DS.RemoveShapeInterference(DSEdge, I);
1051           if(!DS.HasGeometry(DSEdge)) {
1052             //      if(RemInterf || (!lioloi.More())) {//debug
1053             RemoveEdgeSameDomain(iCurrE1, iCurrE2); // NYI : SameDomain
1054             DS.ChangeKeepShape(iCurrE1, FindKeep);
1055             //    } 
1056           }
1057         }
1058       }
1059     }
1060   }
1061 }
1062
1063 //=======================================================================
1064 //function : RemoveEdgeInterferences
1065 //purpose  : case of SectEdge coming from Curve
1066 //           for each of faces F1 and F2, explode into Edges
1067 //           for each Edge :
1068 //           remove the interferences that have a vertex of SectEdge
1069 //           as a geometry. If no other interferences are attached to  
1070 //           these Edges, and if the Edges are not SameDomain,
1071 //           make unKeepShape.
1072 //=======================================================================
1073
1074 void BRepAlgo_DSAccess::RemoveEdgeInterferences
1075 (const Standard_Integer iF1,
1076  const Standard_Integer iF2,
1077  const Standard_Integer iCurve)
1078 {
1079   TopOpeBRepDS_Kind gk1, gk2;
1080   Standard_Integer gi1, gi2;
1081   
1082   PntVtxOnCurve(iCurve, gi1, gk1, gi2, gk2);
1083
1084   if (!mySetOfKeepPoint.IsEmpty()) {
1085     if (mySetOfKeepPoint.Contains(gi1)) gi1 = 0;
1086     if (mySetOfKeepPoint.Contains(gi2)) gi2 = 0;   
1087   }
1088   
1089   if (gi1 || gi2)
1090     RemoveEdgeInterferencesFromFace(iF1, iF2, gi1, gk1, gi2, gk2);
1091 }
1092
1093 //=======================================================================
1094 //function : RemoveFaceInterferences
1095 //purpose  : case of SectEdge coming from Edge(s)
1096 //        Remove interferences between F1 and F2 concerning 
1097 //        DSEdge (= E1 or E2) :
1098 //              a) if DSEdge is not SameDomain -> the edge is Removed
1099 //              b) if among other interferences of DSEdge of 
1100 //                 GeomtryType == VERTEX, there is none 
1101 //                 with Edge of DSFace(= F1 or F2)
1102 //        if DSFace has no more interferences and is not SameDomain,
1103 //        make unkeep DSFace.
1104 //=======================================================================
1105
1106 void BRepAlgo_DSAccess::RemoveFaceInterferences
1107 (const Standard_Integer iF1,
1108  const Standard_Integer iF2,
1109  const Standard_Integer iE1,
1110  const Standard_Integer iE2)
1111 {
1112   if(!iF1 || !iF2)
1113     return;
1114   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1115   TopOpeBRepDS_ListIteratorOfListOfInterference lioloi;//, lioloei, liolofi;
1116   TopTools_ListIteratorOfListOfShape liolos;
1117   TopOpeBRepDS_Kind gk;
1118   TopExp_Explorer exp;
1119   Standard_Integer i, iCurrF1, iCurrF2, j, iCurrE1, /*iCurrE2,*/ gi; // skl
1120   Standard_Boolean RemInterf;
1121   
1122   for(i = 1; i <= 2; i++) {
1123     iCurrF1 = ((i == 1) ? iF1 : iF2);
1124     iCurrF2 = ((i == 1) ? iF2 : iF1);
1125     const TopoDS_Shape& DSFace = DS.Shape(iCurrF1);
1126     if(DSFace.IsNull())
1127       continue;
1128     const TopOpeBRepDS_ListOfInterference& loi = DS.ShapeInterferences(DSFace);
1129     for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
1130       Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
1131       if (I.IsNull()) continue;
1132       if((I->SupportType() != TopOpeBRepDS_FACE) ||
1133          (I->Support() != iCurrF2)) {
1134         continue;
1135       }
1136       gk = I->GeometryType();
1137       gi = I->Geometry();
1138       if(gk != TopOpeBRepDS_EDGE) continue;
1139       for(j = 1; j <= 2; j++) {
1140         iCurrE1 = ((j == 1) ? iE1 : iE2);
1141         //iCurrE2 = ((j == 1) ? iE2 : iE1); // skl
1142         if(gi != iCurrE1) continue;
1143         // a) if DSEdge is not SameDomain -> the interference is Removed
1144         //    et DSEdge
1145         const TopoDS_Shape& DSEdge = DS.Shape(iCurrE1, FindKeep);
1146         if(DSEdge.IsNull())
1147           continue;
1148         if(!myHDS->HasSameDomain(DSEdge)) {
1149           if(!DS.HasGeometry(DSEdge)) {
1150             DS.RemoveShapeInterference(DSFace, I);
1151             DS.ChangeKeepShape(DSEdge, FindKeep);
1152           } else {
1153             // NYI : manage the case when the geometry of DSEdge 
1154             // NYI : is not connected anyhow with two faces
1155           }
1156           if(!DS.HasGeometry(DSFace)) {
1157             DS.ChangeKeepShape(DSFace, FindKeep);
1158           }
1159           continue;
1160         }
1161         // b) if no Edges of  SameDomain(DSEdge),
1162         //    belong to DSFace(= F1 or F2)
1163         //     -> the interference is removed
1164         const TopoDS_Shape& Edge = DS.Shape(iCurrE1, FindKeep);
1165         if(Edge.IsNull())
1166           continue;
1167         const TopTools_ListOfShape& loe = DS.ShapeSameDomain(Edge);
1168         RemInterf = Standard_True;
1169         for(liolos.Initialize(loe); liolos.More(); liolos.Next()) {
1170           const TopoDS_Shape& ESD = liolos.Value();
1171           for(exp.Init(DSFace, TopAbs_EDGE); exp.More(); exp.Next()) {
1172             if(ESD.IsSame(exp.Current())) {
1173               RemInterf = Standard_False;
1174               break;
1175             }
1176           }
1177           if(!RemInterf) break;
1178         }
1179         if(RemInterf) {
1180           //      RemoveSameDomain(iCurrF1, iCurrF2);
1181           
1182           if(!DS.HasGeometry(DSFace)) {
1183             if(!myHDS->HasSameDomain(DSFace))
1184               DS.ChangeKeepShape(DSFace, FindKeep);
1185           }
1186         }
1187         if(!DS.HasGeometry(DSFace) && !myHDS->HasSameDomain(DSFace))
1188           DS.ChangeKeepShape(DSFace, FindKeep);
1189       }
1190     }
1191   }
1192 }
1193
1194 //=======================================================================
1195 //function : RemoveFaceInterferences
1196 //purpose  : case of SectEdge from Curve
1197 //           remove interferences of Geometry iCurve between F1 and F2.
1198 //           if Face(= F1 or F2) has noother interference, and if Face
1199 //           is not SameDomain, make unKeepShape Face.
1200 //=======================================================================
1201
1202 void BRepAlgo_DSAccess::RemoveFaceInterferences
1203 (const Standard_Integer iF1,
1204  const Standard_Integer iF2,
1205  const Standard_Integer iCurve)
1206 {
1207   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1208   TopOpeBRepDS_ListIteratorOfListOfInterference lioloi;
1209   TopOpeBRepDS_Kind gk;
1210   Standard_Integer i, iCurrF1, iCurrF2, gi;
1211
1212   for(i = 1; i <= 2; i++) {
1213     iCurrF1 = ((i == 1) ? iF1 : iF2);
1214     iCurrF2 = ((i == 1) ? iF2 : iF1);
1215     const TopoDS_Shape& DSFace = DS.Shape(iCurrF1);
1216     const TopOpeBRepDS_ListOfInterference& loi = DS.ShapeInterferences(DSFace);
1217     for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
1218       Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
1219       if (I.IsNull()) continue;
1220       if((I->SupportType() != TopOpeBRepDS_FACE) ||
1221          (I->Support() != iCurrF2)) {
1222         break;;
1223       }
1224     }
1225     for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
1226       Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
1227       if (I.IsNull()) continue;
1228       if((I->SupportType() != TopOpeBRepDS_FACE) ||
1229          (I->Support() != iCurrF2)) {
1230         continue;
1231       }
1232       gk = I->GeometryType();
1233       gi = I->Geometry();
1234       if(gk != TopOpeBRepDS_CURVE) continue;
1235       if(gi != iCurve) continue;
1236       DS.RemoveShapeInterference(DSFace, I);
1237 //      const TopoDS_Shape& interferenceface = DS.Shape(iCurrF2);
1238 //      DS.RemoveShapeInterference(interferenceface, I);
1239       if(!DS.HasGeometry(DSFace)) {
1240         const TopTools_ListOfShape& los = DS.ShapeSameDomain(DSFace);
1241         if(los.IsEmpty())
1242           DS.ChangeKeepShape(DSFace, FindKeep);
1243       }
1244 //      if(!DS.HasGeometry(interferenceface)) {
1245 //      const TopTools_ListOfShape& los = DS.ShapeSameDomain(interferenceface);
1246 //      if(los.IsEmpty())
1247 //        DS.ChangeKeepShape(interferenceface, FindKeep);
1248 //      }
1249     }
1250   }
1251 }
1252
1253 //=======================================================================
1254 //function : RemoveEdgeInterferencesFromFace
1255 //purpose  : Remove interferences of Edges from iF1 or iF2
1256 //           that have GeometryType kind1/kind2 and 
1257 //           Geometry ipv1/ipv2.
1258 //           if kind1/kind2 == TopAbs_VERTEX -> RemoveEdgeFromFace
1259 //=======================================================================
1260
1261 void BRepAlgo_DSAccess::RemoveEdgeInterferencesFromFace
1262 (const Standard_Integer iF1,
1263  const Standard_Integer iF2,
1264  const Standard_Integer ipv1,
1265  const TopOpeBRepDS_Kind kind1,
1266  const Standard_Integer ipv2,
1267  const TopOpeBRepDS_Kind kind2)
1268 {
1269   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1270   TopOpeBRepDS_ListIteratorOfListOfInterference lioloi;
1271   TopExp_Explorer exp, exp2;
1272   TopOpeBRepDS_Kind sk, gk;
1273   Standard_Integer i, iCurrF1, iCurrF2, iE = 0, si, gi;
1274
1275   for(i = 1; i <= 2; i++) {
1276     iCurrF1 = ((i == 1) ? iF1 : iF2);
1277     iCurrF2 = ((i == 1) ? iF2 : iF1);
1278     const TopoDS_Shape& DSFace = DS.Shape(iCurrF1, FindKeep);
1279     if(DSFace.IsNull())
1280       continue;
1281     exp.Init(DSFace, TopAbs_EDGE);
1282     for(; exp.More(); exp.Next()) {
1283       const TopoDS_Shape& DSEdge = exp.Current();
1284       iE = DS.Shape(DSEdge, FindKeep);
1285       if(!iE) continue;
1286       const TopOpeBRepDS_ListOfInterference& loi =
1287         DS.ShapeInterferences(DSEdge);
1288       for(lioloi.Initialize(loi); lioloi.More(); lioloi.Next()) {
1289         Handle(TopOpeBRepDS_Interference) I = lioloi.Value();
1290         if (I.IsNull()) continue;
1291         sk = I->SupportType();
1292         si = I->Support();
1293         if((sk != TopOpeBRepDS_FACE) || (si != iCurrF2)) {
1294           if(sk != TopOpeBRepDS_EDGE)
1295             continue;
1296           const TopoDS_Shape& DSFace2 = DS.Shape(iCurrF2, FindKeep);
1297           exp2.Init(DSFace2, TopAbs_EDGE);
1298           for(; exp2.More(); exp2.Next()) {
1299             if(si == DS.Shape(exp2.Current(), FindKeep))
1300               break;
1301           }
1302           if(!exp2.More())
1303             continue;
1304         }
1305         gk = I->GeometryType();
1306         gi = I->Geometry();
1307         if(gk == kind1) {
1308           if(gi == ipv1) {
1309             DS.RemoveShapeInterference(DSEdge, I);
1310 //          if(!DS.HasGeometry(DSEdge)) {
1311 //            const TopTools_ListOfShape& los = DS.ShapeSameDomain(DSEdge);
1312 //            if(los.IsEmpty()) {
1313 //              DS.ChangeKeepShape(iE, FindKeep);
1314 //            }
1315 //          }
1316           }
1317           else if(gk == kind2) {
1318             if(gi == ipv2) {
1319               DS.RemoveShapeInterference(DSEdge, I);
1320 //            if(!DS.HasGeometry(DSEdge)) {
1321 //              const TopTools_ListOfShape& los = DS.ShapeSameDomain(DSEdge);
1322 //              if(los.IsEmpty()) {
1323 //                DS.ChangeKeepShape(iE, FindKeep);
1324 //              }
1325 //            }
1326             }
1327           }
1328           else continue;
1329         }
1330       }
1331     }
1332     if(kind1 == TopOpeBRepDS_VERTEX)
1333       RemoveEdgeFromFace(iCurrF1,ipv1);
1334     if(kind2 == TopOpeBRepDS_VERTEX)
1335       RemoveEdgeFromFace(iCurrF1,ipv2);
1336   }
1337 }
1338
1339 //=======================================================================
1340 //function : RemoveEdgeFromFace
1341 //purpose  : Remove from DS the Edges, which belong to iF
1342 //           and have iV as vertex if they do not have Geometry and
1343 //           are not SameDomain.
1344 //=======================================================================
1345
1346 void BRepAlgo_DSAccess::RemoveEdgeFromFace
1347 (const Standard_Integer iF,
1348  const Standard_Integer iV)
1349 {
1350   if(!iF || !iV)
1351     return;
1352   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1353   const TopoDS_Shape& DSFace = DS.Shape(iF, FindKeep);
1354   const TopoDS_Shape& Vertex = DS.Shape(iV, FindKeep);
1355   if(DSFace.IsNull() || Vertex.IsNull())
1356     return;
1357   TopExp_Explorer exp(DSFace, TopAbs_EDGE), exp2;
1358   for(; exp.More(); exp.Next()) {
1359     const TopoDS_Shape& Edge = exp.Current();
1360 #ifdef DEB
1361 //    Standard_Integer iEdge2 = DS.Shape(Edge, FindKeep);
1362 //    Standard_Integer iEdge3 = DS.Shape(Edge);
1363 #endif
1364                               
1365     if(!DS.HasShape(Edge)) 
1366       continue;
1367     exp2.Init(Edge, TopAbs_VERTEX);
1368     for(; exp2.More(); exp2.Next()) {
1369 #ifdef DEB
1370 //      Standard_Integer iEdge5 = DS.Shape(Vertex, FindKeep);
1371 //      Standard_Integer iEdge4 = DS.Shape(Vertex);
1372 //      Standard_Integer iEdge6 = DS.Shape(exp2.Current(), FindKeep);
1373 //      Standard_Integer iEdge7 = DS.Shape(exp2.Current());
1374 #endif
1375                                 
1376       if(Vertex.IsSame(exp2.Current())) {
1377         if(!DS.HasGeometry(Edge)) {
1378           const TopTools_ListOfShape& los = DS.ShapeSameDomain(Edge);
1379           if(los.IsEmpty()) {
1380 #ifdef DEB
1381 //          Standard_Integer iEdge = DS.Shape(Edge);
1382 #endif
1383             DS.ChangeKeepShape(Edge, FindKeep);
1384           }
1385         }
1386       }
1387     }
1388   }
1389 }
1390
1391 //=======================================================================
1392 //function : PntVtxOnCurve
1393 //purpose  : To find the points/vertices on curves
1394 //=======================================================================
1395
1396 void BRepAlgo_DSAccess::PntVtxOnCurve
1397 (const Standard_Integer iCurve,
1398  Standard_Integer& ipv1,
1399  TopOpeBRepDS_Kind& pvk1,
1400  Standard_Integer& ipv2,
1401  TopOpeBRepDS_Kind& pvk2)
1402 {
1403   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1404   
1405   const TopOpeBRepDS_Curve& C = DS.Curve(iCurve);
1406   TopOpeBRepDS_Kind pvk; 
1407   Standard_Integer ipv, iMother = C.Mother(), igoodC = iCurve, comp = 0;
1408   if(iMother) igoodC = iMother;
1409 //#ifndef DEB
1410   TopOpeBRepDS_PointIterator PII = myHDS->CurvePoints(igoodC);
1411   TopOpeBRepDS_PointIterator& PIt = PII; // skl : I change "M_PI" to "PIt"
1412 //#else
1413 //  TopOpeBRepDS_PointIterator& PIt = myHDS->CurvePoints(igoodC);
1414 //#endif
1415   for(;PIt.More(); PIt.Next()) {
1416     comp++;
1417     if(comp > 2)
1418       // Standard_Error ...
1419       return;
1420     ipv = PIt.Current();
1421     // a point or a vertex is removed from the DS
1422     if(PIt.IsPoint()) {
1423       pvk = TopOpeBRepDS_POINT;
1424       DS.ChangeKeepPoint(ipv, FindKeep);
1425     }
1426     else if(PIt.IsVertex()) {
1427       pvk = TopOpeBRepDS_VERTEX;
1428       DS.ChangeKeepShape(ipv, FindKeep);
1429     }
1430     else continue;
1431     ((comp == 1) ? ipv1 : ipv2) = ipv;
1432     ((comp == 1) ? pvk1 : pvk2) = pvk;
1433   }
1434 }
1435
1436 //=======================================================================
1437 //function : PntVtxOnSectEdge
1438 //purpose  : Points/Vertexes  on  SectEdge are found
1439 //=======================================================================
1440
1441 void BRepAlgo_DSAccess::PntVtxOnSectEdge
1442 (const TopoDS_Shape& SectEdge,
1443  Standard_Integer& ipv1,
1444  TopOpeBRepDS_Kind& pvk1,
1445  Standard_Integer& ipv2,
1446  TopOpeBRepDS_Kind& pvk2)
1447 {
1448 //  myHB->ChangeBuilder();
1449   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1450   TopOpeBRepDS_Kind kind = TopOpeBRepDS_POINT;
1451   TopExp_Explorer exp(SectEdge, TopAbs_VERTEX);
1452   Standard_Integer i = 1, ipv;
1453   
1454   for(; exp.More(); exp.Next(), i++) {    
1455     const TopoDS_Shape& DSVertex = exp.Current();
1456     ipv = myHB->GetDSPointFromNewVertex(DSVertex);
1457     if(!ipv) {
1458       ipv = DS.Shape(DSVertex, FindKeep);
1459       kind = TopOpeBRepDS_VERTEX;
1460       if(!ipv)
1461         // Standard_Error ...
1462         return;
1463     }
1464     
1465     if(i == 1) {
1466       ipv1 = ipv;
1467       pvk1 = kind;
1468     }    
1469     else if(i == 2) {
1470       ipv2 = ipv;
1471       pvk2 = kind;
1472     }
1473     else
1474       // Standard_Error ...
1475       return;
1476   }
1477 }
1478
1479 //=======================================================================
1480 //function : RemoveEdgeSameDomain
1481 //purpose  : 
1482 //=======================================================================
1483
1484 void BRepAlgo_DSAccess::RemoveEdgeSameDomain
1485 (const Standard_Integer /*iE1*/,
1486  const Standard_Integer /*iE2*/)
1487 {
1488   return;
1489 /*  TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1490   const TopoDS_Shape& E1 = DS.Shape(iE1);
1491   const TopoDS_Shape& E2 = DS.Shape(iE2);
1492   TopAbs_ShapeEnum ts1, ts2;
1493   ts1 = E1.ShapeType();
1494   ts2 = E2.ShapeType();
1495   if((ts1 != TopAbs_EDGE) ||
1496      (ts2 != TopAbs_EDGE)) 
1497     return;
1498   TopTools_ListOfShape& lossd = DS.ChangeShapeSameDomain(E1);
1499   if(lossd.IsEmpty())
1500     return;
1501   Standard_Integer exte = lossd.Extent();
1502   if(exte == 1) {
1503     if(lossd.First().IsSame(E2))
1504       DS.UnfillShapesSameDomain(E1,E2);
1505     return;
1506   }*/
1507 }
1508
1509 //=======================================================================
1510 //function : RemoveFaceSameDomain
1511 //purpose  : remove SameDomain information of glued faces 
1512 //=======================================================================
1513
1514 void BRepAlgo_DSAccess::RemoveFaceSameDomain
1515 (const TopoDS_Shape& C)
1516 {
1517 //  myHB->ChangeBuilder();
1518   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1519
1520 //TColStd_ListIteratorOfListOfInteger it;
1521   TopExp_Explorer exp(C, TopAbs_EDGE);
1522   Standard_Integer  iE1, iE2, iE, /*NbF,*/ iF1, iF2, iCurrF1, iCurrF2,   iC =0; // skl
1523   iF1 = iF2 = iCurrF1 = iCurrF2 = 0;
1524   Standard_Boolean b;
1525   const TopoDS_Shape& SectEdge = exp.Current();
1526   
1527   for(; exp.More(); exp.Next()) {
1528     iC = myHB->GetDSCurveFromSectEdge(SectEdge);
1529     if(!iC && !SectEdge.IsNull())
1530       break;
1531 //  const TopoDS_Shape& SectEdge = exp.Current();
1532   }
1533   if(!iC && !SectEdge.IsNull()) {
1534     iE1 = myHB->GetDSEdgeFromSectEdge(SectEdge, 1);
1535     iE2 = myHB->GetDSEdgeFromSectEdge(SectEdge, 2);
1536     if(iE1 && iE2) return;
1537     iE = (iE1 ? iE1 : iE2);
1538     if(!iE) return;
1539     
1540     TColStd_ListOfInteger& loi = FindGoodFace(iE, iF1, b);
1541     if(!b) return;
1542     if(exp.More())
1543       exp.Next();
1544     //NbF = loi.Extent(); // skl
1545     for(; exp.More(); exp.Next()) {
1546       // skl : I change "SectEdge" to "SectEdg"
1547       const TopoDS_Shape& SectEdg = exp.Current();
1548       iC = myHB->GetDSCurveFromSectEdge(SectEdg);
1549       if(!iC) {
1550         iE1 = myHB->GetDSEdgeFromSectEdge(SectEdg, 1);
1551         iE2 = myHB->GetDSEdgeFromSectEdge(SectEdg, 2);
1552         if(iE1 && iE2) return;
1553         iE = (iE1 ? iE1 : iE2);
1554         if(!iE) return;
1555         
1556         TColStd_ListOfInteger& loi2 = FindGoodFace(iE, iCurrF1, b);
1557         if(!b) return;
1558         if(!iCurrF1 || !iF1) return;
1559         if(iCurrF1 != iF1) {
1560           if(loi2.Extent() == 1) iCurrF2 = loi2.First();
1561           if(iCurrF2 == iF1) continue;
1562           if(loi.Extent() == 1) iF2 = loi.First();
1563
1564           if(!iCurrF2 || !iF2) return;
1565           if((iCurrF1 == iF2) ||
1566              (iCurrF2 == iF2)) {
1567             iF1 = iF2;
1568             continue;
1569           }
1570           return;
1571         } 
1572       }
1573     }
1574     
1575     const TopoDS_Shape& FSD = DS.Shape(iF1);
1576     if(FSD.IsNull()) 
1577       return;
1578     TopTools_ListOfShape& ssd = DS.ChangeShapeSameDomain(FSD);
1579     TopTools_ListIteratorOfListOfShape itssd(ssd);
1580     TopExp_Explorer exp2;
1581     for(; itssd.More(); itssd.Next()) {
1582       exp2.Init(itssd.Value(), TopAbs_VERTEX);
1583       for(; exp2.More(); exp2.Next()) {
1584         const TopoDS_Shape& exp2Curr = exp2.Current();
1585         exp.Init(C, TopAbs_VERTEX);
1586         for(; exp.More(); exp.Next()) {
1587           if(exp2Curr.IsSame(exp.Current()))
1588             break;
1589         }
1590         if(exp.More())
1591           break;
1592       }
1593       if(exp2.More())
1594         break;
1595     }
1596     
1597     if(exp2.More()) {
1598       const TopoDS_Shape& FSD2 = itssd.Value();
1599       Standard_Integer iFSD = DS.Shape(FSD), iFSD2 = DS.Shape(FSD2);
1600       RemoveFaceSameDomain(iFSD, iFSD2);      
1601 //      DS.UnfillShapesSameDomain(FSD, FSD2);
1602     }
1603   }
1604 }
1605
1606 //=======================================================================
1607 //function : FindGoodFace
1608 //purpose  : 
1609 //=======================================================================
1610
1611 TColStd_ListOfInteger& BRepAlgo_DSAccess::FindGoodFace
1612 (const Standard_Integer iE,
1613  Standard_Integer& iF1,
1614  Standard_Boolean& b)
1615 {
1616 //  myHB->ChangeBuilder();
1617   b = Standard_False;
1618   TColStd_ListOfInteger& loi = myHB->GetDSFaceFromDSEdge(iE, 1);
1619   if(loi.Extent() == 1) {
1620     iF1 = loi.First();
1621     b = Standard_True;
1622     TColStd_ListOfInteger& loi2 = myHB->GetDSFaceFromDSEdge(iE, 2);
1623     return loi2;
1624   }
1625   else {
1626     TColStd_ListOfInteger& loi2 = myHB->GetDSFaceFromDSEdge(iE, 2);
1627     if(loi2.Extent() == 1) {
1628       b = Standard_True;
1629       iF1 = loi2.First();
1630       return loi;
1631     }
1632   }
1633   b = Standard_False;
1634   return myEmptyListOfInteger;
1635 }
1636
1637 //=======================================================================
1638 //function : RemoveFaceSameDomain
1639 //purpose  : 
1640 //=======================================================================
1641
1642 void BRepAlgo_DSAccess::RemoveFaceSameDomain
1643 (const Standard_Integer iF1,
1644  const Standard_Integer iF2)
1645 {
1646   TopOpeBRepDS_DataStructure& DS = myHDS->ChangeDS();
1647   const TopoDS_Shape& F1 = DS.Shape(iF1, FindKeep);
1648   const TopoDS_Shape& F2 = DS.Shape(iF2, FindKeep);
1649   if(F1.IsNull() || F2.IsNull())
1650     return;
1651
1652
1653   Standard_Integer iref1 = DS.SameDomainRef(F1),
1654   iref2 = DS.SameDomainRef(F2), istart, iend;
1655   if(iref1 == iF1)
1656     DS.SameDomainRef(F2,iF2);
1657   if(iref2 == iF1)
1658     DS.SameDomainRef(F1,iF1);
1659   DS.UnfillShapesSameDomain(F1,F2);
1660
1661   if(iref1 != iref2)
1662     return;
1663   Standard_Boolean iF1iF2IsConnected = Standard_False;
1664   TColStd_IndexedMapOfInteger moi;
1665   moi.Clear();
1666   if(iref2 == iF2) {
1667     istart = iF2;
1668     iend = iF1;
1669   }
1670   else {
1671     istart = iF1;
1672     iend = iF2;
1673   }
1674   moi.Add(istart);
1675   Standard_Integer NbConnect = 0, icurr;
1676   while(moi.Extent() > NbConnect) {
1677     NbConnect++;
1678     icurr = moi.FindKey(NbConnect);
1679     DS.SameDomainRef(icurr, istart);
1680     const TopTools_ListOfShape& los = DS.ShapeSameDomain(icurr);
1681     if(los.IsEmpty()) {
1682       const TopoDS_Shape& SNSD = DS.Shape(icurr);
1683       DS.SameDomainRef(SNSD, 0);
1684     }
1685     TopTools_ListIteratorOfListOfShape li(los);
1686     for(; li.More(); li.Next()) {
1687       Standard_Integer iCurrShap = DS.Shape(li.Value(), FindKeep);
1688       if(!iCurrShap)
1689         return;
1690       if(iCurrShap == iend)
1691         iF1iF2IsConnected = Standard_True;
1692       moi.Add(iCurrShap);
1693     }
1694   }
1695   if(!iF1iF2IsConnected) {
1696     moi.Clear();
1697     moi.Add(iend);
1698     NbConnect = 0;
1699     while(moi.Extent() > NbConnect) {
1700       NbConnect++;
1701       icurr = moi.FindKey(NbConnect);
1702       DS.SameDomainRef(icurr, iend);
1703       const TopTools_ListOfShape& los = DS.ShapeSameDomain(icurr);
1704       if(los.IsEmpty()) {
1705         const TopoDS_Shape& SNSD = DS.Shape(icurr);
1706         DS.SameDomainRef(SNSD, 0);
1707       }
1708       TopTools_ListIteratorOfListOfShape li(los);
1709       for(; li.More(); li.Next()) {
1710         Standard_Integer iCurrShap = DS.Shape(li.Value(), FindKeep);
1711         if(!iCurrShap)
1712           return;
1713         moi.Add(iCurrShap);
1714       }
1715     }
1716   }
1717 }
1718
1719 //=======================================================================
1720 //function : DS
1721 //purpose  : 
1722 //=======================================================================
1723
1724 const Handle(TopOpeBRepDS_HDataStructure)&
1725 BRepAlgo_DSAccess::DS() const
1726 {
1727   return myHDS;
1728 }
1729
1730 //=======================================================================
1731 //function : changeDS
1732 //purpose  : 
1733 //=======================================================================
1734 Handle(TopOpeBRepDS_HDataStructure)&
1735 BRepAlgo_DSAccess::ChangeDS()
1736 {
1737   return myHDS;
1738 }
1739
1740 //=======================================================================
1741 //function : Builder
1742 //purpose  : 
1743 //=======================================================================
1744
1745 const Handle(TopOpeBRepBuild_HBuilder)& 
1746 BRepAlgo_DSAccess::Builder() const
1747 {
1748   return myHB;
1749 }
1750
1751 //=======================================================================
1752 //function : ChangeBuilder
1753 //purpose  : 
1754 //=======================================================================
1755
1756 Handle(TopOpeBRepBuild_HBuilder)& 
1757 BRepAlgo_DSAccess::ChangeBuilder()
1758 {
1759   return myHB;
1760 }