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