]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_FillingDriver.cxx
Salome HOME
IPAL22903: TC650: Store/Restore last GUI state does not work for Isos
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FillingDriver.cxx
1 // Copyright (C) 2007-2012  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
102   if (aFunction->GetType() != BASIC_FILLING) return 0;
103
104   GEOMImpl_IFilling IF (aFunction);
105   Handle(GEOM_Function) aShapeFunction = IF.GetShape();
106   if (aShapeFunction.IsNull()) return 0;
107   TopoDS_Shape aShape;
108
109   BRepBuilderAPI_Copy Copy(aShapeFunction->GetValue());
110   if( Copy.IsDone() )
111     aShape = Copy.Shape();
112
113   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
114
115   Standard_Integer mindeg = IF.GetMinDeg();
116   Standard_Integer maxdeg = IF.GetMaxDeg();
117   Standard_Real tol3d = IF.GetTol2D();
118   Standard_Real tol2d = IF.GetTol3D();
119   Standard_Integer nbiter = IF.GetNbIter();
120   Standard_Boolean isApprox = IF.GetApprox();
121   Standard_Integer aMethod = IF.GetMethod();
122
123   if (mindeg > maxdeg) {
124     Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
125   }
126
127   /* we verify the contents of the shape */
128   TopExp_Explorer Ex;
129   TopoDS_Shape Scurrent;
130   Standard_Real First, Last;
131   Handle(Geom_Curve) C;
132
133   TopoDS_Compound aComp;
134   BRep_Builder B;
135   B.MakeCompound(aComp);
136
137   TopoDS_Iterator It (aShape);
138   for (; It.More(); It.Next()) {
139     Scurrent = It.Value();
140     if (Scurrent.ShapeType() != TopAbs_EDGE) {
141       Handle(Geom_BSplineCurve) newC;
142       if (Scurrent.ShapeType() == TopAbs_WIRE) {
143         TColgp_SequenceOfPnt PntSeq;
144         // collect points
145         for (Ex.Init(Scurrent, TopAbs_EDGE); Ex.More(); Ex.Next()) {
146           TopoDS_Edge E = TopoDS::Edge(Ex.Current());
147           if (BRep_Tool::Degenerated(E)) continue;
148           C = BRep_Tool::Curve(E, First, Last);
149           if( E.Orientation() == TopAbs_REVERSED ) {
150             C->Reverse();
151           }
152           Handle(Geom_TrimmedCurve) tc = Handle(Geom_TrimmedCurve)::DownCast(C);
153           while( !tc.IsNull() ) {
154             C = tc->BasisCurve();
155             tc = Handle(Geom_TrimmedCurve)::DownCast(C);
156           }
157           int nbp = 10;
158           if( C->IsKind(STANDARD_TYPE(Geom_Line)) ) {
159             nbp = 4;
160           }
161           else if( C->IsKind(STANDARD_TYPE(Geom_Circle)) || 
162                    C->IsKind(STANDARD_TYPE(Geom_Ellipse)) ) {
163             nbp = (int)25*fabs(Last-First)/(2.*M_PI);
164           }
165           else if( C->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) {
166             Handle(Geom_BezierCurve) C3d = Handle(Geom_BezierCurve)::DownCast(C);
167             nbp = C3d->NbPoles();
168           }
169           else if( C->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ) {
170             Handle(Geom_BSplineCurve) C3d = Handle(Geom_BSplineCurve)::DownCast(C);
171             nbp = C3d->NbPoles();
172           }
173           else {
174           }
175           if( nbp<4 ) nbp = 4;
176           double dp = (Last-First)/(nbp-1);
177           for(int i=1; i<nbp; i++) {
178             gp_Pnt P;
179             C->D0(First+dp*(i-1),P);
180             PntSeq.Append(P);
181           }
182         }
183         // add last point
184         gp_Pnt P;
185         C->D0(Last,P);
186         PntSeq.Append(P);
187         // create BSpline 
188         if(PntSeq.Length()>1) {
189           TColgp_Array1OfPnt Pnts(1,PntSeq.Length());
190           // check orientation of wire
191           if( Scurrent.Orientation() == TopAbs_REVERSED ) {
192             for(int i=1; i<=PntSeq.Length(); i++) {
193               Pnts.SetValue(PntSeq.Length()-i+1,PntSeq.Value(i));
194             }
195           }
196           else {
197             for(int i=1; i<=PntSeq.Length(); i++) {
198               Pnts.SetValue(i,PntSeq.Value(i));
199             }
200           }
201           GeomAPI_PointsToBSpline PTB(Pnts);
202           newC = Handle(Geom_BSplineCurve)::DownCast(PTB.Curve());
203           // set periodic flag if curve is closed
204           //if( newC->IsClosed() ) {
205           //  newC->SetPeriodic();
206           //}
207           // create edge
208           double fp = newC->FirstParameter();
209           double lp = newC->FirstParameter();
210           gp_Pnt PF,PL;
211           newC->D0(fp,PF);
212           newC->D0(lp,PL);
213           TopoDS_Vertex VF,VL;
214           B.MakeVertex(VF,PF,1.e-7);
215           B.MakeVertex(VL,PL,1.e-7);
216           TopoDS_Edge newE;
217           B.MakeEdge(newE,newC,1.e-7);
218           B.Add(newE,VF);
219           B.Add(newE,VL.Reversed());
220           Scurrent = newE;
221         }
222       }
223       if(newC.IsNull()) {
224         Standard_ConstructionError::Raise("The argument compound must contain only edges");
225       }
226     }
227     B.Add(aComp,Scurrent);
228   }
229   aShape = aComp;
230
231   if (!isApprox) {
232     // make filling as in old version of SALOME (before 4.1.1)
233     GeomFill_SectionGenerator Section;
234     Standard_Integer i = 0;
235     Handle(Geom_Curve) aLastC;
236     gp_Pnt PL1,PL2;
237     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
238       Scurrent = Ex.Current();
239       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
240       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
241       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
242       //if (Scurrent.Orientation() == TopAbs_REVERSED)
243       //  // Mantis isuue 0020659: consider the orientation of the edges
244       //  C = new Geom_TrimmedCurve(C, Last, First);
245       //else
246       //  C = new Geom_TrimmedCurve(C, First, Last);
247       C = new Geom_TrimmedCurve(C, First, Last);
248       gp_Pnt P1,P2;
249       C->D0(First,P1);
250       C->D0(Last,P2);
251
252       if( aMethod==1 && Scurrent.Orientation() == TopAbs_REVERSED ) {
253         C->Reverse();
254       }
255       else if( aMethod==2 ) {
256         if( i==0 ) {
257           PL1 = P1;
258           PL2 = P2;
259         }
260         else {
261           double d1 = PL1.Distance(P1) + PL2.Distance(P2);
262           double d2 = PL1.Distance(P2) + PL2.Distance(P1);
263           if(d2<d1) {
264             C->Reverse();
265             PL1 = P2;
266             PL2 = P1;
267           }
268           else {
269             PL1 = P1;
270             PL2 = P2;
271           }
272         }
273       }
274
275       Section.AddCurve(C);
276       i++;
277     }
278
279     /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
280     Section.Perform(Precision::Confusion());
281     Handle(GeomFill_Line) Line = new GeomFill_Line(i);
282
283     GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
284     App.Perform(Line, Section);
285
286     if (!App.IsDone()) return 0;
287     Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
288     App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
289     Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
290       (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
291        App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
292
293     if (GBS.IsNull()) return 0;
294 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
295     aShape = BRepBuilderAPI_MakeFace(GBS, Precision::Confusion());
296 #else
297     aShape = BRepBuilderAPI_MakeFace(GBS);
298 #endif
299   }
300   else {
301     // implemented by skl 20.03.2008 for bug 16568
302     // make approximation - try to create bspline surface
303     // using GeomAPI_PointsToBSplineSurface
304     TColGeom_SequenceOfCurve aSeq;
305     int MaxNbPoles = 0;
306     // add curves from edges to sequence and find maximal
307     // number of poles if some of them are bsplines
308     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
309       Scurrent = Ex.Current();
310       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
311       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
312       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
313       Handle(Geom_TrimmedCurve) TC = Handle(Geom_TrimmedCurve)::DownCast(C);
314       if(TC.IsNull()) {
315         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(C);
316         if(!BC.IsNull()) {
317           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
318         }
319       }
320       else {
321         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(TC->BasisCurve());
322         if(BC.IsNull()) {
323           Handle(Geom_TrimmedCurve) TC1 = Handle(Geom_TrimmedCurve)::DownCast(TC->BasisCurve());
324           if(!TC1.IsNull()) {
325             BC = Handle(Geom_BSplineCurve)::DownCast(TC1->BasisCurve());
326           }
327         }
328         if(!BC.IsNull()) {
329           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
330         }
331       }
332       aSeq.Append(C);
333     }
334     // prepare array of points for creation bspline surface
335     // size of this array: by U parameter - number of curves,
336     // by V parameter - determ using MaxNbPoles but it's
337     // value must be between 21(min) and 101(max)
338     int nbc = aSeq.Length();
339     int nbp = Max(21,2*MaxNbPoles-1);
340     if(nbp>101) nbp = 101;
341     TColgp_Array2OfPnt Points(1,nbc,1,nbp);
342     int ic = 1;
343     for(; ic<=nbc; ic++) {
344       Handle(Geom_Curve) C = aSeq.Value(ic);
345       double fp = C->FirstParameter();
346       double lp = C->LastParameter();
347       double dp = (lp-fp)/(nbp-1);
348       int j = 0;
349       gp_Pnt P;
350       for(; j<nbp; j++) {
351         C->D0(fp+dp*j,P);
352         Points.SetValue(ic,j+1,P);
353       }
354     }
355     GeomAPI_PointsToBSplineSurface PTB(Points,mindeg,maxdeg,GeomAbs_C2,tol3d);
356     Handle(Geom_BSplineSurface) BS = PTB.Surface();
357 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
358     BRepBuilderAPI_MakeFace BB (BS, Precision::Confusion());
359 #else
360     BRepBuilderAPI_MakeFace BB (BS);
361 #endif
362     TopoDS_Face NewF = BB.Face();
363     Handle(ShapeFix_Face) sff = new ShapeFix_Face(NewF);
364     sff->Perform();
365     sff->FixOrientation();
366     aShape = sff->Face();
367   }
368
369   /* We test the validity of resulting shape */
370   if (!BRepAlgo::IsValid((aShape))) {
371     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
372     return 0;
373   }
374
375   aFunction->SetValue(aShape);
376
377   log.SetTouched(Label());
378   return 1;
379 }
380
381
382 //=======================================================================
383 //function :  GEOMImpl_FillingDriver_Type_
384 //purpose  :
385 //=======================================================================
386 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FillingDriver_Type_()
387 {
388   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
389   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
390   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
391   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
392   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
393   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
394
395   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
396   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FillingDriver",
397                                                          sizeof(GEOMImpl_FillingDriver),
398                                                          1,
399                                                          (Standard_Address)_Ancestors,
400                                                          (Standard_Address)NULL);
401
402   return _aType;
403 }
404
405 //=======================================================================
406 //function : DownCast
407 //purpose  :
408 //=======================================================================
409 const Handle(GEOMImpl_FillingDriver) Handle(GEOMImpl_FillingDriver)::DownCast
410                                  (const Handle(Standard_Transient)& AnObject)
411 {
412   Handle(GEOMImpl_FillingDriver) _anOtherObject;
413
414   if (!AnObject.IsNull()) {
415      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FillingDriver))) {
416        _anOtherObject = Handle(GEOMImpl_FillingDriver)((Handle(GEOMImpl_FillingDriver)&)AnObject);
417      }
418   }
419
420   return _anOtherObject;
421 }