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