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