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