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