Salome HOME
d17febfd2406184c13cafbb5d5e9bf3efad2d902
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FillingDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_FillingDriver.hxx>
24 #include <GEOM_Function.hxx>
25 #include <GEOMImpl_IFilling.hxx>
26 #include <GEOMImpl_Types.hxx>
27
28 #include <BRep_Tool.hxx>
29 #include <BRepAlgo.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31
32 #include <TopAbs.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopExp_Explorer.hxx>
36
37 #include <Geom_Curve.hxx>
38 #include <Geom_Surface.hxx>
39 #include <Geom_TrimmedCurve.hxx>
40 #include <Geom_BSplineSurface.hxx>
41 #include <GeomFill_Line.hxx>
42 #include <GeomFill_AppSurf.hxx>
43 #include <GeomFill_SectionGenerator.hxx>
44
45 #include <Precision.hxx>
46 #include <Standard_ConstructionError.hxx>
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
53 {
54   static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
55   return aFillingDriver;
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_FillingDriver
61 //purpose  :
62 //=======================================================================
63
64 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //=======================================================================
72 Standard_Integer GEOMImpl_FillingDriver::Execute(TFunction_Logbook& log) const
73 {
74   if (Label().IsNull()) return 0;
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
76   if (aFunction.IsNull()) return 0;
77
78   if (aFunction->GetType() != BASIC_FILLING) return 0;
79
80   GEOMImpl_IFilling IF (aFunction);
81   Handle(GEOM_Function) aShapeFunction = IF.GetShape();
82   if (aShapeFunction.IsNull()) return 0;
83   TopoDS_Shape aShape = aShapeFunction->GetValue();
84   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
85
86   Standard_Integer mindeg = IF.GetMinDeg();
87   Standard_Integer maxdeg = IF.GetMaxDeg();
88   Standard_Real tol3d = IF.GetTol2D();
89   Standard_Real tol2d = IF.GetTol3D();
90   Standard_Integer nbiter = IF.GetNbIter();
91   Standard_Boolean isApprox = IF.GetApprox();
92
93   if (mindeg > maxdeg) {
94     Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
95   }
96
97   /* we verify the contents of the shape */
98   TopExp_Explorer Ex;
99   TopoDS_Shape Scurrent;
100   Standard_Real First, Last;
101   Handle(Geom_Curve) C;
102   GeomFill_SectionGenerator Section;
103
104   Standard_Integer i = 0;
105   for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
106     Scurrent = Ex.Current() ;
107     if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
108     C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
109     C = new Geom_TrimmedCurve(C, First, Last);
110     Section.AddCurve(C);
111     i++;
112   }
113
114   /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
115   Section.Perform(Precision::Confusion());
116   Handle(GeomFill_Line) Line = new GeomFill_Line(i);
117
118   GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
119   App.Perform(Line, Section, isApprox);
120
121   if (!App.IsDone()) return 0;
122   Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
123   App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
124   Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
125     (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
126      App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
127
128   if (GBS.IsNull()) return 0;
129   aShape = BRepBuilderAPI_MakeFace(GBS);
130
131   /* We test the validity of resulting shape */
132   if (!BRepAlgo::IsValid((aShape))) {
133     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
134     return 0;
135   }
136
137   aFunction->SetValue(aShape);
138
139   log.SetTouched(Label());
140   return 1;
141 }
142
143
144 //=======================================================================
145 //function :  GEOMImpl_FillingDriver_Type_
146 //purpose  :
147 //=======================================================================
148 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FillingDriver_Type_()
149 {
150
151   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
152   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
153   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
154   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
155   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
156   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
157
158
159   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
160   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FillingDriver",
161                                                          sizeof(GEOMImpl_FillingDriver),
162                                                          1,
163                                                          (Standard_Address)_Ancestors,
164                                                          (Standard_Address)NULL);
165
166   return _aType;
167 }
168
169 //=======================================================================
170 //function : DownCast
171 //purpose  :
172 //=======================================================================
173
174 const Handle(GEOMImpl_FillingDriver) Handle(GEOMImpl_FillingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
175 {
176   Handle(GEOMImpl_FillingDriver) _anOtherObject;
177
178   if (!AnObject.IsNull()) {
179      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FillingDriver))) {
180        _anOtherObject = Handle(GEOMImpl_FillingDriver)((Handle(GEOMImpl_FillingDriver)&)AnObject);
181      }
182   }
183
184   return _anOtherObject ;
185 }
186
187