Salome HOME
Minor corrections: coding rules
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Cylinder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Cylinder.cpp
4 // Created:     05 Jan 2016
5 // Author:      Clarisse Genrault (CEA)
6
7 #include <GeomAlgoAPI_Cylinder.h>
8
9 #include <gp_Ax2.hxx>
10 #include <gp_Dir.hxx>
11 #include <TopoDS_Edge.hxx>
12 #include <TopoDS_Shape.hxx>
13
14 #include <BRepPrimAPI_MakeCylinder.hxx>
15
16 #include <iostream>
17
18 //=================================================================================================
19 GeomAlgoAPI_Cylinder::GeomAlgoAPI_Cylinder()
20 {
21 }
22
23 //=================================================================================================
24 GeomAlgoAPI_Cylinder::GeomAlgoAPI_Cylinder(std::shared_ptr<GeomAPI_Ax2> theAxis,
25                                            const double theRadius,
26                                            const double theHeight)
27 {
28   withAngle = false;
29   myAxis = theAxis;
30   myRadius = theRadius;
31   myHeight = theHeight;
32 }
33
34 //=================================================================================================
35 GeomAlgoAPI_Cylinder::GeomAlgoAPI_Cylinder(std::shared_ptr<GeomAPI_Ax2> theAxis,
36                                            const double theRadius,
37                                            const double theHeight,
38                                            const double theAngle)
39 {
40   withAngle = true;
41   myAxis = theAxis;
42   myRadius = theRadius;
43   myHeight = theHeight;
44   myAngle = theAngle;
45 }
46
47 //=================================================================================================
48 bool GeomAlgoAPI_Cylinder::check()
49 {
50   if (!myAxis) {
51     myError = "Cylinder builder :: axis is not valid.";
52     return false;
53   }
54   if (myRadius < Precision::Confusion()) {
55     myError = "Cylinder builder :: radius is negative or null.";
56     return false;
57   }
58   if (myHeight < Precision::Confusion()) {
59     myError = "Cylinder builder :: height is negative or null.";
60     return false;
61   }
62   if (withAngle) {
63     if (myAngle < Precision::Angular() * 180./M_PI) {
64       myError = "Cylinder builder :: angle is negative or null.";
65       return false;
66     }
67     if (myAngle > 360.) {
68       myError = "Cylinder builder :: angle greater than 360 degrees.";
69       return false;
70     }
71   }
72   return true;
73 }
74
75 //=================================================================================================
76 void GeomAlgoAPI_Cylinder::build()
77 {
78   myCreatedFaces.clear();
79
80   const gp_Ax2& anAxis = myAxis->impl<gp_Ax2>();
81
82   // Construct the cylinder
83   BRepPrimAPI_MakeCylinder *aCylinderMaker;
84
85   if (withAngle) {
86     aCylinderMaker =
87       new BRepPrimAPI_MakeCylinder(anAxis, myRadius, myHeight, myAngle * M_PI / 180.);
88   } else {
89     aCylinderMaker = new BRepPrimAPI_MakeCylinder(anAxis, myRadius, myHeight);
90   }
91
92   aCylinderMaker->Build();
93
94   if (!aCylinderMaker->IsDone()) {
95     return;
96   }
97
98   TopoDS_Shape aResult = aCylinderMaker->Shape();
99   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
100   aShape->setImpl(new TopoDS_Shape(aResult));
101   setShape(aShape);
102
103   // Test on the shapes
104   if (!aShape.get() || aShape->isNull()) {
105     myError = "Cylinder builder :: resulting shape is null.";
106     return;
107   }
108
109   setImpl(aCylinderMaker);
110
111   setDone(true);
112 }