//=======================================================================
// function:  CommonBlock
// purpose:
//=======================================================================
  NMTTools_CommonBlock& NMTTools_CommonBlockAPI::CommonBlock(const BOPTools_PaveBlock& aPB)const
{
  static NMTTools_CommonBlock aCBTMP;
  //
  NMTTools_ListIteratorOfListOfCommonBlock anItCB;
  BOPTools_ListIteratorOfListOfPaveBlock anItPB;
  //
  NMTTools_ListOfCommonBlock* pLCB=(NMTTools_ListOfCommonBlock*)myListOfCommonBlock;
  //
  anItCB.Initialize(*pLCB);
  for (; anItCB.More(); anItCB.Next()) {
    NMTTools_CommonBlock& aCB=anItCB.ChangeValue();
    //
    const BOPTools_ListOfPaveBlock& aLPB=aCB.PaveBlocks();
    anItPB.Initialize(aLPB);
    for (; anItPB.More(); anItPB.Next()) {
      const BOPTools_PaveBlock& aPBx=anItPB.Value();
      if (aPBx.IsEqual(aPB)) {
        return aCB;
      }
    }
  }
  return aCBTMP;
}
//=======================================================================
// function: UpdateCommonBlocks
// purpose:
//=======================================================================
void NMTTools_PaveFiller::UpdateCommonBlocks()
{
  myIsDone=Standard_False;
  //
  Standard_Integer nE, aNbS,  nSp, nEx, nSpx;
  NMTTools_ListIteratorOfListOfCommonBlock aCBIt;
  BOPTools_ListIteratorOfListOfPaveBlock aPBIt;
  //
  aNbS=myDS->NumberOfShapesOfTheObject();
  //
  for (nE=1; nE<=aNbS; ++nE) {
    if (myDS->GetShapeType(nE)!=TopAbs_EDGE){
      continue;
    }
    if (BRep_Tool::Degenerated(TopoDS::Edge(myDS->Shape(nE)))){
      continue;
    }
    //
    NMTTools_ListOfCommonBlock& aLCB=myCommonBlockPool(myDS->RefEdge(nE));
    /*BOPTools_ListOfPaveBlock& aLPB=*/mySplitShapesPool  (myDS->RefEdge(nE));
    //
    aCBIt.Initialize(aLCB);
    for (; aCBIt.More(); aCBIt.Next()) {
      NMTTools_CommonBlock& aCB=aCBIt.ChangeValue();
      //
      // Among all PBs of aCB the first PB will be one
      // that have max tolerance value
      {
        Standard_Real aTolEx, aTolExMax;
        BOPTools_ListOfPaveBlock *pLPB, aLPBx;
        //
        aTolExMax=-1.;
        pLPB=(BOPTools_ListOfPaveBlock *)&aCB.PaveBlocks();
        aPBIt.Initialize(*pLPB);
        for (; aPBIt.More(); aPBIt.Next()) {
          const BOPTools_PaveBlock& aPBx=aPBIt.Value();
          nEx=aPBx.OriginalEdge();
          const TopoDS_Edge& aEx=TopoDS::Edge(myDS->Shape(nEx));
          aTolEx=BRep_Tool::Tolerance(aEx);
          if (aTolEx>aTolExMax) {
            aTolExMax=aTolEx;
            aLPBx.Prepend(aPBx);
          }
          else{
            aLPBx.Append(aPBx);
          }
        }
        //
        pLPB->Clear();
        *pLPB=aLPBx;
      }
      //
      BOPTools_PaveBlock& aPB=aCB.PaveBlock1(nE);
      nSp=SplitIndex(aPB);
      aPB.SetEdge(nSp);
      //
      const BOPTools_ListOfPaveBlock& aCBLPB=aCB.PaveBlocks();
      aPBIt.Initialize(aCBLPB);
      for (; aPBIt.More(); aPBIt.Next()) {
        BOPTools_PaveBlock& aPBx=aPBIt.Value();
        nEx=aPBx.OriginalEdge();
        if (nEx==nE) {
          continue;
        }
        //
        nSpx=SplitIndex(aPBx);
        aPBx.SetEdge(nSpx);
      }
      //
    }
  }
}
//=======================================================================
// function: UpdateCommonBlocks
// purpose:
//=======================================================================
void NMTTools_PaveFiller::UpdateCommonBlocks(const Standard_Integer)
{
  Standard_Integer nE, aNbS,  nEx, nEMax, j, aNbPoints, aNbLCB, nF;
  Standard_Real aTolEx, aTolExMax, aTSRMax[2], aTx[2], aTmp;
  TColStd_ListIteratorOfListOfInteger aItLI;
  gp_Pnt aPMax[2];
  TopoDS_Edge aEMax;
  BOPTools_ListIteratorOfListOfPaveBlock aItLPB, aItLPBS;
  NMTTools_ListIteratorOfListOfCommonBlock aItLCB;
  NMTTools_MapOfPaveBlock aMPB;
  //
  myIsDone=Standard_False;
  //
  aNbS=myDS->NumberOfShapesOfTheObject();
  for (nE=1; nE<=aNbS; ++nE) {
    if (myDS->GetShapeType(nE)!=TopAbs_EDGE){
      continue;
    }
    //
    const TopoDS_Edge& aE=*((TopoDS_Edge*)&myDS->Shape(nE));
    if (BRep_Tool::Degenerated(aE)){
      continue;
    }
    //
    NMTTools_ListOfCommonBlock& aLCB=myCommonBlockPool(myDS->RefEdge(nE));
    //modified by NIZNHY-PKV Thu Jan 19 09:03:19 2012f
    aNbLCB=aLCB.Extent();
    if (!aNbLCB) {
      continue;
    }
    // 0
    NMTTools_ListOfCommonBlock aLCBx;
    //
    aItLCB.Initialize(aLCB);
    for (; aItLCB.More(); aItLCB.Next()) {
      NMTTools_CommonBlock aCBx;
      //
      NMTTools_CommonBlock& aCB=aItLCB.ChangeValue();
      const BOPTools_ListOfPaveBlock &aLPB=aCB.PaveBlocks();
      aItLPB.Initialize(aLPB);
      for (; aItLPB.More(); aItLPB.Next()) {
        const BOPTools_PaveBlock& aPBx=aItLPB.Value();
        nEx=aPBx.OriginalEdge();
        BOPTools_ListOfPaveBlock& aLPBS=mySplitShapesPool(myDS->RefEdge(nEx));
        aItLPBS.Initialize(aLPBS);
        for (; aItLPBS.More(); aItLPBS.Next()) {
          const BOPTools_PaveBlock& aPBSx=aItLPBS.Value();
          if (aPBSx.IsEqual(aPBx)) {
            aCBx.AddPaveBlock(aPBSx);
            break;
          }
        }// for (; aItLPBS.More(); aItLPBS.Next()) {
      }// for (; aItLPB.More(); aItLPB.Next()) {
      //
      const TColStd_ListOfInteger& aLI=aCB.Faces();
      aItLI.Initialize(aLI);
      for (; aItLI.More(); aItLI.Next()) {
        nF=aItLI.Value();
        aCBx.AddFace(nF);
      }
      //
      aLCBx.Append(aCBx);
    }//for (; aItLCB.More(); aItLCB.Next()) {
    //
    aLCB.Clear();
    //
    aItLCB.Initialize(aLCBx);
    for (; aItLCB.More(); aItLCB.Next()) {
      NMTTools_CommonBlock& aCBx=aItLCB.ChangeValue();
      aLCB.Append(aCBx);
    }
    //modified by NIZNHY-PKV Thu Jan 19 09:03:30 2012t
    // 1
    aItLCB.Initialize(aLCB);
    for (; aItLCB.More(); aItLCB.Next()) {
      NMTTools_CommonBlock& aCB=aItLCB.ChangeValue();
      //
      BOPTools_PaveBlock aPBMax;
      aTolExMax=-1.;
      const BOPTools_ListOfPaveBlock &aLPB=aCB.PaveBlocks();
      aItLPB.Initialize(aLPB);
      for (; aItLPB.More(); aItLPB.Next()) {
        const BOPTools_PaveBlock& aPBx=aItLPB.Value();
        nEx=aPBx.OriginalEdge();
        const TopoDS_Edge& aEx=*((TopoDS_Edge*)&myDS->Shape(nEx));
        aTolEx=BRep_Tool::Tolerance(aEx);
        if (aTolEx>aTolExMax) {
          aTolExMax=aTolEx;
          aEMax=aEx;
          aPBMax=aPBx;
        }
      }
      //
      // 2
      if (aMPB.Contains(aPBMax)) {
        continue;
      }
      aMPB.Add(aPBMax);
      //
      nEMax=aPBMax.OriginalEdge();
      const IntTools_ShrunkRange& aISRMax=aPBMax.ShrunkRange();
      const IntTools_Range& aSRMax=aISRMax.ShrunkRange();
      const Bnd_Box& aBoxMax=aISRMax.BndBox();
      aSRMax.Range(aTSRMax[0], aTSRMax[1]);
      for (j=0; j<2; ++j) {
        BOPTools_Tools::PointOnEdge(aEMax, aTSRMax[j], aPMax[j]);
      }
      //
      // 3
      aItLPB.Initialize(aLPB);
      for (; aItLPB.More(); aItLPB.Next()) {
        const BOPTools_PaveBlock& aPBx=aItLPB.Value();
        nEx=aPBx.OriginalEdge();
        if (nEx==nEMax) {
          continue;
        }
        //
        const TopoDS_Edge& aEx=*((TopoDS_Edge*)&myDS->Shape(nEx));
        GeomAPI_ProjectPointOnCurve& aPPCx=myContext->ProjPC(aEx);
        //
        for (j=0; j<2; ++j) {
          aPPCx.Perform(aPMax[j]);
          aNbPoints=aPPCx.NbPoints();
          if (!aNbPoints) {
            break;
          }
          aTx[j]=aPPCx.LowerDistanceParameter();
        }
        if (!aNbPoints) {
          // correction the range is impossible due to
          // a projection problem
          continue;
        }
        //
        if (aTx[0]>aTx[1]){
          aTmp=aTx[0];
          aTx[0]=aTx[1];
          aTx[1]=aTmp;
        }
        //
        // 4 Correction
        // 4.1 aPBx
        {
          const IntTools_ShrunkRange& aISRx=aPBx.ShrunkRange();
          IntTools_Range *pSRx=(IntTools_Range *)(&aISRx.ShrunkRange());
          Bnd_Box *pBoxx=(Bnd_Box *)(&aISRx.BndBox());
          //
          pSRx->SetFirst(aTx[0]);
          pSRx->SetLast(aTx[1]);
          *pBoxx=aBoxMax;
        }
        //
        // 4.2 aPBSx
        BOPTools_ListOfPaveBlock& aLPBSx=mySplitShapesPool(myDS->RefEdge(nEx));
        aItLPBS.Initialize(aLPBSx);
        for (; aItLPBS.More(); aItLPBS.Next()) {
          const BOPTools_PaveBlock& aPBSx=aItLPBS.Value();
          if (!aPBSx.IsEqual(aPBx)) {
            continue;
          }
          //
          const IntTools_ShrunkRange& aISRx=aPBSx.ShrunkRange();
          IntTools_Range *pSRx=(IntTools_Range *)(&aISRx.ShrunkRange());
          Bnd_Box *pBoxx=(Bnd_Box *)(&aISRx.BndBox());
          //
          pSRx->SetFirst(aTx[0]);
          pSRx->SetLast(aTx[1]);
          *pBoxx=aBoxMax;
        }
        //
        //
      }//for (; aItLPB.More(); aItLPB.Next()) {
    }//for (; aItLCB.More(); aItLCB.Next()) {
  }//for (nE=1; nE<=aNbS; ++nE) {
}