1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_ShapeAPI.cpp
4 // Created: 17 Mar 2016
5 // Author: Clarisse Genrault (CEA)
7 #include "GeomAlgoAPI_ShapeAPI.h"
9 #include <GeomAlgoAPI_Box.h>
10 #include <GeomAlgoAPI_CompoundBuilder.h>
11 #include <GeomAlgoAPI_ConeSegment.h>
12 #include <GeomAlgoAPI_Cylinder.h>
13 #include <GeomAlgoAPI_EdgeBuilder.h>
14 #include <GeomAlgoAPI_Rotation.h>
15 #include <GeomAlgoAPI_Scale.h>
16 #include <GeomAlgoAPI_Sphere.h>
17 #include <GeomAlgoAPI_Symmetry.h>
18 #include <GeomAlgoAPI_Translation.h>
20 #include <GeomAPI_Lin.h>
24 namespace GeomAlgoAPI_ShapeAPI
26 //===============================================================================================
27 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
28 const double theDx, const double theDy,
29 const double theDz) throw (GeomAlgoAPI_Exception)
31 GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
33 if (!aBoxAlgo.check()) {
34 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
39 if(!aBoxAlgo.isDone()) {
40 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
42 if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
43 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
45 return aBoxAlgo.shape();
48 //===============================================================================================
49 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
50 std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
51 std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
53 GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
55 if (!aBoxAlgo.check()) {
56 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
61 if(!aBoxAlgo.isDone()) {
62 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
64 if (!aBoxAlgo.checkValid("Box builder with two points")) {
65 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
67 return aBoxAlgo.shape();
70 //===============================================================================================
71 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
72 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
73 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
75 // Check if the base point is OK
77 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
79 // Check if the edge is OK
81 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
84 std::shared_ptr<GeomAPI_Ax2> anAxis;
85 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
86 theEdge->line()->direction()));
88 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
90 if (!aCylinderAlgo.check()) {
91 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
94 aCylinderAlgo.build();
96 if(!aCylinderAlgo.isDone()) {
97 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
99 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
100 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
102 return aCylinderAlgo.shape();
105 //===============================================================================================
106 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
107 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
108 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
110 // Check if the base point is OK
112 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
114 // Check if the edge is OK
116 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
119 std::shared_ptr<GeomAPI_Ax2> anAxis;
120 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
121 theEdge->line()->direction()));
123 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
125 if (!aCylinderAlgo.check()) {
126 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
129 aCylinderAlgo.build();
131 if(!aCylinderAlgo.isDone()) {
132 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
134 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
135 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
137 return aCylinderAlgo.shape();
140 //===============================================================================================
141 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
142 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
144 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
145 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
146 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
147 std::shared_ptr<GeomAPI_Ax2> anAxis;
148 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
149 aEdge->line()->direction()));
151 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
153 if (!aCylinderAlgo.check()) {
154 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
157 aCylinderAlgo.build();
159 if(!aCylinderAlgo.isDone()) {
160 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
162 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
163 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
165 return aCylinderAlgo.shape();
168 //===============================================================================================
169 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
170 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
172 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
173 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
174 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
175 std::shared_ptr<GeomAPI_Ax2> anAxis;
176 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
177 aEdge->line()->direction()));
179 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
181 if (!aCylinderAlgo.check()) {
182 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
185 aCylinderAlgo.build();
187 if(!aCylinderAlgo.isDone()) {
188 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
190 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
191 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
193 return aCylinderAlgo.shape();
196 //===============================================================================================
197 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSphere(
198 std::shared_ptr<GeomAPI_Pnt> theCenterPoint, double theRadius) throw (GeomAlgoAPI_Exception)
200 GeomAlgoAPI_Sphere aSphereAlgo(theCenterPoint, theRadius);
202 if (!aSphereAlgo.check()) {
203 throw GeomAlgoAPI_Exception(aSphereAlgo.getError());
208 if(!aSphereAlgo.isDone()) {
209 throw GeomAlgoAPI_Exception(aSphereAlgo.getError());
212 if (!aSphereAlgo.checkValid("Sphere builder")) {
213 throw GeomAlgoAPI_Exception(aSphereAlgo.getError());
215 return aSphereAlgo.shape();
218 //===============================================================================================
219 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSphere(double theRadius)
220 throw (GeomAlgoAPI_Exception)
222 std::shared_ptr<GeomAPI_Pnt> aCenterPoint =
223 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
225 GeomAlgoAPI_Sphere aSphereAlgo(aCenterPoint, theRadius);
227 if (!aSphereAlgo.check()) {
228 throw GeomAlgoAPI_Exception(aSphereAlgo.getError());
233 if(!aSphereAlgo.isDone()) {
234 throw GeomAlgoAPI_Exception(aSphereAlgo.getError());
237 if (!aSphereAlgo.checkValid("Sphere builder")) {
238 throw GeomAlgoAPI_Exception(aSphereAlgo.getError());
240 return aSphereAlgo.shape();
243 //===============================================================================================
244 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
245 std::shared_ptr<GeomAPI_Shape> theSourceShape,
246 std::shared_ptr<GeomAPI_Ax1> theAxis,
247 const double theDistance) throw (GeomAlgoAPI_Exception)
249 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
251 if (!aTranslationAlgo.check()) {
252 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
255 aTranslationAlgo.build();
257 if(!aTranslationAlgo.isDone()) {
258 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
261 return aTranslationAlgo.shape();
264 //===============================================================================================
265 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
266 std::shared_ptr<GeomAPI_Shape> theSourceShape,
269 const double theDz) throw (GeomAlgoAPI_Exception)
271 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
273 if (!aTranslationAlgo.check()) {
274 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
277 aTranslationAlgo.build();
279 if(!aTranslationAlgo.isDone()) {
280 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
283 return aTranslationAlgo.shape();
286 //===============================================================================================
287 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
288 std::shared_ptr<GeomAPI_Shape> theSourceShape,
289 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
290 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
292 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
294 if (!aTranslationAlgo.check()) {
295 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
298 aTranslationAlgo.build();
300 if(!aTranslationAlgo.isDone()) {
301 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
304 return aTranslationAlgo.shape();
307 //===============================================================================================
308 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
309 std::shared_ptr<GeomAPI_Shape> theSourceShape,
310 std::shared_ptr<GeomAPI_Ax1> theAxis,
311 const double theAngle) throw (GeomAlgoAPI_Exception)
313 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
315 if (!aRotationAlgo.check()) {
316 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
319 aRotationAlgo.build();
321 if(!aRotationAlgo.isDone()) {
322 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
325 return aRotationAlgo.shape();
328 //===============================================================================================
329 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
330 std::shared_ptr<GeomAPI_Shape> theSourceShape,
331 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
332 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
333 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
335 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
337 if (!aRotationAlgo.check()) {
338 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
341 aRotationAlgo.build();
343 if(!aRotationAlgo.isDone()) {
344 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
347 return aRotationAlgo.shape();
350 //===============================================================================================
351 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
352 std::shared_ptr<GeomAPI_Shape> theSourceShape,
353 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
355 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
357 if (!aSymmetryAlgo.check()) {
358 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
361 aSymmetryAlgo.build();
363 if(!aSymmetryAlgo.isDone()) {
364 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
367 return aSymmetryAlgo.shape();
370 //===============================================================================================
371 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
372 std::shared_ptr<GeomAPI_Shape> theSourceShape,
373 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
375 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
377 if (!aSymmetryAlgo.check()) {
378 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
381 aSymmetryAlgo.build();
383 if(!aSymmetryAlgo.isDone()) {
384 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
387 return aSymmetryAlgo.shape();
390 //===============================================================================================
391 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
392 std::shared_ptr<GeomAPI_Shape> theSourceShape,
393 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
395 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
397 if (!aSymmetryAlgo.check()) {
398 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
401 aSymmetryAlgo.build();
403 if(!aSymmetryAlgo.isDone()) {
404 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
407 return aSymmetryAlgo.shape();
410 //===============================================================================================
411 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
412 std::shared_ptr<GeomAPI_Shape> theSourceShape,
413 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
414 const double theScaleFactor) throw (GeomAlgoAPI_Exception)
416 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
418 if (!aScaleAlgo.check()) {
419 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
424 if(!aScaleAlgo.isDone()) {
425 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
428 return aScaleAlgo.shape();
431 //===============================================================================================
432 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
433 std::shared_ptr<GeomAPI_Shape> theSourceShape,
434 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
435 const double theScaleFactorX,
436 const double theScaleFactorY,
437 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
439 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
440 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
442 if (!aScaleAlgo.check()) {
443 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
448 if(!aScaleAlgo.isDone()) {
449 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
452 return aScaleAlgo.shape();
455 //===============================================================================================
456 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
457 std::shared_ptr<GeomAPI_Shape> theSourceShape,
458 std::shared_ptr<GeomAPI_Ax1> theAxis,
459 const double theStep,
460 const int theNumber) throw (GeomAlgoAPI_Exception)
463 std::string aError = "Multitranslation builder ";
464 aError+=":: the number of copies for the first direction is null or negative.";
465 throw GeomAlgoAPI_Exception(aError);
468 ListOfShape aListOfShape;
469 for (int i=0; i<theNumber; i++) {
471 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
473 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
476 //===============================================================================================
477 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
478 std::shared_ptr<GeomAPI_Shape> theSourceShape,
479 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
480 const double theFirstStep,
481 const int theFirstNumber,
482 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
483 const double theSecondStep,
484 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
486 if (theFirstNumber <=0) {
487 std::string aError = "Multitranslation builder ";
488 aError+=":: the number of copies for the first direction is null or negative.";
489 throw GeomAlgoAPI_Exception(aError);
492 if (theSecondNumber <=0) {
493 std::string aError = "Multitranslation builder ";
494 aError+=":: the number of copies for the second direction is null or negative.";
495 throw GeomAlgoAPI_Exception(aError);
498 // Coord theFirstAxis
499 double x1 = theFirstAxis->dir()->x();
500 double y1 = theFirstAxis->dir()->y();
501 double z1 = theFirstAxis->dir()->z();
502 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
504 // Coord theSecondAxis
505 double x2 = theSecondAxis->dir()->x();
506 double y2 = theSecondAxis->dir()->y();
507 double z2 = theSecondAxis->dir()->z();
508 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
510 ListOfShape aListOfShape;
511 for (int j=0; j<theSecondStep; j++) {
512 for (int i=0; i<theFirstNumber; i++) {
513 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
514 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
515 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
516 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
519 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
522 //===============================================================================================
523 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
524 const double theRMin1, const double theRMax1,
525 const double theRMin2, const double theRMax2,
527 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
529 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
530 theZ, theStartPhi, theDeltaPhi);
532 if (!aConeSegmentAlgo.check()) {
533 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
536 aConeSegmentAlgo.build();
538 if(!aConeSegmentAlgo.isDone()) {
539 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
541 if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
542 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
544 return aConeSegmentAlgo.shape();