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