Back to index

salome-geom  6.5.0
GEOMImpl_CircleDriver.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_CircleDriver.hxx>
00026 #include <GEOMImpl_ICircle.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 <GC_MakeCircle.hxx>
00040 #include <Geom_Circle.hxx>
00041 
00042 #include <Standard_ConstructionError.hxx>
00043 #include <Precision.hxx>
00044 #include <gp_Pnt.hxx>
00045 #include <gp_Vec.hxx>
00046 #include <gp_Circ.hxx>
00047 
00048 //=======================================================================
00049 //function : GetID
00050 //purpose  :
00051 //======================================================================= 
00052 const Standard_GUID& GEOMImpl_CircleDriver::GetID()
00053 {
00054   static Standard_GUID aCircleDriver("FF1BBB32-5D14-4df2-980B-3A668264EA16");
00055   return aCircleDriver; 
00056 }
00057 
00058 
00059 //=======================================================================
00060 //function : GEOMImpl_CircleDriver
00061 //purpose  : 
00062 //=======================================================================
00063 GEOMImpl_CircleDriver::GEOMImpl_CircleDriver() 
00064 {
00065 }
00066 
00067 //=======================================================================
00068 //function : Execute
00069 //purpose  :
00070 //======================================================================= 
00071 Standard_Integer GEOMImpl_CircleDriver::Execute(TFunction_Logbook& log) const
00072 {
00073   if (Label().IsNull()) return 0;    
00074   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00075 
00076   GEOMImpl_ICircle aCI (aFunction);
00077   Standard_Integer aType = aFunction->GetType();
00078 
00079   TopoDS_Shape aShape;
00080 
00081   if (aType == CIRCLE_PNT_VEC_R) {
00082     // Center
00083     gp_Pnt aP = gp::Origin();
00084     Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
00085     if (!aRefPoint.IsNull()) {
00086       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
00087       if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
00088         Standard_ConstructionError::Raise
00089           ("Circle creation aborted: invalid center argument, must be a point");
00090       }
00091       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
00092     }
00093     // Normal
00094     gp_Vec aV = gp::DZ();
00095     Handle(GEOM_Function) aRefVector = aCI.GetVector();
00096     if (!aRefVector.IsNull()) {
00097       TopoDS_Shape aShapeVec = aRefVector->GetValue();
00098       if (aShapeVec.ShapeType() != TopAbs_EDGE) {
00099         Standard_ConstructionError::Raise
00100           ("Circle creation aborted: invalid vector argument, must be a vector or an edge");
00101       }
00102       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
00103       TopoDS_Vertex V1, V2;
00104       TopExp::Vertices(anE, V1, V2, Standard_True);
00105       if (!V1.IsNull() && !V2.IsNull()) {
00106         aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
00107         if (aV.Magnitude() < gp::Resolution()) {
00108           Standard_ConstructionError::Raise
00109             ("Circle creation aborted: vector of zero length is given");
00110         }
00111       }
00112     }
00113     // Axes
00114     gp_Ax2 anAxes (aP, aV);
00115     // Radius
00116     double anR = aCI.GetRadius();
00117     char aMsg[] = "Circle creation aborted: radius value less than 1e-07 is not acceptable";
00118     if (anR < Precision::Confusion())
00119       Standard_ConstructionError::Raise(aMsg);
00120     // Circle
00121     gp_Circ aCirc (anAxes, anR);
00122     aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
00123   }
00124   else if (aType == CIRCLE_CENTER_TWO_PNT) {
00125     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
00126     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
00127     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
00128     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
00129     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
00130     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
00131     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
00132         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
00133         aShapePnt3.ShapeType() == TopAbs_VERTEX)
00134     {
00135       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
00136       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
00137       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
00138 
00139       if (aP1.Distance(aP2) < gp::Resolution() ||
00140           aP1.Distance(aP3) < gp::Resolution() ||
00141           aP2.Distance(aP3) < gp::Resolution())
00142         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
00143 
00144       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
00145         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
00146 
00147       double x, y, z, x1, y1, z1, x2, y2, z2, dx, dy, dz, dx2, dy2, dz2, dx3, dy3, dz3, aRadius;
00148       //Calculations for Radius
00149       x = aP1.X(); y = aP1.Y(); z = aP1.Z();
00150       x1 = aP2.X(); y1 = aP2.Y(); z1 = aP2.Z();
00151       dx = x1 - x;
00152       dy = y1 - y;
00153       dz = z1 - z;
00154       aRadius = sqrt(dx*dx + dy*dy + dz*dz);
00155       //Calculations for Plane Vector
00156       x2 = aP3.X(); y2 = aP3.Y(); z2 = aP3.Z();
00157       dx2 = x2 - x; dy2 = y2 - y; dz2 = z2 - z;
00158       dx3 = ((dy*dz2) - (dy2*dz))/100;
00159       dy3 = ((dx2*dz) - (dx*dz2))/100;
00160       dz3 = ((dx*dy2) - (dx2*dy))/100;
00161       //Make Plane Vector
00162       gp_Dir aDir ( dx3, dy3, dz3 );
00163       //Make Circle
00164       gp_Ax2 anAxes (aP1, aDir);
00165       gp_Circ aCirc (anAxes, aRadius);
00166       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();  
00167     }
00168   }
00169   else if (aType == CIRCLE_THREE_PNT) {
00170     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
00171     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
00172     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
00173     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
00174     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
00175     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
00176     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
00177         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
00178         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
00179       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
00180       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
00181       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
00182       if (aP1.Distance(aP2) < gp::Resolution() ||
00183           aP1.Distance(aP3) < gp::Resolution() ||
00184           aP2.Distance(aP3) < gp::Resolution())
00185         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
00186       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
00187         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
00188       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP1, aP2, aP3).Value();
00189       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
00190     }  
00191   }
00192   else {
00193   }
00194 
00195   if (aShape.IsNull()) return 0;
00196 
00197   aFunction->SetValue(aShape);
00198 
00199   log.SetTouched(Label()); 
00200 
00201   return 1;    
00202 }
00203 
00204 
00205 //=======================================================================
00206 //function :  GEOMImpl_CircleDriver_Type_
00207 //purpose  :
00208 //======================================================================= 
00209 Standard_EXPORT Handle_Standard_Type& GEOMImpl_CircleDriver_Type_()
00210 {
00211 
00212   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00213   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00214   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00215   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
00216   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00217   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00218  
00219 
00220   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00221   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_CircleDriver",
00222                                                          sizeof(GEOMImpl_CircleDriver),
00223                                                          1,
00224                                                          (Standard_Address)_Ancestors,
00225                                                          (Standard_Address)NULL);
00226 
00227   return _aType;
00228 }
00229 
00230 //=======================================================================
00231 //function : DownCast
00232 //purpose  :
00233 //======================================================================= 
00234 const Handle(GEOMImpl_CircleDriver) Handle(GEOMImpl_CircleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00235 {
00236   Handle(GEOMImpl_CircleDriver) _anOtherObject;
00237 
00238   if (!AnObject.IsNull()) {
00239      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_CircleDriver))) {
00240        _anOtherObject = Handle(GEOMImpl_CircleDriver)((Handle(GEOMImpl_CircleDriver)&)AnObject);
00241      }
00242   }
00243 
00244   return _anOtherObject ;
00245 }