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