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