]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Solid.cpp
Salome HOME
Issue 2556: Functionality of inspection “WhatIs”
[modules/shaper.git] / src / GeomAPI / GeomAPI_Solid.cpp
1 // Copyright (C) 2018-20xx  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 "GeomAPI_Solid.h"
22
23 #include "GeomAPI_Box.h"
24 #include "GeomAPI_Cone.h"
25 #include "GeomAPI_Cylinder.h"
26 #include "GeomAPI_Dir.h"
27 #include "GeomAPI_Face.h"
28 #include "GeomAPI_Pln.h"
29 #include "GeomAPI_Pnt.h"
30 #include "GeomAPI_Shell.h"
31 #include "GeomAPI_Sphere.h"
32 #include "GeomAPI_Torus.h"
33 #include "GeomAPI_XYZ.h"
34
35 #include <BRep_Builder.hxx>
36 #include <Precision.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopoDS_Wire.hxx>
39
40 //==================================================================================================
41 GeomAPI_Solid::GeomAPI_Solid() : GeomAPI_Shape()
42 {
43 }
44
45 //==================================================================================================
46 GeomAPI_Solid::GeomAPI_Solid(const std::shared_ptr<GeomAPI_Shape>& theShape)
47 {
48   if (!theShape->isNull() && theShape->isSolid()) {
49     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
50   }
51 }
52
53 //==================================================================================================
54 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Solid::getSphere() const
55 {
56   GeomSpherePtr aSphere;
57   ListOfShape aShells = subShapes(SHELL);
58   if (aShells.size() == 1)
59     aSphere = aShells.front()->shell()->getSphere();
60   return aSphere;
61 }
62
63 //==================================================================================================
64 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Solid::getCylinder() const
65 {
66   bool isCylinder = false;
67
68   GeomPointPtr aLocation;
69   GeomDirPtr anAxis;
70   double aRadius;
71   double aHeight;
72
73   GeomPlanePtr aCaps[2];
74
75   for (TopExp_Explorer anExp(impl<TopoDS_Shape>(), TopAbs_FACE); anExp.More(); anExp.Next()) {
76     GeomFacePtr aFace(new GeomAPI_Face);
77     aFace->setImpl(new TopoDS_Shape(anExp.Current()));
78
79     GeomCylinderPtr aCurCyl = aFace->getCylinder();
80     if (aCurCyl) {
81       if (isCylinder) { // at least one cylindrical face is found
82         // compare radii
83         if (Abs(aRadius - aCurCyl->radius()) >= Precision::Confusion() ||
84           // check directions are collinear
85           !anAxis->isParallel(aCurCyl->axis()) ||
86           // check current center is on the main axis
87           anAxis->xyz()->cross(aLocation->xyz()->decreased(aCurCyl->location()->xyz())
88           )->squareModulus() >= Precision::SquareConfusion()) {
89           isCylinder = false;
90           break;
91         }
92       }
93       else { // first cylinder is found
94         aLocation = aCurCyl->location();
95         if (anAxis) {
96           // the plane is found => compare directions
97           if (!anAxis->isParallel(aCurCyl->axis()))
98             break;
99         }
100         else
101           anAxis = aCurCyl->axis();
102         aRadius = aCurCyl->radius();
103         aHeight = aCurCyl->height();
104         isCylinder = true;
105       }
106     }
107     else {
108       // check the face is planar
109       bool isPlaneApplicable = false;
110       GeomPlanePtr aCurPln = aFace->getPlane();
111       if (aCurPln) {
112         // verify the plane is already exists
113         int aLastPlanIndex = 0;
114         while (aLastPlanIndex < 2) {
115           if (!aCaps[aLastPlanIndex]) {
116             // add new plane
117             aCaps[aLastPlanIndex] = aCurPln;
118             break;
119           }
120           if (aCaps[aLastPlanIndex]->isCoincident(aCurPln))
121             break;
122           ++aLastPlanIndex;
123         }
124
125         isPlaneApplicable = aLastPlanIndex < 2;
126       }
127
128       if (isPlaneApplicable) {
129         if (!anAxis) // no cylinder is found, store the normal as further cylinder's axis
130           anAxis = aCurPln->direction();
131       }
132       else {
133         isCylinder = false;
134         break;
135       }
136     }
137   }
138
139   isCylinder = isCylinder && aCaps[0] && aCaps[1] &&
140                aCaps[0]->direction()->isParallel(anAxis) &&
141                aCaps[1]->direction()->isParallel(anAxis);
142
143   GeomCylinderPtr aCylinder;
144   if (isCylinder) {
145     // intersect planes with cylinder's axis
146     std::shared_ptr<GeomAPI_XYZ> anAxisXYZ = anAxis->xyz();
147     std::shared_ptr<GeomAPI_XYZ> aLocationXYZ = aLocation->xyz();
148     double aParam0 = anAxisXYZ->dot( aCaps[0]->location()->xyz()->decreased(aLocationXYZ) );
149     double aParam1 = anAxisXYZ->dot( aCaps[1]->location()->xyz()->decreased(aLocationXYZ) );
150     if (aParam0 > aParam1 + Precision::Confusion()) {
151       double tmp = aParam0;
152       aParam0 = aParam1;
153       aParam1 = tmp;
154     }
155
156     // update location of cylinder to be coincident with one of planes
157     aLocation->setX(aLocation->x() + aParam0 * anAxis->x());
158     aLocation->setY(aLocation->y() + aParam0 * anAxis->y());
159     aLocation->setZ(aLocation->z() + aParam0 * anAxis->z());
160
161     aHeight = aParam1 - aParam0;
162
163     aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, anAxis, aRadius, aHeight));
164   }
165   return aCylinder;
166 }
167
168 //==================================================================================================
169 std::shared_ptr<GeomAPI_Cone> GeomAPI_Solid::getCone() const
170 {
171   bool isCone = false;
172
173   GeomPointPtr anApex;
174   GeomDirPtr anAxis;
175   double aSemiAngle;
176   double aHeight;
177
178   GeomPlanePtr aCaps[2];
179
180   for (TopExp_Explorer anExp(impl<TopoDS_Shape>(), TopAbs_FACE); anExp.More(); anExp.Next()) {
181     GeomFacePtr aFace(new GeomAPI_Face);
182     aFace->setImpl(new TopoDS_Shape(anExp.Current()));
183
184     GeomConePtr aCurCone = aFace->getCone();
185     if (aCurCone) {
186       if (isCone) { // at least one conical face is found
187         // check equal apexes
188         if (anApex->distance(aCurCone->apex()) >= Precision::Confusion() ||
189         // check semi-angle
190             Abs(aSemiAngle - aCurCone->semiAngle() >= Precision::Confusion()) ||
191         // check axes are collinear
192             !anAxis->isParallel(aCurCone->axis())) {
193           isCone = false;
194           break;
195         }
196       }
197       else { // first cone is found
198         anApex = aCurCone->apex();
199         if (anAxis) {
200           // the plane is found => compare directions
201           if (!anAxis->isParallel(aCurCone->axis()))
202             break;
203         }
204         else
205           anAxis = aCurCone->axis();
206         aSemiAngle = aCurCone->semiAngle();
207         aHeight = aCurCone->height();
208         isCone = true;
209       }
210     }
211     else {
212       // check the face is planar
213       bool isPlaneApplicable = false;
214       GeomPlanePtr aCurPln = aFace->getPlane();
215       if (aCurPln) {
216         // verify the plane is already exists
217         int aLastPlanIndex = 0;
218         while (aLastPlanIndex < 2) {
219           if (!aCaps[aLastPlanIndex]) {
220             // add new plane
221             aCaps[aLastPlanIndex] = aCurPln;
222             break;
223           }
224           if (aCaps[aLastPlanIndex]->isCoincident(aCurPln))
225             break;
226           ++aLastPlanIndex;
227         }
228
229         isPlaneApplicable = aLastPlanIndex < 2;
230       }
231
232       if (isPlaneApplicable) {
233         if (!anAxis) // no cone is found, store the normal as further cone's axis
234           anAxis = aCurPln->direction();
235       }
236       else {
237         isCone = false;
238         break;
239       }
240     }
241   }
242
243   isCone = isCone && aCaps[0] && aCaps[0]->direction()->isParallel(anAxis);
244   if (isCone && aCaps[1]) // cone map have only one cap, if it is bounded by the apex
245     isCone = aCaps[1]->direction()->isParallel(anAxis);
246
247   GeomConePtr aCone;
248   if (isCone) {
249     // intersect planes with cone's axis
250     std::shared_ptr<GeomAPI_XYZ> anAxisXYZ = anAxis->xyz();
251     std::shared_ptr<GeomAPI_XYZ> anApexXYZ = anApex->xyz();
252     double aParam0 = anAxisXYZ->dot(aCaps[0]->location()->xyz()->decreased(anApexXYZ));
253     double aParam1 =
254         aCaps[1] ? anAxisXYZ->dot(aCaps[1]->location()->xyz()->decreased(anApexXYZ)) : 0.0;
255     if (aParam0 <= 0.0 && aParam1 <= 0.0) {
256       // reverse axis to make smaller cap be the first
257       anAxis->reverse();
258       aParam0 = -aParam0;
259       aParam1 = -aParam1;
260     }
261     if (aParam0 > aParam1 + Precision::Confusion()) {
262       double tmp = aParam0;
263       aParam0 = aParam1;
264       aParam1 = tmp;
265     }
266
267     // calculate location of cone to be coincident with one of planes
268     GeomPointPtr aLocation(new GeomAPI_Pnt(
269         anApex->x() + aParam0 * anAxis->x(),
270         anApex->y() + aParam0 * anAxis->y(),
271         anApex->z() + aParam0 * anAxis->z()));
272
273     // calculate radii of caps
274     aParam0 /= Cos(aSemiAngle);
275     aParam1 /= Cos(aSemiAngle);
276
277     aCone = GeomConePtr(new GeomAPI_Cone(aLocation, anAxis, aSemiAngle, aParam0, aParam1));
278   }
279   return aCone;
280 }
281
282 //==================================================================================================
283 std::shared_ptr<GeomAPI_Torus> GeomAPI_Solid::getTorus() const
284 {
285   GeomTorusPtr aTorus;
286   ListOfShape aShells = subShapes(SHELL);
287   if (aShells.size() == 1)
288     aTorus = aShells.front()->shell()->getTorus();
289   return aTorus;
290 }
291
292 //==================================================================================================
293 std::shared_ptr<GeomAPI_Box> GeomAPI_Solid::getParallelepiped() const
294 {
295   GeomBoxPtr aBox;
296   ListOfShape aShells = subShapes(SHELL);
297   if (aShells.size() == 1)
298     aBox = aShells.front()->shell()->getParallelepiped();
299   return aBox;
300 }