Salome HOME
0021684: EDF 2221 : Display the arguments and the name of the operations
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FillingDriver.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_FillingDriver.hxx>
26 #include <GEOMImpl_IFilling.hxx>
27 #include <GEOMImpl_Types.hxx>
28
29 #include <GEOM_Function.hxx>
30
31 #include <Basics_OCCTVersion.hxx>
32
33 #include <ShapeFix_Face.hxx>
34
35 #include <BRep_Tool.hxx>
36 #include <BRepAlgo.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39 #include <BRepBuilderAPI_Copy.hxx>
40
41 #include <TopAbs.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopoDS.hxx>
44 #include <TopoDS_Compound.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Vertex.hxx>
48
49 #include <Geom_Curve.hxx>
50 #include <Geom_Surface.hxx>
51 #include <Geom_TrimmedCurve.hxx>
52 #include <Geom_Line.hxx>
53 #include <Geom_Circle.hxx>
54 #include <Geom_Ellipse.hxx>
55 #include <Geom_BezierCurve.hxx>
56 #include <Geom_BSplineCurve.hxx>
57 #include <Geom_BSplineSurface.hxx>
58 #include <GeomFill_Line.hxx>
59 #include <GeomFill_AppSurf.hxx>
60 #include <GeomFill_SectionGenerator.hxx>
61 #include <GeomAPI_PointsToBSplineSurface.hxx>
62 #include <GeomAPI_PointsToBSpline.hxx>
63
64 #include <TColGeom_SequenceOfCurve.hxx>
65
66 #include <TColgp_SequenceOfPnt.hxx>
67 #include <TColgp_Array1OfPnt.hxx>
68
69 #include <Precision.hxx>
70
71 #include <Standard_ConstructionError.hxx>
72
73 //=======================================================================
74 //function : GetID
75 //purpose  :
76 //=======================================================================
77 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
78 {
79   static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
80   return aFillingDriver;
81 }
82
83 //=======================================================================
84 //function : GEOMImpl_FillingDriver
85 //purpose  :
86 //=======================================================================
87
88 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
89 {
90 }
91
92 //=======================================================================
93 //function : Execute
94 //purpose  :
95 //=======================================================================
96 Standard_Integer GEOMImpl_FillingDriver::Execute(TFunction_Logbook& log) const
97 {
98   if (Label().IsNull()) return 0;
99   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
100   if (aFunction.IsNull()) return 0;
101   if (aFunction->GetType() != BASIC_FILLING) return 0;
102
103   GEOMImpl_IFilling IF (aFunction);
104   Handle(GEOM_Function) aShapeFunction = IF.GetShape();
105   if (aShapeFunction.IsNull()) return 0;
106
107   TopoDS_Shape aShape;
108   BRepBuilderAPI_Copy Copy (aShapeFunction->GetValue());
109   if (Copy.IsDone())
110     aShape = Copy.Shape();
111
112   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
113
114   Standard_Integer mindeg = IF.GetMinDeg();
115   Standard_Integer maxdeg = IF.GetMaxDeg();
116   Standard_Real tol3d = IF.GetTol3D();
117   Standard_Boolean isApprox = IF.GetApprox();
118
119   if (mindeg > maxdeg) {
120     Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
121   }
122
123   /* we verify the contents of the shape */
124   TopExp_Explorer Ex;
125   TopoDS_Shape Scurrent;
126   Standard_Real First, Last;
127   Handle(Geom_Curve) C;
128
129   TopoDS_Compound aComp;
130   BRep_Builder B;
131   B.MakeCompound(aComp);
132
133   // 1. Convert argument wires, if any, into BSpline edges
134   TopoDS_Iterator It (aShape);
135   for (; It.More(); It.Next()) {
136     Scurrent = It.Value();
137     if (Scurrent.ShapeType() != TopAbs_EDGE) {
138       Handle(Geom_BSplineCurve) newC;
139       if (Scurrent.ShapeType() == TopAbs_WIRE) {
140         TColgp_SequenceOfPnt PntSeq;
141         // collect points
142         for (Ex.Init(Scurrent, TopAbs_EDGE); Ex.More(); Ex.Next()) {
143           TopoDS_Edge E = TopoDS::Edge(Ex.Current());
144           if (BRep_Tool::Degenerated(E)) continue;
145           C = BRep_Tool::Curve(E, First, Last);
146           if( E.Orientation() == TopAbs_REVERSED ) {
147             C->Reverse();
148           }
149           Handle(Geom_TrimmedCurve) tc = Handle(Geom_TrimmedCurve)::DownCast(C);
150           while( !tc.IsNull() ) {
151             C = tc->BasisCurve();
152             tc = Handle(Geom_TrimmedCurve)::DownCast(C);
153           }
154           int nbp = 10;
155           if( C->IsKind(STANDARD_TYPE(Geom_Line)) ) {
156             nbp = 4;
157           }
158           else if( C->IsKind(STANDARD_TYPE(Geom_Circle)) || 
159                    C->IsKind(STANDARD_TYPE(Geom_Ellipse)) ) {
160             nbp = (int)25*fabs(Last-First)/(2.*M_PI);
161           }
162           else if( C->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) {
163             Handle(Geom_BezierCurve) C3d = Handle(Geom_BezierCurve)::DownCast(C);
164             nbp = C3d->NbPoles();
165           }
166           else if( C->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ) {
167             Handle(Geom_BSplineCurve) C3d = Handle(Geom_BSplineCurve)::DownCast(C);
168             nbp = C3d->NbPoles();
169           }
170           else {
171           }
172           if( nbp<4 ) nbp = 4;
173           double dp = (Last-First)/(nbp-1);
174           for(int i=1; i<nbp; i++) {
175             gp_Pnt P;
176             C->D0(First+dp*(i-1),P);
177             PntSeq.Append(P);
178           }
179         }
180         // add last point
181         gp_Pnt P;
182         C->D0(Last,P);
183         PntSeq.Append(P);
184         // create BSpline 
185         if(PntSeq.Length()>1) {
186           TColgp_Array1OfPnt Pnts(1,PntSeq.Length());
187           // check orientation of wire
188           if( Scurrent.Orientation() == TopAbs_REVERSED ) {
189             for(int i=1; i<=PntSeq.Length(); i++) {
190               Pnts.SetValue(PntSeq.Length()-i+1,PntSeq.Value(i));
191             }
192           }
193           else {
194             for(int i=1; i<=PntSeq.Length(); i++) {
195               Pnts.SetValue(i,PntSeq.Value(i));
196             }
197           }
198           GeomAPI_PointsToBSpline PTB (Pnts);
199           newC = Handle(Geom_BSplineCurve)::DownCast(PTB.Curve());
200           // set periodic flag if curve is closed
201           //if( newC->IsClosed() ) {
202           //  newC->SetPeriodic();
203           //}
204           // create edge
205           double fp = newC->FirstParameter();
206           double lp = newC->FirstParameter();
207           gp_Pnt PF,PL;
208           newC->D0(fp,PF);
209           newC->D0(lp,PL);
210           TopoDS_Vertex VF,VL;
211           B.MakeVertex(VF,PF,1.e-7);
212           B.MakeVertex(VL,PL,1.e-7);
213           TopoDS_Edge newE;
214           B.MakeEdge(newE,newC,1.e-7);
215           B.Add(newE,VF);
216           B.Add(newE,VL.Reversed());
217           Scurrent = newE;
218         }
219       }
220       if (newC.IsNull()) {
221         Standard_ConstructionError::Raise("The argument compound must contain only edges");
222       }
223     }
224     B.Add(aComp,Scurrent);
225   }
226   aShape = aComp;
227
228   // 2. The surface construction
229   if (!isApprox) {
230     // make filling as in old version of SALOME (before 4.1.1)
231
232     Standard_Real tol2d = IF.GetTol2D();
233     Standard_Integer nbiter = IF.GetNbIter();
234     Standard_Integer aMethod = IF.GetMethod();
235
236     GeomFill_SectionGenerator Section;
237     Standard_Integer i = 0;
238     Handle(Geom_Curve) aLastC;
239     gp_Pnt PL1,PL2;
240     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
241       Scurrent = Ex.Current();
242       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
243       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
244       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
245       //if (Scurrent.Orientation() == TopAbs_REVERSED)
246       //  // Mantis isuue 0020659: consider the orientation of the edges
247       //  C = new Geom_TrimmedCurve(C, Last, First);
248       //else
249       //  C = new Geom_TrimmedCurve(C, First, Last);
250       C = new Geom_TrimmedCurve(C, First, Last);
251       gp_Pnt P1,P2;
252       C->D0(First,P1);
253       C->D0(Last,P2);
254
255       if (aMethod == 1 && Scurrent.Orientation() == TopAbs_REVERSED) {
256         C->Reverse();
257       }
258       else if (aMethod == 2) {
259         if (i == 0) {
260           PL1 = P1;
261           PL2 = P2;
262         }
263         else {
264           double d1 = PL1.Distance(P1) + PL2.Distance(P2);
265           double d2 = PL1.Distance(P2) + PL2.Distance(P1);
266           if (d2 < d1) {
267             C->Reverse();
268             PL1 = P2;
269             PL2 = P1;
270           }
271           else {
272             PL1 = P1;
273             PL2 = P2;
274           }
275         }
276       }
277
278       Section.AddCurve(C);
279       i++;
280     }
281
282     /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
283     Section.Perform(Precision::Confusion());
284     Handle(GeomFill_Line) Line = new GeomFill_Line(i);
285
286     GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
287     App.Perform(Line, Section);
288
289     if (!App.IsDone()) return 0;
290     Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
291     App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
292     Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
293       (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
294        App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
295
296     if (GBS.IsNull()) return 0;
297 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
298     aShape = BRepBuilderAPI_MakeFace(GBS, Precision::Confusion());
299 #else
300     aShape = BRepBuilderAPI_MakeFace(GBS);
301 #endif
302   }
303   else {
304     // implemented by skl 20.03.2008 for bug 16568
305     // make approximation - try to create bspline surface
306     // using GeomAPI_PointsToBSplineSurface
307
308     TColGeom_SequenceOfCurve aSeq;
309     int MaxNbPoles = 0;
310
311     // add curves from edges to sequence and find maximal
312     // number of poles if some of them are bsplines
313     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
314       Scurrent = Ex.Current();
315       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
316       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
317       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
318       Handle(Geom_TrimmedCurve) TC = Handle(Geom_TrimmedCurve)::DownCast(C);
319       if (TC.IsNull()) {
320         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(C);
321         if (!BC.IsNull()) {
322           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
323         }
324       }
325       else {
326         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(TC->BasisCurve());
327         if (BC.IsNull()) {
328           Handle(Geom_TrimmedCurve) TC1 = Handle(Geom_TrimmedCurve)::DownCast(TC->BasisCurve());
329           if (!TC1.IsNull()) {
330             BC = Handle(Geom_BSplineCurve)::DownCast(TC1->BasisCurve());
331           }
332         }
333         if (!BC.IsNull()) {
334           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
335         }
336       }
337       aSeq.Append(C);
338     }
339     // prepare array of points for creation bspline surface
340     // size of this array: by U parameter - number of curves,
341     // by V parameter - determ using MaxNbPoles but it's
342     // value must be between 21(min) and 101(max)
343     int nbc = aSeq.Length();
344     int nbp = Max(21, 2*MaxNbPoles-1);
345
346     // commented for Mantis issue 0021541
347     //if (nbp > 101) nbp = 101;
348
349     TColgp_Array2OfPnt Points (1, nbc, 1, nbp);
350     int ic = 1;
351     for (; ic <= nbc; ic++) {
352       Handle(Geom_Curve) C = aSeq.Value(ic);
353       double fp = C->FirstParameter();
354       double lp = C->LastParameter();
355       double dp = (lp-fp)/(nbp-1);
356       int j = 0;
357       gp_Pnt P;
358       for (; j < nbp; j++) {
359         C->D0(fp+dp*j, P);
360         Points.SetValue(ic, j+1, P);
361      }
362     }
363     GeomAPI_PointsToBSplineSurface PTB (Points, mindeg, maxdeg, GeomAbs_C2, tol3d);
364     Handle(Geom_BSplineSurface) BS = PTB.Surface();
365 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
366     BRepBuilderAPI_MakeFace BB (BS, Precision::Confusion());
367 #else
368     BRepBuilderAPI_MakeFace BB (BS);
369 #endif
370     TopoDS_Face NewF = BB.Face();
371     Handle(ShapeFix_Face) sff = new ShapeFix_Face (NewF);
372     sff->Perform();
373     sff->FixOrientation();
374     aShape = sff->Face();
375   }
376
377   /* We test the validity of resulting shape */
378   if (!BRepAlgo::IsValid((aShape))) {
379     Standard_ConstructionError::Raise("Algorithm has produced an invalid shape result");
380     return 0;
381   }
382
383   aFunction->SetValue(aShape);
384
385   log.SetTouched(Label());
386   return 1;
387 }
388
389 //================================================================================
390 /*!
391  * \brief Returns a name of creation operation and names and values of creation parameters
392  */
393 //================================================================================
394
395 bool GEOMImpl_FillingDriver::
396 GetCreationInformation(std::string&             theOperationName,
397                        std::vector<GEOM_Param>& theParams)
398 {
399   if (Label().IsNull()) return 0;
400   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
401
402   GEOMImpl_IFilling aCI( function );
403   Standard_Integer aType = function->GetType();
404
405   theOperationName = "FILLING";
406
407   switch ( aType ) {
408   case BASIC_FILLING:
409   {
410     AddParam( theParams, "Input compound", aCI.GetShape() );
411     AddParam( theParams, "Method", aCI.GetMethod() );
412     const char* method[3] =
413       { "Standard", "Use edges orientation", "Correct edges orientation" };
414     if ( 0 <= aCI.GetMethod() && aCI.GetMethod() < 3 )
415       theParams[1] << " = " << method[ aCI.GetMethod() ];
416     AddParam( theParams, "Min deg", aCI.GetMinDeg() );
417     AddParam( theParams, "Max deg", aCI.GetMaxDeg() );
418     AddParam( theParams, "Nb. Iter", aCI.GetNbIter() );
419     AddParam( theParams, "Tol. 2D", aCI.GetTol2D() );
420     AddParam( theParams, "Tol. 3D", aCI.GetTol3D() );
421     AddParam( theParams, "Approximation", aCI.GetApprox() );
422     break;
423   }
424   default:
425     return false;
426   }
427
428   return true;
429 }
430
431 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_FillingDriver,GEOM_BaseDriver);
432 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_FillingDriver,GEOM_BaseDriver);