Back to index

salome-geom  6.5.0
GEOMImpl_FillingDriver.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 #include <Standard_Stream.hxx>
00024 
00025 #include <GEOMImpl_FillingDriver.hxx>
00026 #include <GEOMImpl_IFilling.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 
00029 #include <GEOM_Function.hxx>
00030 
00031 #include <Basics_OCCTVersion.hxx>
00032 
00033 #include <ShapeFix_Face.hxx>
00034 
00035 #include <BRep_Tool.hxx>
00036 #include <BRepAlgo.hxx>
00037 #include <BRep_Builder.hxx>
00038 #include <BRepBuilderAPI_MakeFace.hxx>
00039 #include <BRepBuilderAPI_Copy.hxx>
00040 
00041 #include <TopAbs.hxx>
00042 #include <TopExp_Explorer.hxx>
00043 #include <TopoDS.hxx>
00044 #include <TopoDS_Compound.hxx>
00045 #include <TopoDS_Edge.hxx>
00046 #include <TopoDS_Shape.hxx>
00047 #include <TopoDS_Vertex.hxx>
00048 
00049 #include <Geom_Curve.hxx>
00050 #include <Geom_Surface.hxx>
00051 #include <Geom_TrimmedCurve.hxx>
00052 #include <Geom_Line.hxx>
00053 #include <Geom_Circle.hxx>
00054 #include <Geom_Ellipse.hxx>
00055 #include <Geom_BezierCurve.hxx>
00056 #include <Geom_BSplineCurve.hxx>
00057 #include <Geom_BSplineSurface.hxx>
00058 #include <GeomFill_Line.hxx>
00059 #include <GeomFill_AppSurf.hxx>
00060 #include <GeomFill_SectionGenerator.hxx>
00061 #include <GeomAPI_PointsToBSplineSurface.hxx>
00062 #include <GeomAPI_PointsToBSpline.hxx>
00063 
00064 #include <TColGeom_SequenceOfCurve.hxx>
00065 
00066 #include <TColgp_SequenceOfPnt.hxx>
00067 #include <TColgp_Array1OfPnt.hxx>
00068 
00069 #include <Precision.hxx>
00070 
00071 #include <Standard_ConstructionError.hxx>
00072 
00073 //=======================================================================
00074 //function : GetID
00075 //purpose  :
00076 //=======================================================================
00077 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
00078 {
00079   static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
00080   return aFillingDriver;
00081 }
00082 
00083 //=======================================================================
00084 //function : GEOMImpl_FillingDriver
00085 //purpose  :
00086 //=======================================================================
00087 
00088 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
00089 {
00090 }
00091 
00092 //=======================================================================
00093 //function : Execute
00094 //purpose  :
00095 //=======================================================================
00096 Standard_Integer GEOMImpl_FillingDriver::Execute(TFunction_Logbook& log) const
00097 {
00098   if (Label().IsNull()) return 0;
00099   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00100   if (aFunction.IsNull()) return 0;
00101 
00102   if (aFunction->GetType() != BASIC_FILLING) return 0;
00103 
00104   GEOMImpl_IFilling IF (aFunction);
00105   Handle(GEOM_Function) aShapeFunction = IF.GetShape();
00106   if (aShapeFunction.IsNull()) return 0;
00107   TopoDS_Shape aShape;
00108 
00109   BRepBuilderAPI_Copy Copy(aShapeFunction->GetValue());
00110   if( Copy.IsDone() )
00111     aShape = Copy.Shape();
00112 
00113   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
00114 
00115   Standard_Integer mindeg = IF.GetMinDeg();
00116   Standard_Integer maxdeg = IF.GetMaxDeg();
00117   Standard_Real tol3d = IF.GetTol2D();
00118   Standard_Real tol2d = IF.GetTol3D();
00119   Standard_Integer nbiter = IF.GetNbIter();
00120   Standard_Boolean isApprox = IF.GetApprox();
00121   Standard_Integer aMethod = IF.GetMethod();
00122 
00123   if (mindeg > maxdeg) {
00124     Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
00125   }
00126 
00127   /* we verify the contents of the shape */
00128   TopExp_Explorer Ex;
00129   TopoDS_Shape Scurrent;
00130   Standard_Real First, Last;
00131   Handle(Geom_Curve) C;
00132 
00133   TopoDS_Compound aComp;
00134   BRep_Builder B;
00135   B.MakeCompound(aComp);
00136 
00137   TopoDS_Iterator It (aShape);
00138   for (; It.More(); It.Next()) {
00139     Scurrent = It.Value();
00140     if (Scurrent.ShapeType() != TopAbs_EDGE) {
00141       Handle(Geom_BSplineCurve) newC;
00142       if (Scurrent.ShapeType() == TopAbs_WIRE) {
00143         TColgp_SequenceOfPnt PntSeq;
00144         // collect points
00145         for (Ex.Init(Scurrent, TopAbs_EDGE); Ex.More(); Ex.Next()) {
00146           TopoDS_Edge E = TopoDS::Edge(Ex.Current());
00147           if (BRep_Tool::Degenerated(E)) continue;
00148           C = BRep_Tool::Curve(E, First, Last);
00149           if( E.Orientation() == TopAbs_REVERSED ) {
00150             C->Reverse();
00151           }
00152           Handle(Geom_TrimmedCurve) tc = Handle(Geom_TrimmedCurve)::DownCast(C);
00153           while( !tc.IsNull() ) {
00154             C = tc->BasisCurve();
00155             tc = Handle(Geom_TrimmedCurve)::DownCast(C);
00156           }
00157           int nbp = 10;
00158           if( C->IsKind(STANDARD_TYPE(Geom_Line)) ) {
00159             nbp = 4;
00160           }
00161           else if( C->IsKind(STANDARD_TYPE(Geom_Circle)) || 
00162                    C->IsKind(STANDARD_TYPE(Geom_Ellipse)) ) {
00163             nbp = (int)25*fabs(Last-First)/(2.*M_PI);
00164           }
00165           else if( C->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) {
00166             Handle(Geom_BezierCurve) C3d = Handle(Geom_BezierCurve)::DownCast(C);
00167             nbp = C3d->NbPoles();
00168           }
00169           else if( C->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ) {
00170             Handle(Geom_BSplineCurve) C3d = Handle(Geom_BSplineCurve)::DownCast(C);
00171             nbp = C3d->NbPoles();
00172           }
00173           else {
00174           }
00175           if( nbp<4 ) nbp = 4;
00176           double dp = (Last-First)/(nbp-1);
00177           for(int i=1; i<nbp; i++) {
00178             gp_Pnt P;
00179             C->D0(First+dp*(i-1),P);
00180             PntSeq.Append(P);
00181           }
00182         }
00183         // add last point
00184         gp_Pnt P;
00185         C->D0(Last,P);
00186         PntSeq.Append(P);
00187         // create BSpline 
00188         if(PntSeq.Length()>1) {
00189           TColgp_Array1OfPnt Pnts(1,PntSeq.Length());
00190           // check orientation of wire
00191           if( Scurrent.Orientation() == TopAbs_REVERSED ) {
00192             for(int i=1; i<=PntSeq.Length(); i++) {
00193               Pnts.SetValue(PntSeq.Length()-i+1,PntSeq.Value(i));
00194             }
00195           }
00196           else {
00197             for(int i=1; i<=PntSeq.Length(); i++) {
00198               Pnts.SetValue(i,PntSeq.Value(i));
00199             }
00200           }
00201           GeomAPI_PointsToBSpline PTB(Pnts);
00202           newC = Handle(Geom_BSplineCurve)::DownCast(PTB.Curve());
00203           // set periodic flag if curve is closed
00204           //if( newC->IsClosed() ) {
00205           //  newC->SetPeriodic();
00206           //}
00207           // create edge
00208           double fp = newC->FirstParameter();
00209           double lp = newC->FirstParameter();
00210           gp_Pnt PF,PL;
00211           newC->D0(fp,PF);
00212           newC->D0(lp,PL);
00213           TopoDS_Vertex VF,VL;
00214           B.MakeVertex(VF,PF,1.e-7);
00215           B.MakeVertex(VL,PL,1.e-7);
00216           TopoDS_Edge newE;
00217           B.MakeEdge(newE,newC,1.e-7);
00218           B.Add(newE,VF);
00219           B.Add(newE,VL.Reversed());
00220           Scurrent = newE;
00221         }
00222       }
00223       if(newC.IsNull()) {
00224         Standard_ConstructionError::Raise("The argument compound must contain only edges");
00225       }
00226     }
00227     B.Add(aComp,Scurrent);
00228   }
00229   aShape = aComp;
00230 
00231   if (!isApprox) {
00232     // make filling as in old version of SALOME (before 4.1.1)
00233     GeomFill_SectionGenerator Section;
00234     Standard_Integer i = 0;
00235     Handle(Geom_Curve) aLastC;
00236     gp_Pnt PL1,PL2;
00237     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
00238       Scurrent = Ex.Current();
00239       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
00240       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
00241       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
00242       //if (Scurrent.Orientation() == TopAbs_REVERSED)
00243       //  // Mantis isuue 0020659: consider the orientation of the edges
00244       //  C = new Geom_TrimmedCurve(C, Last, First);
00245       //else
00246       //  C = new Geom_TrimmedCurve(C, First, Last);
00247       C = new Geom_TrimmedCurve(C, First, Last);
00248       gp_Pnt P1,P2;
00249       C->D0(First,P1);
00250       C->D0(Last,P2);
00251 
00252       if( aMethod==1 && Scurrent.Orientation() == TopAbs_REVERSED ) {
00253         C->Reverse();
00254       }
00255       else if( aMethod==2 ) {
00256         if( i==0 ) {
00257           PL1 = P1;
00258           PL2 = P2;
00259         }
00260         else {
00261           double d1 = PL1.Distance(P1) + PL2.Distance(P2);
00262           double d2 = PL1.Distance(P2) + PL2.Distance(P1);
00263           if(d2<d1) {
00264             C->Reverse();
00265             PL1 = P2;
00266             PL2 = P1;
00267           }
00268           else {
00269             PL1 = P1;
00270             PL2 = P2;
00271           }
00272         }
00273       }
00274 
00275       Section.AddCurve(C);
00276       i++;
00277     }
00278 
00279     /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
00280     Section.Perform(Precision::Confusion());
00281     Handle(GeomFill_Line) Line = new GeomFill_Line(i);
00282 
00283     GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
00284     App.Perform(Line, Section);
00285 
00286     if (!App.IsDone()) return 0;
00287     Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
00288     App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
00289     Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
00290       (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
00291        App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
00292 
00293     if (GBS.IsNull()) return 0;
00294 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00295     aShape = BRepBuilderAPI_MakeFace(GBS, Precision::Confusion());
00296 #else
00297     aShape = BRepBuilderAPI_MakeFace(GBS);
00298 #endif
00299   }
00300   else {
00301     // implemented by skl 20.03.2008 for bug 16568
00302     // make approximation - try to create bspline surface
00303     // using GeomAPI_PointsToBSplineSurface
00304     TColGeom_SequenceOfCurve aSeq;
00305     int MaxNbPoles = 0;
00306     // add curves from edges to sequence and find maximal
00307     // number of poles if some of them are bsplines
00308     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
00309       Scurrent = Ex.Current();
00310       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
00311       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
00312       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
00313       Handle(Geom_TrimmedCurve) TC = Handle(Geom_TrimmedCurve)::DownCast(C);
00314       if(TC.IsNull()) {
00315         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(C);
00316         if(!BC.IsNull()) {
00317           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
00318         }
00319       }
00320       else {
00321         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(TC->BasisCurve());
00322         if(BC.IsNull()) {
00323           Handle(Geom_TrimmedCurve) TC1 = Handle(Geom_TrimmedCurve)::DownCast(TC->BasisCurve());
00324           if(!TC1.IsNull()) {
00325             BC = Handle(Geom_BSplineCurve)::DownCast(TC1->BasisCurve());
00326           }
00327         }
00328         if(!BC.IsNull()) {
00329           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
00330         }
00331       }
00332       aSeq.Append(C);
00333     }
00334     // prepare array of points for creation bspline surface
00335     // size of this array: by U parameter - number of curves,
00336     // by V parameter - determ using MaxNbPoles but it's
00337     // value must be between 21(min) and 101(max)
00338     int nbc = aSeq.Length();
00339     int nbp = Max(21,2*MaxNbPoles-1);
00340     if(nbp>101) nbp = 101;
00341     TColgp_Array2OfPnt Points(1,nbc,1,nbp);
00342     int ic = 1;
00343     for(; ic<=nbc; ic++) {
00344       Handle(Geom_Curve) C = aSeq.Value(ic);
00345       double fp = C->FirstParameter();
00346       double lp = C->LastParameter();
00347       double dp = (lp-fp)/(nbp-1);
00348       int j = 0;
00349       gp_Pnt P;
00350       for(; j<nbp; j++) {
00351         C->D0(fp+dp*j,P);
00352         Points.SetValue(ic,j+1,P);
00353       }
00354     }
00355     GeomAPI_PointsToBSplineSurface PTB(Points,mindeg,maxdeg,GeomAbs_C2,tol3d);
00356     Handle(Geom_BSplineSurface) BS = PTB.Surface();
00357 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00358     BRepBuilderAPI_MakeFace BB (BS, Precision::Confusion());
00359 #else
00360     BRepBuilderAPI_MakeFace BB (BS);
00361 #endif
00362     TopoDS_Face NewF = BB.Face();
00363     Handle(ShapeFix_Face) sff = new ShapeFix_Face(NewF);
00364     sff->Perform();
00365     sff->FixOrientation();
00366     aShape = sff->Face();
00367   }
00368 
00369   /* We test the validity of resulting shape */
00370   if (!BRepAlgo::IsValid((aShape))) {
00371     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
00372     return 0;
00373   }
00374 
00375   aFunction->SetValue(aShape);
00376 
00377   log.SetTouched(Label());
00378   return 1;
00379 }
00380 
00381 
00382 //=======================================================================
00383 //function :  GEOMImpl_FillingDriver_Type_
00384 //purpose  :
00385 //=======================================================================
00386 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FillingDriver_Type_()
00387 {
00388   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00389   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00390   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00391   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00392   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00393   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00394 
00395   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00396   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FillingDriver",
00397                                                          sizeof(GEOMImpl_FillingDriver),
00398                                                          1,
00399                                                          (Standard_Address)_Ancestors,
00400                                                          (Standard_Address)NULL);
00401 
00402   return _aType;
00403 }
00404 
00405 //=======================================================================
00406 //function : DownCast
00407 //purpose  :
00408 //=======================================================================
00409 const Handle(GEOMImpl_FillingDriver) Handle(GEOMImpl_FillingDriver)::DownCast
00410                                  (const Handle(Standard_Transient)& AnObject)
00411 {
00412   Handle(GEOMImpl_FillingDriver) _anOtherObject;
00413 
00414   if (!AnObject.IsNull()) {
00415      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FillingDriver))) {
00416        _anOtherObject = Handle(GEOMImpl_FillingDriver)((Handle(GEOMImpl_FillingDriver)&)AnObject);
00417      }
00418   }
00419 
00420   return _anOtherObject;
00421 }