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