Salome HOME
Merge branch 'master' into cgt/devCEA
[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   //myBasePoint = theBasePoint;
30   myAxis = theAxis;
31   myRadius = theRadius;
32   myHeight = theHeight;
33 }
34
35 //=================================================================================================
36 GeomAlgoAPI_Cylinder::GeomAlgoAPI_Cylinder(std::shared_ptr<GeomAPI_Ax2> theAxis,
37                                            const double theRadius,
38                                            const double theHeight,
39                                            const double theAngle)
40 {
41   withAngle = true;
42   myAxis = theAxis;
43   myRadius = theRadius;
44   myHeight = theHeight;
45   myAngle = theAngle;
46 }
47
48 //=================================================================================================
49 bool GeomAlgoAPI_Cylinder::check()
50 {
51   if (!myAxis) {
52     myError = "Cylinder builder :: axis is invalid.";
53     return false;
54   }
55   if (myRadius < Precision::Confusion()) {
56     myError = "Cylinder builder :: radius is negative or null.";
57     return false;
58   }
59   if (myHeight < Precision::Confusion()) {
60     myError = "Cylinder builder :: height is negative or null.";
61     return false;
62   }
63   if (withAngle) {
64     if (myAngle < Precision::Angular() * 180./M_PI) {
65       myError = "Cylinder builder :: angle is negative or null.";
66       return false;
67     }
68     if (myAngle > 360.) {
69       myError = "Cylinder builder :: angle greater than 360 degrees.";
70       return false;
71     }
72   }
73   return true;
74 }
75
76 //=================================================================================================
77 void GeomAlgoAPI_Cylinder::build()
78 {
79   myCreatedFaces.clear();
80   
81   const gp_Ax2& anAxis = myAxis->impl<gp_Ax2>();
82   
83   // Construct the cylinder
84   BRepPrimAPI_MakeCylinder *aCylinderMaker;
85   
86   if (withAngle) {
87     aCylinderMaker = 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 }