1 // Copyright (C) 2014-2020 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) throw (GeomAlgoAPI_Exception)
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)
53 throw (GeomAlgoAPI_Exception)
56 throw GeomAlgoAPI_Exception(theAlgo.getError());
60 if (!theAlgo.isDone() || !theAlgo.checkValid(theMsg))
61 throw GeomAlgoAPI_Exception(theAlgo.getError());
63 return theAlgo.shape();
66 namespace GeomAlgoAPI_ShapeAPI
68 //===============================================================================================
69 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
70 const double theDx, const double theDy,
71 const double theDz) throw (GeomAlgoAPI_Exception)
73 static const std::string aMsg("Box builder with dimensions");
74 GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
75 return runAlgoAndCheckShape(aBoxAlgo, aMsg);
78 //===============================================================================================
79 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
80 std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
81 std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
83 static const std::string aMsg("Box builder with two points");
84 GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
85 return runAlgoAndCheckShape(aBoxAlgo, aMsg);
88 //===============================================================================================
89 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
90 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
91 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
93 // Check if the base point is OK
95 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
97 // Check if the edge is OK
99 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
102 std::shared_ptr<GeomAPI_Ax2> anAxis;
103 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
104 theEdge->line()->direction()));
106 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
108 static const std::string aMsg("Cylinder builder");
109 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
112 //===============================================================================================
113 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
114 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
115 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
117 // Check if the base point is OK
119 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
121 // Check if the edge is OK
123 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
126 std::shared_ptr<GeomAPI_Ax2> anAxis;
127 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
128 theEdge->line()->direction()));
130 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
132 static const std::string aMsg("Cylinder portion builder");
133 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
136 //===============================================================================================
137 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
138 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
140 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
141 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
142 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
143 std::shared_ptr<GeomAPI_Ax2> anAxis;
144 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
145 aEdge->line()->direction()));
147 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
149 static const std::string aMsg("Cylinder builder");
150 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
153 //===============================================================================================
154 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
155 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
157 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
158 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
159 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
160 std::shared_ptr<GeomAPI_Ax2> anAxis;
161 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
162 aEdge->line()->direction()));
164 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
166 static const std::string aMsg("Cylinder portion builder");
167 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
170 //===============================================================================================
171 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSphere(
172 std::shared_ptr<GeomAPI_Pnt> theCenterPoint, double theRadius) throw (GeomAlgoAPI_Exception)
174 static const std::string aMsg("Sphere builder");
175 GeomAlgoAPI_Sphere aSphereAlgo(theCenterPoint, theRadius);
176 return runAlgoAndCheckShape(aSphereAlgo, aMsg);
179 //===============================================================================================
180 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSphere(double theRadius)
181 throw (GeomAlgoAPI_Exception)
183 std::shared_ptr<GeomAPI_Pnt> aCenterPoint =
184 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
186 GeomAlgoAPI_Sphere aSphereAlgo(aCenterPoint, theRadius);
188 static const std::string aMsg("Sphere builder");
189 return runAlgoAndCheckShape(aSphereAlgo, aMsg);
192 //===============================================================================================
193 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTorus(
194 std::shared_ptr<GeomAPI_Pnt> theBasePoint,
195 std::shared_ptr<GeomAPI_Edge> theEdge,double theRadius, double theRingRadius)
196 throw (GeomAlgoAPI_Exception)
198 // Check if the base point is OK
200 throw GeomAlgoAPI_Exception("Torus builder :: the base point is not valid.");
202 // Check if the edge is OK
204 throw GeomAlgoAPI_Exception("Torus builder :: the axis is not valid.");
207 std::shared_ptr<GeomAPI_Ax2> anAxis;
208 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
209 theEdge->line()->direction()));
211 GeomAlgoAPI_Torus aTorusAlgo(anAxis, theRadius, theRingRadius);
213 static const std::string aMsg("Torus builder");
214 return runAlgoAndCheckShape(aTorusAlgo, aMsg);
217 //===============================================================================================
218 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTorus(double theRadius,
219 double theRingRadius) throw (GeomAlgoAPI_Exception)
221 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
222 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
223 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
224 std::shared_ptr<GeomAPI_Ax2> anAxis;
225 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
226 aEdge->line()->direction()));
228 GeomAlgoAPI_Torus aTorusAlgo(anAxis, theRadius, theRingRadius);
230 static const std::string aMsg("Torus builder");
231 return runAlgoAndCheckShape(aTorusAlgo, aMsg);
234 //===============================================================================================
235 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCone(
236 std::shared_ptr<GeomAPI_Pnt> theBasePoint,
237 std::shared_ptr<GeomAPI_Edge> theEdge,
238 double theBaseRadius, double theTopRadius,
239 double theHeight) throw (GeomAlgoAPI_Exception)
241 // Check if the base point is OK
243 throw GeomAlgoAPI_Exception("Cone builder :: the base point is not valid.");
245 // Check if the edge is OK
247 throw GeomAlgoAPI_Exception("Cone builder :: the axis is not valid.");
250 std::shared_ptr<GeomAPI_Ax2> anAxis;
251 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
252 theEdge->line()->direction()));
254 GeomAlgoAPI_Cone aConeAlgo(anAxis, theBaseRadius, theTopRadius, theHeight);
256 static const std::string aMsg("Cone builder");
257 return runAlgoAndCheckShape(aConeAlgo, aMsg);
260 //===============================================================================================
261 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCone(
262 double theBaseRadius, double theTopRadius,
263 double theHeight) throw (GeomAlgoAPI_Exception)
265 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
266 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
267 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
268 std::shared_ptr<GeomAPI_Ax2> anAxis;
269 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
270 aEdge->line()->direction()));
272 GeomAlgoAPI_Cone aConeAlgo(anAxis, theBaseRadius, theTopRadius, theHeight);
274 static const std::string aMsg("Cone builder");
275 return runAlgoAndCheckShape(aConeAlgo, aMsg);
278 //===============================================================================================
279 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
280 std::shared_ptr<GeomAPI_Shape> theSourceShape,
281 std::shared_ptr<GeomAPI_Ax1> theAxis,
282 const double theDistance) throw (GeomAlgoAPI_Exception)
284 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
285 return runAlgo(aTranslationAlgo);
288 //===============================================================================================
289 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
290 std::shared_ptr<GeomAPI_Shape> theSourceShape,
293 const double theDz) throw (GeomAlgoAPI_Exception)
295 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
296 return runAlgo(aTranslationAlgo);
299 //===============================================================================================
300 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
301 std::shared_ptr<GeomAPI_Shape> theSourceShape,
302 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
303 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
305 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
306 return runAlgo(aTranslationAlgo);
309 //===============================================================================================
310 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
311 std::shared_ptr<GeomAPI_Shape> theSourceShape,
312 std::shared_ptr<GeomAPI_Ax1> theAxis,
313 const double theAngle) throw (GeomAlgoAPI_Exception)
315 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
316 return runAlgo(aRotationAlgo);
319 //===============================================================================================
320 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
321 std::shared_ptr<GeomAPI_Shape> theSourceShape,
322 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
323 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
324 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
326 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
327 return runAlgo(aRotationAlgo);
330 //===============================================================================================
331 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
332 std::shared_ptr<GeomAPI_Shape> theSourceShape,
333 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
335 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
336 return runAlgo(aSymmetryAlgo);
339 //===============================================================================================
340 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
341 std::shared_ptr<GeomAPI_Shape> theSourceShape,
342 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
344 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
345 return runAlgo(aSymmetryAlgo);
348 //===============================================================================================
349 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
350 std::shared_ptr<GeomAPI_Shape> theSourceShape,
351 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
353 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
354 return runAlgo(aSymmetryAlgo);
357 //===============================================================================================
358 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
359 std::shared_ptr<GeomAPI_Shape> theSourceShape,
360 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
361 const double theScaleFactor) throw (GeomAlgoAPI_Exception)
363 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
364 return runAlgo(aScaleAlgo);
367 //===============================================================================================
368 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
369 std::shared_ptr<GeomAPI_Shape> theSourceShape,
370 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
371 const double theScaleFactorX,
372 const double theScaleFactorY,
373 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
375 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
376 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
377 return runAlgo(aScaleAlgo);
380 //===============================================================================================
381 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
382 std::shared_ptr<GeomAPI_Shape> theSourceShape,
383 std::shared_ptr<GeomAPI_Ax1> theAxis,
384 const double theStep,
385 const int theNumber) throw (GeomAlgoAPI_Exception)
388 std::string aError = "Multitranslation builder ";
389 aError+=":: the first axis is not valid";
390 throw GeomAlgoAPI_Exception(aError);
394 std::string aError = "Multitranslation builder ";
395 aError+=":: the number of copies for the first direction is null or negative.";
396 throw GeomAlgoAPI_Exception(aError);
399 ListOfShape aListOfShape;
400 for (int i=0; i<theNumber; i++) {
402 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
404 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
407 //===============================================================================================
408 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
409 std::shared_ptr<GeomAPI_Shape> theSourceShape,
410 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
411 const double theFirstStep,
412 const int theFirstNumber,
413 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
414 const double theSecondStep,
415 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
418 std::string aError = "Multitranslation builder ";
419 aError+=":: the first axis is not valid";
420 throw GeomAlgoAPI_Exception(aError);
423 if (!theSecondAxis) {
424 std::string aError = "Multitranslation builder ";
425 aError+=":: the second axis is not valid";
426 throw GeomAlgoAPI_Exception(aError);
429 if (theFirstNumber <=0) {
430 std::string aError = "Multitranslation builder ";
431 aError+=":: the number of copies for the first direction is null or negative.";
432 throw GeomAlgoAPI_Exception(aError);
435 if (theSecondNumber <=0) {
436 std::string aError = "Multitranslation builder ";
437 aError+=":: the number of copies for the second direction is null or negative.";
438 throw GeomAlgoAPI_Exception(aError);
441 // Coord theFirstAxis
442 double x1 = theFirstAxis->dir()->x();
443 double y1 = theFirstAxis->dir()->y();
444 double z1 = theFirstAxis->dir()->z();
445 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
447 // Coord theSecondAxis
448 double x2 = theSecondAxis->dir()->x();
449 double y2 = theSecondAxis->dir()->y();
450 double z2 = theSecondAxis->dir()->z();
451 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
453 ListOfShape aListOfShape;
454 for (int j=0; j<theSecondStep; j++) {
455 for (int i=0; i<theFirstNumber; i++) {
456 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
457 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
458 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
459 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
462 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
465 //===============================================================================================
466 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiRotation(
467 std::shared_ptr<GeomAPI_Shape> theSourceShape,
468 std::shared_ptr<GeomAPI_Ax1> theAxis,
469 const int theNumber) throw (GeomAlgoAPI_Exception)
472 std::string aError = "Multirotation builder ";
473 aError+=":: the axis is not valid";
474 throw GeomAlgoAPI_Exception(aError);
478 std::string aError = "Multirotation builder ";
479 aError+=":: the number of copies is null or negative.";
480 throw GeomAlgoAPI_Exception(aError);
483 double anAngle = 360./theNumber;
485 ListOfShape aListOfShape;
486 for (int i=0; i<theNumber; i++) {
488 push_back(GeomAlgoAPI_ShapeAPI::makeRotation(theSourceShape, theAxis, i*anAngle));
490 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
493 //===============================================================================================
494 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiRotation(
495 std::shared_ptr<GeomAPI_Shape> theSourceShape,
496 std::shared_ptr<GeomAPI_Ax1> theAxis,
497 const double theStep,
498 const int theNumber) throw (GeomAlgoAPI_Exception)
501 std::string aError = "Multirotation builder ";
502 aError+=":: the axis is not valid";
503 throw GeomAlgoAPI_Exception(aError);
507 std::string aError = "Multirotation builder ";
508 aError+=":: the number of copies is null or negative.";
509 throw GeomAlgoAPI_Exception(aError);
512 ListOfShape aListOfShape;
513 for (int i=0; i<theNumber; i++) {
515 push_back(GeomAlgoAPI_ShapeAPI::makeRotation(theSourceShape, theAxis, i*theStep));
517 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
520 //===============================================================================================
521 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
522 const double theRMin1, const double theRMax1,
523 const double theRMin2, const double theRMax2,
525 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
527 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
528 theZ, theStartPhi, theDeltaPhi);
530 static const std::string aMsg("Cone Segment builder");
531 return runAlgoAndCheckShape(aConeSegmentAlgo, aMsg);