Salome HOME
Issue #2811: Update content of Object node on creation moment
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ConeSegment.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_ConeSegment.h>
22
23 #include <gp_Dir.hxx>
24 #include <gp_Ax1.hxx>
25 #include <gp_Pnt.hxx>
26 #include <TopoDS_Edge.hxx>
27
28 #include <BRepPrimAPI_MakeRevol.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepBuilderAPI_MakeWire.hxx>
31 #include <BRepBuilderAPI_MakeFace.hxx>
32 #include <Precision.hxx>
33
34 #include <math.h>
35
36 //=================================================================================================
37 GeomAlgoAPI_ConeSegment::GeomAlgoAPI_ConeSegment()
38 {
39 }
40
41 //=================================================================================================
42
43 GeomAlgoAPI_ConeSegment::GeomAlgoAPI_ConeSegment(const double theRMin1,
44                                                  const double theRMax1,
45                                                  const double theRMin2,
46                                                  const double theRMax2,
47                                                  const double theZ,
48                                                  const double theStartPhi,
49                                                  const double theDeltaPhi)
50 {
51   myRMin1 = theRMin1;
52   myRMax1 = theRMax1;
53   myRMin2 = theRMin2;
54   myRMax2 = theRMax2;
55   myZ = theZ;
56   myStartPhi = theStartPhi;
57   myDeltaPhi = theDeltaPhi;
58 }
59
60 //=================================================================================================
61 bool GeomAlgoAPI_ConeSegment::check()
62 {
63   if (myRMin1 < 0.)
64   {
65     myError = "Cone Segment builder :: rmin1 is negative.";
66     return false;
67   }
68   if (myRMin2 < 0.)
69   {
70     myError = "Cone Segment builder :: rmin2 is negative.";
71     return false;
72   }
73   if ((myRMax1-myRMin1) < Precision::Confusion())
74   {
75     myError = "Cone Segment builder :: rmin1 is larger than rmax1.";
76     return false;
77   }
78   if ((myRMax2-myRMin2) < Precision::Confusion())
79   {
80     myError = "Cone Segment builder :: rmin2 is larger than rmax2.";
81     return false;
82   }
83   if (myZ < Precision::Confusion())
84   {
85     myError = "Cone Segment builder :: z is negative or null.";
86     return false;
87   }
88   if (myDeltaPhi < Precision::Angular() * 180./M_PI)
89   {
90     myError = "Cone Segment builder :: deltaphi is negative or null.";
91     return false;
92   }
93   if (myDeltaPhi > 360)
94   {
95     myError = "Cone Segment builder :: deltaphi is larger than 360 degree.";
96     return false;
97   }
98   return true;
99 }
100
101 //=================================================================================================
102 void GeomAlgoAPI_ConeSegment::build()
103 {
104   myCreatedFaces.clear();
105
106   const double aStartPhiRad = myStartPhi * M_PI/180.;
107   BRepBuilderAPI_MakeWire aWireBuilder;
108
109   // Define the section from the 4 vertices
110   gp_Pnt aPointOuterBase(myRMax1 * cos(aStartPhiRad), myRMax1 * sin(aStartPhiRad), -myZ/2.);
111   gp_Pnt aPointInnerBase(myRMin1 * cos(aStartPhiRad), myRMin1 * sin(aStartPhiRad), -myZ/2.);
112   gp_Pnt aPointOuterTop(myRMax2 * cos(aStartPhiRad), myRMax2 * sin(aStartPhiRad), myZ/2.);
113   gp_Pnt aPointInnerTop(myRMin2 * cos(aStartPhiRad), myRMin2 * sin(aStartPhiRad), myZ/2.);
114
115   if ((myRMax1 - myRMin1) >= Precision::Confusion()){
116     BRepBuilderAPI_MakeEdge anEdgeBuilderBase(aPointOuterBase, aPointInnerBase);
117     anEdgeBuilderBase.Build();
118     aWireBuilder.Add(anEdgeBuilderBase.Edge());
119   }
120
121   BRepBuilderAPI_MakeEdge anEdgeBuilderOuter(aPointOuterBase, aPointOuterTop);
122   anEdgeBuilderOuter.Build();
123   aWireBuilder.Add(anEdgeBuilderOuter.Edge());
124   if ((myRMax2 - myRMin2) >= Precision::Confusion()){
125     BRepBuilderAPI_MakeEdge anEdgeBuilderTop(aPointOuterTop, aPointInnerTop);
126     anEdgeBuilderTop.Build();
127     aWireBuilder.Add(anEdgeBuilderTop.Edge());
128   }
129
130   BRepBuilderAPI_MakeEdge anEdgeBuilderInner(aPointInnerTop, aPointInnerBase);
131   anEdgeBuilderInner.Build();
132   aWireBuilder.Add(anEdgeBuilderInner.Edge());
133
134   aWireBuilder.Build();
135   BRepBuilderAPI_MakeFace aFaceBuilder(aWireBuilder.Wire());
136   aFaceBuilder.Build();
137
138   if (!aFaceBuilder.IsDone()){
139     myError = "Cone Segment builder :: section is not valid";
140     return;
141   }
142
143   // Perform a revolution based on the section to build the solid
144   gp_Dir aZDir(0., 0., 1.);
145   gp_Pnt anOrigin(0., 0., 0.);
146   gp_Ax1 aZAxis(anOrigin, aZDir);
147   BRepPrimAPI_MakeRevol* aRevolBuilder =
148     new BRepPrimAPI_MakeRevol(aFaceBuilder.Face(), aZAxis, myDeltaPhi * M_PI/180., Standard_True);
149   if(!aRevolBuilder) {
150     return;
151     myError = "Cone Segment builder :: section revolution did not succeed";
152   }
153   if(!aRevolBuilder->IsDone()) {
154     myError = "Cone Segment builder :: section revolution did not succeed";
155     return;
156   }
157
158   setImpl(aRevolBuilder);
159   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
160
161   std::shared_ptr<GeomAPI_Shape> aResultShape =
162     std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
163   aResultShape->setImpl(new TopoDS_Shape(aRevolBuilder->Shape()));
164
165   // Test on the shapes
166   if (!(aResultShape).get() || aResultShape->isNull()) {
167     myError = "Cone Segment builder  :: resulting shape is null.";
168     return;
169   }
170
171   setShape(aResultShape);
172   setDone(true);
173 }