]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_FillingDriver.cxx
Salome HOME
patch for incorrect compilation
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FillingDriver.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 <BRepAlgo.hxx>
34 #include <BRepBuilderAPI_Copy.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRep_Tool.hxx>
38 #include <BRepOffsetAPI_MakeFilling.hxx>
39
40 #include <GeomAPI_PointsToBSpline.hxx>
41 #include <GeomAPI_PointsToBSplineSurface.hxx>
42 #include <GeomFill_AppSurf.hxx>
43 #include <GeomFill_Line.hxx>
44 #include <GeomFill_SectionGenerator.hxx>
45 #include <Geom_BSplineCurve.hxx>
46 #include <Geom_BSplineSurface.hxx>
47 #include <Geom_BezierCurve.hxx>
48 #include <Geom_Circle.hxx>
49 #include <Geom_Curve.hxx>
50 #include <Geom_Ellipse.hxx>
51 #include <Geom_Line.hxx>
52 #include <Geom_Surface.hxx>
53 #include <Geom_TrimmedCurve.hxx>
54
55 #include <Precision.hxx>
56 #include <ShapeFix_Face.hxx>
57
58 #include <TColGeom_SequenceOfCurve.hxx>
59 #include <TColgp_Array1OfPnt.hxx>
60 #include <TColgp_SequenceOfPnt.hxx>
61
62 #include <TopAbs.hxx>
63 #include <TopExp_Explorer.hxx>
64 #include <TopTools_SequenceOfShape.hxx>
65 #include <TopoDS.hxx>
66 #include <TopoDS_Compound.hxx>
67 #include <TopoDS_Edge.hxx>
68 #include <TopoDS_Face.hxx>
69 #include <TopoDS_Shape.hxx>
70 #include <TopoDS_Vertex.hxx>
71
72 #include <Standard_ConstructionError.hxx>
73
74 //=======================================================================
75 //function : GetID
76 //purpose  :
77 //=======================================================================
78 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
79 {
80   static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
81   return aFillingDriver;
82 }
83
84 //=======================================================================
85 //function : GEOMImpl_FillingDriver
86 //purpose  :
87 //=======================================================================
88
89 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
90 {
91 }
92
93 //=======================================================================
94 //function : Execute
95 //purpose  :
96 //=======================================================================
97 Standard_Integer GEOMImpl_FillingDriver::Execute(LOGBOOK& log) const
98 {
99   if (Label().IsNull()) return 0;
100   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
101   if (aFunction.IsNull()) return 0;
102   TopoDS_Shape aShape;
103   GEOMImpl_IFilling IF (aFunction);
104   if( aFunction->GetType() == BASIC_FILLING )
105   {
106     GEOMImpl_IFilling IF (aFunction);
107     Standard_Integer   mindeg = IF.GetMinDeg();
108     Standard_Integer   maxdeg = IF.GetMaxDeg();
109     Standard_Real       tol3d = IF.GetTol3D();
110     Standard_Boolean isApprox = IF.GetApprox();
111
112     if (mindeg > maxdeg) {
113       Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
114     }
115
116     /* we verify the contents of the shape */
117     TopExp_Explorer Ex;
118     TopoDS_Shape Scurrent;
119     Standard_Real First, Last;
120     Handle(Geom_Curve) C;
121
122     TopoDS_Compound aComp;
123     BRep_Builder B;
124     B.MakeCompound(aComp);
125
126     // input is either a list or compound of contours
127     TopTools_SequenceOfShape contours;
128     Handle(TColStd_HSequenceOfTransient) aShapeFunctions = IF.GetShapes();
129     if ( aShapeFunctions.IsNull() || aShapeFunctions->IsEmpty() ) return 0;
130     for ( int i = 1; i <= aShapeFunctions->Length(); ++i )
131     {
132       Handle(GEOM_Function) fun = Handle(GEOM_Function)::DownCast( aShapeFunctions->Value( i ));
133       if ( fun.IsNull() ) return 0;
134       TopoDS_Shape s = fun->GetValue();
135       if ( s.IsNull() ) return 0;
136       BRepBuilderAPI_Copy Copy (s);
137       if ( Copy.IsDone() )
138         contours.Append( Copy.Shape() );
139     }
140
141     // 1. Convert argument wires, if any, into BSpline edges
142     for ( int i = 1; i <= contours.Length(); ++i )
143     {
144       Scurrent = contours.Value( i );
145       if (Scurrent.ShapeType() != TopAbs_EDGE) {
146       
147         if (Scurrent.ShapeType() == TopAbs_WIRE)
148         {
149           const TopoDS_Wire& CurWire = TopoDS::Wire(Scurrent);
150           TopoDS_Edge NewEdge = BRepAlgo::ConcatenateWireC0(CurWire);
151           if (NewEdge.IsNull())
152             Standard_ConstructionError::Raise("Failed to join several edges into one");
153           Scurrent = NewEdge;
154         }
155         else if (Scurrent.ShapeType() == TopAbs_COMPOUND)
156         {
157           for ( TopoDS_Iterator It( Scurrent ); It.More(); It.Next() )
158             contours.Append( It.Value() );
159           continue;
160         }
161         else
162         {
163           Standard_ConstructionError::Raise("Input must contain only edges or/and wires");
164         }
165       }
166       B.Add(aComp,Scurrent);
167     }
168     aShape = aComp;
169
170     // 2. The surface construction
171     if (!isApprox) {
172       // make filling as in old version of SALOME (before 4.1.1)
173
174       Standard_Real      tol2d = IF.GetTol2D();
175       Standard_Integer  nbiter = IF.GetNbIter();
176       Standard_Integer aMethod = IF.GetMethod();
177
178       GeomFill_SectionGenerator Section;
179       Standard_Integer aNumSection = 0;
180       Handle(Geom_Curve) aLastC;
181       gp_Pnt PL1,PL2;
182       for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
183         Scurrent = Ex.Current();
184         if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
185         if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
186         C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
187         //if (Scurrent.Orientation() == TopAbs_REVERSED)
188         //  // Mantis isuue 0020659: consider the orientation of the edges
189         //  C = new Geom_TrimmedCurve(C, Last, First);
190         //else
191         //  C = new Geom_TrimmedCurve(C, First, Last);
192         C = new Geom_TrimmedCurve(C, First, Last);
193         gp_Pnt P1,P2;
194         C->D0(First,P1);
195         C->D0(Last,P2);
196
197         if (aMethod == 1 && Scurrent.Orientation() == TopAbs_REVERSED) {
198           C->Reverse();
199         }
200         else if (aMethod == 2) {
201           if (aNumSection == 0) {
202             PL1 = P1;
203             PL2 = P2;
204           }
205           else {
206             double d1 = PL1.Distance(P1) + PL2.Distance(P2);
207             double d2 = PL1.Distance(P2) + PL2.Distance(P1);
208             if (d2 < d1) {
209               C->Reverse();
210               PL1 = P2;
211               PL2 = P1;
212             }
213             else {
214               PL1 = P1;
215               PL2 = P2;
216             }
217           }
218         }
219
220         Section.AddCurve(C);
221         aNumSection++;
222       }
223
224       /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
225       Section.Perform(Precision::PConfusion());
226       //imn: to fix the bug 23351: "Crash is filling has only one edge"
227       //after fix related occt bug 27875 must be removed
228       if (aNumSection < 2) {
229         Standard_ConstructionError::Raise("Input must contain more than one edge");
230       }
231       Handle(GeomFill_Line) Line = new GeomFill_Line(aNumSection);
232
233       GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
234       App.Perform(Line, Section);
235
236       if (!App.IsDone()) return 0;
237       Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
238       App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
239       Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
240         (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
241          App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
242
243       if (GBS.IsNull()) return 0;
244       aShape = BRepBuilderAPI_MakeFace(GBS, Precision::Confusion());
245     }
246     else {
247       // implemented by skl 20.03.2008 for bug 16568
248       // make approximation - try to create bspline surface
249       // using GeomAPI_PointsToBSplineSurface
250
251       TColGeom_SequenceOfCurve aSeq;
252       int MaxNbPoles = 0;
253
254       // add curves from edges to sequence and find maximal
255       // number of poles if some of them are bsplines
256       for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
257         Scurrent = Ex.Current();
258         if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
259         if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
260         C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
261         Handle(Geom_TrimmedCurve) TC = Handle(Geom_TrimmedCurve)::DownCast(C);
262         if (TC.IsNull()) {
263           Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(C);
264           if (!BC.IsNull()) {
265             MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
266           }
267         }
268         else {
269           Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(TC->BasisCurve());
270           if (BC.IsNull()) {
271             Handle(Geom_TrimmedCurve) TC1 = Handle(Geom_TrimmedCurve)::DownCast(TC->BasisCurve());
272             if (!TC1.IsNull()) {
273               BC = Handle(Geom_BSplineCurve)::DownCast(TC1->BasisCurve());
274             }
275           }
276           if (!BC.IsNull()) {
277             MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
278           }
279         }
280         aSeq.Append(C);
281       }
282       // prepare array of points for creation bspline surface
283       // size of this array: by U parameter - number of curves,
284       // by V parameter - determ using MaxNbPoles but it's
285       // value must be between 21(min) and 101(max)
286       int nbc = aSeq.Length();
287       int nbp = Max(21, 2*MaxNbPoles-1);
288
289       // commented for Mantis issue 0021541
290       //if (nbp > 101) nbp = 101;
291
292       TColgp_Array2OfPnt Points (1, nbc, 1, nbp);
293       int ic = 1;
294       for (; ic <= nbc; ic++) {
295         Handle(Geom_Curve) C = aSeq.Value(ic);
296         double fp = C->FirstParameter();
297         double lp = C->LastParameter();
298         double dp = (lp-fp)/(nbp-1);
299         int j = 0;
300         gp_Pnt P;
301         for (; j < nbp; j++) {
302           C->D0(fp+dp*j, P);
303           Points.SetValue(ic, j+1, P);
304        }
305       }
306       GeomAPI_PointsToBSplineSurface PTB (Points, mindeg, maxdeg, GeomAbs_C2, tol3d);
307       Handle(Geom_BSplineSurface) BS = PTB.Surface();
308       BRepBuilderAPI_MakeFace BB (BS, Precision::Confusion());
309       TopoDS_Face NewF = BB.Face();
310       Handle(ShapeFix_Face) sff = new ShapeFix_Face (NewF);
311       sff->Perform();
312       sff->FixOrientation();
313       aShape = sff->Face();
314     }
315   }
316   else if( aFunction->GetType() == FILLING_ON_CONSTRAINTS )
317   {
318     BRepOffsetAPI_MakeFilling MakeFilling;
319
320     Handle(TColStd_HSequenceOfTransient) aConstraints = IF.GetShapes();
321
322     for ( unsigned int ind = 1; ind <= aConstraints->Length(); ind++ ) {
323       TopoDS_Edge E;
324       TopoDS_Face F;
325       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast( aConstraints->Value(ind) );
326       if ( !aRefShape->GetValue().IsNull() && aRefShape->GetValue().ShapeType() == TopAbs_EDGE )
327         E = TopoDS::Edge(aRefShape->GetValue() );
328       else {
329         Standard_RangeError::Raise("Wrong parameters");
330       }
331       if ( ind < aConstraints->Length() ) {
332         Handle(GEOM_Function) aRefFaceShape = Handle(GEOM_Function)::DownCast( aConstraints->Value(ind+1) );
333         if ( !aRefFaceShape->GetValue().IsNull() && aRefFaceShape->GetValue().ShapeType() == TopAbs_FACE ) {
334           F = TopoDS::Face( aRefFaceShape->GetValue() );
335           ind++;
336         }
337       }
338       if ( !F.IsNull() )
339         MakeFilling.Add( E, F, GeomAbs_G1 );
340       else
341         MakeFilling.Add( E, GeomAbs_C0 );
342     }
343
344     MakeFilling.Build();
345     if ( !MakeFilling.IsDone() )
346     {
347       Standard_RangeError::Raise("Filling on constraints failed");
348     }
349
350     aShape = TopoDS::Face( MakeFilling.Shape() );
351   }
352
353   /* We test the validity of resulting shape */
354   if (!BRepAlgo::IsValid((aShape))) {
355     Standard_ConstructionError::Raise("Algorithm has produced an invalid shape result");
356   }
357
358   aFunction->SetValue(aShape);
359
360 #if OCC_VERSION_MAJOR < 7
361   log.SetTouched(Label());
362 #else
363   log->SetTouched(Label());
364 #endif
365   return 1;
366 }
367
368 //================================================================================
369 /*!
370  * \brief Returns a name of creation operation and names and values of creation parameters
371  */
372 //================================================================================
373
374 bool GEOMImpl_FillingDriver::
375 GetCreationInformation(std::string&             theOperationName,
376                        std::vector<GEOM_Param>& theParams)
377 {
378   if (Label().IsNull()) return 0;
379   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
380
381   GEOMImpl_IFilling aCI( function );
382   Standard_Integer aType = function->GetType();
383
384   theOperationName = "FILLING";
385
386   switch ( aType ) {
387   case BASIC_FILLING:
388   {
389     AddParam( theParams, "Input compound", aCI.GetShapes() );
390     AddParam( theParams, "Method", aCI.GetMethod() );
391     const char* method[3] =
392       { "Standard", "Use edges orientation", "Correct edges orientation" };
393     if ( 0 <= aCI.GetMethod() && aCI.GetMethod() < 3 )
394       theParams[1] << " = " << method[ aCI.GetMethod() ];
395     AddParam( theParams, "Min deg", aCI.GetMinDeg() );
396     AddParam( theParams, "Max deg", aCI.GetMaxDeg() );
397     AddParam( theParams, "Nb. Iter", aCI.GetNbIter() );
398     AddParam( theParams, "Tol. 2D", aCI.GetTol2D() );
399     AddParam( theParams, "Tol. 3D", aCI.GetTol3D() );
400     AddParam( theParams, "Approximation", aCI.GetApprox() );
401     break;
402   }
403   case FILLING_ON_CONSTRAINTS:
404   {
405         theOperationName = "FACE";
406     AddParam( theParams, "Edges/Faces", aCI.GetShapes() );
407     break;
408   }
409   default:
410     return false;
411   }
412
413   return true;
414 }
415
416 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_FillingDriver,GEOM_BaseDriver);