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