]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_FillingDriver.cxx
Salome HOME
0020628: EDF 1144 GEOM : In TUI, need a function to know the orientation of an edge...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FillingDriver.cxx
1 //  Copyright (C) 2007-2008  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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_FillingDriver.hxx>
25 #include <GEOM_Function.hxx>
26 #include <GEOMImpl_IFilling.hxx>
27 #include <GEOMImpl_Types.hxx>
28
29 #include <BRep_Tool.hxx>
30 #include <BRepAlgo.hxx>
31 #include <BRepBuilderAPI_MakeFace.hxx>
32
33 #include <TopAbs.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #include <Geom_Curve.hxx>
39 #include <Geom_Surface.hxx>
40 #include <Geom_TrimmedCurve.hxx>
41 #include <Geom_BSplineSurface.hxx>
42 #include <GeomFill_Line.hxx>
43 #include <GeomFill_AppSurf.hxx>
44 #include <GeomFill_SectionGenerator.hxx>
45
46 #include <Precision.hxx>
47 #include <Standard_ConstructionError.hxx>
48
49 #include <TColGeom_SequenceOfCurve.hxx>
50 #include <ShapeFix_Face.hxx>
51 #include <GeomAPI_PointsToBSplineSurface.hxx>
52 #include <Geom_BSplineCurve.hxx>
53
54 //=======================================================================
55 //function : GetID
56 //purpose  :
57 //=======================================================================
58 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
59 {
60   static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
61   return aFillingDriver;
62 }
63
64
65 //=======================================================================
66 //function : GEOMImpl_FillingDriver
67 //purpose  :
68 //=======================================================================
69
70 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
71 {
72 }
73
74 //=======================================================================
75 //function : Execute
76 //purpose  :
77 //=======================================================================
78 Standard_Integer GEOMImpl_FillingDriver::Execute(TFunction_Logbook& log) const
79 {
80   if (Label().IsNull()) return 0;
81   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
82   if (aFunction.IsNull()) return 0;
83
84   if (aFunction->GetType() != BASIC_FILLING) return 0;
85
86   GEOMImpl_IFilling IF (aFunction);
87   Handle(GEOM_Function) aShapeFunction = IF.GetShape();
88   if (aShapeFunction.IsNull()) return 0;
89   TopoDS_Shape aShape = aShapeFunction->GetValue();
90   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
91
92   Standard_Integer mindeg = IF.GetMinDeg();
93   Standard_Integer maxdeg = IF.GetMaxDeg();
94   Standard_Real tol3d = IF.GetTol2D();
95   Standard_Real tol2d = IF.GetTol3D();
96   Standard_Integer nbiter = IF.GetNbIter();
97   Standard_Boolean isApprox = IF.GetApprox();
98
99   if (mindeg > maxdeg) {
100     Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
101   }
102
103   /* we verify the contents of the shape */
104   TopExp_Explorer Ex;
105   TopoDS_Shape Scurrent;
106   Standard_Real First, Last;
107   Handle(Geom_Curve) C;
108
109   TopoDS_Iterator It (aShape);
110   for (; It.More(); It.Next()) {
111     Scurrent = It.Value();
112     if (Scurrent.ShapeType() != TopAbs_EDGE)
113       Standard_ConstructionError::Raise("The argument compound must contain only edges");
114   }
115
116   if (!isApprox) {
117     // make filling as in old version of SALOME (before 4.1.1)
118     GeomFill_SectionGenerator Section;
119     Standard_Integer i = 0;
120     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
121       Scurrent = Ex.Current();
122       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
123       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
124       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
125       if (Scurrent.Orientation() == TopAbs_REVERSED)
126         // Mantis isuue 0020659: consider the orientation of the edges
127         C = new Geom_TrimmedCurve(C, Last, First);
128       else
129         C = new Geom_TrimmedCurve(C, First, Last);
130       Section.AddCurve(C);
131       i++;
132     }
133
134     /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
135     Section.Perform(Precision::Confusion());
136     Handle(GeomFill_Line) Line = new GeomFill_Line(i);
137
138     GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
139     App.Perform(Line, Section);
140
141     if (!App.IsDone()) return 0;
142     Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
143     App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
144     Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
145       (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
146        App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
147
148     if (GBS.IsNull()) return 0;
149     aShape = BRepBuilderAPI_MakeFace(GBS);
150   }
151   else {
152     // implemented by skl 20.03.2008 for bug 16568
153     // make approximation - try to create bspline surface
154     // using GeomAPI_PointsToBSplineSurface
155     TColGeom_SequenceOfCurve aSeq;
156     int MaxNbPoles = 0;
157     // add curves from edges to sequence and find maximal
158     // number of poles if some of them are bsplines
159     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
160       Scurrent = Ex.Current();
161       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
162       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
163       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
164       Handle(Geom_TrimmedCurve) TC = Handle(Geom_TrimmedCurve)::DownCast(C);
165       if(TC.IsNull()) {
166         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(C);
167         if(!BC.IsNull()) {
168           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
169         }
170       }
171       else {
172         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(TC->BasisCurve());
173         if(BC.IsNull()) {
174           Handle(Geom_TrimmedCurve) TC1 = Handle(Geom_TrimmedCurve)::DownCast(TC->BasisCurve());
175           if(!TC1.IsNull()) {
176             BC = Handle(Geom_BSplineCurve)::DownCast(TC1->BasisCurve());
177           }
178         }
179         if(!BC.IsNull()) {
180           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
181         }
182       }
183       aSeq.Append(C);
184     }
185     // prepare array of points for creation bspline surface
186     // size of this array: by U parameter - number of curves,
187     // by V parameter - determ using MaxNbPoles but it's
188     // value must be between 21(min) and 101(max)
189     int nbc = aSeq.Length();
190     int nbp = Max(21,2*MaxNbPoles-1);
191     if(nbp>101) nbp = 101;
192     TColgp_Array2OfPnt Points(1,nbc,1,nbp);
193     int ic = 1;
194     for(; ic<=nbc; ic++) {
195       Handle(Geom_Curve) C = aSeq.Value(ic);
196       double fp = C->FirstParameter();
197       double lp = C->LastParameter();
198       double dp = (lp-fp)/(nbp-1);
199       int j = 0;
200       gp_Pnt P;
201       for(; j<nbp; j++) {
202         C->D0(fp+dp*j,P);
203         Points.SetValue(ic,j+1,P);
204       }
205     }
206     GeomAPI_PointsToBSplineSurface PTB(Points,mindeg,maxdeg,GeomAbs_C2,tol3d);
207     Handle(Geom_BSplineSurface) BS = PTB.Surface();
208     BRepBuilderAPI_MakeFace BB(BS);
209     TopoDS_Face NewF = BB.Face();
210     Handle(ShapeFix_Face) sff = new ShapeFix_Face(NewF);
211     sff->Perform();
212     sff->FixOrientation();
213     aShape = sff->Face();
214   }
215
216   /* We test the validity of resulting shape */
217   if (!BRepAlgo::IsValid((aShape))) {
218     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
219     return 0;
220   }
221
222   aFunction->SetValue(aShape);
223
224   log.SetTouched(Label());
225   return 1;
226 }
227
228
229 //=======================================================================
230 //function :  GEOMImpl_FillingDriver_Type_
231 //purpose  :
232 //=======================================================================
233 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FillingDriver_Type_()
234 {
235
236   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
237   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
238   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
239   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
240   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
241   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
242
243
244   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
245   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FillingDriver",
246                                                          sizeof(GEOMImpl_FillingDriver),
247                                                          1,
248                                                          (Standard_Address)_Ancestors,
249                                                          (Standard_Address)NULL);
250
251   return _aType;
252 }
253
254 //=======================================================================
255 //function : DownCast
256 //purpose  :
257 //=======================================================================
258
259 const Handle(GEOMImpl_FillingDriver) Handle(GEOMImpl_FillingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
260 {
261   Handle(GEOMImpl_FillingDriver) _anOtherObject;
262
263   if (!AnObject.IsNull()) {
264      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FillingDriver))) {
265        _anOtherObject = Handle(GEOMImpl_FillingDriver)((Handle(GEOMImpl_FillingDriver)&)AnObject);
266      }
267   }
268
269   return _anOtherObject;
270 }