Back to index

salome-geom  6.5.0
GEOMImpl_VectorDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include <Standard_Stream.hxx>
00024 
00025 #include <GEOMImpl_VectorDriver.hxx>
00026 #include <GEOMImpl_IVector.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <BRep_Tool.hxx>
00031 #include <BRepBuilderAPI_MakeEdge.hxx>
00032 
00033 #include <TopAbs.hxx>
00034 #include <TopExp.hxx>
00035 #include <TopoDS.hxx>
00036 #include <TopoDS_Edge.hxx>
00037 #include <TopoDS_Shape.hxx>
00038 #include <TopoDS_Vertex.hxx>
00039 
00040 #include <gp_Pnt.hxx>
00041 #include <Precision.hxx>
00042 #include <Geom_Curve.hxx>
00043 #include <gp_Vec.hxx>
00044 #include <TCollection_AsciiString.hxx>
00045 #include <Standard_ConstructionError.hxx>
00046 
00047 //=======================================================================
00048 //function : GetID
00049 //purpose  :
00050 //=======================================================================
00051 const Standard_GUID& GEOMImpl_VectorDriver::GetID()
00052 {
00053   static Standard_GUID aVectorDriver("FF1BBB04-5D14-4df2-980B-3A668264EA16");
00054   return aVectorDriver;
00055 }
00056 
00057 
00058 //=======================================================================
00059 //function : GEOMImpl_VectorDriver
00060 //purpose  :
00061 //=======================================================================
00062 GEOMImpl_VectorDriver::GEOMImpl_VectorDriver()
00063 {
00064 }
00065 
00066 //=======================================================================
00067 //function : Execute
00068 //purpose  :
00069 //=======================================================================
00070 Standard_Integer GEOMImpl_VectorDriver::Execute(TFunction_Logbook& log) const
00071 {
00072   if (Label().IsNull())  return 0;
00073   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00074 
00075   GEOMImpl_IVector aPI (aFunction);
00076   Standard_Integer aType = aFunction->GetType();
00077   if (aType != VECTOR_DX_DY_DZ && aType != VECTOR_TWO_PNT &&
00078       aType != VECTOR_TANGENT_CURVE_PAR && aType != VECTOR_REVERSE) return 0;
00079 
00080   TopoDS_Shape aShape;
00081 
00082   if (aType == VECTOR_DX_DY_DZ) {
00083     gp_Pnt P1 = gp::Origin();
00084     gp_Pnt P2 (aPI.GetDX(), aPI.GetDY(), aPI.GetDZ());
00085     if (P1.Distance(P2) < Precision::Confusion()) {
00086       TCollection_AsciiString aMsg ("Can not build vector with length, less than ");
00087       aMsg += TCollection_AsciiString(Precision::Confusion());
00088       Standard_ConstructionError::Raise(aMsg.ToCString());
00089     }
00090     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
00091   }
00092   else if (aType == VECTOR_TWO_PNT) {
00093     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
00094     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
00095     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
00096     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
00097     if (aShape1.ShapeType() != TopAbs_VERTEX ||
00098         aShape2.ShapeType() != TopAbs_VERTEX) return 0;
00099     if (aShape1.IsSame(aShape2)) {
00100       Standard_ConstructionError::Raise("The end points must be different");
00101     }
00102     TopoDS_Vertex V1 = TopoDS::Vertex(aShape1);
00103     TopoDS_Vertex V2 = TopoDS::Vertex(aShape2);
00104     gp_Pnt P1 = BRep_Tool::Pnt(V1);
00105     gp_Pnt P2 = BRep_Tool::Pnt(V2);
00106     if (P1.Distance(P2) < Precision::Confusion()) {
00107       Standard_ConstructionError::Raise("The end points are too close");
00108     }
00109     aShape = BRepBuilderAPI_MakeEdge(V1, V2).Shape();
00110   } 
00111   else if (aType == VECTOR_TANGENT_CURVE_PAR) {
00112     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
00113     TopoDS_Shape aRefShape = aRefCurve->GetValue();
00114     if (aRefShape.ShapeType() != TopAbs_EDGE) {
00115       Standard_TypeMismatch::Raise
00116         ("Tangent On Curve creation aborted : curve shape is not an edge");
00117     }
00118     Standard_Real aFParam =0., aLParam =0., aParam =0.;
00119     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFParam, aLParam);
00120     if(aCurve.IsNull()) {
00121       Standard_TypeMismatch::Raise
00122         ("Tangent On Curve creation aborted : curve is null");
00123     }
00124 
00125     aParam = aFParam + (aLParam - aFParam) * aPI.GetParameter();
00126     gp_Pnt aPoint1,aPoint2;
00127     gp_Vec aVec;
00128     aCurve->D1(aParam,aPoint1,aVec);
00129     if(aVec.Magnitude() < gp::Resolution())
00130       Standard_TypeMismatch::Raise
00131         ("Tangent On Curve creation aborted : invalid value of tangent");
00132     aPoint2.SetXYZ(aPoint1.XYZ() + aVec.XYZ());
00133     BRepBuilderAPI_MakeEdge aBuilder(aPoint1,aPoint2);
00134     if(aBuilder.IsDone())
00135       aShape = aBuilder.Shape();
00136   }
00137   else if (aType == VECTOR_REVERSE) {
00138     Handle(GEOM_Function) aRefVec = aPI.GetCurve();
00139     TopoDS_Shape aRefShape = aRefVec->GetValue();
00140     if (aRefShape.ShapeType() != TopAbs_EDGE) {
00141       Standard_TypeMismatch::Raise
00142         ("Reversed vector creation aborted : vector shape is not an edge");
00143     }
00144     TopoDS_Edge anE = TopoDS::Edge(aRefShape);
00145     TopoDS_Vertex V1, V2;
00146     TopExp::Vertices(anE, V1, V2, Standard_True);
00147     aShape = BRepBuilderAPI_MakeEdge(V2, V1).Shape();
00148   }
00149 
00150   if (aShape.IsNull()) return 0;
00151 
00152   aFunction->SetValue(aShape);
00153 
00154   log.SetTouched(Label());
00155 
00156   return 1;
00157 }
00158 
00159 
00160 //=======================================================================
00161 //function :  GEOMImpl_VectorDriver_Type_
00162 //purpose  :
00163 //=======================================================================
00164 Standard_EXPORT Handle_Standard_Type& GEOMImpl_VectorDriver_Type_()
00165 {
00166 
00167   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00168   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00169   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00170   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00171   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00172   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00173 
00174 
00175   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00176   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_VectorDriver",
00177                                                          sizeof(GEOMImpl_VectorDriver),
00178                                                          1,
00179                                                          (Standard_Address)_Ancestors,
00180                                                          (Standard_Address)NULL);
00181 
00182   return _aType;
00183 }
00184 
00185 //=======================================================================
00186 //function : DownCast
00187 //purpose  :
00188 //=======================================================================
00189 const Handle(GEOMImpl_VectorDriver) Handle(GEOMImpl_VectorDriver)::DownCast
00190        (const Handle(Standard_Transient)& AnObject)
00191 {
00192   Handle(GEOMImpl_VectorDriver) _anOtherObject;
00193 
00194   if (!AnObject.IsNull()) {
00195      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_VectorDriver))) {
00196        _anOtherObject = Handle(GEOMImpl_VectorDriver)((Handle(GEOMImpl_VectorDriver)&)AnObject);
00197      }
00198   }
00199 
00200   return _anOtherObject;
00201 }