Salome HOME
Copyrights update
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SplineDriver.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/
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_SplineDriver.hxx>
24 #include <GEOMImpl_ISpline.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <BRep_Tool.hxx>
30
31 #include <TopAbs.hxx>
32 #include <TopExp.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Vertex.hxx>
37
38 #include <Geom_BezierCurve.hxx>
39 //#include <GeomAPI_PointsToBSpline.hxx>
40 #include <GeomAPI_Interpolate.hxx>
41
42 #include <gp.hxx>
43 #include <gp_Pnt.hxx>
44 #include <gp_Circ.hxx>
45 #include <Precision.hxx>
46 #include <TColgp_Array1OfPnt.hxx>
47 #include <TColgp_HArray1OfPnt.hxx>
48
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //=======================================================================
53 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
54 {
55   static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
56   return aSplineDriver;
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_SplineDriver
62 //purpose  :
63 //=======================================================================
64 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //=======================================================================
72 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
73 {
74   if (Label().IsNull()) return 0;
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
76
77   GEOMImpl_ISpline aCI (aFunction);
78   Standard_Integer aType = aFunction->GetType();
79
80   TopoDS_Shape aShape;
81
82   if (aType == SPLINE_BEZIER || aType == SPLINE_INTERPOLATION) {
83     int ind, aLen = aCI.GetLength();
84     if (aLen < 2) return 0;
85     Standard_Boolean isSeveral = Standard_False;
86     gp_Pnt aPrevP;
87     TColgp_Array1OfPnt CurvePoints (1, aLen);
88     for (ind = 1; ind <= aLen; ind++) {
89       Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
90       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
91       if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
92         gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
93         if (!isSeveral && ind > 1) {
94           if (aP.Distance(aPrevP) > Precision::Confusion()) {
95             isSeveral = Standard_True;
96           }
97         }
98         CurvePoints.SetValue(ind, aP);
99         aPrevP = aP;
100       }
101     }
102     if (aType == SPLINE_BEZIER) {
103       if (!isSeveral) {
104         Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
105       }
106       Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve(CurvePoints);
107       aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
108     } else {
109 //      GeomAPI_PointsToBSpline GBC (CurvePoints);
110 //      aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
111       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt(1, aLen);
112       for (ind = 1; ind <= aLen; ind++) {
113         aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
114       }
115       GeomAPI_Interpolate GBC (aHCurvePoints, Standard_False, gp::Resolution());
116       GBC.Perform();
117       if (GBC.IsDone())
118         aShape = BRepBuilderAPI_MakeEdge(GBC.Curve()).Edge();
119       else
120         return 0;
121     }
122   }
123   else {
124   }
125
126   if (aShape.IsNull()) return 0;
127
128   aFunction->SetValue(aShape);
129
130   log.SetTouched(Label());
131
132   return 1;
133 }
134
135
136 //=======================================================================
137 //function :  GEOMImpl_SplineDriver_Type_
138 //purpose  :
139 //=======================================================================
140 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SplineDriver_Type_()
141 {
142
143   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
144   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
145   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
146   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
147   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
148   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
149
150
151   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
152   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_SplineDriver",
153                                                          sizeof(GEOMImpl_SplineDriver),
154                                                          1,
155                                                          (Standard_Address)_Ancestors,
156                                                          (Standard_Address)NULL);
157
158   return _aType;
159 }
160
161 //=======================================================================
162 //function : DownCast
163 //purpose  :
164 //=======================================================================
165 const Handle(GEOMImpl_SplineDriver) Handle(GEOMImpl_SplineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
166 {
167   Handle(GEOMImpl_SplineDriver) _anOtherObject;
168
169   if (!AnObject.IsNull()) {
170      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SplineDriver))) {
171        _anOtherObject = Handle(GEOMImpl_SplineDriver)((Handle(GEOMImpl_SplineDriver)&)AnObject);
172      }
173   }
174
175   return _anOtherObject ;
176 }