Salome HOME
0023505: Sigsegv with fuse on cylinder and cone
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SphereDriver.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_SphereDriver.hxx>
26 #include <GEOMImpl_ISphere.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepPrimAPI_MakeSphere.hxx>
31 #include <BRep_Tool.hxx>
32 #include <gp_Pnt.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopAbs.hxx>
37
38 //=======================================================================
39 //function : GetID
40 //purpose  :
41 //======================================================================= 
42 const Standard_GUID& GEOMImpl_SphereDriver::GetID()
43 {
44   static Standard_GUID aSphereDriver("FF1BBB16-5D14-4df2-980B-3A668264EA16");
45   return aSphereDriver; 
46 }
47
48
49 //=======================================================================
50 //function : GEOMImpl_SphereDriver
51 //purpose  : 
52 //=======================================================================
53 GEOMImpl_SphereDriver::GEOMImpl_SphereDriver() 
54 {
55 }
56
57 //=======================================================================
58 //function : Execute
59 //purpose  :
60 //======================================================================= 
61 Standard_Integer GEOMImpl_SphereDriver::Execute(LOGBOOK& log) const
62 {
63   if (Label().IsNull()) return 0;    
64   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
65
66   GEOMImpl_ISphere aCI (aFunction);
67   Standard_Integer aType = aFunction->GetType();
68
69   TopoDS_Shape aShape;
70
71   char aMsg[] = "Sphere creation aborted: radius value less than 1e-07 is not acceptable";
72
73   if (aType == SPHERE_R) {
74     double anR = aCI.GetR();
75     if (anR < Precision::Confusion())
76       Standard_ConstructionError::Raise(aMsg);
77
78     aShape = BRepPrimAPI_MakeSphere(anR).Shape();
79   }
80   else if (aType == SPHERE_PNT_R) {
81     double anR = aCI.GetR();
82     if (anR < Precision::Confusion())
83       Standard_ConstructionError::Raise(aMsg);
84
85     Handle(GEOM_Function) aRefPoint  = aCI.GetPoint();
86     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
87     if (aShapePnt.ShapeType() != TopAbs_VERTEX)
88       Standard_ConstructionError::Raise("Invalid shape given for sphere center: it must be a point");
89     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
90
91     aShape = BRepPrimAPI_MakeSphere(aP, anR).Shape();
92   }
93   else {
94   }
95
96   if (aShape.IsNull()) return 0;
97
98   aFunction->SetValue(aShape);
99
100 #if OCC_VERSION_MAJOR < 7
101   log.SetTouched(Label());
102 #else
103   log->SetTouched(Label());
104 #endif
105
106   return 1;    
107 }
108
109 //================================================================================
110 /*!
111  * \brief Returns a name of creation operation and names and values of creation parameters
112  */
113 //================================================================================
114
115 bool GEOMImpl_SphereDriver::
116 GetCreationInformation(std::string&             theOperationName,
117                        std::vector<GEOM_Param>& theParams)
118 {
119   if (Label().IsNull()) return 0;
120   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
121
122   GEOMImpl_ISphere aCI( function );
123   Standard_Integer aType = function->GetType();
124
125   theOperationName = "SPHERE";
126
127   switch ( aType ) {
128   case SPHERE_R:
129     AddParam( theParams, "Radius", aCI.GetR() );
130     break;
131   case SPHERE_PNT_R:
132     AddParam( theParams, "Center", aCI.GetPoint() );
133     AddParam( theParams, "Radius", aCI.GetR() );
134     break;
135   default:
136     return false;
137   }
138   
139   return true;
140 }
141
142 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_SphereDriver,GEOM_BaseDriver);