1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_ShapeAPI.h"
22 #include <GeomAlgoAPI_Box.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_Cone.h>
25 #include <GeomAlgoAPI_ConeSegment.h>
26 #include <GeomAlgoAPI_Cylinder.h>
27 #include <GeomAlgoAPI_EdgeBuilder.h>
28 #include <GeomAlgoAPI_Rotation.h>
29 #include <GeomAlgoAPI_Scale.h>
30 #include <GeomAlgoAPI_Sphere.h>
31 #include <GeomAlgoAPI_Symmetry.h>
32 #include <GeomAlgoAPI_Torus.h>
33 #include <GeomAlgoAPI_Translation.h>
35 #include <GeomAPI_Lin.h>
39 static GeomShapePtr runAlgo(GeomAlgoAPI_MakeShape& theAlgo)
42 throw GeomAlgoAPI_Exception(theAlgo.getError());
46 if (!theAlgo.isDone())
47 throw GeomAlgoAPI_Exception(theAlgo.getError());
49 return theAlgo.shape();
52 static GeomShapePtr runAlgoAndCheckShape(GeomAlgoAPI_MakeShape& theAlgo, const std::string& theMsg)
55 throw GeomAlgoAPI_Exception(theAlgo.getError());
59 if (!theAlgo.isDone() || !theAlgo.checkValid(theMsg))
60 throw GeomAlgoAPI_Exception(theAlgo.getError());
62 return theAlgo.shape();
65 namespace GeomAlgoAPI_ShapeAPI
67 //===============================================================================================
68 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
69 const double theDx, const double theDy,
72 static const std::string aMsg("Box builder with dimensions");
73 GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
74 return runAlgoAndCheckShape(aBoxAlgo, aMsg);
77 //===============================================================================================
78 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
79 std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
80 std::shared_ptr<GeomAPI_Pnt> theSecondPoint)
82 static const std::string aMsg("Box builder with two points");
83 GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
84 return runAlgoAndCheckShape(aBoxAlgo, aMsg);
87 //===============================================================================================
88 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
89 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
90 double theRadius, double theHeight)
92 // Check if the base point is OK
94 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
96 // Check if the edge is OK
98 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
101 std::shared_ptr<GeomAPI_Ax2> anAxis;
102 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
103 theEdge->line()->direction()));
105 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
107 static const std::string aMsg("Cylinder builder");
108 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
111 //===============================================================================================
112 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
113 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
114 double theRadius, double theHeight, double theAngle)
116 // Check if the base point is OK
118 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
120 // Check if the edge is OK
122 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
125 std::shared_ptr<GeomAPI_Ax2> anAxis;
126 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
127 theEdge->line()->direction()));
129 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
131 static const std::string aMsg("Cylinder portion builder");
132 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
135 //===============================================================================================
136 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
137 double theRadius, double theHeight)
139 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
140 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
141 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
142 std::shared_ptr<GeomAPI_Ax2> anAxis;
143 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
144 aEdge->line()->direction()));
146 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
148 static const std::string aMsg("Cylinder builder");
149 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
152 //===============================================================================================
153 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
154 double theRadius, double theHeight, double theAngle)
156 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
157 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
158 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
159 std::shared_ptr<GeomAPI_Ax2> anAxis;
160 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
161 aEdge->line()->direction()));
163 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
165 static const std::string aMsg("Cylinder portion builder");
166 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
169 //===============================================================================================
170 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSphere(
171 std::shared_ptr<GeomAPI_Pnt> theCenterPoint, double theRadius)
173 static const std::string aMsg("Sphere builder");
174 GeomAlgoAPI_Sphere aSphereAlgo(theCenterPoint, theRadius);
175 return runAlgoAndCheckShape(aSphereAlgo, aMsg);
178 //===============================================================================================
179 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSphere(double theRadius)
181 std::shared_ptr<GeomAPI_Pnt> aCenterPoint =
182 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
184 GeomAlgoAPI_Sphere aSphereAlgo(aCenterPoint, theRadius);
186 static const std::string aMsg("Sphere builder");
187 return runAlgoAndCheckShape(aSphereAlgo, aMsg);
190 //===============================================================================================
191 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTorus(
192 std::shared_ptr<GeomAPI_Pnt> theBasePoint,
193 std::shared_ptr<GeomAPI_Edge> theEdge,double theRadius, double theRingRadius)
195 // Check if the base point is OK
197 throw GeomAlgoAPI_Exception("Torus builder :: the base point is not valid.");
199 // Check if the edge is OK
201 throw GeomAlgoAPI_Exception("Torus builder :: the axis is not valid.");
204 std::shared_ptr<GeomAPI_Ax2> anAxis;
205 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
206 theEdge->line()->direction()));
208 GeomAlgoAPI_Torus aTorusAlgo(anAxis, theRadius, theRingRadius);
210 static const std::string aMsg("Torus builder");
211 return runAlgoAndCheckShape(aTorusAlgo, aMsg);
214 //===============================================================================================
215 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTorus(double theRadius,
216 double theRingRadius)
218 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
219 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
220 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
221 std::shared_ptr<GeomAPI_Ax2> anAxis;
222 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
223 aEdge->line()->direction()));
225 GeomAlgoAPI_Torus aTorusAlgo(anAxis, theRadius, theRingRadius);
227 static const std::string aMsg("Torus builder");
228 return runAlgoAndCheckShape(aTorusAlgo, aMsg);
231 //===============================================================================================
232 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCone(
233 std::shared_ptr<GeomAPI_Pnt> theBasePoint,
234 std::shared_ptr<GeomAPI_Edge> theEdge,
235 double theBaseRadius, double theTopRadius,
238 // Check if the base point is OK
240 throw GeomAlgoAPI_Exception("Cone builder :: the base point is not valid.");
242 // Check if the edge is OK
244 throw GeomAlgoAPI_Exception("Cone builder :: the axis is not valid.");
247 std::shared_ptr<GeomAPI_Ax2> anAxis;
248 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
249 theEdge->line()->direction()));
251 GeomAlgoAPI_Cone aConeAlgo(anAxis, theBaseRadius, theTopRadius, theHeight);
253 static const std::string aMsg("Cone builder");
254 return runAlgoAndCheckShape(aConeAlgo, aMsg);
257 //===============================================================================================
258 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCone(
259 double theBaseRadius, double theTopRadius,
262 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
263 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
264 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
265 std::shared_ptr<GeomAPI_Ax2> anAxis;
266 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
267 aEdge->line()->direction()));
269 GeomAlgoAPI_Cone aConeAlgo(anAxis, theBaseRadius, theTopRadius, theHeight);
271 static const std::string aMsg("Cone builder");
272 return runAlgoAndCheckShape(aConeAlgo, aMsg);
275 //===============================================================================================
276 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
277 std::shared_ptr<GeomAPI_Shape> theSourceShape,
278 std::shared_ptr<GeomAPI_Ax1> theAxis,
279 const double theDistance)
281 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
282 return runAlgo(aTranslationAlgo);
285 //===============================================================================================
286 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
287 std::shared_ptr<GeomAPI_Shape> theSourceShape,
292 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
293 return runAlgo(aTranslationAlgo);
296 //===============================================================================================
297 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
298 std::shared_ptr<GeomAPI_Shape> theSourceShape,
299 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
300 std::shared_ptr<GeomAPI_Pnt> theEndPoint)
302 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
303 return runAlgo(aTranslationAlgo);
306 //===============================================================================================
307 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
308 std::shared_ptr<GeomAPI_Shape> theSourceShape,
309 std::shared_ptr<GeomAPI_Ax1> theAxis,
310 const double theAngle)
312 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
313 return runAlgo(aRotationAlgo);
316 //===============================================================================================
317 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
318 std::shared_ptr<GeomAPI_Shape> theSourceShape,
319 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
320 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
321 std::shared_ptr<GeomAPI_Pnt> theEndPoint)
323 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
324 return runAlgo(aRotationAlgo);
327 //===============================================================================================
328 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
329 std::shared_ptr<GeomAPI_Shape> theSourceShape,
330 std::shared_ptr<GeomAPI_Pnt> thePoint)
332 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
333 return runAlgo(aSymmetryAlgo);
336 //===============================================================================================
337 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
338 std::shared_ptr<GeomAPI_Shape> theSourceShape,
339 std::shared_ptr<GeomAPI_Ax1> theAxis)
341 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
342 return runAlgo(aSymmetryAlgo);
345 //===============================================================================================
346 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
347 std::shared_ptr<GeomAPI_Shape> theSourceShape,
348 std::shared_ptr<GeomAPI_Ax2> thePlane)
350 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
351 return runAlgo(aSymmetryAlgo);
354 //===============================================================================================
355 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
356 std::shared_ptr<GeomAPI_Shape> theSourceShape,
357 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
358 const double theScaleFactor)
360 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
361 return runAlgo(aScaleAlgo);
364 //===============================================================================================
365 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
366 std::shared_ptr<GeomAPI_Shape> theSourceShape,
367 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
368 const double theScaleFactorX,
369 const double theScaleFactorY,
370 const double theScaleFactorZ)
372 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
373 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
374 return runAlgo(aScaleAlgo);
377 //===============================================================================================
378 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
379 std::shared_ptr<GeomAPI_Shape> theSourceShape,
380 std::shared_ptr<GeomAPI_Ax1> theAxis,
381 const double theStep,
385 std::string aError = "Multitranslation builder ";
386 aError+=":: the first axis is not valid";
387 throw GeomAlgoAPI_Exception(aError);
391 std::string aError = "Multitranslation builder ";
392 aError+=":: the number of copies for the first direction is null or negative.";
393 throw GeomAlgoAPI_Exception(aError);
396 ListOfShape aListOfShape;
397 for (int i=0; i<theNumber; i++) {
399 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
401 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
404 //===============================================================================================
405 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
406 std::shared_ptr<GeomAPI_Shape> theSourceShape,
407 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
408 const double theFirstStep,
409 const int theFirstNumber,
410 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
411 const double theSecondStep,
412 const int theSecondNumber)
415 std::string aError = "Multitranslation builder ";
416 aError+=":: the first axis is not valid";
417 throw GeomAlgoAPI_Exception(aError);
420 if (!theSecondAxis) {
421 std::string aError = "Multitranslation builder ";
422 aError+=":: the second axis is not valid";
423 throw GeomAlgoAPI_Exception(aError);
426 if (theFirstNumber <=0) {
427 std::string aError = "Multitranslation builder ";
428 aError+=":: the number of copies for the first direction is null or negative.";
429 throw GeomAlgoAPI_Exception(aError);
432 if (theSecondNumber <=0) {
433 std::string aError = "Multitranslation builder ";
434 aError+=":: the number of copies for the second direction is null or negative.";
435 throw GeomAlgoAPI_Exception(aError);
438 // Coord theFirstAxis
439 double x1 = theFirstAxis->dir()->x();
440 double y1 = theFirstAxis->dir()->y();
441 double z1 = theFirstAxis->dir()->z();
442 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
444 // Coord theSecondAxis
445 double x2 = theSecondAxis->dir()->x();
446 double y2 = theSecondAxis->dir()->y();
447 double z2 = theSecondAxis->dir()->z();
448 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
450 ListOfShape aListOfShape;
451 for (int j=0; j<theSecondStep; j++) {
452 for (int i=0; i<theFirstNumber; i++) {
453 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
454 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
455 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
456 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
459 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
462 //===============================================================================================
463 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiRotation(
464 std::shared_ptr<GeomAPI_Shape> theSourceShape,
465 std::shared_ptr<GeomAPI_Ax1> theAxis,
469 std::string aError = "Multirotation builder ";
470 aError+=":: the axis is not valid";
471 throw GeomAlgoAPI_Exception(aError);
475 std::string aError = "Multirotation builder ";
476 aError+=":: the number of copies is null or negative.";
477 throw GeomAlgoAPI_Exception(aError);
480 double anAngle = 360./theNumber;
482 ListOfShape aListOfShape;
483 for (int i=0; i<theNumber; i++) {
485 push_back(GeomAlgoAPI_ShapeAPI::makeRotation(theSourceShape, theAxis, i*anAngle));
487 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
490 //===============================================================================================
491 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiRotation(
492 std::shared_ptr<GeomAPI_Shape> theSourceShape,
493 std::shared_ptr<GeomAPI_Ax1> theAxis,
494 const double theStep,
498 std::string aError = "Multirotation builder ";
499 aError+=":: the axis is not valid";
500 throw GeomAlgoAPI_Exception(aError);
504 std::string aError = "Multirotation builder ";
505 aError+=":: the number of copies is null or negative.";
506 throw GeomAlgoAPI_Exception(aError);
509 ListOfShape aListOfShape;
510 for (int i=0; i<theNumber; i++) {
512 push_back(GeomAlgoAPI_ShapeAPI::makeRotation(theSourceShape, theAxis, i*theStep));
514 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
517 //===============================================================================================
518 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
519 const double theRMin1, const double theRMax1,
520 const double theRMin2, const double theRMax2,
522 const double theStartPhi, const double theDeltaPhi)
524 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
525 theZ, theStartPhi, theDeltaPhi);
527 static const std::string aMsg("Cone Segment builder");
528 return runAlgoAndCheckShape(aConeSegmentAlgo, aMsg);