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