Back to index

salome-geom  6.5.0
BlockFix_UnionEdges.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 
00022 // File:      BlockFix_UnionEdges.cxx
00023 // Created:   07.12.04 15:27:30
00024 // Author:    Sergey KUUL
00025 
00026 #include <BlockFix_UnionEdges.ixx>
00027 
00028 #include <ShapeAnalysis_Edge.hxx>
00029 
00030 #include <ShapeFix_Edge.hxx>
00031 #include <ShapeFix_Face.hxx>
00032 #include <ShapeFix_Shell.hxx>
00033 
00034 #include <BRep_Builder.hxx>
00035 #include <BRep_CurveRepresentation.hxx>
00036 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
00037 #include <BRep_TEdge.hxx>
00038 #include <BRep_Tool.hxx>
00039 #include <BRepAdaptor_HCompCurve.hxx>
00040 #include <BRepLib.hxx>
00041 #include <BRepLib_MakeEdge.hxx>
00042 
00043 #include <TopExp.hxx>
00044 #include <TopExp_Explorer.hxx>
00045 
00046 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
00047 #include <TopTools_IndexedMapOfShape.hxx>
00048 #include <TopTools_ListOfShape.hxx>
00049 #include <TopTools_MapOfShape.hxx>
00050 #include <TopTools_ListIteratorOfListOfShape.hxx>
00051 #include <TopTools_SequenceOfShape.hxx>
00052 
00053 #include <TopoDS.hxx>
00054 #include <TopoDS_Edge.hxx>
00055 #include <TopoDS_Face.hxx>
00056 #include <TopoDS_Shell.hxx>
00057 #include <TopoDS_Solid.hxx>
00058 #include <TopoDS_Vertex.hxx>
00059 #include <TopoDS_Iterator.hxx>
00060 
00061 #include <Approx_Curve3d.hxx>
00062 
00063 #include <GC_MakeCircle.hxx>
00064 
00065 #include <Geom_BSplineCurve.hxx>
00066 #include <Geom_Circle.hxx>
00067 #include <Geom_Curve.hxx>
00068 #include <Geom_Line.hxx>
00069 #include <Geom_TrimmedCurve.hxx>
00070 #include <GeomConvert.hxx>
00071 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
00072 
00073 #include <Geom2dConvert.hxx>
00074 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
00075 #include <Geom2d_TrimmedCurve.hxx>
00076 #include <Geom2d_BSplineCurve.hxx>
00077 
00078 #include <TColGeom_SequenceOfSurface.hxx>
00079 #include <TColGeom_Array1OfBSplineCurve.hxx>
00080 #include <TColGeom_HArray1OfBSplineCurve.hxx>
00081 #include <TColGeom2d_Array1OfBSplineCurve.hxx>
00082 #include <TColGeom2d_HArray1OfBSplineCurve.hxx>
00083 #include <TColGeom2d_SequenceOfBoundedCurve.hxx>
00084 #include <TColgp_SequenceOfPnt.hxx>
00085 #include <TColStd_Array1OfReal.hxx>
00086 #include <TColStd_MapOfInteger.hxx>
00087 
00088 #include "utilities.h"
00089 
00090 //=======================================================================
00091 //function : BlockFix_UnionEdges()
00092 //purpose  : Constructor
00093 //=======================================================================
00094 BlockFix_UnionEdges::BlockFix_UnionEdges (  )
00095 {
00096 }
00097 
00098 //=======================================================================
00099 //function : GlueEdgesWithPCurves
00100 //purpose  : Glues the pcurves of the sequence of edges
00101 //           and glues their 3d curves
00102 //=======================================================================
00103 static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
00104                                         const TopoDS_Vertex& FirstVertex,
00105                                         const TopoDS_Vertex& LastVertex)
00106 {
00107   Standard_Integer i, j;
00108 
00109   TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
00110   //TColGeom2d_SequenceOfCurve PCurveSeq;
00111   TColGeom_SequenceOfSurface SurfSeq;
00112   //TopTools_SequenceOfShape LocSeq;
00113   
00114   BRep_ListIteratorOfListOfCurveRepresentation itr( (Handle(BRep_TEdge)::DownCast(FirstEdge.TShape()))->Curves() );
00115   for (; itr.More(); itr.Next())
00116   {
00117     Handle(BRep_CurveRepresentation) CurveRep = itr.Value();
00118     if (CurveRep->IsCurveOnSurface())
00119     {
00120       //PCurveSeq.Append(CurveRep->PCurve());
00121       SurfSeq.Append(CurveRep->Surface());
00122       /*
00123       TopoDS_Shape aLocShape;
00124       aLocShape.Location(CurveRep->Location());
00125       LocSeq.Append(aLocShape);
00126       */
00127     }
00128   }
00129 
00130   Standard_Real fpar, lpar;
00131   BRep_Tool::Range(FirstEdge, fpar, lpar);
00132   TopoDS_Edge PrevEdge = FirstEdge;
00133   TopoDS_Vertex CV;
00134   Standard_Real MaxTol = 0.;
00135   
00136   TopoDS_Edge ResEdge;
00137   BRep_Builder BB;
00138 
00139   Standard_Integer nb_curve = aChain.Length();   //number of curves
00140   TColGeom_Array1OfBSplineCurve tab_c3d(0,nb_curve-1);                    //array of the curves
00141   TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2);  //array of the tolerances
00142     
00143   TopoDS_Vertex PrevVertex = FirstVertex;
00144   for (i = 1; i <= nb_curve; i++)
00145   {
00146     TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
00147     TopoDS_Vertex VF, VL;
00148     TopExp::Vertices(anEdge, VF, VL);
00149     Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
00150     
00151     Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
00152     Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
00153     if (Tol1 > MaxTol)
00154       MaxTol = Tol1;
00155     if (Tol2 > MaxTol)
00156       MaxTol = Tol2;
00157     
00158     if (i > 1)
00159     {
00160       TopExp::CommonVertex(PrevEdge, anEdge, CV);
00161       Standard_Real Tol = BRep_Tool::Tolerance(CV);
00162       tabtolvertex(i-2) = Tol;
00163     }
00164     
00165     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
00166     Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(aCurve, fpar, lpar);
00167     tab_c3d(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
00168     GeomConvert::C0BSplineToC1BSplineCurve(tab_c3d(i-1), Precision::Confusion());
00169     if (ToReverse)
00170       tab_c3d(i-1)->Reverse();
00171     PrevVertex = (ToReverse)? VF : VL;
00172     PrevEdge = anEdge;
00173   }
00174   Handle(TColGeom_HArray1OfBSplineCurve)  concatcurve;     //array of the concatenated curves
00175   Handle(TColStd_HArray1OfInteger)        ArrayOfIndices;  //array of the remining Vertex
00176   GeomConvert::ConcatC1(tab_c3d,
00177                         tabtolvertex,
00178                         ArrayOfIndices,
00179                         concatcurve,
00180                         Standard_False,
00181                         Precision::Confusion());   //C1 concatenation
00182   
00183   if (concatcurve->Length() > 1)
00184   {
00185     GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
00186     
00187     for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
00188       Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
00189     
00190     concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
00191   }
00192   Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
00193   
00194   TColGeom2d_SequenceOfBoundedCurve ResPCurves;
00195   TopLoc_Location aLoc;
00196   for (j = 1; j <= SurfSeq.Length(); j++)
00197   {
00198     TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
00199     
00200     PrevVertex = FirstVertex;
00201     PrevEdge = FirstEdge;
00202     //TopLoc_Location theLoc = LocSeq(j).Location();
00203     for (i = 1; i <= nb_curve; i++)
00204     {
00205       TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
00206       TopoDS_Vertex VF, VL;
00207       TopExp::Vertices(anEdge, VF, VL);
00208       Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
00209 
00210       /*
00211       Handle(Geom2d_Curve) aPCurve =
00212         BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), anEdge.Location()*theLoc, fpar, lpar);
00213       */
00214       Handle(Geom2d_Curve) aPCurve =
00215         BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), aLoc, fpar, lpar);
00216       Handle(Geom2d_TrimmedCurve) aTrPCurve = new Geom2d_TrimmedCurve(aPCurve, fpar, lpar);
00217       tab_c2d(i-1) = Geom2dConvert::CurveToBSplineCurve(aTrPCurve);
00218       Geom2dConvert::C0BSplineToC1BSplineCurve(tab_c2d(i-1), Precision::Confusion());
00219       if (ToReverse)
00220         tab_c2d(i-1)->Reverse();
00221       PrevVertex = (ToReverse)? VF : VL;
00222       PrevEdge = anEdge;
00223     }
00224     Handle(TColGeom2d_HArray1OfBSplineCurve)  concatc2d;     //array of the concatenated curves
00225     Handle(TColStd_HArray1OfInteger)        ArrayOfInd2d;  //array of the remining Vertex
00226     Geom2dConvert::ConcatC1(tab_c2d,
00227                             tabtolvertex,
00228                             ArrayOfInd2d,
00229                             concatc2d,
00230                             Standard_False,
00231                             Precision::Confusion());   //C1 concatenation
00232     
00233     if (concatc2d->Length() > 1)
00234     {
00235       Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
00236       
00237       for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
00238         Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
00239       
00240       concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
00241     }
00242     Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
00243     ResPCurves.Append(aResPCurve);
00244   }
00245   
00246   ResEdge = BRepLib_MakeEdge(ResCurve,
00247                              FirstVertex, LastVertex,
00248                              ResCurve->FirstParameter(), ResCurve->LastParameter());
00249   BB.SameRange(ResEdge, Standard_False);
00250   BB.SameParameter(ResEdge, Standard_False);
00251   for (j = 1; j <= ResPCurves.Length(); j++)
00252   {
00253     BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), aLoc, MaxTol);
00254     BB.Range(ResEdge, SurfSeq(j), aLoc, ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
00255   }
00256 
00257   BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
00258   
00259   return ResEdge;
00260 }
00261 
00262 //=======================================================================
00263 //function : MergeEdges
00264 //purpose  : auxilary
00265 //=======================================================================
00266 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
00267                                    const TopoDS_Face& aFace,
00268                                    const Standard_Real Tol,
00269                                    TopoDS_Edge& anEdge)
00270 {
00271   // make chain for union
00272   BRep_Builder B;
00273   ShapeAnalysis_Edge sae;
00274   TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
00275   TopoDS_Edge LastE = FirstE;
00276   TopoDS_Vertex VF = sae.FirstVertex(FirstE);
00277   TopoDS_Vertex VL = sae.LastVertex(LastE);
00278   TopTools_SequenceOfShape aChain;
00279   aChain.Append(FirstE);
00280   TColStd_MapOfInteger IndUsedEdges;
00281   IndUsedEdges.Add(1);
00282   Standard_Integer j;
00283   for(j=2; j<=SeqEdges.Length(); j++) {
00284     for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
00285       if(IndUsedEdges.Contains(k)) continue;
00286       TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
00287       TopoDS_Vertex VF2 = sae.FirstVertex(edge);
00288       TopoDS_Vertex VL2 = sae.LastVertex(edge);
00289       if(sae.FirstVertex(edge).IsSame(VL)) {
00290         aChain.Append(edge);
00291         LastE = edge;
00292         VL = sae.LastVertex(LastE);
00293         IndUsedEdges.Add(k);
00294       }
00295       else if(sae.LastVertex(edge).IsSame(VF)) {
00296         aChain.Prepend(edge);
00297         FirstE = edge;
00298         VF = sae.FirstVertex(FirstE);
00299         IndUsedEdges.Add(k);
00300       }
00301     }
00302   }
00303   if(aChain.Length()<SeqEdges.Length()) {
00304     MESSAGE ("can not create correct chain...");
00305     return Standard_False;
00306   }
00307   // union edges in chain
00308   // first step: union lines and circles
00309   TopLoc_Location Loc;
00310   Standard_Real fp1,lp1,fp2,lp2;
00311   for(j=1; j<aChain.Length(); j++) {
00312     TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
00313     Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,Loc,fp1,lp1);
00314     if(c3d1.IsNull()) break;
00315     while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
00316       Handle(Geom_TrimmedCurve) tc =
00317         Handle(Geom_TrimmedCurve)::DownCast(c3d1);
00318       c3d1 = tc->BasisCurve();
00319     }
00320     TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
00321     Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,Loc,fp2,lp2);
00322     if(c3d2.IsNull()) break;
00323     while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
00324       Handle(Geom_TrimmedCurve) tc =
00325         Handle(Geom_TrimmedCurve)::DownCast(c3d2);
00326       c3d2 = tc->BasisCurve();
00327     }
00328     if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
00329       // union lines
00330       Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
00331       Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
00332       gp_Dir Dir1 = L1->Position().Direction();
00333       gp_Dir Dir2 = L2->Position().Direction();
00334       //if(!Dir1.IsEqual(Dir2,Precision::Angular())) {
00335       //if(!Dir1.IsParallel(Dir2,Precision::Angular())) {
00336       if(!Dir1.IsParallel(Dir2,Tol)) {
00337         continue;
00338       }
00339       // can union lines => create new edge
00340       TopoDS_Vertex V1 = sae.FirstVertex(edge1);
00341       gp_Pnt PV1 = BRep_Tool::Pnt(V1);
00342       TopoDS_Vertex V2 = sae.LastVertex(edge2);
00343       gp_Pnt PV2 = BRep_Tool::Pnt(V2);
00344       gp_Vec Vec(PV1,PV2);
00345       Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
00346       Standard_Real dist = PV1.Distance(PV2);
00347       Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
00348       TopoDS_Edge E;
00349       B.MakeEdge (E,tc,Precision::Confusion());
00350       B.Add (E,V1);  B.Add (E,V2);
00351       B.UpdateVertex(V1, 0., E, 0.);
00352       B.UpdateVertex(V2, dist, E, 0.);
00353       //ShapeFix_Edge sfe;
00354       //sfe.FixAddPCurve(E,aFace,Standard_False);
00355       //sfe.FixSameParameter(E);
00356       aChain.Remove(j);
00357       aChain.SetValue(j,E);
00358       j--;
00359     }
00360     if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
00361       // union circles
00362       Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
00363       Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
00364       gp_Pnt P01 = C1->Location();
00365       gp_Pnt P02 = C2->Location();
00366       if (P01.Distance(P02) > Precision::Confusion()) continue;
00367       // can union circles => create new edge
00368       TopoDS_Vertex V1 = sae.FirstVertex(edge1);
00369       gp_Pnt PV1 = BRep_Tool::Pnt(V1);
00370       TopoDS_Vertex V2 = sae.LastVertex(edge2);
00371       gp_Pnt PV2 = BRep_Tool::Pnt(V2);
00372       TopoDS_Vertex VM = sae.LastVertex(edge1);
00373       gp_Pnt PVM = BRep_Tool::Pnt(VM);
00374       GC_MakeCircle MC (PV1,PVM,PV2);
00375       Handle(Geom_Circle) C = MC.Value();
00376       TopoDS_Edge E;
00377       if (!MC.IsDone() || C.IsNull()) {
00378         // jfa for Mantis issue 0020228
00379         if (PV1.Distance(PV2) > Precision::Confusion()) continue;
00380         // closed chain
00381         C = C1;
00382         B.MakeEdge (E,C,Precision::Confusion());
00383         B.Add(E,V1);
00384         B.Add(E,V2);
00385       }
00386       else {
00387         gp_Pnt P0 = C->Location();
00388         gp_Dir D1(gp_Vec(P0,PV1));
00389         gp_Dir D2(gp_Vec(P0,PV2));
00390         Standard_Real fpar = C->XAxis().Direction().Angle(D1);
00391         if(fabs(fpar)>Precision::Confusion()) {
00392           // check orientation
00393           gp_Dir ND =  C->XAxis().Direction().Crossed(D1);
00394           if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
00395             fpar = -fpar;
00396           }
00397         }
00398         Standard_Real lpar = C->XAxis().Direction().Angle(D2);
00399         if(fabs(lpar)>Precision::Confusion()) {
00400           // check orientation
00401           gp_Dir ND =  C->XAxis().Direction().Crossed(D2);
00402           if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
00403             lpar = -lpar;
00404           }
00405         }
00406         if (lpar < fpar) lpar += 2*M_PI;
00407         Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
00408         B.MakeEdge (E,tc,Precision::Confusion());
00409         B.Add(E,V1);
00410         B.Add(E,V2);
00411         B.UpdateVertex(V1, fpar, E, 0.);
00412         B.UpdateVertex(V2, lpar, E, 0.);
00413       }
00414       aChain.Remove(j);
00415       aChain.SetValue(j,E);
00416       j--;
00417     }
00418   }
00419   if (j < aChain.Length()) {
00420     MESSAGE ("null curve3d in edge...");
00421     return Standard_False;
00422   }
00423   if (aChain.Length() > 1) {
00424     // second step: union edges with various curves
00425     // skl for bug 0020052 from Mantis: perform such unions
00426     // only if curves are bspline or bezier
00427     bool NeedUnion = true;
00428     for(j=1; j<=aChain.Length(); j++) {
00429       TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
00430       Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
00431       if(c3d.IsNull()) continue;
00432       while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
00433         Handle(Geom_TrimmedCurve) tc =
00434           Handle(Geom_TrimmedCurve)::DownCast(c3d);
00435         c3d = tc->BasisCurve();
00436       }
00437       if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
00438             c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
00439       NeedUnion = false;
00440       break;
00441     }
00442     if(NeedUnion) {
00443       MESSAGE ("can not make analitical union => make approximation");
00444       TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
00445       /*
00446       TopoDS_Wire W;
00447       B.MakeWire(W);
00448       for(j=1; j<=aChain.Length(); j++) {
00449         TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
00450         B.Add(W,edge);
00451       }
00452       Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
00453       Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
00454       Handle(Geom_BSplineCurve) bc = Conv.Curve();
00455       TopoDS_Edge E;
00456       B.MakeEdge (E,bc,Precision::Confusion());
00457       B.Add (E,VF);
00458       B.Add (E,VL);
00459       */
00460       aChain.SetValue(1,E);
00461     }
00462     else {
00463       MESSAGE ("can not make approximation for such types of curves");
00464       return Standard_False;
00465     }
00466   }
00467 
00468   anEdge = TopoDS::Edge(aChain.Value(1));
00469   return Standard_True;
00470 }
00471 
00472 //=======================================================================
00473 //function : Perform
00474 //purpose  :
00475 //=======================================================================
00476 TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
00477                                           const Standard_Real Tol)
00478 {
00479   myContext = new ShapeBuild_ReShape;
00480   myTolerance = Tol;
00481   TopoDS_Shape aResult = myContext->Apply(Shape);
00482 
00483   // processing each solid
00484   TopAbs_ShapeEnum aType = TopAbs_SOLID;
00485   TopExp_Explorer exps (Shape, aType);
00486   if (!exps.More()) {
00487     aType = TopAbs_SHELL;
00488     exps.Init(Shape, aType);
00489   }
00490   for (; exps.More(); exps.Next()) {
00491     //TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
00492     TopoDS_Shape aSolid = exps.Current();
00493 
00494     TopTools_IndexedMapOfShape ChangedFaces;
00495 
00496     // creating map of edge faces
00497     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
00498     TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
00499 
00500     Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
00501     TopoDS_Shape aRes = aSolid;
00502     aRes = aContext->Apply(aSolid);
00503 
00504     // processing each face
00505     TopExp_Explorer exp;
00506     for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
00507       TopoDS_Face aFace =
00508         TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
00509       TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
00510 
00511       for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
00512         TopoDS_Edge edge = TopoDS::Edge(expe.Current());
00513         if (!aMapEdgeFaces.Contains(edge)) continue;
00514         const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
00515         TopTools_ListIteratorOfListOfShape anIter(aList);
00516         for ( ; anIter.More(); anIter.Next()) {
00517           TopoDS_Face face = TopoDS::Face(anIter.Value());
00518           TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
00519           if (face1.IsSame(aFace)) continue;
00520           if (aMapFacesEdges.Contains(face)) {
00521             aMapFacesEdges.ChangeFromKey(face).Append(edge);
00522           }
00523           else {
00524             TopTools_ListOfShape ListEdges;
00525             ListEdges.Append(edge);
00526             aMapFacesEdges.Add(face,ListEdges);
00527           }
00528         }
00529       }
00530 
00531       for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
00532         const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
00533         TopTools_SequenceOfShape SeqEdges;
00534         TopTools_ListIteratorOfListOfShape anIter(ListEdges);
00535         for ( ; anIter.More(); anIter.Next()) {
00536           SeqEdges.Append(anIter.Value());
00537         }
00538         if (SeqEdges.Length()==1) continue;
00539         TopoDS_Edge E;
00540         if ( MergeEdges(SeqEdges,aFace,Tol,E) ) {
00541           // now we have only one edge - aChain.Value(1)
00542           // we have to replace old ListEdges with this new edge
00543           aContext->Replace(SeqEdges(1),E);
00544           for (Standard_Integer j=2; j<=SeqEdges.Length(); j++) {
00545             aContext->Remove(SeqEdges(j));
00546           }
00547           TopoDS_Face tmpF = TopoDS::Face(exp.Current());
00548           if ( !ChangedFaces.Contains(tmpF) )
00549             ChangedFaces.Add(tmpF);
00550           tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
00551           if ( !ChangedFaces.Contains(tmpF) )
00552             ChangedFaces.Add(tmpF);
00553         }
00554       }
00555 
00556     } // end processing each face
00557 
00558     // fix changed faces and replace them in the local context
00559     for (Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
00560       TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
00561       Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
00562       sff->SetContext(myContext);
00563       sff->SetPrecision(myTolerance);
00564       sff->SetMinTolerance(myTolerance);
00565       sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
00566       sff->Perform();
00567       aContext->Replace(aFace,sff->Face());
00568     }
00569 
00570     if (ChangedFaces.Extent() > 0) {
00571       // fix changed shell and replace it in the local context
00572       TopoDS_Shape aRes1 = aContext->Apply(aRes);
00573       TopExp_Explorer expsh;
00574       for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
00575         TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
00576         Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
00577         sfsh->FixFaceOrientation(aShell);
00578         aContext->Replace(aShell,sfsh->Shell());
00579       }
00580       TopoDS_Shape aRes2 = aContext->Apply(aRes1);
00581       // put new solid into global context
00582       myContext->Replace(aSolid,aRes2);
00583     }
00584 
00585   } // end processing each solid
00586 
00587   aResult = myContext->Apply(Shape);
00588   return aResult;
00589 }