Salome HOME
Copyright update 2021
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Sphere.cpp
1 // Copyright (C) 2017-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        GeomAlgoAPI_Sphere.h
21 // Created:     16 Mar 2017
22 // Author:      Clarisse Genrault (CEA)
23
24 #include <GeomAlgoAPI_Sphere.h>
25
26 #include <BRepPrimAPI_MakeSphere.hxx>
27
28 //=================================================================================================
29 GeomAlgoAPI_Sphere::GeomAlgoAPI_Sphere(std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
30                                        const double theRadius)
31 {
32   myCenterPoint = theCenterPoint;
33   myRadius = theRadius;
34 }
35
36 //=================================================================================================
37 bool GeomAlgoAPI_Sphere::check()
38 {
39   if (!myCenterPoint) {
40     myError = "Sphere builder :: center is not valid.";
41     return false;
42   }
43   if (myRadius < Precision::Confusion()) {
44     myError = "Sphere builder :: radius is negative or null.";
45     return false;
46   }
47   return true;
48 }
49
50 //=================================================================================================
51 void GeomAlgoAPI_Sphere::build()
52 {
53   myCreatedFaces.clear();
54
55   const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
56
57   // Construct the sphere
58   BRepPrimAPI_MakeSphere *aSphereMaker = new BRepPrimAPI_MakeSphere(aCenterPoint, myRadius);
59
60   aSphereMaker->Build();
61
62   if (!aSphereMaker->IsDone()) {
63     return;
64   }
65
66   TopoDS_Shape aResult = aSphereMaker->Shape();
67   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
68   aShape->setImpl(new TopoDS_Shape(aResult));
69   setShape(aShape);
70
71   // Test on the shapes
72   if (!aShape.get() || aShape->isNull()) {
73     myError = "Sphere builder :: resulting shape is null.";
74     return;
75   }
76
77   setImpl(aSphereMaker);
78
79   setDone(true);
80 }