Salome HOME
0021355: [CEA] Problem of partition
[modules/geom.git] / src / NMTTools / NMTTools_PaveFiller_6.cxx
1 //  Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File:        NMTTools_PaveFiller_6.cxx
23 //  Created:     Fri Dec 19 10:27:31 2003
24 //  Author:      Peter KURNEV
25
26 #include <NMTTools_PaveFiller.ixx>
27
28 #include <Basics_OCCTVersion.hxx>
29
30 #include <Precision.hxx>
31
32 #include <TColStd_IndexedMapOfInteger.hxx>
33 #include <TColStd_MapOfInteger.hxx>
34 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
35 #include <TColStd_DataMapOfIntegerListOfInteger.hxx>
36 #include <TColStd_ListOfInteger.hxx>
37 #include <TColStd_ListIteratorOfListOfInteger.hxx>
38 #include <TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger.hxx>
39
40
41 #include <Geom_TrimmedCurve.hxx>
42 #include <Geom2d_TrimmedCurve.hxx>
43 #include <Geom2d_Curve.hxx>
44 #include <GeomAdaptor_Curve.hxx>
45 #include <GeomAdaptor_Surface.hxx>
46 #include <Geom_Surface.hxx>
47
48 #include <BndLib_Add3dCurve.hxx>
49
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Compound.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <TopoDS_Edge.hxx>
55
56 #include <TopExp.hxx>
57
58 #include <BRepLib.hxx>
59 #include <BRep_Builder.hxx>
60 #include <BRep_Tool.hxx>
61 #include <BRepBndLib.hxx>
62
63 #include <TopTools_IndexedMapOfShape.hxx>
64 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
65 #include <TopTools_ListOfShape.hxx>
66 #include <TopTools_ListIteratorOfListOfShape.hxx>
67
68 #include <BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger.hxx>
69 #include <BOPTColStd_IndexedDataMapOfIntegerInteger.hxx>
70
71 #include <BooleanOperations_IndexedDataMapOfShapeInteger.hxx>
72 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
73 #include <BooleanOperations_OnceExplorer.hxx>
74 #include <BooleanOperations_ShapesDataStructure.hxx>
75
76 #include <IntTools_SequenceOfPntOn2Faces.hxx>
77 #include <IntTools_SequenceOfCurves.hxx>
78 #include <IntTools_FaceFace.hxx>
79 #include <IntTools_Tools.hxx>
80 #include <IntTools_ShrunkRange.hxx>
81 #include <IntTools_Context.hxx>
82
83 #include <BOPTools_CArray1OfSSInterference.hxx>
84 #include <BOPTools_VSInterference.hxx>
85 #include <BOPTools_ESInterference.hxx>
86 #include <BOPTools_SSInterference.hxx>
87 #include <BOPTools_ListOfPaveBlock.hxx>
88 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
89 #include <BOPTools_PaveBlock.hxx>
90 #include <BOPTools_ListIteratorOfListOfPave.hxx>
91 #include <BOPTools_Tools.hxx>
92 #include <BOPTools_PaveBlockIterator.hxx>
93 #include <BOPTools_Tools2D.hxx>
94 #include <BOPTools_Tools3D.hxx>
95
96 #include <NMTDS_Iterator.hxx>
97 #include <NMTDS_ShapesDataStructure.hxx>
98 #include <NMTDS_InterfPool.hxx>
99
100 #include <NMTTools_Tools.hxx>
101 #include <NMTTools_IndexedDataMapOfShapePaveBlock.hxx>
102 #include <NMTTools_CommonBlock.hxx>
103 #include <NMTTools_CommonBlockAPI.hxx>
104 #include <NMTTools_ListOfCommonBlock.hxx>
105 #include <NMTTools_ListIteratorOfListOfCommonBlock.hxx>
106 #include <NMTTools_DataMapOfIntegerListOfPaveBlock.hxx>
107 #include <NMTTools_ListIteratorOfListOfCommonBlock.hxx>
108 #include <NMTTools_DataMapIteratorOfDataMapOfIntegerListOfPaveBlock.hxx>
109 #include <NMTTools_MapOfPaveBlock.hxx>
110 #include <NMTTools_MapIteratorOfMapOfPaveBlock.hxx>
111 #include <NMTTools_FaceInfo.hxx>
112 #include <NMTTools_DataMapIteratorOfDataMapOfIntegerFaceInfo.hxx>
113
114 static
115   Standard_Boolean IsMicroEdge(const TopoDS_Edge& aE,
116                                IntTools_Context& aCtx);
117
118 //=======================================================================
119 // function: PerformFF
120 // purpose:
121 //=======================================================================
122   void NMTTools_PaveFiller::PerformFF()
123 {
124   myIsDone=Standard_False;
125   //
126   Standard_Boolean bToApproxC3d, bToApproxC2dOnS1, bToApproxC2dOnS2, bIsDone;
127   Standard_Boolean bJustAdd, bToSplit;
128   Standard_Integer n1, n2, anIndexIn, nF1, nF2, aBlockLength, aNbFFs;
129   Standard_Integer aNbCurves, aNbPoints;
130   Standard_Real anApproxTol, aTolR3D, aTolR2D;
131   BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger aMapWhat, aMapWith;
132   IntTools_SequenceOfPntOn2Faces aPnts;
133   IntTools_SequenceOfCurves aCvs;
134   //
135   BOPTools_CArray1OfSSInterference& aFFs=myIP->SSInterferences();
136   //
137   //  F/F Interferences  [BooleanOperations_SurfaceSurface]
138   myDSIt->Initialize(TopAbs_FACE, TopAbs_FACE);
139   //
140   // BlockLength correction
141   aNbFFs=myDSIt->BlockLength();
142   aBlockLength=aFFs.BlockLength();
143   if (aNbFFs > aBlockLength) {
144     aFFs.SetBlockLength(aNbFFs);
145   }
146   //
147   //modified by NIZNHY-PKV Thu Sep 15 08:02:52 2011f
148   bToSplit=Standard_False;
149   //modified by NIZNHY-PKV Thu Sep 15 08:02:55 2011t
150   //
151   for (; myDSIt->More(); myDSIt->Next()) {
152     myDSIt->Current(n1, n2, bJustAdd);
153     //
154     nF1 = n2;
155     nF2 = n1;
156     if(n1 < n2) {
157       nF1 = n1;
158       nF2 = n2;
159     }
160     anIndexIn=0;
161     aPnts.Clear();
162     aCvs.Clear();
163     //
164     const TopoDS_Face aF1=TopoDS::Face(myDS->Shape(nF1));//mpv
165     const TopoDS_Face aF2=TopoDS::Face(myDS->Shape(nF2));//mpv
166     //
167     // FF
168     bToApproxC3d     = mySectionAttribute.Approximation();
169     bToApproxC2dOnS1 = mySectionAttribute.PCurveOnS1();
170     bToApproxC2dOnS2 = mySectionAttribute.PCurveOnS2();
171     //
172     anApproxTol=1.e-7;
173     //
174     IntTools_FaceFace aFF;
175     //
176     aFF.SetParameters (bToApproxC3d, bToApproxC2dOnS1,
177                        bToApproxC2dOnS2, anApproxTol);
178     //
179     aFF.Perform(aF1, aF2);
180     //
181     bIsDone=aFF.IsDone();
182     //
183     if (!bIsDone) {
184       BOPTools_SSInterference anInterf (nF1, nF2, 1.e-07, 1.e-07, aCvs, aPnts);
185       anIndexIn=aFFs.Append(anInterf);
186       continue;
187     }
188     //
189     aTolR3D=aFF.TolReached3d();
190     aTolR2D=aFF.TolReached2d();
191     if (aTolR3D < 1.e-7){
192       aTolR3D=1.e-7;
193     }
194     //
195     //modified by NIZNHY-PKV Thu Sep 15 08:03:02 2011f
196 #if OCC_VERSION_LARGE > 0x06050100 // For OCCT6.5.2 and higher
197     aFF.PrepareLines3D(bToSplit);
198 #else
199     aFF.PrepareLines3D();
200 #endif // OCC_VERSION_LARGE > 0x06050100 // For OCCT6.5.2 and higher
201     //modified by NIZNHY-PKV Thu Sep 15 08:03:04 2011t
202     //
203     const IntTools_SequenceOfCurves& aCvsX=aFF.Lines();
204     const IntTools_SequenceOfPntOn2Faces& aPntsX=aFF.Points();
205     //
206     aNbCurves=aCvsX.Length();
207     aNbPoints=aPntsX.Length();
208     //
209     if (!aNbCurves && !aNbPoints) {
210       BOPTools_SSInterference anInterf (nF1, nF2, 1.e-07, 1.e-07, aCvs, aPnts);
211       anIndexIn=aFFs.Append(anInterf);
212       continue;
213     }
214     //
215     {
216       BOPTools_SSInterference anInterf (nF1, nF2, aTolR3D, aTolR2D, aCvsX, aPntsX);
217       anIndexIn=aFFs.Append(anInterf);
218     }
219     //
220   }// for (; myDSIt.More(); myDSIt.Next())
221   //
222   myIsDone=Standard_True;
223 }
224
225 //=======================================================================
226 // function: MakeBlocks
227 // purpose:
228 //=======================================================================
229 void NMTTools_PaveFiller::MakeBlocks()
230 {
231   myIsDone=Standard_False;
232   //
233   Standard_Boolean bIsExistingPaveBlock, bIsValidIn2D, bIsCoincided;
234   Standard_Boolean bIsMicroEdge, bHasES;
235   Standard_Integer i, aNbFFs, nF1, nF2;
236   Standard_Integer nV1, nV2, j, aNbCurves;
237   Standard_Real aTolR3D, aTol2D, aT1, aT2, aTolPPC=Precision::PConfusion();
238   TopoDS_Face aF1, aF2;
239   NMTTools_IndexedDataMapOfShapePaveBlock aMEPB;
240   BooleanOperations_IndexedDataMapOfShapeInteger aMapEI;
241   BOPTools_ListIteratorOfListOfPaveBlock anIt;
242   //
243   BOPTools_CArray1OfSSInterference& aFFs=myIP->SSInterferences();
244   //
245   
246   //
247   // 1. Make Section Edges from intersection curves
248   //    between each pair of faces
249   aNbFFs=aFFs.Extent();
250   if (!aNbFFs) {
251     return;
252   }
253   //
254   FillFaceInfo();
255   //
256   for (i=1; i<=aNbFFs; ++i) {
257     BOPTools_ListOfPaveBlock aLPB;
258     TColStd_MapOfInteger aMVStick;
259     TopTools_ListOfShape aLSE;
260     TColStd_ListOfInteger aLNE;
261     BOPTools_PaveSet aPSF;
262     NMTTools_MapOfPaveBlock aMPBX;
263     TColStd_MapIteratorOfMapOfInteger aItMI;
264     NMTTools_MapIteratorOfMapOfPaveBlock aItMPB;
265     //
266     BOPTools_SSInterference& aFFi=aFFs(i);
267     //
268     // Faces
269     aFFi.Indices(nF1, nF2);
270     aF1=*((TopoDS_Face*)(&myDS->Shape(nF1)));
271     aF2=*((TopoDS_Face*)(&myDS->Shape(nF2)));
272     //
273     SharedEdges(nF1, nF2, aLNE, aLSE);
274     aFFi.SetSharedEdges(aLNE);
275     //
276     // aMVStick
277     const NMTTools_FaceInfo& aFI1=myFaceInfo.Find(nF1);
278     const NMTTools_FaceInfo& aFI2=myFaceInfo.Find(nF2);
279     //
280     const TColStd_MapOfInteger& aMVOn1=aFI1.VerticesOn();
281     const TColStd_MapOfInteger& aMVIn1=aFI1.VerticesIn();
282     const TColStd_MapOfInteger& aMVOn2=aFI2.VerticesOn();
283     const TColStd_MapOfInteger& aMVIn2=aFI2.VerticesIn();
284     //
285     for (j=0; j<2; ++j) {
286       const TColStd_MapOfInteger& aMV1=(!j) ? aMVOn1 :aMVIn1;
287       aItMI.Initialize(aMV1);
288       for (; aItMI.More(); aItMI.Next()) {
289         nV1=aItMI.Key();
290         if (aMVOn2.Contains(nV1) || aMVIn2.Contains(nV1)) {
291           aMVStick.Add(nV1);
292         }
293       }
294     }
295     //
296     //  aLPB
297     const NMTTools_MapOfPaveBlock& aMPBIn1=aFI1.PaveBlocksIn();
298     const NMTTools_MapOfPaveBlock& aMPBOn1=aFI1.PaveBlocksOn();
299     const NMTTools_MapOfPaveBlock& aMPBIn2=aFI2.PaveBlocksIn();
300     const NMTTools_MapOfPaveBlock& aMPBOn2=aFI2.PaveBlocksOn();
301     //
302     aMPBX.Clear();
303     for (j=0; j<4; ++j) {
304       NMTTools_MapOfPaveBlock *pMPB;
305       //
306       if (!j) {
307         pMPB=((NMTTools_MapOfPaveBlock*)&aMPBIn1);
308       }
309       else if (j==1) {
310         pMPB=((NMTTools_MapOfPaveBlock*)&aMPBOn1);
311       }
312       else if (j==2) {
313         pMPB=((NMTTools_MapOfPaveBlock*)&aMPBIn2);
314       }
315       else if (j==3) {
316         pMPB=((NMTTools_MapOfPaveBlock*)&aMPBOn2);
317       }
318       //
319       const NMTTools_MapOfPaveBlock& aMPB=*pMPB;
320       aItMPB.Initialize(aMPB);
321       for (; aItMPB.More(); aItMPB.Next()) {
322         const BOPTools_PaveBlock& aPB=aItMPB.Key();
323         if (aMPBX.Add(aPB)) {
324           aLPB.Append(aPB);
325         }
326         //
327         else {
328           if (j>1) {
329             aFFi.AppendBlock(aPB);
330           }
331         }
332         //
333       }
334     }
335     //
336     BOPTools_SequenceOfCurves& aSCvs=aFFi.Curves();
337     aNbCurves=aSCvs.Length();
338     if (!aNbCurves) {
339       continue;
340     }
341     //
342     aTolR3D=aFFi.TolR3D();
343     aTol2D=(aTolR3D < 1.e-3) ? 1.e-3 : aTolR3D;
344     //
345     CorrectTolR3D(aFFi, aMVStick, aTolR3D);
346     //
347     PrepareSetForFace (nF1, nF2, aLPB, aPSF);
348     //
349     // Put Paves On Curves
350     for (j=1; j<=aNbCurves; ++j) {
351       BOPTools_Curve& aBC=aSCvs(j);
352       const IntTools_Curve& aC=aBC.Curve();
353       // DEBUG f
354       Handle(Geom_Curve) aC3D = aC.Curve();
355       // DEBUG t
356       PutPaveOnCurve (aPSF, aTolR3D, aBC);
357     }
358     //
359     // Put bounding paves on curves
360     for (j=1; j<=aNbCurves; ++j) {
361       BOPTools_Curve& aBC=aSCvs(j);
362       PutBoundPaveOnCurve (aBC, aFFi);
363     }
364     //modified by NIZNHY-PKV Wed Sep 14 13:12:14 2011f
365 #if OCC_VERSION_LARGE > 0x06050100 // For OCCT6.5.2 and higher
366     //
367     // Put closing pave if needded
368     for (j=1; j<=aNbCurves; ++j) {
369       BOPTools_Curve& aBC=aSCvs(j);
370       PutClosingPaveOnCurve (aBC, aFFi);
371     }
372 #endif // OCC_VERSION_LARGE > 0x06050100 // For OCCT6.5.2 and higher
373     //modified by NIZNHY-PKV Wed Sep 14 13:12:17 2011t
374     //
375     //  Pave Blocks on Curves
376     bHasES=Standard_False;
377     for (j=1; j<=aNbCurves; ++j) {
378       BOPTools_Curve& aBC=aSCvs(j);
379       const IntTools_Curve& aIC= aBC.Curve();
380       BOPTools_PaveSet& aPaveSet=aBC.Set();
381       //
382       BOPTools_PaveBlockIterator aPBIter(0, aPaveSet);
383       for (; aPBIter.More(); aPBIter.Next()) {
384         BOPTools_PaveBlock& aPBNew=aPBIter.Value();
385         aPBNew.SetCurve(aIC);
386         aPBNew.SetFace1(nF1);
387         aPBNew.SetFace2(nF2);
388         //
389         nV1=aPBNew.Pave1().Index();
390         nV2=aPBNew.Pave2().Index();
391         aT1=aPBNew.Pave1().Param();
392         aT2=aPBNew.Pave2().Param();
393         //
394         if((nV1==nV2) && (Abs(aT2 - aT1) < aTolPPC)) {
395           continue;// mkk ft ???
396         }
397         //
398         // 1
399         bIsExistingPaveBlock=IsExistingPaveBlock(aPBNew, aLPB, aTolR3D);
400         if (bIsExistingPaveBlock) {
401           continue;
402         }
403         //
404         bIsCoincided=CheckCoincidence(aPBNew, aLPB);
405         if(bIsCoincided) {
406           continue;
407         }
408         //
409         // 2
410         bIsExistingPaveBlock=IsExistingPaveBlock(aPBNew, aLSE, aTolR3D);
411         if (bIsExistingPaveBlock) {
412           continue;
413         }
414         //
415         // Checking of validity in 2D
416         //
417         bIsValidIn2D=myContext.IsValidBlockForFaces(aT1, aT2, aIC, aF1, aF2, aTol2D);
418         if (!bIsValidIn2D) {
419           continue;
420         }
421         //
422         //
423         // Make Section Edge
424         TopoDS_Edge aES;
425         //
426         const TopoDS_Vertex aV1=TopoDS::Vertex(myDS->Shape(nV1));
427         const TopoDS_Vertex aV2=TopoDS::Vertex(myDS->Shape(nV2));
428         //
429         {
430           Standard_Real aT;
431           //
432           myContext.IsVertexOnLine(aV1, aIC, aTolR3D, aT);
433           BOPTools_Tools::UpdateVertex (aIC, aT, aV1);
434           //
435           myContext.IsVertexOnLine(aV2, aIC, aTolR3D, aT);
436           BOPTools_Tools::UpdateVertex (aIC, aT, aV2);
437         }
438         //
439         BOPTools_Tools::MakeSectEdge (aIC, aV1, aT1, aV2, aT2, aES);
440         //
441         NMTTools_Tools::UpdateEdge (aES, aTolR3D);
442         bIsMicroEdge=IsMicroEdge(aES, myContext);
443         if (bIsMicroEdge) {
444           continue;
445         }
446         //
447         {
448           Handle(Geom2d_Curve) aC2D1, aC2D2;
449           //
450           aC2D1=aIC.FirstCurve2d();
451           aC2D2=aIC.SecondCurve2d();
452           //
453           NMTTools_Tools::MakePCurve(aES, aF1, aC2D1);
454           NMTTools_Tools::MakePCurve(aES, aF2, aC2D2);
455         }
456         //
457         aMEPB.Add(aES, aPBNew);
458         aMapEI.Add(aES, i);
459         //
460         bHasES=Standard_True;
461       }// for (; aPBIter.More(); aPBIter.Next())
462     } // end of for (j=1; j<=aNbCurves; ++j)
463     // qqf
464     if (bHasES) {
465       myIP->Add(nF1, nF2, Standard_True, NMTDS_TI_FF);
466     }
467     // qqt
468   }// for (i=1; i<=aNbFFs; ++i)
469   //=============================================================
470   //
471   // II. Post treatment
472   //
473   // Input data: aMEPB, aMapEI
474   // Result    : section edges in myDS
475   //
476   Standard_Integer aNbSE;
477   //
478   aNbSE=aMEPB.Extent();
479   if (!aNbSE) {
480     // there is nothing to do here
481     return;
482   }
483   //
484   BRep_Builder aBB;
485   TopoDS_Compound aCompound;
486   //
487   // 1. Make compound from SE
488   aBB.MakeCompound(aCompound);
489   for (i=1; i<=aNbSE; ++i) {
490     const TopoDS_Shape& aSE=aMEPB.FindKey(i);
491     aBB.Add(aCompound, aSE);
492   }
493   //
494   //
495   // 2. Intersect SE using auxiliary Filler
496   NMTTools_PaveFiller tPF;
497   //
498   tPF.SetCompositeShape(aCompound);
499   //
500   // 2.1.VV
501   tPF.Init();
502   tPF.PerformVV();
503   //
504   // 2.2.VE
505   tPF.myPavePool.Resize (tPF.myNbEdges);
506   tPF.PrepareEdges();
507   tPF.PerformVE();
508   //
509   // 2.3.VF
510   tPF.PerformVF();
511   //
512   // 2.4.EE
513   tPF.myCommonBlockPool.Resize (tPF.myNbEdges);
514   tPF.mySplitShapesPool.Resize (tPF.myNbEdges);
515   tPF.myPavePoolNew    .Resize (tPF.myNbEdges);
516
517   tPF.PreparePaveBlocks(TopAbs_VERTEX, TopAbs_EDGE);
518   tPF.PreparePaveBlocks(TopAbs_EDGE, TopAbs_EDGE);
519   //
520   tPF.PerformEE();
521   //
522   tPF.RefinePavePool ();
523   //
524   tPF.myPavePoolNew.Destroy();
525   //
526   tPF.MakeSplitEdges();
527   tPF.UpdateCommonBlocks();
528   //
529   // 3. Treatment of the result of intersection
530   //
531   Standard_Integer aNbOld, aNbLines, aNbPB, mV1, mV2, nE, mE, iFF;
532   TopAbs_ShapeEnum aType;
533   BOPTools_ListIteratorOfListOfPaveBlock aIt;
534   BOPTColStd_IndexedDataMapOfIntegerInteger aMNewOld;
535   //
536   const NMTDS_ShapesDataStructure& tDS=*(tPF.DS());
537   const BOPTools_SplitShapesPool& aSSP=tPF.mySplitShapesPool;
538   const NMTTools_CommonBlockPool& aCBP=tPF.myCommonBlockPool;
539   //
540   aNbLines=tDS.NumberOfInsertedShapes();
541   aNbOld=tDS.NumberOfShapesOfTheObject();
542   //
543   // 3.1 Links between indices in tDS and DS (kept in aMNewOld)
544   //
545   // 3.1.1.Old vertices [ links ]
546   for (i=1; i<=aNbOld; ++i) {
547     const TopoDS_Shape& aV=tDS.Shape(i);
548     aType=aV.ShapeType();
549     if (aType!=TopAbs_VERTEX) {
550       continue;
551     }
552     //
553     for (j=1; j<=aNbSE; ++j) {
554       const BOPTools_PaveBlock& aPBSE=aMEPB(j);
555       nV1=aPBSE.Pave1().Index();
556       const TopoDS_Shape aV1=myDS->Shape(nV1);//mpv
557       if (aV1.IsSame(aV)) {
558         aMNewOld.Add(i, nV1);
559         break;
560       }
561       nV2=aPBSE.Pave2().Index();
562       const TopoDS_Shape aV2=myDS->Shape(nV2);//mpv
563       if (aV2.IsSame(aV)) {
564         aMNewOld.Add(i, nV2);
565         break;
566       }
567     }
568   }
569   //
570   // 3.1.2. New vertices [ links ]
571   i=tDS.NumberOfSourceShapes()+1;
572   for (; i<=aNbLines; ++i) {
573     const TopoDS_Shape& aV=tDS.Shape(i);
574     aType=aV.ShapeType();
575     if (aType!=TopAbs_VERTEX) {
576       continue;
577     }
578     //
579     // Insert new vertex in myDS
580     BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
581     myDS->InsertShapeAndAncestorsSuccessors(aV, anASSeq);
582     nV1=myDS->NumberOfInsertedShapes();
583     // link
584     aMNewOld.Add(i, nV1);
585   }
586   //
587   // 3.2. Treatment of section edges (SE)
588   for (i=1; i<=aNbOld; ++i) {
589     const TopoDS_Shape& aE=tDS.Shape(i);
590     aType=aE.ShapeType();
591     if (aType!=TopAbs_EDGE) {
592       continue;
593     }
594     //
595     //  block of section edge that we already have for this SE
596     BOPTools_PaveBlock& aPBSE=aMEPB.ChangeFromKey(aE);
597     //
598     // Corresponding FF-interference
599     iFF=aMapEI.FindFromKey(aE);
600     BOPTools_SSInterference& aFFi=aFFs(iFF);
601     BOPTools_SequenceOfCurves& aSCvs=aFFi.Curves();
602     //
603     BOPTools_Curve& aBC=aSCvs(1);
604     //
605     const BOPTools_ListOfPaveBlock& aLPB=aSSP(tDS.RefEdge(i));
606     aNbPB=aLPB.Extent();
607     //
608     if (!aNbPB) {
609       // no pave blocks -> use aPBSE and whole edge aE
610       BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
611       //
612       nV1=aPBSE.Pave1().Index();
613       const TopoDS_Shape aV1=myDS->Shape(nV1);//mpv
614       nV2=aPBSE.Pave2().Index();
615       const TopoDS_Shape aV2=myDS->Shape(nV2);//mpv
616       //
617       anASSeq.SetNewSuccessor(nV1);
618       anASSeq.SetNewOrientation(aV1.Orientation());
619       anASSeq.SetNewSuccessor(nV2);
620       anASSeq.SetNewOrientation(aV2.Orientation());
621       //
622       myDS->InsertShapeAndAncestorsSuccessors(aE, anASSeq);
623       nE=myDS->NumberOfInsertedShapes();
624       //
625       aPBSE.SetEdge(nE);
626       aBC.AppendNewBlock(aPBSE);
627       //
628       continue;
629     }
630     //
631     nF1=aPBSE.Face1();
632     nF2=aPBSE.Face2();
633     //
634     const NMTTools_ListOfCommonBlock& aLCB=aCBP(tDS.RefEdge(i));
635     NMTTools_CommonBlockAPI aCBAPI(aLCB);
636     //
637     aIt.Initialize(aLPB);
638     for (; aIt.More(); aIt.Next()) {
639       BOPTools_PaveBlock aPB=aIt.Value();
640       //
641       const TopoDS_Face aF1=TopoDS::Face(myDS->Shape(nF1));
642       const TopoDS_Face aF2=TopoDS::Face(myDS->Shape(nF2));
643       //
644       if (aCBAPI.IsCommonBlock(aPB)) {
645         // it can be Common Block
646         Standard_Real aTolEx;
647         Handle(Geom2d_Curve) aC2D1, aC2D2;
648         TopoDS_Face aF1FWD, aF2FWD;
649         //
650         NMTTools_CommonBlock& aCB=aCBAPI.CommonBlock(aPB);
651         //const BOPTools_ListOfPaveBlock& aLPBx=aCB.PaveBlocks();
652         //
653         aPB=aCB.PaveBlock1();
654         mE=aPB.Edge(); // index of edge in tDS
655         const TopoDS_Edge& aEx=TopoDS::Edge(tDS.Shape(mE));
656         aTolEx=BRep_Tool::Tolerance(aEx);
657         //
658         aF1FWD=aF1;
659         aF1FWD.Orientation(TopAbs_FORWARD);
660         aF2FWD=aF2;
661         aF2FWD.Orientation(TopAbs_FORWARD);
662         //
663         NMTTools_Tools::MakePCurve(aEx, aF1FWD, aC2D1);
664         NMTTools_Tools::MakePCurve(aEx, aF2FWD, aC2D2);
665         NMTTools_Tools::UpdateEdge (aEx, aTolEx);
666       } //if (aCBAPI.IsCommonBlock(aPB))
667       //
668       // new SE
669       mE=aPB.Edge(); // index of edge in tDS
670       const TopoDS_Shape& aSp=tDS.Shape(mE);
671       //
672       const BOPTools_Pave& aPave1=aPB.Pave1();
673       aT1=aPave1.Param();
674       mV1=aPave1.Index();            // index in tDS
675       nV1=aMNewOld.FindFromKey(mV1); // index in myDS
676       const TopoDS_Shape aV1=myDS->Shape(nV1);//mpv
677       //
678       const BOPTools_Pave& aPave2=aPB.Pave2();
679       aT2=aPave2.Param();
680       mV2=aPave2.Index();
681       nV2=aMNewOld.FindFromKey(mV2);
682       const TopoDS_Shape aV2=myDS->Shape(nV2);//mpv
683       //
684       if (!aMNewOld.Contains(mE)) {
685         // add new SE to the myDS
686         BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
687         //
688         anASSeq.SetNewSuccessor(nV1);
689         anASSeq.SetNewOrientation(aV1.Orientation());
690
691         anASSeq.SetNewSuccessor(nV2);
692         anASSeq.SetNewOrientation(aV2.Orientation());
693
694         myDS->InsertShapeAndAncestorsSuccessors(aSp, anASSeq);
695         nE=myDS->NumberOfInsertedShapes();
696         //
697         aMNewOld.Add(mE, nE);
698       }
699       else {
700         nE=aMNewOld.FindFromKey(mE);
701       }
702       // Form PaveBlock;
703       BOPTools_PaveBlock aPBx;
704       BOPTools_Pave aP1, aP2;
705       //
706       aPBx.SetFace1(nF1);
707       aPBx.SetFace1(nF2);
708       //
709       aP1.SetIndex(nV1);
710       aP1.SetParam(aT1);
711       //
712       aP2.SetIndex(nV2);
713       aP2.SetParam(aT2);
714       //
715       aPBx.SetPave1(aP1);
716       aPBx.SetPave2(aP2);
717       //
718       aPBx.SetEdge(nE);
719       //
720       aBC.AppendNewBlock(aPBx);
721     }// for (; aIt.More(); aIt.Next())
722   }// for (i=1; i<=aNbOld; ++i)
723   //
724   myIsDone=Standard_True;
725 }
726
727 //=======================================================================
728 // function: MakePCurves
729 // purpose:
730 //=======================================================================
731 void NMTTools_PaveFiller::MakePCurves()
732 {
733   Standard_Integer i, aNb,  nF1, nF2, nE;
734   Standard_Integer aNbCB, aNbF, nSp, nF;
735   TopAbs_ShapeEnum aType;
736   TopoDS_Face aF1FWD, aF2FWD;
737   TColStd_ListIteratorOfListOfInteger aItF;
738   BOPTools_ListIteratorOfListOfPaveBlock anIt;
739   NMTTools_ListIteratorOfListOfCommonBlock aItCB;
740   //
741   BOPTools_CArray1OfSSInterference& aFFs=myIP->SSInterferences();
742   //
743   aNb=aFFs.Extent();
744   for (i=1; i<=aNb; i++) {
745     BOPTools_SSInterference& aFF=aFFs(i);
746     aFF.Indices(nF1, nF2);
747     //
748     const TopoDS_Face aF1=TopoDS::Face(myDS->Shape(nF1));//mpv
749     const TopoDS_Face aF2=TopoDS::Face(myDS->Shape(nF2));//mpv
750     //
751     aF1FWD=aF1;
752     aF1FWD.Orientation(TopAbs_FORWARD);
753     aF2FWD=aF2;
754     aF2FWD.Orientation(TopAbs_FORWARD);
755     //
756     // In, On parts processing
757     const BOPTools_ListOfPaveBlock& aLPBInOn=aFF.PaveBlocks();
758     //
759     anIt.Initialize(aLPBInOn);
760     for (; anIt.More(); anIt.Next()) {
761       const BOPTools_PaveBlock& aPB=anIt.Value();
762       nE=aPB.Edge();
763       const TopoDS_Edge aE=TopoDS::Edge(myDS->Shape(nE));//mpv
764
765       BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aF1FWD);
766       BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aF2FWD);
767     }
768   }
769   // Check common blocks between edges and faces
770   // Build P-Curves if they were not built in previos block.
771   // The main case is :arguments for e.g aEdge, aFace -> no FFs,
772   // but p-curves are needed.
773   //
774   aNb=myDS->NumberOfShapesOfTheObject();
775   for (i=1; i<=aNb; ++i) {
776     const TopoDS_Shape& aS=myDS->Shape(i);
777     aType=aS.ShapeType();
778     //
779     if (aType!=TopAbs_EDGE) {
780       continue;
781     }
782     const TopoDS_Edge& aE=TopoDS::Edge(aS);
783     //
784     if (BRep_Tool::Degenerated(aE)) {
785       continue;
786     }
787     //
788     const NMTTools_ListOfCommonBlock& aLCB=myCommonBlockPool(myDS->RefEdge(i));
789     aNbCB=aLCB.Extent();
790     if (!aNbCB) {
791       continue;
792     }
793     //
794     aItCB.Initialize(aLCB);
795     for (; aItCB.More(); aItCB.Next()) {
796       const NMTTools_CommonBlock& aCB=aItCB.Value();
797       const BOPTools_PaveBlock &aPB1=aCB.PaveBlock1();
798       //
799       const TColStd_ListOfInteger& aLF=aCB.Faces();
800       aNbF=aLF.Extent();
801       if (!aNbF) {
802         continue;
803       }
804       //
805       nSp=aPB1.Edge();
806       const TopoDS_Edge aSp=TopoDS::Edge(myDS->Shape(nSp));//mpv
807       //
808       aItF.Initialize(aLF);
809       for (; aItF.More(); aItF.Next()) {
810         nF=aItF.Value();
811         aF1FWD=TopoDS::Face(myDS->Shape(nF));
812         aF1FWD.Orientation(TopAbs_FORWARD);
813         //
814         BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aSp, aF1FWD);
815       } // for (; aItCB.More(); aItCB.Next()) {
816     }//if (aS.ShapeType()==TopAbs_EDGE) {
817   }
818 }
819
820 //=======================================================================
821 // function: IsExistingPaveBlock
822 // purpose:
823 //=======================================================================
824 Standard_Boolean NMTTools_PaveFiller::IsExistingPaveBlock(const BOPTools_PaveBlock& aPBNew,
825                                                           const BOPTools_ListOfPaveBlock& aLPBR,
826                                                           const Standard_Real aTolR3D)
827 {
828   Standard_Boolean bFlag;
829   Standard_Integer nVNew1, nVNew2, nV1, nV2, iC;
830   BOPTools_ListIteratorOfListOfPaveBlock anIt;
831   //
832   bFlag=Standard_False;
833   nVNew1=aPBNew.Pave1().Index();
834   nVNew2=aPBNew.Pave2().Index();
835   //
836   anIt.Initialize(aLPBR);
837   for (; anIt.More(); anIt.Next()) {
838     const BOPTools_PaveBlock& aPBR=anIt.Value();
839     nV1=aPBR.Pave1().Index();
840     nV2=aPBR.Pave2().Index();
841     if (nVNew1==nV1 || nVNew1==nV2 || nVNew2==nV1 || nVNew2==nV2) {
842       //
843       iC=CheckIntermediatePoint(aPBNew, aPBR, aTolR3D);
844       if (!iC) {
845         return !bFlag;
846       }
847     }
848   }
849   return bFlag;
850 }
851
852 //=======================================================================
853 // function: IsExistingPaveBlock
854 // purpose:
855 //=======================================================================
856 Standard_Boolean NMTTools_PaveFiller::IsExistingPaveBlock(const BOPTools_PaveBlock& aPBNew,
857                                                           const TopTools_ListOfShape& aLSE,
858                                                           const Standard_Real aTolR3D)
859 {
860   Standard_Boolean bFlag;
861   Standard_Integer aNbSE, iC;
862   Standard_Real aTolE, aTol;
863   TopTools_ListIteratorOfListOfShape anIt;
864   //
865   bFlag=Standard_False;
866   //
867   aNbSE=aLSE.Extent();
868   if (!aNbSE) {
869     return bFlag;
870   }
871   //
872   anIt.Initialize(aLSE);
873   for (; anIt.More(); anIt.Next()) {
874     const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
875     aTolE=BRep_Tool::Tolerance(aE);
876     aTol=aTolR3D;
877     if (aTolE>aTol) {
878       aTol=aTolE;
879     }
880     iC=CheckIntermediatePoint(aPBNew, aE, aTol);
881     if (!iC) {
882       return !bFlag;
883     }
884   }
885   return bFlag;
886 }
887
888 //=======================================================================
889 // function: CheckIntermediatePoint
890 // purpose:
891 //=======================================================================
892 Standard_Integer NMTTools_PaveFiller::CheckIntermediatePoint(const BOPTools_PaveBlock& aPB,
893                                                              const BOPTools_PaveBlock& aPBR,
894                                                              const Standard_Real aTolC)
895 {
896   Standard_Integer iVM, nE2;
897   //
898   nE2=aPBR.Edge();
899   const TopoDS_Edge aE2=TopoDS::Edge(myDS->Shape(nE2));//mpv
900   iVM=CheckIntermediatePoint(aPB, aE2, aTolC);
901   //
902   return iVM;
903 }
904
905 //=======================================================================
906 // function: CheckIntermediatePoint
907 // purpose:
908 //=======================================================================
909 Standard_Integer NMTTools_PaveFiller::CheckIntermediatePoint(const BOPTools_PaveBlock& aPB,
910                                                              const TopoDS_Edge& aE2,
911                                                              const Standard_Real aTolC)
912 {
913   Standard_Real aT11, aT12, aTM, aTmp;
914   Standard_Integer iVM;
915   gp_Pnt aPM;
916   BRep_Builder aBB;
917   TopoDS_Vertex aVM;
918   //
919   // Vertex
920   const BOPTools_Pave& aPave11=aPB.Pave1();
921   aT11=aPave11.Param();
922   //
923   const BOPTools_Pave& aPave12=aPB.Pave2();
924   aT12=aPave12.Param();
925   //
926   aTM=IntTools_Tools::IntermediatePoint (aT11, aT12);
927   //
928   const IntTools_Curve& aIC=aPB.Curve();
929   aIC.D0(aTM, aPM);
930   //
931   aBB.MakeVertex (aVM, aPM, aTolC);
932   //
933   iVM=myContext.ComputeVE(aVM, aE2, aTmp);
934   //
935   return iVM;
936 }
937
938 //=======================================================================
939 // function: PutBoundPaveOnCurve
940 // purpose:
941 //=======================================================================
942 void NMTTools_PaveFiller::PutBoundPaveOnCurve(BOPTools_Curve& aBC,
943                                               BOPTools_SSInterference& aFFi)
944 {
945   Standard_Boolean bHasBounds, bVF;
946   Standard_Integer nF1, nF2;
947   Standard_Real aT1, aT2, aTolR3D;
948   gp_Pnt aP1, aP2;
949   //
950   const IntTools_Curve& aIC=aBC.Curve();
951   bHasBounds=aIC.HasBounds ();
952   if (!bHasBounds){
953     return;
954   }
955   //
956   // Bounds
957   aIC.Bounds (aT1, aT2, aP1, aP2);
958   //
959   // Faces
960   aFFi.Indices(nF1, nF2);
961   aTolR3D=aFFi.TolR3D();
962   //
963   const TopoDS_Face aF1=TopoDS::Face(myDS->GetShape(nF1));//mpv
964   const TopoDS_Face aF2=TopoDS::Face(myDS->GetShape(nF2));//mpv
965   //
966   bVF=myContext.IsValidPointForFaces (aP1, aF1, aF2, aTolR3D);
967   if (bVF) {
968     PutBoundPaveOnCurve (aP1, aT1, aBC, aFFi);
969   }
970   //
971   bVF=myContext.IsValidPointForFaces (aP2, aF1, aF2, aTolR3D);
972   if (bVF) {
973     PutBoundPaveOnCurve (aP2, aT2, aBC, aFFi);
974   }
975 }
976
977 //=======================================================================
978 // function: PutBoundPaveOnCurve
979 // purpose:
980 //=======================================================================
981 void NMTTools_PaveFiller::PutBoundPaveOnCurve(const gp_Pnt& aP,
982                                               const Standard_Real aT,
983                                               BOPTools_Curve& aBC,
984                                               BOPTools_SSInterference& aFFi)
985 {
986   Standard_Boolean bFound1, bFound2;
987   Standard_Integer nV;
988   Standard_Real aTolV=aFFi.TolR3D();
989
990   BOPTools_Pave aPave1, aPave2, aPave;
991   BOPTools_PaveSet& aCPS=aBC.Set();
992   BOPTools_PaveSet& aFFiPS=aFFi.NewPaveSet();
993   const IntTools_Curve& aIC=aBC.Curve();
994   //
995   bFound1=FindPave(aP, aTolV, aCPS  , aPave1);
996   bFound2=FindPave(aP, aTolV, aFFiPS, aPave2);
997   //
998   if (!bFound1 && !bFound2) {
999     TopoDS_Vertex aNewVertex;
1000     BOPTools_Tools::MakeNewVertex(aP, aTolV, aNewVertex);
1001     //
1002     BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
1003     myDS->InsertShapeAndAncestorsSuccessors(aNewVertex, anASSeq);
1004     nV=myDS->NumberOfInsertedShapes();
1005     aPave.SetIndex(nV);
1006     aPave.SetParam(aT);
1007
1008     aCPS.Append(aPave);
1009     aFFiPS.Append(aPave);
1010     //
1011     // Append Techno Vertex to the Curve
1012     TColStd_ListOfInteger& aTVs=aBC.TechnoVertices();
1013     aTVs.Append(nV);
1014   }
1015   if (bFound1 && !bFound2) {
1016     nV=aPave1.Index();
1017     aPave.SetIndex(nV);
1018     aPave.SetParam(aT);
1019     aFFiPS.Append(aPave);
1020     //
1021     const TopoDS_Vertex aV=TopoDS::Vertex(myDS->Shape(nV));//mpv
1022     BOPTools_Tools::UpdateVertex (aIC, aT, aV);
1023   }
1024
1025   if (!bFound1 && bFound2) {
1026     nV=aPave2.Index();
1027     aPave.SetIndex(nV);
1028     aPave.SetParam(aT);
1029     aCPS.Append(aPave);
1030     //
1031     const TopoDS_Vertex aV=TopoDS::Vertex(myDS->Shape(nV));//mpv
1032     BOPTools_Tools::UpdateVertex (aIC, aT, aV);
1033   }
1034 }
1035
1036 //=======================================================================
1037 // function: FindPave
1038 // purpose:
1039 //=======================================================================
1040 Standard_Boolean NMTTools_PaveFiller::FindPave(const gp_Pnt& aP,
1041                                                const Standard_Real aTolPV,
1042                                                const BOPTools_PaveSet& aPS,
1043                                                BOPTools_Pave& aPave)
1044 {
1045   Standard_Integer nV;
1046   Standard_Boolean bIsVertex=Standard_False;
1047
1048   const BOPTools_ListOfPave& aLP=aPS.Set();
1049   BOPTools_ListIteratorOfListOfPave anIt(aLP);
1050   for (; anIt.More(); anIt.Next()) {
1051     const BOPTools_Pave& aPC=anIt.Value();
1052     nV=aPC.Index();
1053     const TopoDS_Vertex aV=TopoDS::Vertex(myDS->Shape(nV));//mpv
1054     bIsVertex=IntTools_Tools::IsVertex (aP, aTolPV, aV);
1055     if (bIsVertex) {
1056       aPave=aPC;
1057       return bIsVertex;
1058     }
1059   }
1060   return bIsVertex;
1061 }
1062
1063 //=======================================================================
1064 // function: PrepareSetForFace
1065 // purpose:
1066 //=======================================================================
1067 void NMTTools_PaveFiller::PrepareSetForFace(const Standard_Integer ,//nF1,
1068                                             const Standard_Integer ,//nF2,
1069                                             const BOPTools_ListOfPaveBlock& aLPBC,
1070                                             BOPTools_PaveSet& aPSF)
1071 {
1072   Standard_Integer nV1, nV2;
1073   TColStd_MapOfInteger aMap;
1074   BOPTools_ListIteratorOfListOfPaveBlock anIt;
1075   //
1076   anIt.Initialize(aLPBC);
1077   for (; anIt.More(); anIt.Next()) {
1078     const BOPTools_PaveBlock& aPB=anIt.Value();
1079     const BOPTools_Pave& aPave1=aPB.Pave1();
1080     nV1=aPave1.Index();
1081     if (!aMap.Contains(nV1)) {
1082       aMap.Add(nV1);
1083       aPSF.Append(aPave1);
1084     }
1085     const BOPTools_Pave& aPave2=aPB.Pave2();
1086     nV2=aPave2.Index();
1087     if (!aMap.Contains(nV2)) {
1088       aMap.Add(nV2);
1089       aPSF.Append(aPave2);
1090     }
1091   }
1092 }
1093
1094 //=======================================================================
1095 // function: CheckCoincidence
1096 // purpose:
1097 //=======================================================================
1098 Standard_Boolean NMTTools_PaveFiller::CheckCoincidence(const BOPTools_PaveBlock& aPBNew,
1099                                                        const BOPTools_ListOfPaveBlock& aLPBFF)
1100 {
1101   Standard_Boolean bRet;
1102   Standard_Real aTE;
1103   Standard_Integer nV11, nV12, nV21, nV22, iVV, iVE, nE2;
1104   Standard_Integer iV, iCount, iCountExt;
1105   BOPTools_ListIteratorOfListOfPaveBlock anIt;
1106   // V11
1107   const BOPTools_Pave& aPave11=aPBNew.Pave1();
1108   nV11=aPave11.Index();
1109   const TopoDS_Vertex& aV11=TopoDS::Vertex(myDS->Shape(nV11));
1110
1111   // V12
1112   const BOPTools_Pave& aPave12=aPBNew.Pave2();
1113   nV12=aPave12.Index();
1114   const TopoDS_Vertex& aV12=TopoDS::Vertex(myDS->Shape(nV12));
1115   //
1116   iCountExt=1;
1117   iCount=0;
1118   anIt.Initialize(aLPBFF);
1119   for (; anIt.More(); anIt.Next()) {
1120     iCount=0;
1121     //
1122     const BOPTools_PaveBlock& aPBR=anIt.Value();
1123     // V21
1124     const BOPTools_Pave& aPave21=aPBR.Pave1();
1125     nV21=aPave21.Index();
1126
1127     // V22
1128     const BOPTools_Pave& aPave22=aPBR.Pave2();
1129     nV22=aPave22.Index();
1130     //
1131     if (nV11==nV21 || nV11==nV22 || nV12==nV21 || nV12==nV22) {
1132       continue;
1133     }
1134     //
1135     // E2
1136     nE2=aPBR.Edge();
1137     //
1138     const TopoDS_Vertex& aV21=TopoDS::Vertex(myDS->Shape(nV21));
1139     const TopoDS_Vertex& aV22=TopoDS::Vertex(myDS->Shape(nV22));
1140     const TopoDS_Edge& aE2=TopoDS::Edge(myDS->Shape(nE2));
1141     //
1142     // VV
1143     iV=0;
1144     iVV=IntTools_Tools::ComputeVV (aV11, aV21);
1145     if (!iVV) {
1146       iCount++;
1147       iV++;
1148       if (iCount>iCountExt) {
1149         break;
1150       }
1151     }
1152     //
1153     iVV=IntTools_Tools::ComputeVV (aV11, aV22);
1154     if (!iVV) {
1155       iCount++;
1156       iV++;
1157       if (iCount>iCountExt) {
1158         break;
1159       }
1160     }
1161     // VE
1162     if (!iV) {
1163       iVE=myContext.ComputeVE (aV11, aE2, aTE);
1164       if (!iVE) {
1165         iCount++;
1166         if (iCount>iCountExt) {
1167           break;
1168         }
1169       }
1170     }
1171     // VV
1172     iV=0;
1173     iVV=IntTools_Tools::ComputeVV (aV12, aV21);
1174     if (!iVV) {
1175       iCount++;
1176       iV++;
1177       if (iCount>iCountExt) {
1178         break;
1179       }
1180     }
1181     //
1182     iVV=IntTools_Tools::ComputeVV (aV12, aV22);
1183     if (!iVV) {
1184       iCount++;
1185       iV++;
1186       if (iCount>iCountExt) {
1187         break;
1188       }
1189     }
1190     // VE
1191     if (!iV) {
1192       iVE=myContext.ComputeVE (aV12, aE2, aTE);
1193       if (!iVE) {
1194         iCount++;
1195         if (iCount>iCountExt) {
1196           break;
1197         }
1198       }
1199     }
1200   } // next aPBR
1201   bRet=(Standard_Boolean)(iCount>iCountExt);
1202   return bRet;
1203 }
1204
1205 //=======================================================================
1206 //function : IsMicroEdge
1207 //purpose  :
1208 //=======================================================================
1209 Standard_Boolean IsMicroEdge(const TopoDS_Edge& aE,
1210                              IntTools_Context& aCtx)
1211 {
1212   Standard_Boolean bRet;
1213   Standard_Integer iErr;
1214   Standard_Real aT1, aT2, aTmp;
1215   Handle(Geom_Curve) aC3D;
1216   TopoDS_Vertex aV1, aV2;
1217   IntTools_Range aR;
1218   //
1219   bRet=(BRep_Tool::Degenerated(aE) ||
1220         !BRep_Tool::IsGeometric(aE));
1221   if (bRet) {
1222     return bRet;
1223   }
1224   //
1225   aC3D=BRep_Tool::Curve(aE, aT1, aT2);
1226   TopExp::Vertices(aE, aV1, aV2);
1227   aT1=BRep_Tool::Parameter(aV1, aE);
1228   aT2=BRep_Tool::Parameter(aV2, aE);
1229   if (aT2<aT1) {
1230     aTmp=aT1;
1231     aT1=aT2;
1232     aT2=aTmp;
1233   }
1234   //
1235   aR.SetFirst(aT1);
1236   aR.SetLast(aT2);
1237   IntTools_ShrunkRange aSR (aE, aV1, aV2, aR, aCtx);
1238   iErr=aSR.ErrorStatus();
1239   bRet=!aSR.IsDone();
1240   //
1241   return bRet;
1242 }
1243 //=======================================================================
1244 // function: PutPaveOnCurve
1245 // purpose:
1246 //=======================================================================
1247 void NMTTools_PaveFiller::PutPaveOnCurve(const BOPTools_PaveSet& aPaveSet,
1248                                          const Standard_Real aTolR3D,
1249                                          BOPTools_Curve& aBC)
1250 {
1251   Standard_Integer nV;
1252   Standard_Boolean bIsVertexOnLine;
1253   Standard_Real aT;
1254   BOPTools_ListIteratorOfListOfPave anIt;
1255   Bnd_Box aBBC;
1256   GeomAdaptor_Curve aGAC;
1257   //
1258   const IntTools_Curve& aC=aBC.Curve();
1259   Handle (Geom_Curve) aC3D= aC.Curve();
1260   aGAC.Load(aC3D);
1261   BndLib_Add3dCurve::Add(aGAC, aTolR3D, aBBC);
1262   //
1263   const BOPTools_ListOfPave& aLP=aPaveSet.Set();
1264   anIt.Initialize(aLP);
1265   for (; anIt.More(); anIt.Next()) {
1266     const BOPTools_Pave& aPave=anIt.Value();
1267     //
1268     nV=aPave.Index();
1269     const TopoDS_Vertex aV=TopoDS::Vertex(myDS->Shape(nV));
1270     //
1271     Bnd_Box aBBV;
1272     BRepBndLib::Add(aV, aBBV);
1273     if (aBBC.IsOut(aBBV)){
1274       continue;
1275     }
1276     //
1277     bIsVertexOnLine=myContext.IsVertexOnLine(aV, aC, aTolR3D, aT);
1278     //
1279     //
1280     if (bIsVertexOnLine) {
1281       BOPTools_Pave aPaveNew(nV, aT, BooleanOperations_SurfaceSurface);
1282       BOPTools_PaveSet& aPS=aBC.Set();
1283       aPS.Append(aPaveNew);
1284       //<-B
1285     }
1286   }
1287 }
1288 //
1289 //=======================================================================
1290 //function : FillFaceInfo
1291 //purpose  : 
1292 //=======================================================================
1293 void NMTTools_PaveFiller::FillFaceInfo()
1294 {
1295   Standard_Integer i, aNbS, aNbFFs, nF, aNbVFs, aNbEFs, j, n1, n2, nX, aNbF;
1296   TopAbs_ShapeEnum aType;
1297   TopoDS_Shape aS;
1298   TColStd_ListIteratorOfListOfInteger aItF; 
1299   BOPTools_ListIteratorOfListOfPaveBlock anItPB;
1300   NMTTools_DataMapIteratorOfDataMapOfIntegerFaceInfo aItMFI;
1301   NMTTools_ListIteratorOfListOfCommonBlock aItCB;
1302   //
1303   myFaceInfo.Clear();
1304   //
1305   BOPTools_CArray1OfSSInterference& aFFs=myIP->SSInterferences();
1306   BOPTools_CArray1OfVSInterference& aVFs=myIP->VSInterferences();
1307   BOPTools_CArray1OfESInterference& aEFs=myIP->ESInterferences();
1308   //
1309   aNbFFs=aFFs.Extent();
1310   if (!aNbFFs) {
1311     return;
1312   }
1313   //
1314   // 0.
1315   for (i=1; i<=aNbFFs; ++i) {
1316     NMTTools_FaceInfo aFI;
1317     //
1318     BOPTools_SSInterference& aFFi=aFFs(i);
1319     aFFi.Indices(n1, n2);
1320     myFaceInfo.Bind(n1, aFI);
1321     myFaceInfo.Bind(n2, aFI);
1322   }
1323   //
1324   // 1.
1325   aNbS=myDS->NumberOfShapesOfTheObject();
1326   for (i=1; i<=aNbS; ++i) {
1327     aS=myDS->Shape(i);
1328     aType=aS.ShapeType();
1329     if (aType==TopAbs_EDGE) {
1330       const NMTTools_ListOfCommonBlock& aLCB=myCommonBlockPool(myDS->RefEdge(i));
1331       aItCB.Initialize(aLCB);
1332       for (; aItCB.More(); aItCB.Next()) {
1333         const NMTTools_CommonBlock& aCB=aItCB.Value();
1334         const BOPTools_PaveBlock &aPB1=aCB.PaveBlock1();
1335         const TColStd_ListOfInteger& aLF=aCB.Faces();
1336         aNbF=aLF.Extent();
1337         if (!aNbF) {
1338           continue;
1339         }
1340         //
1341         aItF.Initialize(aLF);
1342         for (; aItF.More(); aItF.Next()) {
1343           nF=aItF.Value();
1344           if (!myFaceInfo.IsBound(nF)) {
1345             continue;
1346           }
1347           //
1348           NMTTools_FaceInfo& aFI=myFaceInfo.ChangeFind(nF);
1349           aFI.ChangePaveBlocksIn().Add(aPB1);
1350           //
1351           n1=aPB1.Pave1().Index();
1352           n2=aPB1.Pave2().Index();
1353           aFI.ChangeVerticesIn().Add(n1);
1354           aFI.ChangeVerticesIn().Add(n2);
1355         }
1356       }
1357     }// if (aType==TopAbs_EDGE) {
1358     else if (aType==TopAbs_FACE) {
1359       if (!myFaceInfo.IsBound(i)) {
1360         continue;
1361       }
1362       //
1363       BOPTools_ListOfPaveBlock aLPBOn;
1364       //
1365       nF=i;
1366       NMTTools_FaceInfo& aFI=myFaceInfo.ChangeFind(nF);
1367       //
1368       RealSplitsFace(nF, aLPBOn);
1369       //
1370       anItPB.Initialize(aLPBOn);
1371       for (; anItPB.More(); anItPB.Next()) {
1372         const BOPTools_PaveBlock &aPB=anItPB.Value();
1373         aFI.ChangePaveBlocksOn().Add(aPB);
1374         //
1375         n1=aPB.Pave1().Index();
1376         n2=aPB.Pave2().Index();
1377         aFI.ChangeVerticesOn().Add(n1);
1378         aFI.ChangeVerticesOn().Add(n2);
1379       }
1380       //
1381     }// else if (aType==TopAbs_FACE) {
1382   }// for (i=1; i<=aNbS; ++i) {
1383   //
1384   // 2.
1385   aItMFI.Initialize(myFaceInfo);
1386   for (; aItMFI.More(); aItMFI.Next()) {
1387     nF=aItMFI.Key();
1388     NMTTools_FaceInfo& aFI=*((NMTTools_FaceInfo*)&aItMFI.Value());
1389     //
1390     aFI.SetIndex(nF);
1391     //
1392     //
1393     // 2.1 aVFs
1394     aNbVFs=aVFs.Extent();
1395     for (j=1; j<=aNbVFs; ++j) {
1396       BOPTools_VSInterference& aVFj=aVFs(j);
1397       aVFj.Indices(n1, n2);
1398       if (nF==n1) {
1399         aFI.ChangeVerticesIn().Add(n2);
1400       }
1401       else if (nF==n2){
1402         aFI.ChangeVerticesIn().Add(n1);
1403       }
1404     }//  for (j=1; j<=aNbVFs; ++j) {
1405     //
1406     // 2.2 aEFs
1407     aNbEFs=aEFs.Extent();
1408     for (j=1; j<=aNbEFs; ++j) {
1409       BOPTools_ESInterference& aEFj=aEFs(j);
1410       aEFj.Indices(n1, n2);
1411       if (!(nF==n1 || nF==n2)) {
1412         continue;
1413       }
1414       //
1415       nX=aEFj.NewShape();
1416       if (nX<1) {
1417         continue;
1418       }
1419       //
1420       aS=myDS->Shape(nX);
1421       aType=aS.ShapeType();
1422       if (aType!=TopAbs_VERTEX) {
1423         continue;
1424       }
1425       //
1426       aFI.ChangeVerticesIn().Add(nX);
1427     }//  for (j=1; j<=aNbEFs; ++j) {
1428   }// for (; aItMFI.More(); aItMFI.Next()) {
1429 }
1430
1431
1432 #include <gp_Pnt.hxx>
1433 #include <gp_Dir.hxx>
1434 #include <gp_Vec.hxx>
1435 #include <GeomAPI_ProjectPointOnSurf.hxx>
1436 //=======================================================================
1437 //function : CorrectTolR3D
1438 //purpose  : Attempt to correct the value of tolerance aTolR3D for
1439 //           the intersection curve in order to 
1440 //           compel it to pass through the sticks.
1441 //           Prerequisites: 
1442 //             2. The are based on B-Spline surfaces;
1443 //             1. There is at least the one intersection curve;
1444 //             2. The faces have stick vertices to catch the curve;
1445 //             3. The intersection angle is rather small (0.7-7 deg)
1446 //              
1447 //=======================================================================
1448 void NMTTools_PaveFiller::CorrectTolR3D(const BOPTools_SSInterference& aFF,
1449                                         const TColStd_MapOfInteger& aMVStick,
1450                                         Standard_Real& aTolR3D)
1451      
1452 {
1453   Standard_Boolean bHasBounds;
1454   Standard_Integer i, nF[2], nV, aNbCurves;
1455   Standard_Real aT1, aT2, aU, aV, aT, aA, aTolV, aTolVmax;
1456   Standard_Real aTolR, aTolTresh, aAmin, aAmax;
1457   TColStd_MapIteratorOfMapOfInteger aIt;
1458   gp_Pnt aP, aP1, aP2;
1459   gp_Dir aDN[2];
1460   gp_Vec aVT;
1461   Handle(Geom_Surface) aS[2];
1462   Handle(Geom_Curve) aC3D;
1463   GeomAdaptor_Surface aGAS;
1464   GeomAbs_SurfaceType aType;
1465   TopoDS_Face aF[2];
1466   //
1467   aTolTresh=0.0005;
1468   aAmin=0.012;// 0.7-7 deg
1469   aAmax=0.12;
1470   //
1471   if (!aMVStick.Extent()) {
1472     return;
1473   }
1474   //
1475   BOPTools_SSInterference& aFFi=*((BOPTools_SSInterference*)&aFF);
1476   BOPTools_SequenceOfCurves& aSCvs=aFFi.Curves();
1477   aNbCurves=aSCvs.Length();
1478   if (aNbCurves!=1){
1479     return;
1480   }
1481   //
1482   aFFi.Indices(nF[0], nF[1]);
1483   for (i=0; i<2; ++i) {
1484     aF[i]=*((TopoDS_Face*)(&myDS->Shape(nF[i])));
1485     aS[i]=BRep_Tool::Surface(aF[i]);
1486     aGAS.Load(aS[i]);
1487     aType=aGAS.GetType();
1488     if (aType!=GeomAbs_BSplineSurface) {
1489       return;
1490     }
1491   }
1492   //
1493   BOPTools_Curve& aBC=aSCvs(1);
1494   const IntTools_Curve& aIC=aBC.Curve();
1495   bHasBounds=aIC.HasBounds();
1496   if (!bHasBounds){
1497     return;
1498   }
1499   //
1500   aIC.Bounds (aT1, aT2, aP1, aP2);
1501   aT=IntTools_Tools::IntermediatePoint(aT1, aT2);
1502   aC3D=aIC.Curve();
1503   aC3D->D0(aT, aP);
1504   //
1505   for (i=0; i<2; ++i) {
1506     GeomAPI_ProjectPointOnSurf& aPPS=myContext.ProjPS(aF[i]);
1507     aPPS.Perform(aP);
1508     aPPS.LowerDistanceParameters(aU, aV);
1509     BOPTools_Tools3D::GetNormalToSurface(aS[i], aU, aV, aDN[i]);
1510   }
1511   //
1512   aA=aDN[0].Angle(aDN[1]);
1513   aA=fabs(aA);
1514   if (aA>0.5*PI) {
1515     aA=PI-aA;
1516   }
1517   //
1518   if (aA<aAmin || aA>aAmax) {
1519     return;
1520   }
1521   //
1522   aTolVmax=-1.;
1523   aIt.Initialize(aMVStick);
1524   for (; aIt.More(); aIt.Next()) {
1525     nV=aIt.Key();
1526     const TopoDS_Vertex& aV=*((TopoDS_Vertex*)(&myDS->Shape(nV)));
1527     aTolV=BRep_Tool::Tolerance(aV);
1528     if (aTolV>aTolVmax) {
1529       aTolVmax=aTolV;
1530     }
1531   }
1532   //
1533   
1534   aTolR=aTolVmax/aA;
1535   if (aTolR<aTolTresh) {
1536     aTolR3D=aTolR;
1537   }
1538 }
1539
1540 //modified by NIZNHY-PKV Wed Sep 14 13:21:13 2011f
1541 #if OCC_VERSION_LARGE > 0x06050100 // For OCCT6.5.2 and higher
1542 //=======================================================================
1543 // function: PutClosingPaveOnCurve
1544 // purpose:
1545 //=======================================================================
1546 void NMTTools_PaveFiller::PutClosingPaveOnCurve(BOPTools_Curve& aBC,
1547                                                 BOPTools_SSInterference& aFFi)
1548 {
1549   Standard_Boolean bIsClosed, bHasBounds, bAdded;
1550   Standard_Integer nVC, j;
1551   Standard_Real aT[2], aTolR3D, aTC, dT, aTx;
1552   gp_Pnt aP[2] ; 
1553   BOPTools_Pave aPVx;
1554   BOPTools_ListIteratorOfListOfPave aItLP;
1555   //
1556   const IntTools_Curve& aIC=aBC.Curve();
1557   const Handle (Geom_Curve)& aC3D=aIC.Curve();
1558   if(aC3D.IsNull()) {
1559     return;
1560   }
1561   //
1562   bIsClosed=IntTools_Tools::IsClosed(aC3D);
1563   if (!bIsClosed) {
1564     return;
1565   }
1566   //
1567   bHasBounds=aIC.HasBounds ();
1568   if (!bHasBounds){
1569     return;
1570   }
1571   // 
1572   bAdded=Standard_False;
1573   dT=Precision::PConfusion();
1574   aTolR3D=aFFi.TolR3D();
1575   aIC.Bounds (aT[0], aT[1], aP[0], aP[1]);
1576   //
1577   BOPTools_PaveSet& aFFiPS=aFFi.NewPaveSet();
1578   BOPTools_PaveSet& aCPS=aBC.Set();
1579   //
1580   const BOPTools_ListOfPave& aLP=aCPS.Set();
1581   aItLP.Initialize(aLP);
1582   for (; aItLP.More() && !bAdded; aItLP.Next()) {
1583     const BOPTools_Pave& aPC=aItLP.Value();
1584     nVC=aPC.Index();
1585     const TopoDS_Vertex aVC=TopoDS::Vertex(myDS->Shape(nVC));
1586     aTC=aPC.Param();
1587     //
1588     for (j=0; j<2; ++j) {
1589       if (fabs(aTC-aT[j]) < dT) {
1590         aTx=(!j) ? aT[1] : aT[0];
1591         aPVx.SetIndex(nVC);
1592         aPVx.SetParam(aTx);
1593         //
1594         aCPS.Append(aPVx);
1595         aFFiPS.Append(aPVx);
1596         //
1597         bAdded=Standard_True;
1598         break;
1599       }
1600     }
1601   }
1602 }
1603 #endif // OCC_VERSION_LARGE > 0x06050100 // For OCCT6.5.2 and higher
1604 //modified by NIZNHY-PKV Wed Sep 14 13:21:17 2011t