Back to index

salome-geom  6.5.0
ShHealOper_FillHoles.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_FillHoles.cxx
00024 // Created:   26.04.04 17:35:30
00025 // Author:    Galina KULIKOVA
00026 
00027 #include <Basics_OCCTVersion.hxx>
00028 
00029 #include <ShapeFix_Shell.hxx>
00030 #include <ShapeFix_Face.hxx>
00031 #include <ShHealOper_FillHoles.hxx>
00032 #include <ShapeAnalysis_FreeBounds.hxx>
00033 
00034 #include <BRep_Tool.hxx>
00035 #include <BRepAdaptor_Curve.hxx>
00036 #include <BRepAdaptor_HCurve.hxx>
00037 #include <BRep_Builder.hxx>
00038 #include <BRepFill_CurveConstraint.hxx>
00039 #include <BRepBuilderAPI_MakeFace.hxx>
00040 
00041 #include <TopAbs_ShapeEnum.hxx>
00042 #include <TopExp.hxx>
00043 #include <TopExp_Explorer.hxx>
00044 #include <TopoDS.hxx>
00045 #include <TopoDS_Edge.hxx>
00046 #include <TopoDS_Shell.hxx>
00047 #include <TopoDS_Compound.hxx>
00048 #include <TopoDS_Iterator.hxx>
00049 #include <TopLoc_Location.hxx>
00050 #include <TopTools_ListOfShape.hxx>
00051 #include <TopTools_HSequenceOfShape.hxx>
00052 #include <TopTools_IndexedMapOfShape.hxx>
00053 
00054 #include <Geom_Curve.hxx>
00055 #include <Geom_BSplineSurface.hxx>
00056 #include <GeomPlate_Surface.hxx>
00057 #include <GeomPlate_MakeApprox.hxx>
00058 #include <GeomPlate_CurveConstraint.hxx>
00059 #include <GeomPlate_PlateG0Criterion.hxx>
00060 #include <GeomPlate_BuildPlateSurface.hxx>
00061 
00062 #include <TColgp_SequenceOfXY.hxx>
00063 #include <TColgp_SequenceOfXYZ.hxx>
00064 
00065 #include <Precision.hxx>
00066 
00067 //=======================================================================
00068 //function : ShHealOper_FillHoles()
00069 //purpose  : Constructor
00070 //=======================================================================
00071 ShHealOper_FillHoles::ShHealOper_FillHoles ()
00072 {
00073   InitParameters();
00074 }
00075 
00076 //=======================================================================
00077 //function : ShHealOper_FillHoles
00078 //purpose  :
00079 //=======================================================================
00080 ShHealOper_FillHoles::ShHealOper_FillHoles (const TopoDS_Shape& theShape)
00081 {
00082   Init(theShape);
00083   InitParameters();
00084 }
00085 
00086 //=======================================================================
00087 //function : Init
00088 //purpose  :
00089 //=======================================================================
00090 void ShHealOper_FillHoles::Init(const TopoDS_Shape& theShape)
00091 {
00092   ShHealOper_Tool::Init(theShape);
00093   TopExp::MapShapesAndAncestors( myInitShape, TopAbs_EDGE,TopAbs_SHELL   , myEdgeShells);
00094   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_COMPOUND, myEdgeComps );
00095 
00096   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_FACE, myEdgeFaces );
00097 }
00098 
00099 //=======================================================================
00100 //function : InitParameters
00101 //purpose  :
00102 //=======================================================================
00103 void ShHealOper_FillHoles::InitParameters(Standard_Integer theDegree,
00104                                           Standard_Integer theNbPtsOnCur,
00105                                           Standard_Integer theNbIter,
00106                                           Standard_Real theTol3d,
00107                                           Standard_Real theTol2d,
00108                                           Standard_Real theTolAng,
00109                                           Standard_Real theTolCrv,
00110                                           Standard_Integer theMaxDeg,
00111                                           Standard_Integer theMaxSeg)
00112 {
00113   myNbPtsOnCur = theNbPtsOnCur;
00114   myNbIter = theNbIter;
00115   myDegree =theDegree;
00116   myTol2d = theTol2d;
00117   myTol3d = theTol3d;
00118   myTolAng = theTolAng;
00119   myTolCrv = theTolCrv;
00120   myMaxDeg = theMaxDeg;
00121   myMaxSeg =theMaxSeg;
00122 }
00123 //=======================================================================
00124 //function : Fill
00125 //purpose  :
00126 //=======================================================================
00127 Standard_Boolean ShHealOper_FillHoles::Fill()
00128 {
00129   ShapeAnalysis_FreeBounds sab(myInitShape);
00130   TopoDS_Compound aCompClosed = sab.GetClosedWires();
00131   TopoDS_Compound aCompOpen = sab.GetOpenWires();
00132   TopTools_SequenceOfShape aFillWires;
00133   if(!aCompClosed.IsNull()) {
00134     TopoDS_Iterator aIt(aCompClosed);
00135 
00136     for( ; aIt.More(); aIt.Next())
00137       aFillWires.Append(aIt.Value());
00138   }
00139   if(!aCompOpen.IsNull()) {
00140     TopoDS_Iterator aIt(aCompOpen);
00141     for(  ; aIt.More(); aIt.Next())
00142       aFillWires.Append(aIt.Value());
00143   }
00144 
00145   TopExp_Explorer aExp(myInitShape,TopAbs_EDGE,TopAbs_FACE);
00146 
00147   for( ; aExp.More(); aExp.Next())
00148     aFillWires.Append(aExp.Current());
00149 
00150   return Fill(aFillWires);
00151 }
00152 
00153 //=======================================================================
00154 //function : Fill
00155 //purpose  :
00156 //=======================================================================
00157 Standard_Boolean ShHealOper_FillHoles::Fill(const TopTools_SequenceOfShape& theFillShapes)
00158 {
00159   myDone = Standard_False;
00160   myErrorStatus = ShHealOper_NotError;
00161   if(myInitShape.IsNull()) {
00162     myErrorStatus = ShHealOper_InvalidParameters;
00163     return myDone;
00164   }
00165   if(!theFillShapes.Length()) {
00166     return myDone;
00167   }
00168 
00169   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
00170   if(!prepareWires(theFillShapes,aSeqWires)) {
00171     myErrorStatus = ShHealOper_InvalidParameters;
00172     return myDone;
00173   }
00174 
00175   myResultShape = myInitShape;
00176   Standard_Integer i =1;
00177   for( ; i <= aSeqWires->Length(); i++) {
00178     TopoDS_Wire aWire = TopoDS::Wire(aSeqWires->Value(i));
00179     Handle(TColGeom2d_HArray1OfCurve) aCurves2d;
00180     Handle(TColStd_HArray1OfInteger) aOrders;
00181     Handle(TColStd_HArray1OfInteger) aSenses;
00182     Handle(Geom_Surface) aSurf = buildSurface(aWire,aCurves2d,aOrders,aSenses);
00183     if(aSurf.IsNull())
00184       myErrorStatus = ShHealOper_ErrorExecution;
00185     else
00186       myDone = (addFace(aSurf,aWire,aCurves2d,aOrders,aSenses) || myDone);
00187   }
00188   if(myDone)
00189     myResultShape = myContext->Apply(myResultShape);
00190   return myDone;
00191 }
00192 
00193 //=======================================================================
00194 //function : isCircle
00195 //purpose  :
00196 //=======================================================================
00197 static Standard_Boolean isCircle(const TopoDS_Edge theEdge)
00198 {
00199   Standard_Real aFirst, aLast;
00200   Handle(Geom_Curve) aC3D = BRep_Tool::Curve(theEdge,aFirst, aLast );
00201   if(aC3D.IsNull()) return Standard_False;
00202   Standard_Boolean isCirc = (aC3D->Value(aFirst).Distance(aC3D->Value(aLast)) <
00203      aC3D->Value(aFirst).Distance(aC3D->Value((aFirst +aLast)/2)));
00204   return isCirc;
00205 }
00206 //=======================================================================
00207 //function : prepareWires
00208 //purpose  :
00209 //=======================================================================
00210 
00211 Standard_Boolean ShHealOper_FillHoles::prepareWires(const TopTools_SequenceOfShape& theFillShapes,
00212                                                     Handle(TopTools_HSequenceOfShape)& theSeqWires)
00213 {
00214   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
00215   Standard_Integer i =1;
00216   for( ; i <= theFillShapes.Length(); i++) {
00217     TopExp_Explorer aExp;
00218     for (aExp.Init (theFillShapes.Value(i),TopAbs_WIRE); aExp.More(); aExp.Next()) {
00219       TopoDS_Iterator aIt(aExp.Current());
00220       Standard_Boolean isAdd = Standard_True;
00221       for( ; aIt.More() && isAdd; aIt.Next()) {
00222         if(myEdgeFaces.Contains(aIt.Value()))
00223           isAdd =  (myEdgeFaces.FindFromKey(aIt.Value()).Extent() <2);
00224       }
00225       if(isAdd)
00226         theSeqWires->Append(aExp.Current());
00227     }
00228     for (aExp.Init (theFillShapes.Value(i),TopAbs_EDGE, TopAbs_WIRE); aExp.More(); aExp.Next()) {
00229       if (!BRep_Tool::Degenerated (TopoDS::Edge (aExp.Current())))
00230         if(myEdgeFaces.Contains(aExp.Current()) && myEdgeFaces.FindFromKey(aExp.Current()).Extent() >1)
00231           continue;
00232         aSeqEdges->Append(aExp.Current());
00233     }
00234   }
00235 
00236   if(aSeqEdges->Length())
00237   {
00238     Standard_Real aTol = 0.;
00239     Standard_Boolean aShared = Standard_True;
00240     Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
00241     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
00242     Handle(TopTools_HSequenceOfShape) anWiresClosed = new TopTools_HSequenceOfShape,
00243     anWiresOpen   = new TopTools_HSequenceOfShape;
00244     ShapeAnalysis_FreeBounds::SplitWires(aTmpWires, aTol, aShared, anWiresClosed, anWiresOpen);
00245 
00246     for (i = 1; i <= anWiresClosed->Length(); i++)
00247       theSeqWires->Append (anWiresClosed->Value (i));
00248     for (i = 1; i <= anWiresOpen->Length(); i++)
00249       theSeqWires->Append (anWiresOpen->Value (i));
00250   }
00251 
00252   for( i =1; i <= theSeqWires->Length(); i++) {
00253     TopoDS_Wire aWire = TopoDS::Wire(theSeqWires->Value(i));
00254 
00255     TopoDS_Iterator aIt(aWire);
00256     Standard_Integer ne =0;
00257     TopoDS_Edge ae;
00258     for( ; aIt.More(); aIt.Next(), ne++)
00259       ae = TopoDS::Edge(aIt.Value());
00260     if((ne == 1) && ( !isCircle(ae))) {
00261       theSeqWires->Remove(i--);
00262       continue;
00263     }
00264   }
00265   return (theSeqWires->Length());
00266 }
00267 //=======================================================================
00268 //function : buildSurface
00269 //purpose  :
00270 //=======================================================================
00271 
00272 Handle(Geom_Surface) ShHealOper_FillHoles::buildSurface(const TopoDS_Wire& theWire,
00273                                                         Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
00274                                                         Handle(TColStd_HArray1OfInteger)& theOrders,
00275                                                         Handle(TColStd_HArray1OfInteger)& theSenses)
00276 {
00277   Handle(Geom_BSplineSurface) aSurf;
00278   try {
00279       GeomPlate_BuildPlateSurface aBuilder(myDegree, myNbPtsOnCur, myNbIter,
00280                                                  myTol2d, myTol3d, myTolAng, myTolCrv);
00281       TopoDS_Iterator aIter;
00282       for(aIter.Initialize (theWire); aIter.More(); aIter.Next()) {
00283 
00284         TopoDS_Edge ae = TopoDS::Edge(aIter.Value());
00285         BRepAdaptor_Curve adC(ae);
00286         Handle(BRepAdaptor_HCurve) aHAD= new BRepAdaptor_HCurve(adC);
00287         Handle(BRepFill_CurveConstraint) aConst =
00288             new BRepFill_CurveConstraint (aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
00289         //Handle(GeomPlate_CurveConstraint) aConst =
00290          // new GeomPlate_CurveConstraint(aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
00291         aBuilder.Add (aConst);
00292       }
00293       aBuilder.Perform();
00294       if(!aBuilder.IsDone())
00295         return aSurf;
00296       Handle(GeomPlate_Surface) aPlSurf = aBuilder.Surface();
00297 
00298       //for filling holes without initial specified surface
00299       //the initial surface should be build by GeomPlate itself
00300       //following code was taken from BRepFill_Filling::Build
00301 
00302       Standard_Real aDist = aBuilder.G0Error();
00303       TColgp_SequenceOfXY S2d;
00304       TColgp_SequenceOfXYZ S3d;
00305       S2d.Clear();
00306       S3d.Clear();
00307       aBuilder.Disc2dContour(4,S2d);
00308       aBuilder.Disc3dContour(4,0,S3d);
00309       Standard_Real amaxTol = Max( myTol3d, 10* aDist);
00310       GeomPlate_PlateG0Criterion Criterion( S2d, S3d, amaxTol );
00311       GeomPlate_MakeApprox Approx( aPlSurf, Criterion, myTol3d, myMaxSeg, myMaxDeg );
00312       aSurf = Approx.Surface();
00313       if(aSurf.IsNull())
00314         return aSurf;
00315 
00316       theCurves2d = aBuilder.Curves2d();
00317       theOrders    = aBuilder.Order();
00318       theSenses    = aBuilder.Sense();
00319     }
00320 
00321   catch (Standard_Failure) {
00322     aSurf.Nullify();
00323     return aSurf;
00324   }
00325   return aSurf;
00326 }
00327 
00328 //=======================================================================
00329 //function : addFace
00330 //purpose  :
00331 //=======================================================================
00332 
00333 Standard_Boolean ShHealOper_FillHoles::addFace(const Handle(Geom_Surface)& theSurf,
00334                                                const TopoDS_Wire& theWire,
00335                                                const Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
00336                                                const Handle(TColStd_HArray1OfInteger)& theOrders,
00337                                                const Handle(TColStd_HArray1OfInteger)& theSenses)
00338 {
00339 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00340   BRepBuilderAPI_MakeFace aMakeFace (theSurf, Precision::Confusion());
00341 #else
00342   BRepBuilderAPI_MakeFace aMakeFace (theSurf);
00343 #endif
00344   TopoDS_Face aFace = aMakeFace.Face();
00345   aFace.EmptyCopy();
00346 
00347   TopoDS_Wire aWire;
00348   BRep_Builder aB;
00349   aB.MakeWire(aWire);
00350 
00351   TopTools_IndexedMapOfShape aMapParent;
00352   Standard_Integer aInd = 1;
00353   Standard_Boolean hasShell = Standard_False;
00354   TopoDS_Iterator aIter(theWire);
00355   for ( ; aIter.More(); aIter.Next(), aInd++) {
00356     TopoDS_Edge anEdge = TopoDS::Edge (aIter.Value());
00357     Standard_Real aF, aL;
00358     BRep_Tool::Range (anEdge, aF, aL);
00359     TopLoc_Location aLoc;
00360     aB.UpdateEdge (anEdge, theCurves2d->Value (aInd),aFace, 0.);
00361 
00362     aB.Range (anEdge, aFace, aF, aL);
00363 
00364     // Set orientation of the edge: orientation should be changed
00365     // if its orientation does not make sence with curve orientation
00366     // recommended by GeomPlate
00367     if ((anEdge.Orientation() == TopAbs_FORWARD) ==
00368         (theSenses->Value (theOrders->Value (aInd)) == 1)) {
00369       anEdge.Reverse();
00370     }
00371     aB.SameParameter(anEdge,Standard_False);
00372     aB.Add (aWire, anEdge);
00373     TopoDS_Shape aParent;
00374     if(!myEdgeFaces.Contains(anEdge))
00375       continue;
00376 
00377     if(myEdgeFaces.FindFromKey(anEdge).Extent() >1)
00378       continue;
00379 
00380     if(myEdgeShells.Contains(anEdge)) {
00381       if(myEdgeShells.FindFromKey(anEdge).Extent()) {
00382        aParent = myEdgeShells.FindFromKey(anEdge).First();
00383        hasShell = Standard_True;
00384        aMapParent.Add(aParent);
00385      }
00386     }
00387     else if(myEdgeComps.Contains(anEdge)) {
00388       if(myEdgeComps.FindFromKey(anEdge).Extent()) {
00389         aParent = myEdgeComps.FindFromKey(anEdge).First();
00390         aMapParent.Add(aParent);
00391       }
00392     }
00393 
00394   }
00395   aB.Add(aFace,aWire);
00396   Handle(ShapeFix_Face) aSff = new ShapeFix_Face(aFace);
00397   aSff->SetContext(myContext);
00398   aSff->SetPrecision(myTol3d);
00399   aSff->Perform();
00400   if(aSff->Status(ShapeExtend_FAIL)) {
00401     myErrorStatus = ShHealOper_ErrorExecution;
00402     return Standard_False;
00403   }
00404   //theFace = aSff->Face();
00405   TopoDS_Shape aResShape = aSff->Result();
00406   getResShape(aResShape,aMapParent,hasShell);
00407   return Standard_True;
00408 }
00409 
00410 //=======================================================================
00411 //function : getResShape
00412 //purpose  :
00413 //=======================================================================
00414 
00415 void ShHealOper_FillHoles::getResShape(const TopoDS_Shape& theAddShape,
00416                                        const TopTools_IndexedMapOfShape& aMapParent,
00417                                        const Standard_Boolean theHasShell)
00418 {
00419   BRep_Builder aB;
00420 
00421   if(!aMapParent.Extent()) {
00422     TopoDS_Compound aComp;
00423     aB.MakeCompound(aComp);
00424     TopoDS_Shape aresShape = myContext->Apply(myResultShape);
00425     aB.Add(aComp,aresShape);
00426     aB.Add(aComp,theAddShape);
00427     myResultShape = aComp;
00428     return ;
00429   }
00430   Standard_Boolean anhasShell = theHasShell;
00431   TopoDS_Shell aTmpShell;
00432   aB.MakeShell(aTmpShell);
00433   TopTools_SequenceOfShape aseqShells;
00434   if(anhasShell) {
00435     aB.Add(aTmpShell,theAddShape);
00436     Standard_Integer i =1;
00437     for( ; i <= aMapParent.Extent(); i++) {
00438       TopoDS_Shape aParShape = myContext->Apply(aMapParent.FindKey(i));
00439       if(aParShape.ShapeType() == TopAbs_SHELL) {
00440         TopExp_Explorer aexp(aParShape,TopAbs_FACE);
00441         for( ; aexp.More(); aexp.Next())
00442           aB.Add(aTmpShell,aexp.Current());
00443         aseqShells.Append(aParShape);
00444       }
00445     }
00446     anhasShell = aseqShells.Length();
00447   }
00448   if(anhasShell) {
00449     Handle(ShapeFix_Shell) asfs = new ShapeFix_Shell;
00450     asfs->FixFaceOrientation(aTmpShell);
00451     TopoDS_Shape anshape = asfs->Shape();
00452     myContext->Replace(aseqShells.Value(1),anshape);
00453     Standard_Integer i =2;
00454     for( ; i<= aseqShells.Length(); i++)
00455       myContext->Remove(aseqShells.Value(i));
00456   }
00457   else {
00458     TopoDS_Compound aComp;
00459     aB.MakeCompound(aComp);
00460     TopoDS_Shape oldshape = myContext->Apply(aMapParent.FindKey(1));
00461     TopoDS_Iterator aIt(oldshape);
00462     for( ; aIt.More(); aIt.Next())
00463       aB.Add(aComp,aIt.Value());
00464     aB.Add(aComp,theAddShape);
00465     myContext->Replace( oldshape,aComp);
00466   }
00467 }