Salome HOME
[Code coverage GeomAlgoAPI]: Remove default constructors of algorithms
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Cylinder.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAlgoAPI_Cylinder.h>
22
23 #include <gp_Ax2.hxx>
24 #include <gp_Dir.hxx>
25 #include <TopoDS_Edge.hxx>
26 #include <TopoDS_Shape.hxx>
27
28 #include <BRepPrimAPI_MakeCylinder.hxx>
29
30 //=================================================================================================
31 GeomAlgoAPI_Cylinder::GeomAlgoAPI_Cylinder(std::shared_ptr<GeomAPI_Ax2> theAxis,
32                                            const double theRadius,
33                                            const double theHeight)
34 {
35   withAngle = false;
36   myAxis = theAxis;
37   myRadius = theRadius;
38   myHeight = theHeight;
39 }
40
41 //=================================================================================================
42 GeomAlgoAPI_Cylinder::GeomAlgoAPI_Cylinder(std::shared_ptr<GeomAPI_Ax2> theAxis,
43                                            const double theRadius,
44                                            const double theHeight,
45                                            const double theAngle)
46 {
47   withAngle = true;
48   myAxis = theAxis;
49   myRadius = theRadius;
50   myHeight = theHeight;
51   myAngle = theAngle;
52 }
53
54 //=================================================================================================
55 bool GeomAlgoAPI_Cylinder::check()
56 {
57   if (!myAxis) {
58     myError = "Cylinder builder :: axis is not valid.";
59     return false;
60   }
61   if (myRadius < Precision::Confusion()) {
62     myError = "Cylinder builder :: radius is negative or null.";
63     return false;
64   }
65   if (myHeight < Precision::Confusion()) {
66     myError = "Cylinder builder :: height is negative or null.";
67     return false;
68   }
69   if (withAngle) {
70     if (myAngle < Precision::Angular() * 180./M_PI) {
71       myError = "Cylinder builder :: angle is negative or null.";
72       return false;
73     }
74     if (myAngle > 360.) {
75       myError = "Cylinder builder :: angle greater than 360 degrees.";
76       return false;
77     }
78   }
79   return true;
80 }
81
82 //=================================================================================================
83 void GeomAlgoAPI_Cylinder::build()
84 {
85   myCreatedFaces.clear();
86
87   const gp_Ax2& anAxis = myAxis->impl<gp_Ax2>();
88
89   // Construct the cylinder
90   BRepPrimAPI_MakeCylinder *aCylinderMaker;
91
92   if (withAngle) {
93     aCylinderMaker =
94       new BRepPrimAPI_MakeCylinder(anAxis, myRadius, myHeight, myAngle * M_PI / 180.);
95   } else {
96     aCylinderMaker = new BRepPrimAPI_MakeCylinder(anAxis, myRadius, myHeight);
97   }
98
99   aCylinderMaker->Build();
100
101   if (!aCylinderMaker->IsDone()) {
102     return;
103   }
104
105   TopoDS_Shape aResult = aCylinderMaker->Shape();
106   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
107   aShape->setImpl(new TopoDS_Shape(aResult));
108   setShape(aShape);
109
110   // Test on the shapes
111   if (!aShape.get() || aShape->isNull()) {
112     myError = "Cylinder builder :: resulting shape is null.";
113     return;
114   }
115
116   setImpl(aCylinderMaker);
117
118   setDone(true);
119 }