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