0024059: Eliminate compiler warning C4701 in MSVC++ with warning level 4
[occt.git] / src / BOPAlgo / BOPAlgo_PaveFiller_6.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2012 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 //                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 //
7 // The content of this file is subject to the Open CASCADE Technology Public
8 // License Version 6.5 (the "License"). You may not use the content of this file
9 // except in compliance with the License. Please obtain a copy of the License
10 // at http://www.opencascade.org and read it completely before using this file.
11 //
12 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
13 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 //
15 // The Original Code and all software distributed under the License is
16 // distributed on an "AS IS" basis, without warranty of any kind, and the
17 // Initial Developer hereby disclaims all such warranties, including without
18 // limitation, any warranties of merchantability, fitness for a particular
19 // purpose or non-infringement. Please see the License for the specific terms
20 // and conditions governing the rights and limitations under the License.
21
22
23 #include <BOPAlgo_PaveFiller.ixx>
24
25 #include <Precision.hxx>
26 #include <NCollection_IncAllocator.hxx>
27 #include <Bnd_Box.hxx>
28
29 #include <Geom_Curve.hxx>
30 #include <Geom2d_Curve.hxx>
31
32 #include <GeomAPI_ProjectPointOnCurve.hxx>
33 #include <GeomAPI_ProjectPointOnSurf.hxx>
34
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopoDS_Compound.hxx>
39
40 #include <TopExp_Explorer.hxx>
41
42 #include <BRep_Builder.hxx>
43 #include <BRep_Tool.hxx>
44
45 #include <BRepBndLib.hxx>
46 #include <BRepTools.hxx>
47
48 #include <BRepAdaptor_Curve.hxx>
49 #include <BRepAdaptor_Surface.hxx>
50
51 #include <IntTools_FaceFace.hxx>
52 #include <IntTools_SequenceOfCurves.hxx>
53 #include <IntTools_SequenceOfPntOn2Faces.hxx>
54 #include <IntTools_Curve.hxx>
55 #include <IntTools_PntOn2Faces.hxx>
56 #include <IntTools_Tools.hxx>
57
58 #include <IntSurf_ListOfPntOn2S.hxx>
59 #include <IntSurf_PntOn2S.hxx>
60
61 #include <BOPTools_AlgoTools.hxx>
62 #include <BOPTools_AlgoTools3D.hxx>
63
64 #include <BOPCol_MapOfInteger.hxx>
65 #include <BOPCol_ListOfShape.hxx>
66 #include <BOPCol_DataMapOfShapeInteger.hxx>
67 #include <BOPCol_ListOfInteger.hxx>
68 #include <BOPCol_IndexedMapOfInteger.hxx>
69
70 #include <BOPInt_Context.hxx>
71 #include <BOPInt_Tools.hxx>
72
73 #include <BOPDS_Interf.hxx>
74 #include <BOPDS_Iterator.hxx>
75 #include <BOPDS_Curve.hxx>
76 #include <BOPDS_Point.hxx>
77 #include <BOPDS_FaceInfo.hxx>
78 #include <BOPDS_Curve.hxx>
79 #include <BOPDS_MapOfPaveBlock.hxx>
80 #include <BOPDS_PaveBlock.hxx>
81 #include <BOPDS_VectorOfCurve.hxx>
82 #include <BOPDS_VectorOfPoint.hxx>
83 #include <BOPDS_ShapeInfo.hxx>
84 #include <BOPDS_PaveBlock.hxx>
85 #include <BOPDS_ListOfPave.hxx>
86 #include <BOPDS_ListOfPaveBlock.hxx>
87 #include <BOPDS_CoupleOfPaveBlocks.hxx>
88 #include <BOPDS_FaceInfo.hxx>
89 #include <BOPDS_CommonBlock.hxx>
90
91 #include <BOPAlgo_Tools.hxx>
92 #include <BRepBuilderAPI_MakeVertex.hxx>
93 #include <TopExp.hxx>
94 #include <BOPInt_ShrunkRange.hxx>
95 #include <BOPDS_DataMapOfPaveBlockListOfPaveBlock.hxx>
96
97 static void ToleranceFF(const TopoDS_Face& aF1,
98                         const TopoDS_Face& aF2,
99                         Standard_Real& aTolFF);
100
101 //=======================================================================
102 //function : PerformFF
103 //purpose  : 
104 //=======================================================================
105 void BOPAlgo_PaveFiller::PerformFF()
106 {
107   Standard_Integer iSize;
108   Standard_Boolean bValid;
109   //
110   myErrorStatus=0;
111   //
112   myIterator->Initialize(TopAbs_FACE, TopAbs_FACE);
113   iSize=myIterator->ExpectedLength();
114   if (!iSize) {
115     return; 
116   }
117   //
118   Standard_Boolean bJustAdd, bApp, bCompC2D1, bCompC2D2, bIsDone;
119   Standard_Boolean bToSplit, bTangentFaces;
120   Standard_Integer nF1, nF2, aNbCurves, aNbPoints, iX, i, iP, iC, aNbLP;
121   Standard_Real aApproxTol, aTolR3D, aTolR2D, aTolFF;
122   //
123   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
124   aFFs.SetStartSize(iSize);
125   aFFs.SetIncrement(iSize);
126   aFFs.Init();
127   //
128   bApp=mySectionAttribute.Approximation();
129   bCompC2D1=mySectionAttribute.PCurveOnS1();
130   bCompC2D2=mySectionAttribute.PCurveOnS2();
131   aApproxTol=1.e-7;
132   bToSplit = Standard_False;
133   //
134   for (; myIterator->More(); myIterator->Next()) {
135     myIterator->Value(nF1, nF2, bJustAdd);
136     if(bJustAdd) {
137       continue;
138     }
139     //
140     const TopoDS_Face& aF1=(*(TopoDS_Face *)(&myDS->Shape(nF1)));
141     const TopoDS_Face& aF2=(*(TopoDS_Face *)(&myDS->Shape(nF2)));
142     //
143     IntTools_FaceFace aFaceFace;
144     //
145     IntSurf_ListOfPntOn2S aListOfPnts;
146     GetEFPnts(nF1, nF2, aListOfPnts);
147     aNbLP = aListOfPnts.Extent();
148     if (aNbLP) {
149       aFaceFace.SetList(aListOfPnts);
150     }
151
152     aFaceFace.SetParameters(bApp, bCompC2D1, bCompC2D2, aApproxTol);
153     //
154     aFaceFace.Perform(aF1, aF2);
155     //
156     bIsDone=aFaceFace.IsDone();
157     if (bIsDone) {
158       aTolR3D=aFaceFace.TolReached3d();
159       aTolR2D=aFaceFace.TolReached2d();
160       bTangentFaces=aFaceFace.TangentFaces();
161       //
162       ToleranceFF(aF1, aF2, aTolFF);
163       //
164       if (aTolR3D < aTolFF){
165         aTolR3D=aTolFF;
166       }
167       if (aTolR2D < 1.e-7){
168         aTolR2D=1.e-7;
169       }
170       //
171       aFaceFace.PrepareLines3D(bToSplit);
172       //
173       const IntTools_SequenceOfCurves& aCvsX=aFaceFace.Lines();
174       const IntTools_SequenceOfPntOn2Faces& aPntsX=aFaceFace.Points();
175       //
176       aNbCurves=aCvsX.Length();
177       aNbPoints=aPntsX.Length();
178       //
179       myDS->AddInterf(nF1, nF2);
180       //
181       iX=aFFs.Append()-1;
182       BOPDS_InterfFF& aFF=aFFs(iX);
183       aFF.SetIndices(nF1, nF2);
184       //
185       aFF.SetTolR3D(aTolR3D);
186       aFF.SetTolR2D(aTolR2D);
187       aFF.SetTangentFaces(bTangentFaces);
188       //
189       // Curves, Points 
190       aFF.Init(aNbCurves, aNbPoints);
191       //
192       // Curves
193       BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
194       for (i=1; i<=aNbCurves; ++i) {
195         Bnd_Box aBox;
196         //
197         const IntTools_Curve& aIC=aCvsX(i);
198         const Handle(Geom_Curve)& aC3D= aIC.Curve();
199         bValid=BOPInt_Tools::CheckCurve(aC3D, aTolR3D, aBox);
200         if (bValid) {
201           iC=aVNC.Append()-1;
202           BOPDS_Curve& aNC=aVNC(iC);
203           aNC.SetCurve(aIC);
204           aNC.SetBox(aBox);
205         }
206       }
207       //
208       // Points
209       BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
210       for (i=1; i<=aNbPoints; ++i) {
211         const IntTools_PntOn2Faces& aPi=aPntsX(i);
212         const gp_Pnt& aP=aPi.P1().Pnt();
213         //
214         iP=aVNP.Append()-1;
215         BOPDS_Point& aNP=aVNP(iP);
216         aNP.SetPnt(aP);
217       }
218     //}// if (aNbCs || aNbPs)
219     }// if (bIsDone) {
220     else {// 904/L1
221       iX=aFFs.Append()-1;
222       BOPDS_InterfFF& aFF=aFFs(iX);
223       aFF.SetIndices(nF1, nF2);
224       aNbCurves=0;
225       aNbPoints=0;
226       aFF.Init(aNbCurves, aNbPoints);
227     }
228   }// for (; myIterator->More(); myIterator->Next()) {
229 }
230 //=======================================================================
231 //function : MakeBlocks
232 //purpose  : 
233 //=======================================================================
234
235   void BOPAlgo_PaveFiller::MakeBlocks()
236 {
237   Standard_Integer aNbFF;
238   //
239   myErrorStatus=0;
240   //
241   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
242   aNbFF=aFFs.Extent();
243   if (!aNbFF) {
244     return;
245   }
246   //
247   Standard_Boolean bExist, bValid2D;
248   Standard_Integer i, nF1, nF2, aNbC, aNbP, j;
249   Standard_Integer nV1, nV2;
250   Standard_Real aTolR3D, aTolR2D, aT1, aT2;
251   Handle(NCollection_IncAllocator) aAllocator;
252   BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
253   TopoDS_Edge aES;
254   Handle(BOPDS_PaveBlock) aPBOut;
255   //
256   //-----------------------------------------------------scope f
257   aAllocator=new NCollection_IncAllocator();
258   //
259   BOPCol_ListOfInteger aLSE(aAllocator);
260   BOPCol_MapOfInteger aMVOnIn(100, aAllocator), aMF(100, aAllocator),
261                       aMVStick(100,aAllocator), aMVEF(100, aAllocator),
262                       aMVB(100, aAllocator);
263   BOPDS_MapOfPaveBlock aMPBOnIn(100, aAllocator),
264                        aMPBAdd(100, aAllocator);
265   BOPDS_ListOfPaveBlock aLPB(aAllocator);
266   BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks aMSCPB(100, aAllocator); 
267   BOPCol_DataMapOfShapeInteger aMVI(100, aAllocator);
268   BOPDS_DataMapOfPaveBlockListOfPaveBlock aDMExEdges;
269   //
270   for (i=0; i<aNbFF; ++i) {
271     BOPDS_InterfFF& aFF=aFFs(i);
272     aFF.Indices(nF1, nF2);
273     //
274     BOPDS_VectorOfPoint& aVP=aFF.ChangePoints();
275     aNbP=aVP.Extent();
276     BOPDS_VectorOfCurve& aVC=aFF.ChangeCurves();
277     aNbC=aVC.Extent();
278     if (!aNbP && !aNbC) {
279       continue;
280     }
281     //
282     const TopoDS_Face& aF1=(*(TopoDS_Face *)(&myDS->Shape(nF1)));
283     const TopoDS_Face& aF2=(*(TopoDS_Face *)(&myDS->Shape(nF2)));
284     //
285     aTolR3D=aFF.TolR3D();
286     aTolR2D=aFF.TolR2D();
287     //
288     // Update face info
289     if (aMF.Add(nF1)) {
290       myDS->UpdateFaceInfoOn(nF1);
291     }
292     if (aMF.Add(nF2)) {
293       myDS->UpdateFaceInfoOn(nF2);
294     }
295    
296     BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
297     BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
298     //
299     aMVOnIn.Clear();
300     aMPBOnIn.Clear();
301     aMVB.Clear();
302     //
303     myDS->VerticesOnIn(nF1, nF2, aMVOnIn, aMPBOnIn);
304     myDS->SharedEdges(nF1, nF2, aLSE, aAllocator);
305     
306     // 1. Treat Points
307     for (j=0; j<aNbP; ++j) {
308       TopoDS_Vertex aV;
309       BOPDS_CoupleOfPaveBlocks aCPB;
310       //
311       BOPDS_Point& aNP=aVP.ChangeValue(j);
312       const gp_Pnt& aP=aNP.Pnt();
313       //
314       bExist=IsExistingVertex(aP, aTolR3D, aMVOnIn);
315       if (!bExist) {
316         BOPTools_AlgoTools::MakeNewVertex(aP, aTolR3D, aV);
317         //
318         aCPB.SetIndexInterf(i);
319         aCPB.SetIndex(j);
320         aMSCPB.Add(aV, aCPB);
321       }
322     }
323     //
324     // 2. Treat Curves
325     aMVStick.Clear();
326     aMVEF.Clear();
327     GetStickVertices(nF1, nF2, aMVStick, aMVEF);
328     //
329     for (j=0; j<aNbC; ++j) {
330       BOPDS_Curve& aNC=aVC.ChangeValue(j);
331       const IntTools_Curve& aIC=aNC.Curve();
332       // DEBt
333       aNC.InitPaveBlock1();
334       //
335       PutPaveOnCurve(aMVOnIn, aTolR3D, aNC, nF1, nF2, aMVEF);
336       //
337       PutStickPavesOnCurve(nF1, nF2, aNC, aMVStick);
338       //904/F7
339       if (aNbC == 1) {
340         PutEFPavesOnCurve(nF1, nF2, aNC, aMVEF);
341       }
342       //
343       if (aIC.HasBounds()) {
344         PutBoundPaveOnCurve(aF1, aF2, aTolR3D, aNC, aMVOnIn, aMVB);
345       }
346     }//for (j=0; j<aNbC; ++j) {
347     //
348     // Put closing pave if needed
349     for (j=0; j<aNbC; ++j) {
350       BOPDS_Curve& aNC=aVC.ChangeValue(j);
351       PutClosingPaveOnCurve (aNC);
352     }
353     //
354     // 3. Make section edges
355     for (j=0; j<aNbC; ++j) {
356       BOPDS_Curve& aNC=aVC.ChangeValue(j);
357       const IntTools_Curve& aIC=aNC.Curve();
358       //
359       BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
360       Handle(BOPDS_PaveBlock)& aPB1=aNC.ChangePaveBlock1();
361       //
362       aLPB.Clear();
363       aPB1->Update(aLPB, Standard_False);
364       //
365       aItLPB.Initialize(aLPB);
366       for (; aItLPB.More(); aItLPB.Next()) {
367         Handle(BOPDS_PaveBlock)& aPB=aItLPB.ChangeValue();
368         aPB->Indices(nV1, nV2);
369         aPB->Range  (aT1, aT2);
370         //
371         if (fabs(aT1 - aT2) < Precision::PConfusion()) {
372           continue;
373         }
374         //
375         bValid2D=myContext->IsValidBlockForFaces(aT1, aT2, aIC, aF1, aF2, aTolR3D);
376         if (!bValid2D) {
377           continue;
378         }
379         //
380         bExist=IsExistingPaveBlock(aPB, aNC, aTolR3D, aLSE);
381         if (bExist) {
382           continue;
383         }
384         //
385         bExist=IsExistingPaveBlock(aPB, aNC, aTolR3D, aMPBOnIn, aPBOut);
386         if (bExist) {
387           if (aMPBAdd.Add(aPBOut)) {
388             Standard_Boolean bInBothFaces = Standard_True;
389             if (!myDS->IsCommonBlock(aPBOut)) {
390               Standard_Integer nE;
391               Standard_Real aTolE;
392               //
393               nE = aPBOut->Edge();
394               const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(nE);
395               aTolE = BRep_Tool::Tolerance(aE);
396               if (aTolR3D > aTolE) {
397                 myDS->UpdateEdgeTolerance(nE, aTolR3D);
398               }
399               bInBothFaces = Standard_False;
400             } else {
401               bInBothFaces = (aFI1.PaveBlocksOn().Contains(aPBOut) ||
402                               aFI1.PaveBlocksIn().Contains(aPBOut))&&
403                              (aFI2.PaveBlocksOn().Contains(aPBOut) ||
404                               aFI2.PaveBlocksIn().Contains(aPBOut));
405             }
406             if (!bInBothFaces) {
407               PreparePostTreatFF(i, aPBOut, aMSCPB, aMVI, aVC);
408             }
409           }
410           continue;
411         }
412         //
413         // Make Edge
414         const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
415         const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
416         //
417         BOPTools_AlgoTools::MakeEdge (aIC, aV1, aT1, aV2, aT2, aTolR3D, aES);
418         BOPTools_AlgoTools::MakePCurve(aES, aF1, aF2, aIC, 
419                                        mySectionAttribute.PCurveOnS1(),
420                                        mySectionAttribute.PCurveOnS2());
421         //
422         if (BOPTools_AlgoTools::IsMicroEdge(aES, myContext)) {
423           continue;
424         }
425         //
426         // Append the Pave Block to the Curve j
427         aLPBC.Append(aPB);
428         //
429         // Keep info for post treatment 
430         BOPDS_CoupleOfPaveBlocks aCPB;
431         aCPB.SetIndexInterf(i);
432         aCPB.SetIndex(j);
433         aCPB.SetPaveBlock1(aPB);
434         //
435         aMSCPB.Add(aES, aCPB);
436         aMVI.Bind(aV1, nV1);
437         aMVI.Bind(aV2, nV2);
438       }
439       //
440       aLPBC.RemoveFirst();
441     }//for (j=0; j<aNbC; ++j) {
442     ProcessExistingPaveBlocks(i, aMPBOnIn, aMSCPB, aMVI, aMVB, aMPBAdd);
443   }//for (i=0; i<aNbFF; ++i) {
444   // 
445   // post treatment
446   myErrorStatus=PostTreatFF(aMSCPB, aMVI, aDMExEdges, aAllocator);
447   if (myErrorStatus) {
448     return;
449   }
450   //
451   // update face info
452   UpdateFaceInfo(aDMExEdges);
453   //-----------------------------------------------------scope t
454   aMF.Clear();
455   aMVStick.Clear();
456   aMPBOnIn.Clear();
457   aMVOnIn.Clear();
458   aDMExEdges.Clear();
459   aAllocator.Nullify();
460 }
461
462 //=======================================================================
463 //function : PostTreatFF
464 //purpose  : 
465 //=======================================================================
466   Standard_Integer BOPAlgo_PaveFiller::PostTreatFF
467     (BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& theMSCPB,
468      BOPCol_DataMapOfShapeInteger& aMVI,
469      BOPDS_DataMapOfPaveBlockListOfPaveBlock& aDMExEdges,
470      Handle(NCollection_BaseAllocator)& theAllocator)
471 {
472   Standard_Integer iRet, aNbS;
473   //
474   iRet=0;
475   aNbS=theMSCPB.Extent();
476   if (!aNbS) {
477     return iRet;
478   }
479   //
480   Standard_Boolean bHasPaveBlocks, bOld;
481   Standard_Integer iErr, nSx, nVSD, iX, iP, iC, j, nV, iV = 0, iE, k;
482   Standard_Integer jx;
483   Standard_Real aT;
484   Standard_Integer aNbLPBx;
485   TopAbs_ShapeEnum aType;
486   TopoDS_Shape aV, aE;
487   BOPCol_ListIteratorOfListOfShape aItLS;
488   BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
489   BOPDS_PDS aPDS;
490   Handle(BOPDS_PaveBlock) aPB1;
491   BOPDS_Pave aPave[2], aPave1[2];
492   BOPDS_ShapeInfo aSI;
493   //
494   BOPCol_ListOfShape aLS(theAllocator);
495   BOPAlgo_PaveFiller aPF(theAllocator);
496   //
497   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
498   //
499   // <-DEB f
500   //
501   // 0
502   if (aNbS==1) {
503     const TopoDS_Shape& aS=theMSCPB.FindKey(1);
504     const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromIndex(1);
505     
506     //
507     aType=aS.ShapeType();
508     if (aType==TopAbs_VERTEX) {
509       aSI.SetShapeType(aType);
510       aSI.SetShape(aS);
511       iV=myDS->Append(aSI);
512       //
513       iX=aCPB.IndexInterf();
514       iP=aCPB.Index();
515       BOPDS_InterfFF& aFF=aFFs(iX); 
516       BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
517       BOPDS_Point& aNP=aVNP(iP);
518       aNP.SetIndex(iV);
519     }
520     else if (aType==TopAbs_EDGE) {
521       aPB1=aCPB.PaveBlock1();
522       //
523       if (aPB1->HasEdge()) {
524         BOPDS_ListOfPaveBlock aLPBx;
525         aLPBx.Append(aPB1);
526         aDMExEdges.Bind(aPB1, aLPBx);
527       } else {
528         aSI.SetShapeType(aType);
529         aSI.SetShape(aS);
530         iE=myDS->Append(aSI);
531         //
532         aPB1->SetEdge(iE);
533       }
534     }
535     return iRet;
536   }
537   //
538   // 1 prepare arguments
539   for (k=1; k<=aNbS; ++k) {
540     const TopoDS_Shape& aS=theMSCPB.FindKey(k);
541     aLS.Append(aS);
542   }
543   //
544   // 2 Fuse shapes
545   aPF.SetArguments(aLS);
546   aPF.Perform();
547   iErr=aPF.ErrorStatus();
548   if (iErr) {
549     iRet=1;
550     return iRet;
551   }
552   aPDS=aPF.PDS();
553   //
554   aItLS.Initialize(aLS);
555   for (; aItLS.More(); aItLS.Next()) {
556     const TopoDS_Shape& aSx=aItLS.Value();
557     nSx=aPDS->Index(aSx);
558     const BOPDS_ShapeInfo& aSIx=aPDS->ShapeInfo(nSx);
559     //
560     aType=aSIx.ShapeType();
561     //
562     if (aType==TopAbs_VERTEX) {
563       if (aPDS->HasShapeSD(nSx, nVSD)) {
564         aV=aPDS->Shape(nVSD);
565       }
566       else {
567         aV=aSx;
568       }
569       // index of new vertex in theDS -> iV
570       if (!aMVI.IsBound(aV)) {
571         aSI.SetShapeType(aType);
572         aSI.SetShape(aV);
573         iV=myDS->Append(aSI);
574         //
575         aMVI.Bind(aV, iV);
576       }
577       else {
578         iV=aMVI.Find(aV);
579       }
580       // update FF interference
581       const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromKey(aSx);
582       iX=aCPB.IndexInterf();
583       iP=aCPB.Index();
584       BOPDS_InterfFF& aFF=aFFs(iX);
585       BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
586       BOPDS_Point& aNP=aVNP(iP);
587       aNP.SetIndex(iV);
588     }//if (aType==TopAbs_VERTEX) {
589     //
590     else if (aType==TopAbs_EDGE) {
591       bHasPaveBlocks=aPDS->HasPaveBlocks(nSx);
592       const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromKey(aSx);
593       iX=aCPB.IndexInterf();
594       iC=aCPB.Index();
595       aPB1=aCPB.PaveBlock1();
596       //
597       bOld = aPB1->HasEdge();
598       if (bOld) {
599         BOPDS_ListOfPaveBlock aLPBx;
600         aDMExEdges.Bind(aPB1, aLPBx);
601       }
602       //
603       if (!bHasPaveBlocks) {
604         if (bOld) {
605           aDMExEdges.ChangeFind(aPB1).Append(aPB1);
606         } else {
607           aSI.SetShapeType(aType);
608           aSI.SetShape(aSx);
609           iE=myDS->Append(aSI);
610           //
611           aPB1->SetEdge(iE);
612         }
613       }
614       else {
615         BOPDS_InterfFF& aFF=aFFs(iX);
616         BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
617         BOPDS_Curve& aNC=aVNC(iC);
618         BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
619         //
620         const BOPDS_ListOfPaveBlock& aLPBx=aPDS->PaveBlocks(nSx);
621         aNbLPBx=aLPBx.Extent();
622         //
623         if (bOld && !aNbLPBx) {
624           aDMExEdges.ChangeFind(aPB1).Append(aPB1);
625           continue;
626         }
627         //
628         if (!bOld) {
629           aItLPB.Initialize(aLPBC);
630           for (; aItLPB.More(); aItLPB.Next()) {
631             const Handle(BOPDS_PaveBlock)& aPBC=aItLPB.Value();
632             if (aPBC==aPB1) {
633               aLPBC.Remove(aItLPB);
634               break;
635             }
636           } 
637         }
638         //
639         if (!aNbLPBx) {
640           aE=aSx;
641           //
642           if (!aMVI.IsBound(aE)) {
643             aSI.SetShapeType(aType);
644             aSI.SetShape(aE);
645             iE=myDS->Append(aSI);
646             aMVI.Bind(aE, iE);
647           }
648           else {
649             iE=aMVI.Find(aE);
650           }
651           // append new PaveBlock to aLPBC
652           Handle(BOPDS_PaveBlock) aPBC=new BOPDS_PaveBlock();
653           //
654           aPB1->SetEdge(iE);
655           aLPBC.Append(aPB1);
656         } // if (!aNbLPBx) {
657         //
658         else {
659           aItLPB.Initialize(aLPBx);
660           if (bOld) {
661             aPave1[0] = aPB1->Pave1();
662             aPave1[1] = aPB1->Pave2();
663           }
664           for (; aItLPB.More(); aItLPB.Next()) {
665             const Handle(BOPDS_PaveBlock)& aPBx=aItLPB.Value();
666             const Handle(BOPDS_PaveBlock) aPBRx=aPDS->RealPaveBlock(aPBx);
667             //
668             // update vertices of paves
669             aPave[0]=aPBx->Pave1();
670             aPave[1]=aPBx->Pave2();
671             for (j=0; j<2; ++j) {
672               jx = 0;
673               if (bOld) {
674                 aT = aPave[j].Parameter();
675                 if (aT == aPave1[0].Parameter()) {
676                   jx = 1;
677                 } else if (aT == aPave1[1].Parameter()) {
678                   jx = 2;
679                 }
680                 //
681                 if (jx) {
682                   iV = aPave1[jx-1].Index();
683                 }
684               } 
685               if (!jx) {
686                 nV=aPave[j].Index();
687                 aV=aPDS->Shape(nV);
688                 //
689                 if (!aMVI.IsBound(aV)) {// index of new vertex in theDS -> iV
690                   aSI.SetShapeType(TopAbs_VERTEX);
691                   aSI.SetShape(aV);
692                   iV=myDS->Append(aSI);
693                   aMVI.Bind(aV, iV);
694                 }
695                 else {
696                   iV=aMVI.Find(aV);
697                 }
698               }
699               aPave[j].SetIndex(iV);
700             }
701             //
702             // add edge
703             aE=aPDS->Shape(aPBRx->Edge());
704             //
705             if (!aMVI.IsBound(aE)) {
706               aSI.SetShapeType(aType);
707               aSI.SetShape(aE);
708               iE=myDS->Append(aSI);
709               aMVI.Bind(aE, iE);
710             }
711             else {
712               iE=aMVI.Find(aE);
713             }
714             // append new PaveBlock to aLPBC
715             Handle(BOPDS_PaveBlock) aPBC=new BOPDS_PaveBlock();
716             //
717             aPBC->SetPave1(aPave[0]);
718             aPBC->SetPave2(aPave[1]);
719             aPBC->SetEdge(iE);
720             if (bOld) {
721               aPBC->SetOriginalEdge(aPB1->OriginalEdge());
722               aDMExEdges.ChangeFind(aPB1).Append(aPBC);
723             }
724             else {
725               aLPBC.Append(aPBC);
726             }
727           }
728         }
729       }
730     }//else if (aType==TopAbs_EDGE)
731   }//for (; aItLS.More(); aItLS.Next()) {
732   return iRet;
733 }
734
735 //=======================================================================
736 //function : UpdateFaceInfo
737 //purpose  : 
738 //=======================================================================
739   void BOPAlgo_PaveFiller::UpdateFaceInfo(
740        BOPDS_DataMapOfPaveBlockListOfPaveBlock& theDME)
741 {
742   Standard_Integer i, j, nV1, nF1, nF2, 
743                    aNbFF, aNbC, aNbP, aNbS, aNbPBIn;
744   BOPDS_IndexedMapOfPaveBlock aMPBCopy;
745   BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
746   //
747   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
748   aNbFF=aFFs.Extent();
749   //
750   //1. Sections (curves, points);
751   for (i=0; i<aNbFF; ++i) {
752     BOPDS_InterfFF& aFF=aFFs(i);
753     aFF.Indices(nF1, nF2);
754     //
755     BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
756     BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
757     //
758     BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
759     aNbC=aVNC.Extent();
760     for (j=0; j<aNbC; ++j) {
761       BOPDS_Curve& aNC=aVNC(j);
762       BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
763       aItLPB.Initialize(aLPBC);
764       //
765       if (aItLPB.More() && theDME.IsBound(aLPBC.First())) {
766         const Handle(BOPDS_PaveBlock)& aPB=aLPBC.First();
767         BOPDS_ListOfPaveBlock& aLPB = theDME.ChangeFind(aPB);
768         UpdateExistingPaveBlocks(aPB, aLPB, nF1, nF2);
769         aLPBC.Clear();
770         continue;
771       }
772       //
773       for(; aItLPB.More(); aItLPB.Next()) {
774         const Handle(BOPDS_PaveBlock)& aPB=aItLPB.Value();
775         aFI1.ChangePaveBlocksSc().Add(aPB);
776         aFI2.ChangePaveBlocksSc().Add(aPB);
777       }
778     }
779     // VerticesSc
780     const BOPDS_VectorOfPoint& aVNP=aFF.Points();
781     aNbP=aVNP.Extent();
782     for (j=0; j<aNbP; ++j) {
783       const BOPDS_Point& aNP=aVNP(j);
784       nV1=aNP.Index();
785       aFI1.ChangeVerticesSc().Add(nV1);
786       aFI2.ChangeVerticesSc().Add(nV1);
787     }
788   }
789   //
790   //2. PaveBlocksIn
791   if (theDME.IsEmpty()) {
792     return;
793   }
794   //
795   aNbS=myDS->NbSourceShapes();
796   for (i=0; i<aNbS; ++i) {
797     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
798     if (aSI.ShapeType()!=TopAbs_FACE) {
799       continue;
800     }
801     if(!myDS->HasFaceInfo(i)) {
802       continue;
803     }
804     BOPDS_FaceInfo& aFI=myDS->ChangeFaceInfo(i);
805     //
806     BOPDS_IndexedMapOfPaveBlock& aMPBIn=aFI.ChangePaveBlocksIn();
807     aMPBCopy.Assign(aMPBIn);
808     aMPBIn.Clear();
809     //
810     aNbPBIn=aMPBCopy.Extent();
811     for (j=1; j<=aNbPBIn; ++j) {
812       const Handle(BOPDS_PaveBlock)& aPB = aMPBCopy(j);
813       if (theDME.IsBound(aPB)) {
814         const BOPDS_ListOfPaveBlock& aLPB = theDME.Find(aPB);
815         aItLPB.Initialize(aLPB);
816         for (; aItLPB.More(); aItLPB.Next()) {
817           const Handle(BOPDS_PaveBlock)& aPB1 = aItLPB.Value();
818           aMPBIn.Add(aPB1);
819         }
820       } else {
821         aMPBIn.Add(aPB);
822       }
823     }//for (j=1; j<=aNbPBIn; ++j) {
824   }//for (i=0; i<aNbS; ++i) {
825 }
826
827 //=======================================================================
828 //function : IsExistingVertex
829 //purpose  : 
830 //=======================================================================
831   Standard_Boolean BOPAlgo_PaveFiller::IsExistingVertex
832     (const gp_Pnt& aP,
833      const Standard_Real theTolR3D,
834      const BOPCol_MapOfInteger& aMVOnIn)const
835 {
836   Standard_Boolean bRet;
837   Standard_Integer nV, iFlag;
838   Standard_Real aTolV;
839   gp_Pnt aPV;
840   Bnd_Box aBoxP;
841   BOPCol_MapIteratorOfMapOfInteger aIt;
842   //
843   bRet=Standard_True;
844   //
845   aBoxP.Add(aP);
846   aBoxP.Enlarge(theTolR3D);
847   //
848   aIt.Initialize(aMVOnIn);
849   for (; aIt.More(); aIt.Next()) {
850     Bnd_Box aBoxV;
851     //
852     nV=aIt.Value();
853     const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&myDS->Shape(nV)));
854     aPV=BRep_Tool::Pnt(aV);
855     aTolV=BRep_Tool::Tolerance(aV);
856     aBoxV.Add(aP);
857     aBoxV.Enlarge(aTolV);
858     //
859     if (!aBoxP.IsOut(aBoxV)) {
860       iFlag=BOPTools_AlgoTools::ComputeVV(aV, aP, theTolR3D);
861       if (!iFlag) {
862         return bRet;
863       }
864     }
865   }
866   return !bRet;
867 }
868 //=======================================================================
869 //function : IsExistingPaveBlock
870 //purpose  : 
871 //=======================================================================
872   Standard_Boolean BOPAlgo_PaveFiller::IsExistingPaveBlock
873     (const Handle(BOPDS_PaveBlock)& thePB,
874      const BOPDS_Curve& theNC,
875      const Standard_Real theTolR3D,
876      const BOPCol_ListOfInteger& theLSE)
877 {
878   Standard_Boolean bRet=Standard_True;
879   //
880   if (theLSE.IsEmpty()) {
881     return !bRet;
882   } 
883   //
884   Standard_Real aT1, aT2, aTm, aTx, aTol;
885   Standard_Integer nE, iFlag;
886   gp_Pnt aPm;
887   Bnd_Box aBoxPm;
888   BOPCol_ListIteratorOfListOfInteger aItLI;
889   //
890   thePB->Range(aT1, aT2);
891   aTm=IntTools_Tools::IntermediatePoint (aT1, aT2);
892   theNC.Curve().D0(aTm, aPm);
893   aBoxPm.Add(aPm);
894   aBoxPm.Enlarge(theTolR3D);
895   //
896   aItLI.Initialize(theLSE);
897   for (; aItLI.More(); aItLI.Next()) {
898     nE=aItLI.Value();
899     const BOPDS_ShapeInfo& aSIE=myDS->ChangeShapeInfo(nE);
900     const Bnd_Box& aBoxE=aSIE.Box();
901     if (!aBoxE.IsOut(aBoxPm)) {
902       const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&aSIE.Shape()));
903       aTol = BRep_Tool::Tolerance(aE);
904       aTol = aTol > theTolR3D ? aTol : theTolR3D;
905       iFlag=myContext->ComputePE(aPm, aTol, aE, aTx);
906       if (!iFlag) {
907         return bRet;
908       }
909     }
910   }
911   return !bRet;
912 }
913
914 //=======================================================================
915 //function : IsExistingPaveBlock
916 //purpose  : 
917 //=======================================================================
918   Standard_Boolean BOPAlgo_PaveFiller::IsExistingPaveBlock
919     (const Handle(BOPDS_PaveBlock)& thePB,
920      const BOPDS_Curve& theNC,
921      const Standard_Real theTolR3D,
922      const BOPDS_MapOfPaveBlock& theMPBOnIn,
923      Handle(BOPDS_PaveBlock&) aPBOut)
924 {
925   Standard_Boolean bRet;
926   Standard_Real aT1, aT2, aTm, aTx;
927   Standard_Integer nSp, iFlag, nV11, nV12, nV21, nV22;
928   gp_Pnt aP1, aPm, aP2;
929   Bnd_Box aBoxP1, aBoxPm, aBoxP2;
930   BOPDS_MapIteratorOfMapOfPaveBlock aIt;
931   //
932   bRet=Standard_True;
933   const IntTools_Curve& aIC=theNC.Curve();
934   //
935   thePB->Range(aT1, aT2);
936   thePB->Indices(nV11, nV12);
937   //first point
938   aIC.D0(aT1, aP1);
939   aBoxP1.Add(aP1);
940   aBoxP1.Enlarge(theTolR3D);
941   //intermediate point
942   aTm=IntTools_Tools::IntermediatePoint (aT1, aT2);
943   aIC.D0(aTm, aPm);
944   aBoxPm.Add(aPm);
945   aBoxPm.Enlarge(theTolR3D);
946   //last point
947   aIC.D0(aT2, aP2);
948   aBoxP2.Add(aP2);
949   aBoxP2.Enlarge(theTolR3D);
950   //
951   aIt.Initialize(theMPBOnIn);
952   for (; aIt.More(); aIt.Next()) {
953     const Handle(BOPDS_PaveBlock)& aPB=aIt.Value();
954     aPB->Indices(nV21, nV22);
955     nSp=aPB->Edge();
956     const BOPDS_ShapeInfo& aSISp=myDS->ChangeShapeInfo(nSp);
957     const Bnd_Box& aBoxSp=aSISp.Box();
958     if (!aBoxSp.IsOut(aBoxP1) && !aBoxSp.IsOut(aBoxPm) && !aBoxSp.IsOut(aBoxP2)) {
959       const TopoDS_Edge& aSp=(*(TopoDS_Edge *)(&aSISp.Shape()));
960       iFlag=(nV11 == nV21 || nV11 == nV22) ? 0 : 
961                      myContext->ComputePE(aP1, theTolR3D, aSp, aTx);
962       if (!iFlag) {
963         iFlag=(nV12 == nV21 || nV12 == nV22) ? 0 :
964                        myContext->ComputePE(aP2, theTolR3D, aSp, aTx);
965         if (!iFlag) {
966           iFlag=myContext->ComputePE(aPm, theTolR3D, aSp, aTx);
967           if (!iFlag) {
968             aPBOut = aPB;
969             return bRet;
970           }
971         }
972       }
973     }
974   }
975   return !bRet;
976 }
977
978 //=======================================================================
979 //function : PutBoundPaveOnCurve
980 //purpose  : 
981 //=======================================================================
982   void BOPAlgo_PaveFiller::PutBoundPaveOnCurve(const TopoDS_Face& aF1,
983                                                const TopoDS_Face& aF2,
984                                                const Standard_Real aTolR3D,
985                                                BOPDS_Curve& aNC,
986                                                BOPCol_MapOfInteger& aMVOnIn,
987                                                BOPCol_MapOfInteger& aMVB)
988 {
989   Standard_Boolean bVF;
990   Standard_Integer nV, iFlag, nVn, j, aNbEP;
991   Standard_Real aT[2], aTmin, aTmax, aTV, aTol, aTolVnew;
992   gp_Pnt aP[2];
993   TopoDS_Vertex aVn;
994   BOPDS_ListIteratorOfListOfPave aItLP;
995   BOPDS_Pave aPn, aPMM[2];
996   //
997   aTolVnew = Precision::Confusion();
998   //
999   const IntTools_Curve& aIC=aNC.Curve();
1000   aIC.Bounds(aT[0], aT[1], aP[0], aP[1]);
1001   //
1002   Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1003   const BOPDS_ListOfPave& aLP=aPB->ExtPaves();
1004   //
1005   aNbEP=aLP.Extent();
1006   if (aNbEP) {
1007     aTmin=1.e10;
1008     aTmax=-aTmin;
1009     //
1010     aItLP.Initialize(aLP);
1011     for (; aItLP.More(); aItLP.Next()) {
1012       const BOPDS_Pave& aPv=aItLP.Value();
1013       aPv.Contents(nV, aTV);
1014       if (aTV<aTmin) {
1015         aPMM[0]=aPv;
1016         aTmin=aTV;
1017       }
1018       if (aTV>aTmax) {
1019         aPMM[1]=aPv;
1020         aTmax=aTV;
1021       }
1022     }
1023   }
1024   //
1025   for (j=0; j<2; ++j) {
1026     //if curve is closed, process only one bound
1027     if (j && aP[1].IsEqual(aP[0], aTolVnew)) {
1028       continue;
1029     }
1030     //
1031     iFlag=1;
1032     //
1033     if (aNbEP) {
1034       Bnd_Box aBoxP;
1035       //
1036       aBoxP.Set(aP[j]);
1037       aTol = aTolR3D+Precision::Confusion();
1038       aBoxP.Enlarge(aTol);
1039       const BOPDS_Pave& aPV=aPMM[j];
1040       nV=aPV.Index();
1041       const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
1042       const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&aSIV.Shape()));
1043       const Bnd_Box& aBoxV=aSIV.Box();
1044       if (!aBoxP.IsOut(aBoxV)){
1045         iFlag=BOPTools_AlgoTools::ComputeVV(aV, aP[j], aTol);
1046       }
1047     }
1048     if (iFlag) {
1049       // 900/L5
1050       bVF=myContext->IsValidPointForFaces (aP[j], aF1, aF2, aTolR3D);
1051       if (!bVF) {
1052         continue;
1053       }
1054       //
1055       BOPDS_ShapeInfo aSIVn;
1056       //
1057       BOPTools_AlgoTools::MakeNewVertex(aP[j], aTolR3D, aVn);
1058       aSIVn.SetShapeType(TopAbs_VERTEX);
1059       aSIVn.SetShape(aVn);
1060       //
1061       nVn=myDS->Append(aSIVn);
1062       //
1063       aPn.SetIndex(nVn);
1064       aPn.SetParameter(aT[j]);
1065       aPB->AppendExtPave(aPn);
1066       //
1067       aVn=(*(TopoDS_Vertex *)(&myDS->Shape(nVn)));
1068       BOPTools_AlgoTools::UpdateVertex (aIC, aT[j], aVn);
1069       //
1070       aTolVnew = BRep_Tool::Tolerance(aVn);
1071       //
1072       BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nVn);
1073       Bnd_Box& aBoxDS=aSIDS.ChangeBox();
1074       BRepBndLib::Add(aVn, aBoxDS);
1075       aMVOnIn.Add(nVn);
1076       aMVB.Add(nVn);
1077     }
1078   }
1079 }
1080
1081 //=======================================================================
1082 //function : PutPaveOnCurve
1083 //purpose  : 
1084 //=======================================================================
1085   void BOPAlgo_PaveFiller::PutPaveOnCurve(const BOPCol_MapOfInteger& aMVOnIn,
1086                                           const Standard_Real aTolR3D,
1087                                           BOPDS_Curve& aNC,
1088                                           const Standard_Integer nF1,
1089                                           const Standard_Integer nF2,
1090                                           const BOPCol_MapOfInteger& aMVEF)
1091 {
1092   Standard_Boolean bIsVertexOnLine, bInBothFaces;
1093   Standard_Integer nV;
1094   Standard_Real aT;
1095   BOPDS_Pave aPave;
1096   //
1097   BOPCol_MapIteratorOfMapOfInteger aIt;
1098   //
1099   Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1100   const IntTools_Curve& aIC=aNC.Curve();
1101   const Bnd_Box& aBoxC=aNC.Box();
1102   //
1103   aIt.Initialize(aMVOnIn);
1104   for (; aIt.More(); aIt.Next()) {
1105     nV=aIt.Value();
1106     const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
1107     const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&aSIV.Shape()));
1108     //
1109     if (!aMVEF.Contains(nV)) {
1110       const Bnd_Box& aBoxV=aSIV.Box();
1111       //
1112       if (aBoxC.IsOut(aBoxV)){
1113         continue; 
1114       }
1115       if (!myDS->IsNewShape(nV)) {
1116         const BOPDS_FaceInfo& aFI1 = myDS->FaceInfo(nF1);
1117         const BOPDS_FaceInfo& aFI2 = myDS->FaceInfo(nF2);
1118         //
1119         bInBothFaces = (aFI1.VerticesOn().Contains(nV) ||
1120                         aFI1.VerticesIn().Contains(nV))&&
1121                           (aFI2.VerticesOn().Contains(nV) ||
1122                            aFI2.VerticesIn().Contains(nV));
1123         if (!bInBothFaces) {
1124           continue;
1125         }
1126       }
1127     }
1128     //
1129     bIsVertexOnLine=myContext->IsVertexOnLine(aV, aIC, aTolR3D, aT);
1130     if (!bIsVertexOnLine) {
1131       Standard_Real aTolVExt;
1132       BOPCol_MapOfInteger aMI;
1133       //
1134       aTolVExt = BRep_Tool::Tolerance(aV);
1135       //
1136       GetFullFaceMap(nF1, aMI);
1137       GetFullFaceMap(nF2, aMI);
1138       //
1139       ExtendedTolerance(nV, aMI, aTolVExt);
1140       bIsVertexOnLine=myContext->IsVertexOnLine(aV, aTolVExt, aIC, aTolR3D, aT);
1141     }   
1142     //
1143     if (bIsVertexOnLine) {
1144       aPave.SetIndex(nV);
1145       aPave.SetParameter(aT);
1146       //
1147       aPB->AppendExtPave(aPave);
1148       //
1149       BOPTools_AlgoTools::UpdateVertex (aIC, aT, aV);
1150       // 
1151       BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nV);
1152       Bnd_Box& aBoxDS=aSIDS.ChangeBox();
1153       BRepBndLib::Add(aV, aBoxDS);
1154     }
1155   }
1156 }
1157
1158 //=======================================================================
1159 //function : ExtendedTolerance
1160 //purpose  : 
1161 //=======================================================================
1162   Standard_Boolean BOPAlgo_PaveFiller::ExtendedTolerance(const Standard_Integer nV,
1163                                                          const BOPCol_MapOfInteger& aMI,
1164                                                          Standard_Real& aTolVExt)
1165 {
1166   Standard_Boolean bFound = Standard_False;
1167   if (!(myDS->IsNewShape(nV))) {
1168     return bFound;
1169   }
1170
1171   Standard_Integer i, k, aNbLines;
1172   Standard_Real aT11, aT12, aD1, aD2, aD;
1173   TopoDS_Vertex aV;
1174   gp_Pnt aPV, aP11, aP12;
1175
1176   aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
1177   aPV=BRep_Tool::Pnt(aV);
1178   //
1179   BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
1180   BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
1181
1182   for (k=0; k<2; ++k) {
1183     aNbLines = !k ? aEEs.Extent() : aEFs.Extent();
1184     for (i = 0; i < aNbLines; ++i) {
1185       BOPDS_Interf *aInt = !k ? (BOPDS_Interf*) (&aEEs(i)) :
1186                                 (BOPDS_Interf*) (&aEFs(i));
1187       if (aInt->IndexNew() == nV) {
1188         if (aMI.Contains(aInt->Index1()) && aMI.Contains(aInt->Index2())) {
1189           const IntTools_CommonPrt& aComPrt = !k ? aEEs(i).CommonPart() :
1190                                                    aEFs(i).CommonPart();
1191           //
1192           const TopoDS_Edge& aE1=aComPrt.Edge1();
1193           aComPrt.Range1(aT11, aT12);
1194           BOPTools_AlgoTools::PointOnEdge(aE1, aT11, aP11);
1195           BOPTools_AlgoTools::PointOnEdge(aE1, aT12, aP12);
1196           aD1=aPV.Distance(aP11);
1197           aD2=aPV.Distance(aP12);
1198           aD=(aD1>aD2)? aD1 : aD2;
1199           if (aD>aTolVExt) {
1200             aTolVExt=aD;
1201           }
1202           return !bFound;
1203         }//if (aMI.Contains(aEF.Index1()) && aMI.Contains(aEF.Index2())) {
1204       }//if (aInt->IndexNew() == nV) {
1205     }//for (i = 0; i < aNbLines; ++i) {
1206   }//for (k=0; k<2; ++k) {
1207
1208   return bFound;
1209 }
1210
1211 //=======================================================================
1212 //function : GetEFPnts
1213 //purpose  : 
1214 //=======================================================================
1215   void BOPAlgo_PaveFiller::GetEFPnts(const Standard_Integer nF1,
1216                                      const Standard_Integer nF2,
1217                                      IntSurf_ListOfPntOn2S& aListOfPnts)
1218 {
1219   Standard_Integer nE, nF, nFOpposite, aNbEFs, i;
1220   Standard_Real U1, U2, V1, V2, f, l;
1221   BOPCol_MapOfInteger aMIF1, aMIF2;
1222   //
1223   //collect indexes of all shapes from nF1 and nF2.
1224   GetFullFaceMap(nF1, aMIF1);
1225   GetFullFaceMap(nF2, aMIF2);
1226   //
1227   BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
1228   aNbEFs = aEFs.Extent();
1229   //
1230   for(i = 0; i < aNbEFs; ++i) {
1231     const BOPDS_InterfEF& aEF = aEFs(i);
1232     nE = aEF.Index1();
1233     nFOpposite = aEF.Index2();
1234     if(aMIF1.Contains(nE) && aMIF2.Contains(nFOpposite) ||
1235        aMIF1.Contains(nFOpposite) && aMIF2.Contains(nE)) {
1236       //
1237       IntTools_CommonPrt aCP = aEF.CommonPart();
1238       if(aCP.Type() == TopAbs_VERTEX) {
1239         Standard_Real aPar = aCP.VertexParameter1();
1240         const TopoDS_Edge& aE = (*(TopoDS_Edge*)(&myDS->Shape(nE)));
1241         const TopoDS_Face& aFOpposite = (*(TopoDS_Face*)(&myDS->Shape(nFOpposite)));
1242         //
1243         const Handle(Geom_Curve)& aCurve = BRep_Tool::Curve(aE, f, l);
1244         //
1245         nF = (nFOpposite == nF1) ? nF2 : nF1;
1246         const TopoDS_Face& aF = (*(TopoDS_Face*)(&myDS->Shape(nF)));
1247         Handle(Geom2d_Curve) aPCurve = BRep_Tool::CurveOnSurface(aE, aF, f, l);
1248         //
1249         GeomAPI_ProjectPointOnSurf& aProj = myContext->ProjPS(aFOpposite);
1250         //
1251         gp_Pnt aPoint;
1252         aCurve->D0(aPar, aPoint);
1253         IntSurf_PntOn2S aPnt;
1254         if(!aPCurve.IsNull()) {
1255           gp_Pnt2d aP2d = aPCurve->Value(aPar);
1256           aProj.Perform(aPoint);
1257           if(aProj.IsDone()) {
1258             aProj.LowerDistanceParameters(U1,V1);
1259             if (nF == nF1) {
1260               aPnt.SetValue(aP2d.X(),aP2d.Y(),U1,V1);
1261             } else {
1262               aPnt.SetValue(U1,V1,aP2d.X(),aP2d.Y());
1263             }
1264             aListOfPnts.Append(aPnt);
1265           }
1266         }
1267         else {
1268           GeomAPI_ProjectPointOnSurf& aProj1 = myContext->ProjPS(aF);
1269           aProj1.Perform(aPoint);
1270           aProj.Perform(aPoint);
1271           if(aProj1.IsDone() && aProj.IsDone()){
1272             aProj1.LowerDistanceParameters(U1,V1);
1273             aProj.LowerDistanceParameters(U2,V2);
1274             if (nF == nF1) {
1275               aPnt.SetValue(U1,V1,U2,V2);
1276             } else {
1277               aPnt.SetValue(U2,V2,U1,V1);
1278             }
1279             aListOfPnts.Append(aPnt);
1280           }
1281         }
1282       }
1283     }
1284   }
1285 }
1286
1287 //=======================================================================
1288 //function : ProcessUnUsedVertices
1289 //purpose  : 
1290 //=======================================================================
1291   void BOPAlgo_PaveFiller::PutEFPavesOnCurve(const Standard_Integer /*nF1*/,
1292                                              const Standard_Integer /*nF2*/,
1293                                              BOPDS_Curve& aNC,
1294                                              const BOPCol_MapOfInteger& aMVEF)
1295 {
1296   if (!aMVEF.Extent()) {
1297     return;
1298   }
1299   //
1300   const IntTools_Curve& aIC=aNC.Curve();
1301   GeomAbs_CurveType aTypeC;
1302   aTypeC=aIC.Type();
1303   if (!(aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve)) {
1304     return;
1305   }
1306   //
1307   Standard_Integer nV;
1308   BOPCol_MapOfInteger aMV;
1309   //
1310   aMV.Assign(aMVEF);
1311   RemoveUsedVertices(aNC, aMV);
1312   if (!aMV.Extent()) {
1313     return;
1314   }
1315   //
1316   Standard_Real aDist;
1317   BOPDS_Pave aPave;
1318   //
1319   const Handle(Geom_Curve)& aC3D=aIC.Curve();
1320   Handle(BOPDS_PaveBlock)& aPB = aNC.ChangePaveBlock1();
1321
1322   GeomAPI_ProjectPointOnCurve& aProjPT = myContext->ProjPT(aC3D);
1323   
1324   BOPCol_MapIteratorOfMapOfInteger aItMI;
1325   aItMI.Initialize(aMV);
1326   for (; aItMI.More(); aItMI.Next()) {
1327     nV = aItMI.Value();
1328     const TopoDS_Vertex& aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
1329     gp_Pnt aPV = BRep_Tool::Pnt(aV);
1330     aProjPT.Perform(aPV);
1331     Standard_Integer aNbPoints = aProjPT.NbPoints();
1332     if (aNbPoints) {
1333       aDist = aProjPT.LowerDistance();
1334       PutPaveOnCurve(nV, aDist, aNC, aPB);
1335     }
1336   }
1337 }
1338
1339 //=======================================================================
1340 //function : ProcessUnUsedVertices
1341 //purpose  : 
1342 //=======================================================================
1343   void BOPAlgo_PaveFiller::PutStickPavesOnCurve(const Standard_Integer nF1,
1344                                                 const Standard_Integer nF2,
1345                                                 BOPDS_Curve& aNC,
1346                                                 const BOPCol_MapOfInteger& aMVStick)
1347 {
1348   BOPCol_MapOfInteger aMV;
1349   aMV.Assign(aMVStick);
1350   RemoveUsedVertices(aNC, aMV);
1351   //
1352   if (!aMV.Extent()) {
1353     return;
1354   }
1355   //
1356   GeomAbs_SurfaceType aType1, aType2;
1357   const TopoDS_Face& aF1 = (*(TopoDS_Face*)(&myDS->Shape(nF1)));  
1358   const TopoDS_Face& aF2 = (*(TopoDS_Face*)(&myDS->Shape(nF2)));  
1359   Handle(Geom_Surface) aS1=BRep_Tool::Surface(aF1);
1360   Handle(Geom_Surface) aS2=BRep_Tool::Surface(aF2);
1361   GeomAdaptor_Surface aGAS1(aS1);
1362   GeomAdaptor_Surface aGAS2(aS2);
1363   
1364   //
1365   aType1=aGAS1.GetType();
1366   aType2=aGAS2.GetType();
1367   //
1368   if(aType1==GeomAbs_Torus  || aType2==GeomAbs_Torus) {
1369     Standard_Integer nV, m, n;
1370     Standard_Real aTC[2], aD, aD2, u, v, aDT2, aScPr, aDScPr;
1371     GeomAbs_CurveType aTypeC;
1372     gp_Pnt aPC[2], aPV;
1373     gp_Dir aDN[2];
1374     gp_Pnt2d aP2D;
1375     
1376     Handle(Geom2d_Curve) aC2D[2];
1377     //
1378     aDT2=2e-7;     // the rich criteria
1379     aDScPr=5.e-9;  // the creasing criteria
1380     //
1381     const IntTools_Curve& aIC=aNC.Curve();
1382     //Handle(Geom_Curve) aC3D=aIC.Curve(); //DEB
1383     aTypeC=aIC.Type();
1384     if (aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve) {
1385       //
1386       aIC.Bounds(aTC[0], aTC[1], aPC[0], aPC[1]);
1387       aC2D[0]=aIC.FirstCurve2d();
1388       aC2D[1]=aIC.SecondCurve2d();
1389       if (!aC2D[0].IsNull() && !aC2D[1].IsNull()) {
1390         BOPCol_MapIteratorOfMapOfInteger aItMI, aItMI1;
1391         aItMI.Initialize(aMV);
1392         for (; aItMI.More(); aItMI.Next()) {
1393           nV = aItMI.Value();
1394           const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&myDS->Shape(nV));
1395           aPV=BRep_Tool::Pnt(aV);
1396           //
1397           for (m=0; m<2; ++m) {
1398             aD2=aPC[m].SquareDistance(aPV);
1399             if (aD2>aDT2) {// no rich
1400               continue; 
1401             }
1402             //
1403             for (n=0; n<2; ++n) {
1404               Handle(Geom_Surface)& aS=(!n)? aS1 : aS2;
1405               aC2D[n]->D0(aTC[m], aP2D);
1406               aP2D.Coord(u, v);
1407               BOPTools_AlgoTools3D::GetNormalToSurface(aS, u, v, aDN[n]);
1408             }
1409             // 
1410             aScPr=aDN[0]*aDN[1];
1411             if (aScPr<0.) {
1412               aScPr=-aScPr;
1413             }
1414             aScPr=1.-aScPr;
1415             //
1416             if (aScPr>aDScPr) {
1417               continue;
1418             }
1419             //
1420             // The intersection curve aIC is vanishing curve (the crease)
1421             aD=sqrt(aD2);
1422             //
1423             Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1424             PutPaveOnCurve(nV, aD, aNC, aPB);
1425           }
1426         }//for (jVU=1; jVU=aNbVU; ++jVU) {
1427       }
1428     }//if (aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve) {
1429   }//if(aType1==GeomAbs_Torus  || aType2==GeomAbs_Torus) {
1430 }
1431
1432 //=======================================================================
1433 //function : GetStickVertices
1434 //purpose  : 
1435 //=======================================================================
1436   void BOPAlgo_PaveFiller::GetStickVertices(const Standard_Integer nF1,
1437                                             const Standard_Integer nF2,
1438                                             BOPCol_MapOfInteger& aMVStick,
1439                                             BOPCol_MapOfInteger& aMVEF)
1440 {
1441   BOPCol_MapOfInteger aMIF1, aMIF2;
1442   Standard_Integer nV1, nV2, nE1, nE2, nV, nE, nF, nVNew, i;
1443   //
1444   BOPDS_VectorOfInterfVV& aVVs=myDS->InterfVV();
1445   BOPDS_VectorOfInterfVE& aVEs=myDS->InterfVE();
1446   BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
1447   BOPDS_VectorOfInterfVF& aVFs=myDS->InterfVF();
1448   BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
1449   //
1450   Standard_Integer aNbVVs, aNbVEs, aNbEEs, aNbVFs, aNbEFs;
1451   aNbVVs = aVVs.Extent();
1452   aNbVEs = aVEs.Extent();
1453   aNbEEs = aEEs.Extent();
1454   aNbVFs = aVFs.Extent();
1455   aNbEFs = aEFs.Extent();
1456   //
1457   //collect indexes of all shapes from nF1 and nF2.
1458   GetFullFaceMap(nF1, aMIF1);
1459   GetFullFaceMap(nF2, aMIF2);
1460   //collect VV interferences
1461   for(i = 0; i < aNbVVs; ++i) {
1462     const BOPDS_InterfVV& aVV = aVVs(i);
1463     nV1 = aVV.Index1();
1464     nV2 = aVV.Index2();
1465     if(aMIF1.Contains(nV1) && aMIF2.Contains(nV2) ||
1466        aMIF1.Contains(nV2) && aMIF2.Contains(nV1)) {
1467       if (aVV.HasIndexNew()) {
1468         nVNew = aVV.IndexNew();
1469         aMVStick.Add(nVNew);
1470       }
1471     }
1472   }
1473   //collect VE interferences
1474   for(i = 0; i < aNbVEs; ++i) {
1475     const BOPDS_InterfVE& aVE = aVEs(i);
1476     nV = aVE.Index1();
1477     nE = aVE.Index2();
1478     if(aMIF1.Contains(nV) && aMIF2.Contains(nE) ||
1479        aMIF1.Contains(nE) && aMIF2.Contains(nV)) {
1480       aMVStick.Add(nV);
1481     }
1482   }
1483   //collect EE interferences
1484   for(i = 0; i < aNbEEs; ++i) {
1485     const BOPDS_InterfEE& aEE = aEEs(i);
1486     nE1 = aEE.Index1();
1487     nE2 = aEE.Index2();
1488     if(aMIF1.Contains(nE1) && aMIF2.Contains(nE2) ||
1489        aMIF1.Contains(nE2) && aMIF2.Contains(nE1)) {
1490       IntTools_CommonPrt aCP = aEE.CommonPart();
1491       if(aCP.Type() == TopAbs_VERTEX) {
1492         nVNew = aEE.IndexNew();
1493         aMVStick.Add(nVNew);
1494       }
1495     }
1496   }
1497   //collect VF interferences
1498   for(i = 0; i < aNbVFs; ++i) {
1499     const BOPDS_InterfVF& aVF = aVFs(i);
1500     nV = aVF.Index1();
1501     nF = aVF.Index2();
1502     if(aMIF1.Contains(nV) && aMIF2.Contains(nF) ||
1503        aMIF1.Contains(nF) && aMIF2.Contains(nV)) {
1504       aMVStick.Add(nV);
1505     }
1506   }
1507   //collect EF interferences
1508   for(i = 0; i < aNbEFs; ++i) {
1509     const BOPDS_InterfEF& aEF = aEFs(i);
1510     nE = aEF.Index1();
1511     nF = aEF.Index2();
1512     if(aMIF1.Contains(nE) && aMIF2.Contains(nF) ||
1513        aMIF1.Contains(nF) && aMIF2.Contains(nE)) {
1514       IntTools_CommonPrt aCP = aEF.CommonPart();
1515       if(aCP.Type() == TopAbs_VERTEX) {
1516         nVNew = aEF.IndexNew();
1517         aMVStick.Add(nVNew);
1518         aMVEF.Add(nVNew);
1519       }
1520     }
1521   }
1522 }
1523
1524 //=======================================================================
1525 // function: GetFullFaceMap
1526 // purpose: 
1527 //=======================================================================
1528 void BOPAlgo_PaveFiller::GetFullFaceMap(const Standard_Integer nF,
1529                                         BOPCol_MapOfInteger& aMI)
1530 {
1531   Standard_Integer nV, nE;
1532   TopoDS_Edge aE;
1533   TopoDS_Vertex aV;
1534   //face
1535   aMI.Add(nF);
1536   //edges
1537   const TopoDS_Face& aF = (*(TopoDS_Face*)(&myDS->Shape(nF)));
1538   TopExp_Explorer anExp(aF, TopAbs_EDGE);
1539   for (; anExp.More(); anExp.Next()) {
1540     aE = (*(TopoDS_Edge *)(&anExp.Current()));
1541     nE = myDS->Index(aE);
1542     aMI.Add(nE);
1543   }
1544   //vertices
1545   TopExp_Explorer anExpV(aF, TopAbs_VERTEX);
1546   for (; anExpV.More(); anExpV.Next()) {
1547     aV = (*(TopoDS_Vertex *)(&anExpV.Current()));
1548     nV = myDS->Index(aV);
1549     aMI.Add(nV);
1550   }
1551 }
1552
1553 //=======================================================================
1554 // function: RemoveUsedVertices
1555 // purpose: 
1556 //=======================================================================
1557 void BOPAlgo_PaveFiller::RemoveUsedVertices(BOPDS_Curve& aNC,
1558                                             BOPCol_MapOfInteger& aMV)
1559 {
1560   if (!aMV.Extent()) {
1561     return;
1562   }
1563   Standard_Integer nV;
1564   BOPDS_Pave aPave;
1565   BOPDS_ListIteratorOfListOfPave aItLP;
1566   //
1567   Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1568   const BOPDS_ListOfPave& aLP = aPB->ExtPaves();
1569   aItLP.Initialize(aLP);
1570   for (;aItLP.More();aItLP.Next()) {
1571     aPave = aItLP.Value();
1572     nV = aPave.Index();
1573     aMV.Remove(nV);
1574   }
1575 }
1576
1577 //=======================================================================
1578 //function : PutPaveOnCurve
1579 //purpose  : 
1580 //=======================================================================
1581   void BOPAlgo_PaveFiller::PutPaveOnCurve(const Standard_Integer nV,
1582                                           const Standard_Real aTolR3D,
1583                                           const BOPDS_Curve& aNC, 
1584                                           Handle(BOPDS_PaveBlock)& aPB)
1585 {
1586   Standard_Boolean bIsVertexOnLine;
1587   Standard_Real aT;
1588   BOPDS_Pave aPave;
1589   //
1590   const TopoDS_Vertex aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
1591   const IntTools_Curve& aIC = aNC.Curve();
1592   //
1593   bIsVertexOnLine=myContext->IsVertexOnLine(aV, aIC, aTolR3D, aT);
1594   if (bIsVertexOnLine) {
1595     aPave.SetIndex(nV);
1596     aPave.SetParameter(aT);
1597     //
1598     aPB->AppendExtPave(aPave);
1599     //
1600     BOPTools_AlgoTools::UpdateVertex (aIC, aT, aV);
1601     // 
1602     BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nV);
1603     Bnd_Box& aBoxDS=aSIDS.ChangeBox();
1604     BRepBndLib::Add(aV, aBoxDS);
1605   }
1606 }
1607
1608 //=======================================================================
1609 //function : ProcessOldPaveBlocks
1610 //purpose  : 
1611 //=======================================================================
1612   void BOPAlgo_PaveFiller::ProcessExistingPaveBlocks
1613     (const Standard_Integer theInt,
1614      const BOPDS_MapOfPaveBlock& aMPBOnIn,
1615      BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& aMSCPB,
1616      BOPCol_DataMapOfShapeInteger& aMVI,
1617      const BOPCol_MapOfInteger& aMVB,
1618      BOPDS_MapOfPaveBlock& aMPB)
1619 {
1620   Standard_Integer nV, nE, iFlag;
1621   Standard_Real aT;
1622   BOPCol_MapIteratorOfMapOfInteger aItB;
1623   BOPDS_MapIteratorOfMapOfPaveBlock aItPB;
1624   //
1625   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
1626   BOPDS_InterfFF& aFF = aFFs(theInt);
1627   BOPDS_VectorOfCurve& aVC=aFF.ChangeCurves();
1628   //  
1629   aItB.Initialize(aMVB);
1630   for (; aItB.More(); aItB.Next()) {
1631     nV = aItB.Value();
1632     const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
1633     const Bnd_Box& aBoxV=aSIV.Box();
1634     const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&aSIV.Shape();
1635     if (!aMVI.IsBound(aV)) {
1636       continue;
1637     }
1638     //
1639     aItPB.Initialize(aMPBOnIn);
1640     for (; aItPB.More(); aItPB.Next()) {
1641       const Handle(BOPDS_PaveBlock)& aPB = aItPB.Value();
1642       if (aPB->Pave1().Index() == nV || aPB->Pave2().Index() == nV) {
1643         continue;
1644       }
1645       //
1646       if (aMPB.Contains(aPB)) {
1647         continue;
1648       }
1649       nE=aPB->Edge();
1650       const BOPDS_ShapeInfo& aSIE=myDS->ShapeInfo(nE);
1651       const Bnd_Box& aBoxE=aSIE.Box();
1652       //
1653       if (!aBoxV.IsOut(aBoxE)) {
1654         const TopoDS_Edge& aE = *(TopoDS_Edge*)&aSIE.Shape();
1655         //
1656         iFlag=myContext->ComputeVE (aV, aE, aT);
1657         if (!iFlag) {
1658           aMPB.Add(aPB);
1659           //
1660           PreparePostTreatFF(theInt, aPB, aMSCPB, aMVI, aVC);
1661         }
1662       }
1663     }
1664   }
1665 }
1666
1667 //=======================================================================
1668 //function : UpdateExistingPaveBlocks
1669 //purpose  : 
1670 //=======================================================================
1671   void BOPAlgo_PaveFiller::UpdateExistingPaveBlocks
1672     (const Handle(BOPDS_PaveBlock)& aPBf,
1673      BOPDS_ListOfPaveBlock& aLPB,
1674      const Standard_Integer nF1,
1675      const Standard_Integer nF2) 
1676 {
1677   Standard_Integer nE;
1678   Standard_Boolean bCB;
1679   Handle(BOPDS_PaveBlock) aPB, aPB1, aPB2, aPB2n;
1680   Handle(BOPDS_CommonBlock) aCB;
1681   BOPDS_ListIteratorOfListOfPaveBlock aIt, aIt1, aIt2;
1682   BOPDS_IndexedMapOfPaveBlock aMPB;
1683   //
1684   //remove micro edges from aLPB
1685   aIt.Initialize(aLPB);
1686   for (; aIt.More();) {
1687     aPB = aIt.Value();
1688     const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPB->Edge());
1689     if (BOPTools_AlgoTools::IsMicroEdge(aE, myContext)) {
1690       aLPB.Remove(aIt);
1691       continue;
1692     }
1693     aIt.Next();
1694   }
1695   //
1696   if (!aLPB.Extent()) {
1697     return;
1698   }
1699   //update face info
1700   myDS->UpdateFaceInfoOn(nF1);
1701   //
1702   myDS->UpdateFaceInfoOn(nF2);
1703   //
1704   BOPDS_FaceInfo& aFI1 = myDS->ChangeFaceInfo(nF1);
1705   BOPDS_FaceInfo& aFI2 = myDS->ChangeFaceInfo(nF2);
1706   //
1707   BOPDS_IndexedMapOfPaveBlock& aMPBOn1 = aFI1.ChangePaveBlocksOn();
1708   BOPDS_IndexedMapOfPaveBlock& aMPBIn1 = aFI1.ChangePaveBlocksIn();
1709   BOPDS_IndexedMapOfPaveBlock& aMPBOn2 = aFI2.ChangePaveBlocksOn();
1710   BOPDS_IndexedMapOfPaveBlock& aMPBIn2 = aFI2.ChangePaveBlocksIn();
1711   //
1712   // remove old pave blocks
1713   const Handle(BOPDS_CommonBlock)& aCB1 = myDS->CommonBlock(aPBf);
1714   bCB = !aCB1.IsNull();
1715   BOPDS_ListOfPaveBlock aLPB1;
1716   //
1717   if (bCB) {
1718     aLPB1.Assign(aCB1->PaveBlocks());
1719   } else {
1720     aLPB1.Append(aPBf);
1721   }
1722   aIt1.Initialize(aLPB1);
1723   for (; aIt1.More(); aIt1.Next()) {
1724     aPB1 = aIt1.Value();
1725     nE = aPB1->OriginalEdge();
1726     //
1727     BOPDS_ListOfPaveBlock& aLPB2 = myDS->ChangePaveBlocks(nE);
1728     aIt2.Initialize(aLPB2);
1729     for (; aIt2.More(); aIt2.Next()) {
1730       aPB2 = aIt2.Value();
1731       if (aPB1 == aPB2) {
1732         aLPB2.Remove(aIt2);
1733         break;
1734       }
1735     }
1736   }
1737   //
1738   if (bCB) {
1739     //create new pave blocks
1740     const BOPCol_ListOfInteger& aFaces = aCB1->Faces();
1741     aIt.Initialize(aLPB);
1742     for (; aIt.More(); aIt.Next()) {
1743       Handle(BOPDS_PaveBlock)& aPB = aIt.ChangeValue();
1744       //
1745       aCB = new BOPDS_CommonBlock;
1746       aIt1.Initialize(aLPB1);
1747       for (; aIt1.More(); aIt1.Next()) {
1748         aPB2 = aIt1.Value();
1749         nE = aPB2->OriginalEdge();
1750         //
1751         aPB2n = new BOPDS_PaveBlock;
1752         aPB2n->SetPave1(aPB->Pave1());
1753         aPB2n->SetPave2(aPB->Pave2());
1754         aPB2n->SetEdge(aPB->Edge());
1755         aPB2n->SetOriginalEdge(nE);
1756         aCB->AddPaveBlock(aPB2n);
1757         myDS->SetCommonBlock(aPB2n, aCB);
1758         myDS->ChangePaveBlocks(nE).Append(aPB2n);
1759       }
1760       aCB->AddFaces(aFaces);
1761       myDS->SortPaveBlocks(aCB);
1762       //
1763       aPB=aCB->PaveBlocks().First();
1764     }
1765   } 
1766   //
1767   aIt.Initialize(aLPB);
1768   for (; aIt.More(); aIt.Next()) {
1769     Handle(BOPDS_PaveBlock)& aPB = aIt.ChangeValue();
1770     nE = aPB->OriginalEdge();
1771     //
1772     Standard_Integer nF = (aMPBOn1.Contains(aPBf) || 
1773                            aMPBIn1.Contains(aPBf)) ? nF2 : nF1;
1774     const TopoDS_Face& aF = *(TopoDS_Face*)&myDS->Shape(nF);
1775     IntTools_Range aShrR(aPB->Pave1().Parameter(), aPB->Pave2().Parameter());
1776     const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPB->Edge());
1777     //
1778     Standard_Boolean bCom = BOPTools_AlgoTools::IsBlockInOnFace(aShrR, aF, aE, myContext);
1779     if (bCom) {
1780       if (bCB) {
1781         aCB = myDS->CommonBlock(aPB);
1782         aCB->AddFace(nF);
1783       } else {
1784         aCB = new BOPDS_CommonBlock;
1785         aCB->AddPaveBlock(aPB);
1786         aCB->AddFace(nF1);
1787         aCB->AddFace(nF2);
1788         //
1789         myDS->SetCommonBlock(aPB, aCB);
1790       }
1791       aMPB.Add(aPB);
1792     }
1793     if (!bCB) {
1794       myDS->ChangePaveBlocks(nE).Append(aPB);
1795     }
1796   }
1797   //
1798   Standard_Integer i, aNbPB;
1799   Standard_Boolean bIn1, bIn2;
1800   //
1801   bIn1 = aMPBOn1.Contains(aPBf) || aMPBIn1.Contains(aPBf);
1802   bIn2 = aMPBOn2.Contains(aPBf) || aMPBIn2.Contains(aPBf);
1803   //
1804   aNbPB=aMPB.Extent();
1805   for (i=1; i<=aNbPB; ++i) {
1806     aPB = aMPB(i);
1807     if (!bIn1) {
1808       aMPBIn1.Add(aPB);
1809     }
1810     //
1811     if (!bIn2) {
1812       aMPBIn2.Add(aPB);
1813     }
1814   }
1815 }
1816
1817 //=======================================================================
1818 // function: PutClosingPaveOnCurve
1819 // purpose:
1820 //=======================================================================
1821   void BOPAlgo_PaveFiller::PutClosingPaveOnCurve(BOPDS_Curve& aNC)
1822 {
1823   Standard_Boolean bIsClosed, bHasBounds, bAdded;
1824   Standard_Integer nVC, j;
1825   Standard_Real aT[2], aTC, dT, aTx;
1826   gp_Pnt aP[2] ; 
1827   BOPDS_Pave aPVx;
1828   BOPDS_ListIteratorOfListOfPave aItLP;
1829   //
1830   const IntTools_Curve& aIC=aNC.Curve();
1831   const Handle(Geom_Curve)& aC3D=aIC.Curve();
1832   if(aC3D.IsNull()) {
1833     return;
1834   }
1835   //
1836   bIsClosed=IntTools_Tools::IsClosed(aC3D);
1837   if (!bIsClosed) {
1838     return;
1839   }
1840   //
1841   bHasBounds=aIC.HasBounds ();
1842   if (!bHasBounds){
1843     return;
1844   }
1845   // 
1846   bAdded=Standard_False;
1847   dT=Precision::PConfusion();
1848   aIC.Bounds (aT[0], aT[1], aP[0], aP[1]);
1849   //
1850   Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1851   BOPDS_ListOfPave& aLP=aPB->ChangeExtPaves();
1852   //
1853   aItLP.Initialize(aLP);
1854   for (; aItLP.More() && !bAdded; aItLP.Next()) {
1855     const BOPDS_Pave& aPC=aItLP.Value();
1856     nVC=aPC.Index();
1857     aTC=aPC.Parameter();
1858     //
1859     for (j=0; j<2; ++j) {
1860       if (fabs(aTC-aT[j]) < dT) {
1861         aTx=(!j) ? aT[1] : aT[0];
1862         aPVx.SetIndex(nVC);
1863         aPVx.SetParameter(aTx);
1864         aLP.Append(aPVx);
1865         //
1866         bAdded=Standard_True;
1867         break;
1868       }
1869     }
1870   }
1871 }
1872
1873 //=======================================================================
1874 //function : PreparePostTreatFF
1875 //purpose  : 
1876 //=======================================================================
1877   void BOPAlgo_PaveFiller::PreparePostTreatFF
1878     (const Standard_Integer aInt,
1879      const Handle(BOPDS_PaveBlock)& aPB,
1880      BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& aMSCPB,
1881      BOPCol_DataMapOfShapeInteger& aMVI,
1882      BOPDS_VectorOfCurve& aVC) 
1883 {
1884   Standard_Integer nV1, nV2;
1885   //
1886   Standard_Integer iC=aVC.Append()-1;
1887   BOPDS_ListOfPaveBlock& aLPBC = aVC(iC).ChangePaveBlocks();
1888   aLPBC.Append(aPB);
1889   //
1890   aPB->Indices(nV1, nV2);
1891   const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
1892   const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
1893   const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPB->Edge());
1894   // Keep info for post treatment 
1895   BOPDS_CoupleOfPaveBlocks aCPB;
1896   aCPB.SetIndexInterf(aInt);
1897   aCPB.SetIndex(iC);
1898   aCPB.SetPaveBlock1(aPB);
1899   //
1900   aMSCPB.Add(aE, aCPB);
1901   aMVI.Bind(aV1, nV1);
1902   aMVI.Bind(aV2, nV2);
1903 }
1904
1905 //=======================================================================
1906 //function : ToleranceFF
1907 //purpose  : Computes the TolFF according to the tolerance value and 
1908 //           types of the faces.
1909 //=======================================================================
1910   void ToleranceFF(const TopoDS_Face& aF1,
1911                    const TopoDS_Face& aF2,
1912                    Standard_Real& aTolFF)
1913 {
1914   Standard_Real aTol1, aTol2;
1915   Standard_Boolean isAna1, isAna2;
1916   //
1917   aTol1 = BRep_Tool::Tolerance(aF1);
1918   aTol2 = BRep_Tool::Tolerance(aF2);
1919   aTolFF = Max(aTol1, aTol2);
1920   //
1921   BRepAdaptor_Surface BAS1(aF1);
1922   BRepAdaptor_Surface BAS2(aF2);
1923   //
1924   isAna1 = (BAS1.GetType() == GeomAbs_Plane ||
1925             BAS1.GetType() == GeomAbs_Cylinder ||
1926             BAS1.GetType() == GeomAbs_Cone ||
1927             BAS1.GetType() == GeomAbs_Sphere ||
1928             BAS1.GetType() == GeomAbs_Torus);
1929   //
1930   isAna2 = (BAS2.GetType() == GeomAbs_Plane ||
1931             BAS2.GetType() == GeomAbs_Cylinder ||
1932             BAS2.GetType() == GeomAbs_Cone ||
1933             BAS2.GetType() == GeomAbs_Sphere ||
1934             BAS2.GetType() == GeomAbs_Torus);
1935   //
1936   aTolFF = (isAna1 && isAna2) ? aTolFF : Max(aTolFF, 5.e-6);
1937 }
1938
1939
1940 // DEB f
1941   /*
1942   {
1943     BOPDS_DataMapIteratorOfDataMapOfShapeCoupleOfPaveBlocks aItx;
1944     TopoDS_Compound aCx;
1945     //
1946     BRep_Builder aBBx;
1947     aBBx.MakeCompound(aCx);
1948     //
1949     aItx.Initialize(theMSCPB);
1950     for (; aItx.More(); aItx.Next()) {
1951       const TopoDS_Shape& aSx=aItx.Key();
1952       aBBx.Add(aCx, aSx);
1953     }
1954     int a=0;
1955     BRepTools::Write(aCx, "cx");
1956   }
1957   */
1958   // DEB t