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