Salome HOME
Revert "Synchronize adm files"
[modules/geom.git] / src / GEOMImpl / GEOMImpl_VectorDriver.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 "GEOMImpl_VectorDriver.hxx"
24
25 #include "GEOMImpl_IVector.hxx"
26 #include "GEOMImpl_Types.hxx"
27 #include "GEOM_Function.hxx"
28 #include "GEOM_Object.hxx"
29
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRep_Tool.hxx>
32 #include <Geom_Curve.hxx>
33 #include <Precision.hxx>
34 #include <TCollection_AsciiString.hxx>
35 #include <TopAbs.hxx>
36 #include <TopExp.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <gp_Pnt.hxx>
42 #include <gp_Vec.hxx>
43
44 #include <Standard_ConstructionError.hxx>
45
46 //=======================================================================
47 //function : GetID
48 //purpose  :
49 //=======================================================================
50 const Standard_GUID& GEOMImpl_VectorDriver::GetID()
51 {
52   static Standard_GUID aVectorDriver("FF1BBB04-5D14-4df2-980B-3A668264EA16");
53   return aVectorDriver;
54 }
55
56
57 //=======================================================================
58 //function : GEOMImpl_VectorDriver
59 //purpose  :
60 //=======================================================================
61 GEOMImpl_VectorDriver::GEOMImpl_VectorDriver()
62 {
63 }
64
65 //=======================================================================
66 //function : Execute
67 //purpose  :
68 //=======================================================================
69 Standard_Integer GEOMImpl_VectorDriver::Execute(TFunction_Logbook& log) const
70 {
71   if (Label().IsNull())  return 0;
72   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
73
74   GEOMImpl_IVector aPI (aFunction);
75   Standard_Integer aType = aFunction->GetType();
76   if (aType != VECTOR_DX_DY_DZ && aType != VECTOR_TWO_PNT &&
77       aType != VECTOR_TANGENT_CURVE_PAR && aType != VECTOR_REVERSE) return 0;
78
79   TopoDS_Shape aShape;
80
81   if (aType == VECTOR_DX_DY_DZ) {
82     gp_Pnt P1 = gp::Origin();
83     gp_Pnt P2 (aPI.GetDX(), aPI.GetDY(), aPI.GetDZ());
84     if (P1.Distance(P2) < Precision::Confusion()) {
85       TCollection_AsciiString aMsg ("Can not build vector with length, less than ");
86       aMsg += TCollection_AsciiString(Precision::Confusion());
87       Standard_ConstructionError::Raise(aMsg.ToCString());
88     }
89     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
90   }
91   else if (aType == VECTOR_TWO_PNT) {
92     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
93     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
94     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
95     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
96     if (aShape1.ShapeType() != TopAbs_VERTEX ||
97         aShape2.ShapeType() != TopAbs_VERTEX) return 0;
98     if (aShape1.IsSame(aShape2)) {
99       Standard_ConstructionError::Raise("The end points must be different");
100     }
101     TopoDS_Vertex V1 = TopoDS::Vertex(aShape1);
102     TopoDS_Vertex V2 = TopoDS::Vertex(aShape2);
103     gp_Pnt P1 = BRep_Tool::Pnt(V1);
104     gp_Pnt P2 = BRep_Tool::Pnt(V2);
105     if (P1.Distance(P2) < Precision::Confusion()) {
106       Standard_ConstructionError::Raise("The end points are too close");
107     }
108     aShape = BRepBuilderAPI_MakeEdge(V1, V2).Shape();
109   } 
110   else if (aType == VECTOR_TANGENT_CURVE_PAR) {
111     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
112     TopoDS_Shape aRefShape = aRefCurve->GetValue();
113     if (aRefShape.ShapeType() != TopAbs_EDGE) {
114       Standard_TypeMismatch::Raise
115         ("Tangent On Curve creation aborted : curve shape is not an edge");
116     }
117     Standard_Real aFParam =0., aLParam =0., aParam =0.;
118     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFParam, aLParam);
119     if(aCurve.IsNull()) {
120       Standard_TypeMismatch::Raise
121         ("Tangent On Curve creation aborted : curve is null");
122     }
123
124     aParam = aFParam + (aLParam - aFParam) * aPI.GetParameter();
125     gp_Pnt aPoint1,aPoint2;
126     gp_Vec aVec;
127     aCurve->D1(aParam,aPoint1,aVec);
128     if(aVec.Magnitude() < gp::Resolution())
129       Standard_TypeMismatch::Raise
130         ("Tangent On Curve creation aborted : invalid value of tangent");
131     aPoint2.SetXYZ(aPoint1.XYZ() + aVec.XYZ());
132     BRepBuilderAPI_MakeEdge aBuilder(aPoint1,aPoint2);
133     if(aBuilder.IsDone())
134       aShape = aBuilder.Shape();
135   }
136   else if (aType == VECTOR_REVERSE) {
137     Handle(GEOM_Function) aRefVec = aPI.GetCurve();
138     TopoDS_Shape aRefShape = aRefVec->GetValue();
139     if (aRefShape.ShapeType() != TopAbs_EDGE) {
140       Standard_TypeMismatch::Raise
141         ("Reversed vector creation aborted : vector shape is not an edge");
142     }
143     TopoDS_Edge anE = TopoDS::Edge(aRefShape);
144     TopoDS_Vertex V1, V2;
145     TopExp::Vertices(anE, V1, V2, Standard_True);
146     aShape = BRepBuilderAPI_MakeEdge(V2, V1).Shape();
147   }
148
149   if (aShape.IsNull()) return 0;
150
151   aFunction->SetValue(aShape);
152
153   log.SetTouched(Label());
154
155   return 1;
156 }
157
158 //================================================================================
159 /*!
160  * \brief Returns a name of creation operation and names and values of creation parameters
161  */
162 //================================================================================
163
164 bool GEOMImpl_VectorDriver::
165 GetCreationInformation(std::string&             theOperationName,
166                        std::vector<GEOM_Param>& theParams)
167 {
168   if (Label().IsNull()) return 0;
169   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
170
171   GEOMImpl_IVector aCI( function );
172   Standard_Integer aType = function->GetType();
173
174   switch ( aType ) {
175   case VECTOR_DX_DY_DZ:
176     theOperationName = "VECTOR";
177     AddParam( theParams, "Dx", aCI.GetDX() );
178     AddParam( theParams, "Dy", aCI.GetDY() );
179     AddParam( theParams, "Dz", aCI.GetDZ() );
180     break;
181   case VECTOR_TWO_PNT: {
182     TDF_Label label = Label();
183     Handle(GEOM_Object) obj = GEOM_Object::GetObject( label );
184     if ( !obj.IsNull() && obj->GetType() == GEOM_EDGE )
185       theOperationName = "EDGE";
186     else
187       theOperationName = "VECTOR";
188     AddParam( theParams, "Point 1", aCI.GetPoint1() );
189     AddParam( theParams, "Point 2", aCI.GetPoint2() );
190     break;
191   }
192   case VECTOR_TANGENT_CURVE_PAR:
193     theOperationName = "MakeTangentOnCurve";
194     AddParam( theParams, "Curve", aCI.GetCurve() );
195     AddParam( theParams, "Parameter", aCI.GetParameter() );
196     break;
197   case VECTOR_REVERSE:
198     theOperationName = "CHANGE_ORIENTATION";
199     AddParam( theParams, "Vector", aCI.GetCurve() );
200     break;
201   default:
202     return false;
203   }
204
205   return true;
206 }
207
208 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_VectorDriver,GEOM_BaseDriver);
209 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_VectorDriver,GEOM_BaseDriver);