Back to index

salome-geom  6.5.0
GEOMImpl_ConeDriver.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_ConeDriver.hxx>
00026 #include <GEOMImpl_ICone.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <BRepPrimAPI_MakeCone.hxx>
00031 #include <BRepPrimAPI_MakeCylinder.hxx>
00032 #include <BRep_Tool.hxx>
00033 
00034 #include <TopAbs.hxx>
00035 #include <TopoDS.hxx>
00036 #include <TopoDS_Shape.hxx>
00037 #include <TopoDS_Vertex.hxx>
00038 #include <TopExp.hxx>
00039 
00040 #include <Standard_TypeMismatch.hxx>
00041 #include <Standard_NullObject.hxx>
00042 #include <StdFail_NotDone.hxx>
00043 #include <Precision.hxx>
00044 #include <gp_Pnt.hxx>
00045 #include <gp.hxx>
00046 
00047 //=======================================================================
00048 //function : GetID
00049 //purpose  :
00050 //=======================================================================
00051 const Standard_GUID& GEOMImpl_ConeDriver::GetID()
00052 {
00053   static Standard_GUID aConeDriver("FF1BBB15-5D14-4df2-980B-3A668264EA16");
00054   return aConeDriver;
00055 }
00056 
00057 
00058 //=======================================================================
00059 //function : GEOMImpl_ConeDriver
00060 //purpose  :
00061 //=======================================================================
00062 GEOMImpl_ConeDriver::GEOMImpl_ConeDriver()
00063 {
00064 }
00065 
00066 //=======================================================================
00067 //function : Execute
00068 //purpose  :
00069 //=======================================================================
00070 Standard_Integer GEOMImpl_ConeDriver::Execute(TFunction_Logbook& log) const
00071 {
00072   if (Label().IsNull()) return 0;
00073   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00074 
00075   GEOMImpl_ICone aCI (aFunction);
00076   Standard_Integer aType = aFunction->GetType();
00077 
00078   gp_Pnt aP;
00079   gp_Vec aV;
00080 
00081   Standard_Real aR1 = aCI.GetR1();
00082   Standard_Real aR2 = aCI.GetR2();
00083 
00084   if (aType == CONE_R1_R2_H) {
00085     aP = gp::Origin();
00086     aV = gp::DZ();
00087 
00088   } else if (aType == CONE_PNT_VEC_R1_R2_H) {
00089     Handle(GEOM_Function) aRefPoint  = aCI.GetPoint();
00090     Handle(GEOM_Function) aRefVector = aCI.GetVector();
00091     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
00092     TopoDS_Shape aShapeVec = aRefVector->GetValue();
00093     if (aShapePnt.IsNull() || aShapeVec.IsNull()) {
00094       Standard_NullObject::Raise
00095         ("Cone creation aborted: point or vector is not defined");
00096     }
00097     if (aShapePnt.ShapeType() != TopAbs_VERTEX ||
00098         aShapeVec.ShapeType() != TopAbs_EDGE) {
00099       Standard_TypeMismatch::Raise
00100         ("Cone creation aborted: point or vector shapes has wrong type");
00101     }
00102 
00103     aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
00104 
00105     TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
00106     TopoDS_Vertex V1, V2;
00107     TopExp::Vertices(anE, V1, V2, Standard_True);
00108     if (V1.IsNull() || V2.IsNull()) {
00109       Standard_NullObject::Raise
00110         ("Cylinder creation aborted: vector is not defined");
00111     }
00112     aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
00113 
00114   } else {
00115     return 0;
00116   }
00117 
00118   if (aCI.GetH() < 0.0) aV.Reverse();
00119   gp_Ax2 anAxes (aP, aV);
00120 
00121   TopoDS_Shape aShape;
00122   // Cone does not work if same radius
00123   if (fabs(aR1 - aR2) <= Precision::Confusion()) {
00124     BRepPrimAPI_MakeCylinder MC (anAxes, (aR1 + aR2)/2.0, Abs(aCI.GetH()));
00125     MC.Build();
00126     if (!MC.IsDone()) {
00127       StdFail_NotDone::Raise("Cylinder can't be computed from the given parameters");
00128     }
00129     aShape = MC.Shape();
00130   } else {
00131     BRepPrimAPI_MakeCone MC (anAxes, aCI.GetR1(), aCI.GetR2(), Abs(aCI.GetH()));
00132     MC.Build();
00133     if (!MC.IsDone()) {
00134       StdFail_NotDone::Raise("Cylinder can't be computed from the given parameters");
00135     }
00136     aShape = MC.Shape();
00137   }
00138   if (aShape.IsNull()) return 0;
00139 
00140   log.SetTouched(Label());
00141 
00142   aFunction->SetValue(aShape);
00143   return 1;
00144 }
00145 
00146 
00147 //=======================================================================
00148 //function :  GEOMImpl_ConeDriver_Type_
00149 //purpose  :
00150 //=======================================================================
00151 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ConeDriver_Type_()
00152 {
00153 
00154   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00155   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00156   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00157   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00158   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00159   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00160 
00161 
00162   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00163   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ConeDriver",
00164                                                          sizeof(GEOMImpl_ConeDriver),
00165                                                          1,
00166                                                          (Standard_Address)_Ancestors,
00167                                                          (Standard_Address)NULL);
00168 
00169   return _aType;
00170 }
00171 
00172 //=======================================================================
00173 //function : DownCast
00174 //purpose  :
00175 //=======================================================================
00176 const Handle(GEOMImpl_ConeDriver) Handle(GEOMImpl_ConeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00177 {
00178   Handle(GEOMImpl_ConeDriver) _anOtherObject;
00179 
00180   if (!AnObject.IsNull()) {
00181      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ConeDriver))) {
00182        _anOtherObject = Handle(GEOMImpl_ConeDriver)((Handle(GEOMImpl_ConeDriver)&)AnObject);
00183      }
00184   }
00185 
00186   return _anOtherObject ;
00187 }