1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "GEOMImpl_VectorDriver.hxx"
25 #include "GEOMImpl_IVector.hxx"
26 #include "GEOMImpl_Types.hxx"
27 #include "GEOM_Function.hxx"
28 #include "GEOM_Object.hxx"
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRep_Tool.hxx>
32 #include <Geom_Curve.hxx>
33 #include <Precision.hxx>
34 #include <TCollection_AsciiString.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Vertex.hxx>
44 #include <Standard_ConstructionError.hxx>
48 Handle(GEOM_Object) GetOwner( const TDF_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();
57 return GEOM_Object::GetObject( label );
61 //=======================================================================
64 //=======================================================================
65 const Standard_GUID& GEOMImpl_VectorDriver::GetID()
67 static Standard_GUID aVectorDriver("FF1BBB04-5D14-4df2-980B-3A668264EA16");
72 //=======================================================================
73 //function : GEOMImpl_VectorDriver
75 //=======================================================================
76 GEOMImpl_VectorDriver::GEOMImpl_VectorDriver()
80 //=======================================================================
83 //=======================================================================
84 Standard_Integer GEOMImpl_VectorDriver::Execute(LOGBOOK& log) const
86 if (Label().IsNull()) return 0;
87 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
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;
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());
104 aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
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");
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");
123 aShape = BRepBuilderAPI_MakeEdge(V1, V2).Shape();
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");
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");
139 aParam = aFParam + (aLParam - aFParam) * aPI.GetParameter();
140 gp_Pnt aPoint1,aPoint2;
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();
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");
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();
164 if (aShape.IsNull()) return 0;
166 aFunction->SetValue(aShape);
168 #if OCC_VERSION_MAJOR < 7
169 log.SetTouched(Label());
171 log->SetTouched(Label());
177 //================================================================================
179 * \brief Returns a name of creation operation and names and values of creation parameters
181 //================================================================================
183 bool GEOMImpl_VectorDriver::
184 GetCreationInformation(std::string& theOperationName,
185 std::vector<GEOM_Param>& theParams)
187 if (Label().IsNull()) return 0;
188 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
190 GEOMImpl_IVector aCI( function );
191 Standard_Integer aType = function->GetType();
194 case VECTOR_DX_DY_DZ:
195 theOperationName = "VECTOR";
196 AddParam( theParams, "Dx", aCI.GetDX() );
197 AddParam( theParams, "Dy", aCI.GetDY() );
198 AddParam( theParams, "Dz", aCI.GetDZ() );
200 case VECTOR_TWO_PNT: {
201 Handle(GEOM_Object) obj = GetOwner( Label() );
202 if ( !obj.IsNull() && obj->GetType() == GEOM_EDGE )
203 theOperationName = "EDGE";
205 theOperationName = "VECTOR";
206 AddParam( theParams, "Point 1", aCI.GetPoint1() );
207 AddParam( theParams, "Point 2", aCI.GetPoint2() );
210 case VECTOR_TANGENT_CURVE_PAR:
211 theOperationName = "MakeTangentOnCurve";
212 AddParam( theParams, "Curve", aCI.GetCurve() );
213 AddParam( theParams, "Parameter", aCI.GetParameter() );
216 theOperationName = "CHANGE_ORIENTATION";
217 AddParam( theParams, "Vector", aCI.GetCurve() );
226 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_VectorDriver,GEOM_BaseDriver);