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