1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_ShapeAPI.h"
23 #include <GeomAlgoAPI_Box.h>
24 #include <GeomAlgoAPI_CompoundBuilder.h>
25 #include <GeomAlgoAPI_Cone.h>
26 #include <GeomAlgoAPI_ConeSegment.h>
27 #include <GeomAlgoAPI_Cylinder.h>
28 #include <GeomAlgoAPI_EdgeBuilder.h>
29 #include <GeomAlgoAPI_Rotation.h>
30 #include <GeomAlgoAPI_Scale.h>
31 #include <GeomAlgoAPI_Sphere.h>
32 #include <GeomAlgoAPI_Symmetry.h>
33 #include <GeomAlgoAPI_Torus.h>
34 #include <GeomAlgoAPI_Translation.h>
36 #include <GeomAPI_Lin.h>
40 static GeomShapePtr runAlgo(GeomAlgoAPI_MakeShape& theAlgo) throw (GeomAlgoAPI_Exception)
43 throw GeomAlgoAPI_Exception(theAlgo.getError());
47 if (!theAlgo.isDone())
48 throw GeomAlgoAPI_Exception(theAlgo.getError());
50 return theAlgo.shape();
53 static GeomShapePtr runAlgoAndCheckShape(GeomAlgoAPI_MakeShape& theAlgo, const std::string& theMsg)
54 throw (GeomAlgoAPI_Exception)
57 throw GeomAlgoAPI_Exception(theAlgo.getError());
61 if (!theAlgo.isDone() || !theAlgo.checkValid(theMsg))
62 throw GeomAlgoAPI_Exception(theAlgo.getError());
64 return theAlgo.shape();
67 namespace GeomAlgoAPI_ShapeAPI
69 //===============================================================================================
70 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
71 const double theDx, const double theDy,
72 const double theDz) throw (GeomAlgoAPI_Exception)
74 static const std::string aMsg("Box builder with dimensions");
75 GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
76 return runAlgoAndCheckShape(aBoxAlgo, aMsg);
79 //===============================================================================================
80 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
81 std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
82 std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
84 static const std::string aMsg("Box builder with two points");
85 GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
86 return runAlgoAndCheckShape(aBoxAlgo, aMsg);
89 //===============================================================================================
90 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
91 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
92 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
94 // Check if the base point is OK
96 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
98 // Check if the edge is OK
100 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
103 std::shared_ptr<GeomAPI_Ax2> anAxis;
104 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
105 theEdge->line()->direction()));
107 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
109 static const std::string aMsg("Cylinder builder");
110 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
113 //===============================================================================================
114 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
115 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
116 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
118 // Check if the base point is OK
120 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
122 // Check if the edge is OK
124 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
127 std::shared_ptr<GeomAPI_Ax2> anAxis;
128 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
129 theEdge->line()->direction()));
131 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
133 static const std::string aMsg("Cylinder portion builder");
134 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
137 //===============================================================================================
138 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
139 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
141 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
142 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
143 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
144 std::shared_ptr<GeomAPI_Ax2> anAxis;
145 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
146 aEdge->line()->direction()));
148 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
150 static const std::string aMsg("Cylinder builder");
151 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
154 //===============================================================================================
155 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
156 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
158 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
159 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
160 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
161 std::shared_ptr<GeomAPI_Ax2> anAxis;
162 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
163 aEdge->line()->direction()));
165 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
167 static const std::string aMsg("Cylinder portion builder");
168 return runAlgoAndCheckShape(aCylinderAlgo, aMsg);
171 //===============================================================================================
172 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSphere(
173 std::shared_ptr<GeomAPI_Pnt> theCenterPoint, double theRadius) throw (GeomAlgoAPI_Exception)
175 static const std::string aMsg("Sphere builder");
176 GeomAlgoAPI_Sphere aSphereAlgo(theCenterPoint, theRadius);
177 return runAlgoAndCheckShape(aSphereAlgo, aMsg);
180 //===============================================================================================
181 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSphere(double theRadius)
182 throw (GeomAlgoAPI_Exception)
184 std::shared_ptr<GeomAPI_Pnt> aCenterPoint =
185 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
187 GeomAlgoAPI_Sphere aSphereAlgo(aCenterPoint, theRadius);
189 static const std::string aMsg("Sphere builder");
190 return runAlgoAndCheckShape(aSphereAlgo, aMsg);
193 //===============================================================================================
194 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTorus(
195 std::shared_ptr<GeomAPI_Pnt> theBasePoint,
196 std::shared_ptr<GeomAPI_Edge> theEdge,double theRadius, double theRingRadius)
197 throw (GeomAlgoAPI_Exception)
199 // Check if the base point is OK
201 throw GeomAlgoAPI_Exception("Torus builder :: the base point is not valid.");
203 // Check if the edge is OK
205 throw GeomAlgoAPI_Exception("Torus builder :: the axis is not valid.");
208 std::shared_ptr<GeomAPI_Ax2> anAxis;
209 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
210 theEdge->line()->direction()));
212 GeomAlgoAPI_Torus aTorusAlgo(anAxis, theRadius, theRingRadius);
214 static const std::string aMsg("Torus builder");
215 return runAlgoAndCheckShape(aTorusAlgo, aMsg);
218 //===============================================================================================
219 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTorus(double theRadius,
220 double theRingRadius) throw (GeomAlgoAPI_Exception)
222 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
223 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
224 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
225 std::shared_ptr<GeomAPI_Ax2> anAxis;
226 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
227 aEdge->line()->direction()));
229 GeomAlgoAPI_Torus aTorusAlgo(anAxis, theRadius, theRingRadius);
231 static const std::string aMsg("Torus builder");
232 return runAlgoAndCheckShape(aTorusAlgo, aMsg);
235 //===============================================================================================
236 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCone(
237 std::shared_ptr<GeomAPI_Pnt> theBasePoint,
238 std::shared_ptr<GeomAPI_Edge> theEdge,
239 double theBaseRadius, double theTopRadius,
240 double theHeight) throw (GeomAlgoAPI_Exception)
242 // Check if the base point is OK
244 throw GeomAlgoAPI_Exception("Cone builder :: the base point is not valid.");
246 // Check if the edge is OK
248 throw GeomAlgoAPI_Exception("Cone builder :: the axis is not valid.");
251 std::shared_ptr<GeomAPI_Ax2> anAxis;
252 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
253 theEdge->line()->direction()));
255 GeomAlgoAPI_Cone aConeAlgo(anAxis, theBaseRadius, theTopRadius, theHeight);
257 static const std::string aMsg("Cone builder");
258 return runAlgoAndCheckShape(aConeAlgo, aMsg);
261 //===============================================================================================
262 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCone(
263 double theBaseRadius, double theTopRadius,
264 double theHeight) throw (GeomAlgoAPI_Exception)
266 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
267 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
268 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
269 std::shared_ptr<GeomAPI_Ax2> anAxis;
270 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
271 aEdge->line()->direction()));
273 GeomAlgoAPI_Cone aConeAlgo(anAxis, theBaseRadius, theTopRadius, theHeight);
275 static const std::string aMsg("Cone builder");
276 return runAlgoAndCheckShape(aConeAlgo, aMsg);
279 //===============================================================================================
280 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
281 std::shared_ptr<GeomAPI_Shape> theSourceShape,
282 std::shared_ptr<GeomAPI_Ax1> theAxis,
283 const double theDistance) throw (GeomAlgoAPI_Exception)
285 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
286 return runAlgo(aTranslationAlgo);
289 //===============================================================================================
290 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
291 std::shared_ptr<GeomAPI_Shape> theSourceShape,
294 const double theDz) throw (GeomAlgoAPI_Exception)
296 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
297 return runAlgo(aTranslationAlgo);
300 //===============================================================================================
301 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
302 std::shared_ptr<GeomAPI_Shape> theSourceShape,
303 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
304 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
306 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
307 return runAlgo(aTranslationAlgo);
310 //===============================================================================================
311 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
312 std::shared_ptr<GeomAPI_Shape> theSourceShape,
313 std::shared_ptr<GeomAPI_Ax1> theAxis,
314 const double theAngle) throw (GeomAlgoAPI_Exception)
316 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
317 return runAlgo(aRotationAlgo);
320 //===============================================================================================
321 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
322 std::shared_ptr<GeomAPI_Shape> theSourceShape,
323 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
324 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
325 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
327 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
328 return runAlgo(aRotationAlgo);
331 //===============================================================================================
332 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
333 std::shared_ptr<GeomAPI_Shape> theSourceShape,
334 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
336 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
337 return runAlgo(aSymmetryAlgo);
340 //===============================================================================================
341 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
342 std::shared_ptr<GeomAPI_Shape> theSourceShape,
343 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
345 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
346 return runAlgo(aSymmetryAlgo);
349 //===============================================================================================
350 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
351 std::shared_ptr<GeomAPI_Shape> theSourceShape,
352 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
354 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
355 return runAlgo(aSymmetryAlgo);
358 //===============================================================================================
359 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
360 std::shared_ptr<GeomAPI_Shape> theSourceShape,
361 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
362 const double theScaleFactor) throw (GeomAlgoAPI_Exception)
364 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
365 return runAlgo(aScaleAlgo);
368 //===============================================================================================
369 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
370 std::shared_ptr<GeomAPI_Shape> theSourceShape,
371 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
372 const double theScaleFactorX,
373 const double theScaleFactorY,
374 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
376 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
377 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
378 return runAlgo(aScaleAlgo);
381 //===============================================================================================
382 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
383 std::shared_ptr<GeomAPI_Shape> theSourceShape,
384 std::shared_ptr<GeomAPI_Ax1> theAxis,
385 const double theStep,
386 const int theNumber) throw (GeomAlgoAPI_Exception)
389 std::string aError = "Multitranslation builder ";
390 aError+=":: the first axis is not valid";
391 throw GeomAlgoAPI_Exception(aError);
395 std::string aError = "Multitranslation builder ";
396 aError+=":: the number of copies for the first direction is null or negative.";
397 throw GeomAlgoAPI_Exception(aError);
400 ListOfShape aListOfShape;
401 for (int i=0; i<theNumber; i++) {
403 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
405 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
408 //===============================================================================================
409 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
410 std::shared_ptr<GeomAPI_Shape> theSourceShape,
411 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
412 const double theFirstStep,
413 const int theFirstNumber,
414 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
415 const double theSecondStep,
416 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
419 std::string aError = "Multitranslation builder ";
420 aError+=":: the first axis is not valid";
421 throw GeomAlgoAPI_Exception(aError);
424 if (!theSecondAxis) {
425 std::string aError = "Multitranslation builder ";
426 aError+=":: the second axis is not valid";
427 throw GeomAlgoAPI_Exception(aError);
430 if (theFirstNumber <=0) {
431 std::string aError = "Multitranslation builder ";
432 aError+=":: the number of copies for the first direction is null or negative.";
433 throw GeomAlgoAPI_Exception(aError);
436 if (theSecondNumber <=0) {
437 std::string aError = "Multitranslation builder ";
438 aError+=":: the number of copies for the second direction is null or negative.";
439 throw GeomAlgoAPI_Exception(aError);
442 // Coord theFirstAxis
443 double x1 = theFirstAxis->dir()->x();
444 double y1 = theFirstAxis->dir()->y();
445 double z1 = theFirstAxis->dir()->z();
446 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
448 // Coord theSecondAxis
449 double x2 = theSecondAxis->dir()->x();
450 double y2 = theSecondAxis->dir()->y();
451 double z2 = theSecondAxis->dir()->z();
452 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
454 ListOfShape aListOfShape;
455 for (int j=0; j<theSecondStep; j++) {
456 for (int i=0; i<theFirstNumber; i++) {
457 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
458 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
459 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
460 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
463 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
466 //===============================================================================================
467 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiRotation(
468 std::shared_ptr<GeomAPI_Shape> theSourceShape,
469 std::shared_ptr<GeomAPI_Ax1> theAxis,
470 const int theNumber) throw (GeomAlgoAPI_Exception)
473 std::string aError = "Multirotation builder ";
474 aError+=":: the axis is not valid";
475 throw GeomAlgoAPI_Exception(aError);
479 std::string aError = "Multirotation builder ";
480 aError+=":: the number of copies is null or negative.";
481 throw GeomAlgoAPI_Exception(aError);
484 double anAngle = 360./theNumber;
486 ListOfShape aListOfShape;
487 for (int i=0; i<theNumber; i++) {
489 push_back(GeomAlgoAPI_ShapeAPI::makeRotation(theSourceShape, theAxis, i*anAngle));
491 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
494 //===============================================================================================
495 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiRotation(
496 std::shared_ptr<GeomAPI_Shape> theSourceShape,
497 std::shared_ptr<GeomAPI_Ax1> theAxis,
498 const double theStep,
499 const int theNumber) throw (GeomAlgoAPI_Exception)
502 std::string aError = "Multirotation builder ";
503 aError+=":: the axis is not valid";
504 throw GeomAlgoAPI_Exception(aError);
508 std::string aError = "Multirotation builder ";
509 aError+=":: the number of copies is null or negative.";
510 throw GeomAlgoAPI_Exception(aError);
513 ListOfShape aListOfShape;
514 for (int i=0; i<theNumber; i++) {
516 push_back(GeomAlgoAPI_ShapeAPI::makeRotation(theSourceShape, theAxis, i*theStep));
518 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
521 //===============================================================================================
522 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
523 const double theRMin1, const double theRMax1,
524 const double theRMin2, const double theRMax2,
526 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
528 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
529 theZ, theStartPhi, theDeltaPhi);
531 static const std::string aMsg("Cone Segment builder");
532 return runAlgoAndCheckShape(aConeSegmentAlgo, aMsg);