Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / GEOMImpl / GEOMImpl_VectorDriver.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_VectorDriver.hxx>
25 #include <GEOMImpl_IVector.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <BRep_Tool.hxx>
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31
32 #include <TopAbs.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Vertex.hxx>
36
37 #include <gp_Pnt.hxx>
38 #include <Precision.hxx>
39 #include <Geom_Curve.hxx>
40 #include <gp_Vec.hxx>
41 #include <TCollection_AsciiString.hxx>
42 #include <Standard_ConstructionError.hxx>
43
44 //=======================================================================
45 //function : GetID
46 //purpose  :
47 //=======================================================================
48 const Standard_GUID& GEOMImpl_VectorDriver::GetID()
49 {
50   static Standard_GUID aVectorDriver("FF1BBB04-5D14-4df2-980B-3A668264EA16");
51   return aVectorDriver;
52 }
53
54
55 //=======================================================================
56 //function : GEOMImpl_VectorDriver
57 //purpose  :
58 //=======================================================================
59 GEOMImpl_VectorDriver::GEOMImpl_VectorDriver()
60 {
61 }
62
63 //=======================================================================
64 //function : Execute
65 //purpose  :
66 //=======================================================================
67 Standard_Integer GEOMImpl_VectorDriver::Execute(TFunction_Logbook& log) const
68 {
69   if (Label().IsNull())  return 0;
70   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
71
72   GEOMImpl_IVector aPI (aFunction);
73   Standard_Integer aType = aFunction->GetType();
74   if (aType != VECTOR_DX_DY_DZ && aType != VECTOR_TWO_PNT && aType != VECTOR_TANGENT_CURVE_PAR) return 0;
75
76   TopoDS_Shape aShape;
77
78   if (aType == VECTOR_DX_DY_DZ) {
79     gp_Pnt P1 = gp::Origin();
80     gp_Pnt P2 (aPI.GetDX(), aPI.GetDY(), aPI.GetDZ());
81     if (P1.Distance(P2) < Precision::Confusion()) {
82       TCollection_AsciiString aMsg ("Can not build vector with length, less than ");
83       aMsg += TCollection_AsciiString(Precision::Confusion());
84       Standard_ConstructionError::Raise(aMsg.ToCString());
85     }
86     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
87   } else if (aType == VECTOR_TWO_PNT) {
88     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
89     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
90     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
91     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
92     if (aShape1.ShapeType() != TopAbs_VERTEX ||
93         aShape2.ShapeType() != TopAbs_VERTEX) return 0;
94     if (aShape1.IsSame(aShape2)) {
95       Standard_ConstructionError::Raise("The end points must be different");
96     }
97     TopoDS_Vertex V1 = TopoDS::Vertex(aShape1);
98     TopoDS_Vertex V2 = TopoDS::Vertex(aShape2);
99     gp_Pnt P1 = BRep_Tool::Pnt(V1);
100     gp_Pnt P2 = BRep_Tool::Pnt(V2);
101     if (P1.Distance(P2) < Precision::Confusion()) {
102       Standard_ConstructionError::Raise("The end points are too close");
103     }
104     aShape = BRepBuilderAPI_MakeEdge(V1, V2).Shape();
105   } 
106   else if(aType == VECTOR_TANGENT_CURVE_PAR) {
107     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
108     TopoDS_Shape aRefShape = aRefCurve->GetValue();
109     if (aRefShape.ShapeType() != TopAbs_EDGE) {
110       Standard_TypeMismatch::Raise
111         ("Tangent On Curve creation aborted : curve shape is not an edge");
112     }
113     Standard_Real aFParam =0., aLParam =0., aParam =0.;
114     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFParam, aLParam);
115     if(aCurve.IsNull()) {
116       Standard_TypeMismatch::Raise
117         ("Tangent On Curve creation aborted : curve is null");
118     }
119
120     aParam = aFParam + (aLParam - aFParam) * aPI.GetParameter();
121     gp_Pnt aPoint1,aPoint2;
122     gp_Vec aVec;
123     aCurve->D1(aParam,aPoint1,aVec);
124     if(aVec.Magnitude() < gp::Resolution())
125       Standard_TypeMismatch::Raise
126         ("Tangent On Curve creation aborted : invalid value of tangent");
127     aPoint2.SetXYZ(aPoint1.XYZ() + aVec.XYZ());
128     BRepBuilderAPI_MakeEdge aBuilder(aPoint1,aPoint2);
129     if(aBuilder.IsDone())
130       aShape = aBuilder.Shape();
131   }
132
133   if (aShape.IsNull()) return 0;
134
135   aFunction->SetValue(aShape);
136
137   log.SetTouched(Label());
138
139   return 1;
140 }
141
142
143 //=======================================================================
144 //function :  GEOMImpl_VectorDriver_Type_
145 //purpose  :
146 //=======================================================================
147 Standard_EXPORT Handle_Standard_Type& GEOMImpl_VectorDriver_Type_()
148 {
149
150   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
151   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
152   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
153   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
154   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
155   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
156
157
158   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
159   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_VectorDriver",
160                                                          sizeof(GEOMImpl_VectorDriver),
161                                                          1,
162                                                          (Standard_Address)_Ancestors,
163                                                          (Standard_Address)NULL);
164
165   return _aType;
166 }
167
168 //=======================================================================
169 //function : DownCast
170 //purpose  :
171 //=======================================================================
172 const Handle(GEOMImpl_VectorDriver) Handle(GEOMImpl_VectorDriver)::DownCast
173        (const Handle(Standard_Transient)& AnObject)
174 {
175   Handle(GEOMImpl_VectorDriver) _anOtherObject;
176
177   if (!AnObject.IsNull()) {
178      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_VectorDriver))) {
179        _anOtherObject = Handle(GEOMImpl_VectorDriver)((Handle(GEOMImpl_VectorDriver)&)AnObject);
180      }
181   }
182
183   return _anOtherObject ;
184 }