Back to index

salome-geom  6.5.0
GEOMImpl_EllipseDriver.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_EllipseDriver.hxx>
00026 #include <GEOMImpl_IEllipse.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <BRepBuilderAPI_MakeEdge.hxx>
00031 #include <BRep_Tool.hxx>
00032 #include <TopoDS.hxx>
00033 #include <TopoDS_Shape.hxx>
00034 #include <TopoDS_Edge.hxx>
00035 #include <TopoDS_Vertex.hxx>
00036 #include <TopAbs.hxx>
00037 #include <TopExp.hxx>
00038 
00039 #include <gp_Pnt.hxx>
00040 #include <gp_Elips.hxx>
00041 
00042 //=======================================================================
00043 //function : GetID
00044 //purpose  :
00045 //======================================================================= 
00046 const Standard_GUID& GEOMImpl_EllipseDriver::GetID()
00047 {
00048   static Standard_GUID aEllipseDriver("FF1BBB34-5D14-4df2-980B-3A668264EA16");
00049   return aEllipseDriver; 
00050 }
00051 
00052 
00053 //=======================================================================
00054 //function : GEOMImpl_EllipseDriver
00055 //purpose  : 
00056 //=======================================================================
00057 GEOMImpl_EllipseDriver::GEOMImpl_EllipseDriver() 
00058 {
00059 }
00060 
00061 //=======================================================================
00062 //function : Execute
00063 //purpose  :
00064 //======================================================================= 
00065 Standard_Integer GEOMImpl_EllipseDriver::Execute(TFunction_Logbook& log) const
00066 {
00067   if (Label().IsNull()) return 0;    
00068   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00069 
00070   GEOMImpl_IEllipse aCI (aFunction);
00071   Standard_Integer aType = aFunction->GetType();
00072 
00073   TopoDS_Shape aShape;
00074 
00075   if (aType == ELLIPSE_PNT_VEC_RR) {
00076     // Center
00077     gp_Pnt aP = gp::Origin();
00078     Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
00079     if (!aRefPoint.IsNull()) {
00080       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
00081       if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
00082         Standard_ConstructionError::Raise
00083           ("Ellipse creation aborted: invalid center argument, must be a point");
00084       }
00085       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
00086     }
00087     // Normal
00088     gp_Vec aV = gp::DZ();
00089     Handle(GEOM_Function) aRefVector = aCI.GetVector();
00090     if (!aRefVector.IsNull()) {
00091       TopoDS_Shape aShapeVec = aRefVector->GetValue();
00092       if (aShapeVec.ShapeType() != TopAbs_EDGE) {
00093         Standard_ConstructionError::Raise
00094           ("Ellipse creation aborted: invalid normal vector argument, must be a vector or an edge");
00095       }
00096       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
00097       TopoDS_Vertex V1, V2;
00098       TopExp::Vertices(anE, V1, V2, Standard_True);
00099       if (!V1.IsNull() && !V2.IsNull()) {
00100         aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
00101         if (aV.Magnitude() < gp::Resolution()) {
00102           Standard_ConstructionError::Raise
00103             ("Ellipse creation aborted: normal vector of zero length is given");
00104         }
00105       }
00106     }
00107 
00108     // Axes
00109     gp_Ax2 anAxes (aP, aV);
00110 
00111     // Main Axis vector
00112     Handle(GEOM_Function) aRefVectorMaj = aCI.GetVectorMajor();
00113     if (!aRefVectorMaj.IsNull()) {
00114       TopoDS_Shape aShapeVec = aRefVectorMaj->GetValue();
00115       if (aShapeVec.ShapeType() != TopAbs_EDGE) {
00116         Standard_ConstructionError::Raise
00117           ("Ellipse creation aborted: invalid major axis vector argument, must be a vector or an edge");
00118       }
00119       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
00120       TopoDS_Vertex V1, V2;
00121       TopExp::Vertices(anE, V1, V2, Standard_True);
00122       if (!V1.IsNull() && !V2.IsNull()) {
00123         gp_Vec aVM (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
00124         if (aVM.Magnitude() < gp::Resolution()) {
00125           Standard_ConstructionError::Raise
00126             ("Ellipse creation aborted: major axis vector of zero length is given");
00127         }
00128         if (aV.IsParallel(aVM, Precision::Angular())) {
00129           Standard_ConstructionError::Raise
00130             ("Ellipse creation aborted: normal and major axis vectors are parallel");
00131         }
00132         // Axes defined with main axis vector
00133         anAxes  = gp_Ax2 (aP, aV, aVM);
00134       }
00135     }
00136     // Radiuses
00137     double radiusMaj = aCI.GetRMajor();
00138     double radiusMin = aCI.GetRMinor();
00139     if ( radiusMaj < radiusMin )
00140       Standard_ConstructionError::Raise
00141         ("Ellipse creation aborted: a major radius is less that a minor one");
00142     if ( radiusMin < 0.0 )
00143       Standard_ConstructionError::Raise
00144         ("Ellipse creation aborted: raduis must be positive");
00145 
00146     // Ellipse
00147     gp_Elips anEll (anAxes, radiusMaj, radiusMin);
00148     aShape = BRepBuilderAPI_MakeEdge(anEll).Edge();
00149   }
00150   else {
00151   }
00152 
00153   if (aShape.IsNull()) return 0;
00154 
00155   aFunction->SetValue(aShape);
00156 
00157   log.SetTouched(Label()); 
00158 
00159   return 1;    
00160 }
00161 
00162 
00163 //=======================================================================
00164 //function :  GEOMImpl_EllipseDriver_Type_
00165 //purpose  :
00166 //======================================================================= 
00167 Standard_EXPORT Handle_Standard_Type& GEOMImpl_EllipseDriver_Type_()
00168 {
00169 
00170   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00171   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00172   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00173   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
00174   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00175   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00176  
00177 
00178   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00179   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_EllipseDriver",
00180                                                          sizeof(GEOMImpl_EllipseDriver),
00181                                                          1,
00182                                                          (Standard_Address)_Ancestors,
00183                                                          (Standard_Address)NULL);
00184 
00185   return _aType;
00186 }
00187 
00188 //=======================================================================
00189 //function : DownCast
00190 //purpose  :
00191 //======================================================================= 
00192 const Handle(GEOMImpl_EllipseDriver) Handle(GEOMImpl_EllipseDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00193 {
00194   Handle(GEOMImpl_EllipseDriver) _anOtherObject;
00195 
00196   if (!AnObject.IsNull()) {
00197      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_EllipseDriver))) {
00198        _anOtherObject = Handle(GEOMImpl_EllipseDriver)((Handle(GEOMImpl_EllipseDriver)&)AnObject);
00199      }
00200   }
00201 
00202   return _anOtherObject ;
00203 }