Salome HOME
Bug 0020057: EDF GEOM: Impossible to explode an object to faces. Infinite loop.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FillingDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_FillingDriver.hxx>
24 #include <GEOM_Function.hxx>
25 #include <GEOMImpl_IFilling.hxx>
26 #include <GEOMImpl_Types.hxx>
27
28 #include <BRep_Tool.hxx>
29 #include <BRepAlgo.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31
32 #include <TopAbs.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopExp_Explorer.hxx>
36
37 #include <Geom_Curve.hxx>
38 #include <Geom_Surface.hxx>
39 #include <Geom_TrimmedCurve.hxx>
40 #include <Geom_BSplineSurface.hxx>
41 #include <GeomFill_Line.hxx>
42 #include <GeomFill_AppSurf.hxx>
43 #include <GeomFill_SectionGenerator.hxx>
44
45 #include <Precision.hxx>
46 #include <Standard_ConstructionError.hxx>
47
48 #include <TColGeom_SequenceOfCurve.hxx>
49 #include <ShapeFix_Face.hxx>
50 #include <GeomAPI_PointsToBSplineSurface.hxx>
51 #include <Geom_BSplineCurve.hxx>
52
53 //=======================================================================
54 //function : GetID
55 //purpose  :
56 //=======================================================================
57 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
58 {
59   static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
60   return aFillingDriver;
61 }
62
63
64 //=======================================================================
65 //function : GEOMImpl_FillingDriver
66 //purpose  :
67 //=======================================================================
68
69 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
70 {
71 }
72
73 //=======================================================================
74 //function : Execute
75 //purpose  :
76 //=======================================================================
77 Standard_Integer GEOMImpl_FillingDriver::Execute(TFunction_Logbook& log) const
78 {
79   if (Label().IsNull()) return 0;
80   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
81   if (aFunction.IsNull()) return 0;
82
83   if (aFunction->GetType() != BASIC_FILLING) return 0;
84
85   GEOMImpl_IFilling IF (aFunction);
86   Handle(GEOM_Function) aShapeFunction = IF.GetShape();
87   if (aShapeFunction.IsNull()) return 0;
88   TopoDS_Shape aShape = aShapeFunction->GetValue();
89   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
90
91   Standard_Integer mindeg = IF.GetMinDeg();
92   Standard_Integer maxdeg = IF.GetMaxDeg();
93   Standard_Real tol3d = IF.GetTol2D();
94   Standard_Real tol2d = IF.GetTol3D();
95   Standard_Integer nbiter = IF.GetNbIter();
96   Standard_Boolean isApprox = IF.GetApprox();
97
98   if (mindeg > maxdeg) {
99     Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
100   }
101
102   /* we verify the contents of the shape */
103   TopExp_Explorer Ex;
104   TopoDS_Shape Scurrent;
105   Standard_Real First, Last;
106   Handle(Geom_Curve) C;
107
108   if(!isApprox) {
109     // make filling as in old version of SALOME (before 4.1.1)
110     GeomFill_SectionGenerator Section;
111     Standard_Integer i = 0;
112     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
113       Scurrent = Ex.Current() ;
114       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
115       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
116       C = new Geom_TrimmedCurve(C, First, Last);
117       Section.AddCurve(C);
118       i++;
119     }
120     
121     /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
122     Section.Perform(Precision::Confusion());
123     Handle(GeomFill_Line) Line = new GeomFill_Line(i);
124     
125     GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
126     App.Perform(Line, Section);
127     
128     if (!App.IsDone()) return 0;
129     Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
130     App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
131     Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
132       (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
133        App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
134     
135     if (GBS.IsNull()) return 0;
136     aShape = BRepBuilderAPI_MakeFace(GBS);
137   }    
138   else {
139     // implemented by skl 20.03.2008 for bug 16568
140     // make approximation - try to create bspline surface
141     // using GeomAPI_PointsToBSplineSurface
142     TColGeom_SequenceOfCurve aSeq;
143     int MaxNbPoles = 0;
144     // add curves from edges to sequence and find maximal
145     // number of poles if some of them are bsplines
146     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
147       Scurrent = Ex.Current() ;
148       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
149       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
150       Handle(Geom_TrimmedCurve) TC = Handle(Geom_TrimmedCurve)::DownCast(C);
151       if(TC.IsNull()) {
152         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(C);
153         if(!BC.IsNull()) {
154           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
155         }
156       }
157       else {
158         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(TC->BasisCurve());
159         if(BC.IsNull()) {
160           Handle(Geom_TrimmedCurve) TC1 = Handle(Geom_TrimmedCurve)::DownCast(TC->BasisCurve());
161           if(!TC1.IsNull()) {
162             BC = Handle(Geom_BSplineCurve)::DownCast(TC1->BasisCurve());
163           }
164         }
165         if(!BC.IsNull()) {
166           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
167         }
168       }
169       aSeq.Append(C);
170     }
171     // prepare array of points for creation bspline surface
172     // size of this array: by U parameter - number of curves,
173     // by V parameter - determ using MaxNbPoles but it's
174     // value must be between 21(min) and 101(max)
175     int nbc = aSeq.Length();
176     int nbp = Max(21,2*MaxNbPoles-1);
177     if(nbp>101) nbp = 101;
178     TColgp_Array2OfPnt Points(1,nbc,1,nbp);
179     int ic = 1;
180     for(; ic<=nbc; ic++) {
181       Handle(Geom_Curve) C = aSeq.Value(ic);
182       double fp = C->FirstParameter();
183       double lp = C->LastParameter();
184       double dp = (lp-fp)/(nbp-1);
185       int j = 0;
186       gp_Pnt P;
187       for(; j<nbp; j++) {
188         C->D0(fp+dp*j,P);
189         Points.SetValue(ic,j+1,P);
190       }
191     }
192     GeomAPI_PointsToBSplineSurface PTB(Points,mindeg,maxdeg,GeomAbs_C2,tol3d);
193     Handle(Geom_BSplineSurface) BS = PTB.Surface();
194     BRepBuilderAPI_MakeFace BB(BS);
195     TopoDS_Face NewF = BB.Face();
196     Handle(ShapeFix_Face) sff = new ShapeFix_Face(NewF);
197     sff->Perform();
198     sff->FixOrientation();
199     aShape = sff->Face();
200   }
201
202   /* We test the validity of resulting shape */
203   if (!BRepAlgo::IsValid((aShape))) {
204     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
205     return 0;
206   }
207
208   aFunction->SetValue(aShape);
209
210   log.SetTouched(Label());
211   return 1;
212 }
213
214
215 //=======================================================================
216 //function :  GEOMImpl_FillingDriver_Type_
217 //purpose  :
218 //=======================================================================
219 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FillingDriver_Type_()
220 {
221
222   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
223   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
224   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
225   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
226   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
227   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
228
229
230   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
231   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FillingDriver",
232                                                          sizeof(GEOMImpl_FillingDriver),
233                                                          1,
234                                                          (Standard_Address)_Ancestors,
235                                                          (Standard_Address)NULL);
236
237   return _aType;
238 }
239
240 //=======================================================================
241 //function : DownCast
242 //purpose  :
243 //=======================================================================
244
245 const Handle(GEOMImpl_FillingDriver) Handle(GEOMImpl_FillingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
246 {
247   Handle(GEOMImpl_FillingDriver) _anOtherObject;
248
249   if (!AnObject.IsNull()) {
250      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FillingDriver))) {
251        _anOtherObject = Handle(GEOMImpl_FillingDriver)((Handle(GEOMImpl_FillingDriver)&)AnObject);
252      }
253   }
254
255   return _anOtherObject ;
256 }
257
258