Salome HOME
Merge branch 'Dev_2.8.0'
[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()
32 {
33 }
34
35 //=================================================================================================
36 GeomAlgoAPI_Cylinder::GeomAlgoAPI_Cylinder(std::shared_ptr<GeomAPI_Ax2> theAxis,
37                                            const double theRadius,
38                                            const double theHeight)
39 {
40   withAngle = false;
41   myAxis = theAxis;
42   myRadius = theRadius;
43   myHeight = theHeight;
44 }
45
46 //=================================================================================================
47 GeomAlgoAPI_Cylinder::GeomAlgoAPI_Cylinder(std::shared_ptr<GeomAPI_Ax2> theAxis,
48                                            const double theRadius,
49                                            const double theHeight,
50                                            const double theAngle)
51 {
52   withAngle = true;
53   myAxis = theAxis;
54   myRadius = theRadius;
55   myHeight = theHeight;
56   myAngle = theAngle;
57 }
58
59 //=================================================================================================
60 bool GeomAlgoAPI_Cylinder::check()
61 {
62   if (!myAxis) {
63     myError = "Cylinder builder :: axis is not valid.";
64     return false;
65   }
66   if (myRadius < Precision::Confusion()) {
67     myError = "Cylinder builder :: radius is negative or null.";
68     return false;
69   }
70   if (myHeight < Precision::Confusion()) {
71     myError = "Cylinder builder :: height is negative or null.";
72     return false;
73   }
74   if (withAngle) {
75     if (myAngle < Precision::Angular() * 180./M_PI) {
76       myError = "Cylinder builder :: angle is negative or null.";
77       return false;
78     }
79     if (myAngle > 360.) {
80       myError = "Cylinder builder :: angle greater than 360 degrees.";
81       return false;
82     }
83   }
84   return true;
85 }
86
87 //=================================================================================================
88 void GeomAlgoAPI_Cylinder::build()
89 {
90   myCreatedFaces.clear();
91
92   const gp_Ax2& anAxis = myAxis->impl<gp_Ax2>();
93
94   // Construct the cylinder
95   BRepPrimAPI_MakeCylinder *aCylinderMaker;
96
97   if (withAngle) {
98     aCylinderMaker =
99       new BRepPrimAPI_MakeCylinder(anAxis, myRadius, myHeight, myAngle * M_PI / 180.);
100   } else {
101     aCylinderMaker = new BRepPrimAPI_MakeCylinder(anAxis, myRadius, myHeight);
102   }
103
104   aCylinderMaker->Build();
105
106   if (!aCylinderMaker->IsDone()) {
107     return;
108   }
109
110   TopoDS_Shape aResult = aCylinderMaker->Shape();
111   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
112   aShape->setImpl(new TopoDS_Shape(aResult));
113   setShape(aShape);
114
115   // Test on the shapes
116   if (!aShape.get() || aShape->isNull()) {
117     myError = "Cylinder builder :: resulting shape is null.";
118     return;
119   }
120
121   setImpl(aCylinderMaker);
122
123   setDone(true);
124 }