Back to index

salome-geom  6.5.0
ShHealOper_CloseContour.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 
00023 // File:      ShHealOper_CloseContour.cxx
00024 // Created:   20.04.04 11:36:01
00025 // Author:    Galina KULIKOVA
00026 //
00027 #include <ShHealOper_CloseContour.hxx>
00028 #include <ShapeExtend_WireData.hxx>
00029 #include <ShapeFix_Wire.hxx>
00030 #include <TopoDS_Face.hxx>
00031 #include <ShapeFix_Edge.hxx>
00032 #include <TopoDS_Iterator.hxx>
00033 #include <ShapeAnalysis_Wire.hxx>
00034 #include <TColStd_SequenceOfInteger.hxx>
00035 #include <TopTools_IndexedMapOfShape.hxx>
00036 #include <TopoDS_Edge.hxx>
00037 #include <TopTools_ListOfShape.hxx>
00038 #include <TopTools_ListIteratorOfListOfShape.hxx>
00039 #include <TColStd_MapOfInteger.hxx>
00040 #include <TopExp.hxx>
00041 #include <TopoDS.hxx>
00042 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
00043 #include <ShapeAnalysis_Edge.hxx>
00044 #include <BRep_Builder.hxx>
00045 #include <TopoDS_Edge.hxx>
00046 #include <TopoDS_Vertex.hxx>
00047 #include <BRepBuilderAPI_MakeVertex.hxx>
00048 #include <ShapeAnalysis_Edge.hxx>
00049 #include <gp_Pnt.hxx>
00050 #include <gp_Vec2d.hxx>
00051 #include <gp_Vec.hxx>
00052 #include <Geom_Line.hxx>
00053 #include <Geom2d_Line.hxx>
00054 #include <TopoDS_Compound.hxx>
00055 #include <BRep_Tool.hxx>
00056 #include <ShapeBuild_Edge.hxx>
00057 #include <TopExp_Explorer.hxx>
00058 
00059 //=======================================================================
00060 //function : ShHealOper_CloseContour()
00061 //purpose  : Constructor
00062 //=======================================================================
00063 ShHealOper_CloseContour::ShHealOper_CloseContour (const TopoDS_Shape& theShape  ) 
00064 {
00065   Init(theShape);
00066 }
00067 
00068 //=======================================================================
00069 //function : Init
00070 //purpose  : 
00071 //=======================================================================
00072 void ShHealOper_CloseContour::Init(const TopoDS_Shape& theShape)
00073 {
00074   ShHealOper_Tool::Init(theShape);
00075   myTolerance = Precision::Confusion();
00076   myMaxTolerance = 1.0;
00077   TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgesFace);
00078 }
00079 
00080 //=======================================================================
00081 //function : Perform
00082 //purpose  : 
00083 //=======================================================================
00084 Standard_Boolean ShHealOper_CloseContour::Perform(const TopTools_SequenceOfShape& theSeqEdges,
00085                                                   const Standard_Boolean theModeVertex,
00086                                                   const Standard_Boolean theModeFixGapsCurves)
00087 {
00088   myFreeEdges.Clear();
00089   myDone = Standard_False;
00090   myErrorStatus =ShHealOper_NotError;
00091   if(myInitShape.IsNull()) {
00092     myErrorStatus = ShHealOper_InvalidParameters;
00093     return myDone;
00094   }
00095   Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData;
00096   Standard_Integer i =1;
00097   for ( ; i <= theSeqEdges.Length(); i++)
00098     asewd->Add(theSeqEdges.Value(i));
00099 
00100   myModeVertex = theModeVertex;
00101   myModeFixGapsCurves = theModeFixGapsCurves;
00102 
00103   build(asewd);
00104   
00105   return myDone;
00106 }
00107 
00108 //=======================================================================
00109 //function : Perform
00110 //purpose  : 
00111 //=======================================================================
00112 Standard_Boolean ShHealOper_CloseContour::Perform(const TopoDS_Wire& theWire,
00113                                                   const Standard_Boolean theModeVertex,
00114                                                   const Standard_Boolean theModeFixGapsCurves)
00115 {
00116   myFreeEdges.Clear();
00117   myDone = Standard_False;
00118   if(myInitShape.IsNull()) {
00119     myErrorStatus = ShHealOper_InvalidParameters;
00120     return myDone;
00121   }
00122   Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData(theWire);
00123   myModeVertex = theModeVertex;
00124   myModeFixGapsCurves = theModeFixGapsCurves;
00125   build(asewd);
00126   return myDone;
00127 }
00128 
00129 //=======================================================================
00130 //function : Build
00131 //purpose  : 
00132 //=======================================================================
00133 void ShHealOper_CloseContour::build(Handle(ShapeExtend_WireData)& theSewd)
00134 {
00135   TopTools_SequenceOfShape aCommonFaces;
00136   //checks that all specified edges belong the one face or not.
00137   Standard_Boolean isOneFace = checkOneFace(theSewd,aCommonFaces);
00138 
00139   //add edge or increase max tolerance in dependance on specified VertexMode.
00140   //if all edges belong the one face that gap will be closed in the 2D by line
00141   //else gap will be closed in the 3D by line.
00142   myDone = fixGaps(theSewd,aCommonFaces);
00143   updateWire(theSewd);
00144   Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
00145   sfw->SetContext(myContext);
00146   sfw->Load(theSewd);
00147 
00148   sfw->ClosedWireMode() = Standard_True;
00149   sfw->SetPrecision(myTolerance);
00150   sfw->SetMaxTolerance(myMaxTolerance);
00151   
00152   if(isOneFace) {
00153     Standard_Integer ii =1;
00154     for( ; ii <= aCommonFaces.Length(); ii++) {
00155       TopoDS_Face aFace = TopoDS::Face(aCommonFaces.Value(ii));
00156       sfw->SetFace(aFace);
00157       myDone = (sfw->Perform() || myDone);
00158       //if Mode for fix gaps is equal to true that
00159       //curve 3D and curve 2D will be pull to each other.
00160       if(myModeFixGapsCurves) {
00161         Standard_Boolean isFixgaps = Standard_False;
00162         if(ii ==1) {
00163           sfw->FixGaps3d();
00164           isFixgaps = sfw->StatusGaps3d(ShapeExtend_DONE);
00165           if(!isFixgaps && sfw->StatusGaps3d(ShapeExtend_FAIL))
00166              myErrorStatus = ShHealOper_ErrorExecution;
00167         }
00168         if(sfw->FixGaps2d())
00169           sfw->FixSelfIntersection();
00170         else if(sfw->StatusGaps2d(ShapeExtend_FAIL))
00171           myErrorStatus = ShHealOper_ErrorExecution;
00172 
00173         isFixgaps = (isFixgaps || sfw->StatusGaps2d(ShapeExtend_DONE));
00174         myDone = (myDone || isFixgaps);
00175         if(isFixgaps) {
00176           Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
00177           Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
00178           for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
00179             TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
00180             sfe->FixVertexTolerance(aEdge,aFace);
00181             sfe->FixSameParameter(aEdge);
00182           }
00183         }
00184       }
00185     }
00186   }
00187   else {
00188     myDone = (sfw->Perform() || myDone);
00189     if(myModeFixGapsCurves) {
00190       //if Mode for fix gaps is equal to true that
00191       //curve 3D will be pull to each other.
00192       if(sfw->FixGaps3d()) {
00193         Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
00194         Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
00195         for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
00196           TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
00197           sfe->FixVertexTolerance(aEdge);
00198           sfe->FixSameParameter(aEdge);
00199         }
00200       }
00201       else if(sfw->StatusGaps3d(ShapeExtend_FAIL))
00202         myErrorStatus = ShHealOper_ErrorExecution;
00203       myDone = (sfw->StatusGaps3d(ShapeExtend_DONE) || myDone);
00204     }
00205   }
00206 
00207   if(myDone) {
00208     TopoDS_Shape aoldShape = myInitShape;
00209     //if free edges were added they will be added to the result shape
00210     //and type of result shape should be change if initial shape is not COMPOUND.
00211     if( myFreeEdges.Length()) {
00212       BRep_Builder aB;
00213       TopoDS_Compound aComp;
00214       aB.MakeCompound(aComp);
00215       if(aoldShape.ShapeType() == TopAbs_COMPOUND) {
00216         TopoDS_Iterator aIt(aoldShape);
00217         for( ;aIt.More();aIt.Next() )
00218            aB.Add(aComp,aIt.Value());
00219       }
00220       else
00221         aB.Add(aComp,aoldShape);
00222 
00223       Standard_Integer i=1;
00224       for ( ; i <= myFreeEdges.Length(); i++ ) 
00225         aB.Add(aComp,myFreeEdges.Value(i));
00226       aoldShape = aComp;
00227       
00228     }
00229     myResultShape = myContext->Apply(aoldShape);
00230   }
00231   return;
00232 }
00233 
00234 //=======================================================================
00235 //function : checkGaps
00236 //purpose  : 
00237 //=======================================================================
00238 Standard_Boolean ShHealOper_CloseContour::fixGaps(const Handle(ShapeExtend_WireData)& theWire,
00239                                                   const TopTools_SequenceOfShape& theCommonFaces) 
00240 {
00241   Handle(ShapeAnalysis_Wire) asaw = new ShapeAnalysis_Wire;
00242   asaw->Load(theWire);
00243   
00244   Standard_Integer i =1;
00245   Standard_Boolean hasGaps = Standard_False;
00246   for ( ; i <= asaw->NbEdges(); i++) {
00247     if(asaw->CheckGap3d(i)) {
00248       Standard_Real dist = asaw->MinDistance3d();
00249       if(dist > myMaxTolerance) {
00250         hasGaps = Standard_True;
00251         Standard_Integer ind2 = (i ==0 ? theWire->NbEdges() :i);
00252         Standard_Integer ind1 = (ind2 >1 ? ind2 -1 : theWire->NbEdges());
00253         TopoDS_Edge aE1= theWire->Edge(ind1);
00254         TopoDS_Edge aE2= theWire->Edge(ind2);
00255         if(!myModeVertex)
00256           buildEdge(aE1,aE2,theCommonFaces);
00257         else
00258           myMaxTolerance = RealLast();
00259         if(ind2 == ind1) break;
00260       }
00261     }
00262     
00263   }
00264   return  hasGaps;
00265 }
00266 
00267 //=======================================================================
00268 //function : checkOneFace
00269 //purpose  : 
00270 //=======================================================================
00271 Standard_Boolean ShHealOper_CloseContour::checkOneFace(const Handle(ShapeExtend_WireData)& theSewd,
00272                                                        TopTools_SequenceOfShape& theCommonFaces) const
00273 {
00274   TopTools_IndexedMapOfShape amapfaces;
00275   TopoDS_Edge aEdge1 = theSewd->Edge(1);
00276   Standard_Boolean isOneFace = myMapEdgesFace.Contains(aEdge1 );
00277   if(!isOneFace)
00278     return isOneFace;
00279 
00280   //check that all specified edges belong to one face. 
00281   const TopTools_ListOfShape& alfaces = myMapEdgesFace.FindFromKey(aEdge1);
00282   isOneFace = isOneFace && (!alfaces.IsEmpty());
00283   if(!isOneFace)
00284     return Standard_False;
00285 
00286   TopTools_ListIteratorOfListOfShape litr(alfaces);
00287   for( ; litr.More();litr.Next() ) 
00288     amapfaces.Add(litr.Value());
00289   
00290   TColStd_MapOfInteger amapIndex;
00291   Standard_Integer ind  =0;
00292   Standard_Integer i =2;
00293   for( ; i <= theSewd->NbEdges() && isOneFace; i++) {
00294     isOneFace = myMapEdgesFace.Contains(theSewd->Edge(i));
00295     if(isOneFace) {
00296       const TopTools_ListOfShape& alfaces1 = myMapEdgesFace.FindFromKey(theSewd->Edge(i));
00297       
00298         TColStd_MapOfInteger aTmpInd;
00299         for(litr.Initialize(alfaces1) ; litr.More() ;litr.Next() ) {
00300           if(amapfaces.Contains(litr.Value())) {
00301             ind = amapfaces.FindIndex(litr.Value());
00302             if( i == 2) 
00303               amapIndex.Add(ind);
00304             else 
00305               aTmpInd.Add(ind);
00306           }
00307         }
00308       if(i ==2) {
00309         isOneFace = (amapIndex.Extent());
00310         continue;
00311       }
00312       else {
00313         TColStd_MapIteratorOfMapOfInteger aMi(amapIndex);
00314         for( ;  aMi.More();aMi.Next()) {
00315           if(!aTmpInd.Contains(aMi.Key()))
00316             amapIndex.Remove(aMi.Key());
00317         }
00318         isOneFace = (amapIndex.Extent()); 
00319         
00320       }
00321     }
00322   }
00323   if(theSewd->NbEdges() >1) {
00324     Standard_Integer j =1;
00325     for( ; j <= amapfaces.Extent(); j++) {
00326       if(!amapIndex.Contains(j)) continue;
00327       TopoDS_Shape aF = amapfaces.FindKey(j);
00328       theCommonFaces.Append(aF);
00329     }
00330   }
00331   return isOneFace;
00332 }
00333 
00334 //=======================================================================
00335 //function : buildEdge
00336 //purpose  : 
00337 //=======================================================================
00338 void ShHealOper_CloseContour::buildEdge(const TopoDS_Edge& aE1, 
00339                                         const TopoDS_Edge& aE2,
00340                                         const TopTools_SequenceOfShape& theCommonFaces) 
00341 {
00342   ShapeAnalysis_Edge asae;
00343   TopoDS_Vertex aV1 = asae.LastVertex(aE1);
00344   TopoDS_Vertex aV2 = asae.FirstVertex(aE2);
00345   gp_Pnt p1 = BRep_Tool::Pnt(aV1);
00346   gp_Pnt p2 = BRep_Tool::Pnt(aV2);
00347   BRepBuilderAPI_MakeVertex mkver1( p1 );
00348   TopoDS_Vertex newV1 = mkver1.Vertex();
00349   
00350   BRepBuilderAPI_MakeVertex mkver2( p2 );
00351   TopoDS_Vertex newV2 = mkver2.Vertex();
00352   BRep_Builder B;
00353   B.UpdateVertex ( newV1,Precision::Confusion());
00354   B.UpdateVertex ( newV2, Precision::Confusion());
00355   Standard_Boolean isBuild = Standard_False;
00356   TopoDS_Edge edge;
00357   B.MakeEdge ( edge );
00358   ShapeBuild_Edge sbe;
00359   //if all edges belong to one face that gap will be closed in the 2D by line
00360   //than 3D curve will be built by 2D curve
00361   if(theCommonFaces.Length()) {
00362     
00363     TopoDS_Face aF1 = TopoDS::Face(theCommonFaces.Value(1));
00364     Handle(Geom2d_Curve) c2d1,c2d2;
00365     gp_Pnt2d p2d1,p2d2;
00366     Standard_Real a1, b1,a2, b2;
00367     if (  asae.PCurve ( aE1, aF1, c2d1, a1, b1, Standard_True )  && 
00368         asae.PCurve ( aE2, aF1, c2d2, a2, b2, Standard_True )) {
00369       
00370       c2d1->D0 ( b1, p2d1);
00371       c2d2->D0 ( a2, p2d2);
00372       gp_Vec2d v12 ( p2d1, p2d2 );
00373       
00374       Handle(Geom2d_Line) aLine2d = new Geom2d_Line ( p2d1, gp_Dir2d ( v12 ) );
00375       B.UpdateEdge ( edge, aLine2d, aF1, ::Precision::Confusion() );
00376       B.Range ( edge, aF1, 0.0, v12.Magnitude() );
00377       
00378       isBuild = sbe.BuildCurve3d ( edge );
00379     }
00380   }
00381   //else gap will be closed in the 3D by line.
00382   if(!isBuild) {
00383     gp_Vec v1 ( p1, p2 );
00384     Handle(Geom_Line) aLine = new Geom_Line ( p1, gp_Dir ( v1 ) );
00385     B.UpdateEdge ( edge, aLine, ::Precision::Confusion() );
00386     B.Range ( edge,0.0, v1.Magnitude());
00387   }
00388   B.Add ( edge, newV1.Oriented ( TopAbs_FORWARD ) );
00389   B.Add ( edge, newV2.Oriented ( TopAbs_REVERSED ) );
00390   myContext->Replace(aV1, newV1.Oriented (aV1.Orientation()));
00391   myContext->Replace(aV2, newV2.Oriented (aV2.Orientation()));
00392   if(isBuild) {
00393     newV1.Orientation(aV1.Orientation());
00394     TopoDS_Edge newEdge = sbe.CopyReplaceVertices ( aE1,TopoDS_Vertex(), newV1);
00395     TopoDS_Wire aw;
00396     B.MakeWire(aw);
00397     B.Add(aw,newEdge);
00398     B.Add(aw,edge);
00399     TopoDS_Shape anE1 = myContext->Apply(aE1);
00400     myContext->Replace(anE1,aw);
00401   }
00402   else {
00403     myFreeEdges.Append(edge);
00404   }
00405 }
00406 
00407 //=======================================================================
00408 //function : UpdateWire
00409 //purpose  : 
00410 //=======================================================================
00411 void ShHealOper_CloseContour::updateWire (Handle(ShapeExtend_WireData)& sbwd) 
00412 {
00413   Standard_Integer i=1;
00414   for ( ; i <= sbwd->NbEdges(); i++ ) {
00415     TopoDS_Edge E = sbwd->Edge(i);
00416     TopoDS_Shape S = myContext->Apply ( E );
00417     if ( S == E ) continue;
00418     for ( TopExp_Explorer exp(S,TopAbs_EDGE); exp.More(); exp.Next() )
00419       sbwd->Add ( exp.Current(), i++ );
00420     sbwd->Remove ( i-- );
00421   }
00422   for ( i =1; i <= myFreeEdges.Length(); i++ ) {
00423     sbwd->Add(TopoDS::Edge(myFreeEdges.Value(i)));
00424   }
00425 }