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_Cylinder.h>
11 #include <GeomAlgoAPI_CompoundBuilder.h>
12 #include <GeomAlgoAPI_ConeSegment.h>
13 #include <GeomAlgoAPI_EdgeBuilder.h>
14 #include <GeomAlgoAPI_Rotation.h>
15 #include <GeomAlgoAPI_Scale.h>
16 #include <GeomAlgoAPI_Symmetry.h>
17 #include <GeomAlgoAPI_Translation.h>
19 #include <GeomAPI_Lin.h>
23 namespace GeomAlgoAPI_ShapeAPI
25 //===============================================================================================
26 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
27 const double theDx, const double theDy,
28 const double theDz) throw (GeomAlgoAPI_Exception)
30 GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
32 if (!aBoxAlgo.check()) {
33 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
38 if(!aBoxAlgo.isDone()) {
39 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
41 if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
42 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
44 return aBoxAlgo.shape();
47 //===============================================================================================
48 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
49 std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
50 std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
52 GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
54 if (!aBoxAlgo.check()) {
55 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
60 if(!aBoxAlgo.isDone()) {
61 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
63 if (!aBoxAlgo.checkValid("Box builder with two points")) {
64 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
66 return aBoxAlgo.shape();
69 //===============================================================================================
70 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
71 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
72 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
74 // Check if the base point is OK
76 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
78 // Check if the edge is OK
80 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
83 std::shared_ptr<GeomAPI_Ax2> anAxis;
84 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
85 theEdge->line()->direction()));
87 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
89 if (!aCylinderAlgo.check()) {
90 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
93 aCylinderAlgo.build();
95 if(!aCylinderAlgo.isDone()) {
96 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
98 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
99 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
101 return aCylinderAlgo.shape();
104 //===============================================================================================
105 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
106 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
107 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
109 // Check if the base point is OK
111 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
113 // Check if the edge is OK
115 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
118 std::shared_ptr<GeomAPI_Ax2> anAxis;
119 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
120 theEdge->line()->direction()));
122 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
124 if (!aCylinderAlgo.check()) {
125 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
128 aCylinderAlgo.build();
130 if(!aCylinderAlgo.isDone()) {
131 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
133 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
134 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
136 return aCylinderAlgo.shape();
139 //===============================================================================================
140 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
141 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
143 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
144 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
145 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
146 std::shared_ptr<GeomAPI_Ax2> anAxis;
147 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
148 aEdge->line()->direction()));
150 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
152 if (!aCylinderAlgo.check()) {
153 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
156 aCylinderAlgo.build();
158 if(!aCylinderAlgo.isDone()) {
159 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
161 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
162 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
164 return aCylinderAlgo.shape();
167 //===============================================================================================
168 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
169 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
171 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
172 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
173 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
174 std::shared_ptr<GeomAPI_Ax2> anAxis;
175 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
176 aEdge->line()->direction()));
178 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
180 if (!aCylinderAlgo.check()) {
181 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
184 aCylinderAlgo.build();
186 if(!aCylinderAlgo.isDone()) {
187 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
189 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
190 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
192 return aCylinderAlgo.shape();
195 //===============================================================================================
196 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
197 std::shared_ptr<GeomAPI_Shape> theSourceShape,
198 std::shared_ptr<GeomAPI_Ax1> theAxis,
199 const double theDistance) throw (GeomAlgoAPI_Exception)
201 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
203 if (!aTranslationAlgo.check()) {
204 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
207 aTranslationAlgo.build();
209 if(!aTranslationAlgo.isDone()) {
210 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
212 if (!aTranslationAlgo.checkValid("Translation builder with axis and distance")) {
213 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
215 return aTranslationAlgo.shape();
218 //===============================================================================================
219 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
220 std::shared_ptr<GeomAPI_Shape> theSourceShape,
223 const double theDz) throw (GeomAlgoAPI_Exception)
225 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
227 if (!aTranslationAlgo.check()) {
228 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
231 aTranslationAlgo.build();
233 if(!aTranslationAlgo.isDone()) {
234 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
236 if (!aTranslationAlgo.checkValid("Translation builder with dimensions")) {
237 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
239 return aTranslationAlgo.shape();
242 //===============================================================================================
243 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
244 std::shared_ptr<GeomAPI_Shape> theSourceShape,
245 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
246 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
248 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
250 if (!aTranslationAlgo.check()) {
251 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
254 aTranslationAlgo.build();
256 if(!aTranslationAlgo.isDone()) {
257 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
259 if (!aTranslationAlgo.checkValid("Translation builder with two points")) {
260 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
262 return aTranslationAlgo.shape();
265 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
266 std::shared_ptr<GeomAPI_Shape> theSourceShape,
267 std::shared_ptr<GeomAPI_Ax1> theAxis,
268 const double theAngle) throw (GeomAlgoAPI_Exception)
270 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
272 if (!aRotationAlgo.check()) {
273 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
276 aRotationAlgo.build();
278 if(!aRotationAlgo.isDone()) {
279 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
281 if (!aRotationAlgo.checkValid("Rotation builder with two points")) {
282 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
284 return aRotationAlgo.shape();
287 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
288 std::shared_ptr<GeomAPI_Shape> theSourceShape,
289 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
290 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
291 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
293 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
295 if (!aRotationAlgo.check()) {
296 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
299 aRotationAlgo.build();
301 if(!aRotationAlgo.isDone()) {
302 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
304 if (!aRotationAlgo.checkValid("Rotation builder with two points")) {
305 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
307 return aRotationAlgo.shape();
310 //===============================================================================================
311 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
312 std::shared_ptr<GeomAPI_Shape> theSourceShape,
313 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
315 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
317 if (!aSymmetryAlgo.check()) {
318 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
321 aSymmetryAlgo.build();
323 if(!aSymmetryAlgo.isDone()) {
324 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
326 if (!aSymmetryAlgo.checkValid("Symmetry builder by a point")) {
327 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
329 return aSymmetryAlgo.shape();
332 //===============================================================================================
333 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
334 std::shared_ptr<GeomAPI_Shape> theSourceShape,
335 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
337 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
339 if (!aSymmetryAlgo.check()) {
340 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
343 aSymmetryAlgo.build();
345 if(!aSymmetryAlgo.isDone()) {
346 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
348 if (!aSymmetryAlgo.checkValid("Symmetry builder by an axis")) {
349 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
351 return aSymmetryAlgo.shape();
354 //===============================================================================================
355 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
356 std::shared_ptr<GeomAPI_Shape> theSourceShape,
357 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
359 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
361 if (!aSymmetryAlgo.check()) {
362 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
365 aSymmetryAlgo.build();
367 if(!aSymmetryAlgo.isDone()) {
368 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
370 if (!aSymmetryAlgo.checkValid("Symmetry builder by a plane")) {
371 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
373 return aSymmetryAlgo.shape();
376 //===============================================================================================
377 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
378 std::shared_ptr<GeomAPI_Shape> theSourceShape,
379 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
380 const double theScaleFactor) throw (GeomAlgoAPI_Exception)
382 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
384 if (!aScaleAlgo.check()) {
385 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
390 if(!aScaleAlgo.isDone()) {
391 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
393 if (!aScaleAlgo.checkValid("Scale builder by a scale factor")) {
394 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
396 return aScaleAlgo.shape();
399 //===============================================================================================
400 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
401 std::shared_ptr<GeomAPI_Shape> theSourceShape,
402 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
403 const double theScaleFactorX,
404 const double theScaleFactorY,
405 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
407 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
408 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
410 if (!aScaleAlgo.check()) {
411 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
416 if(!aScaleAlgo.isDone()) {
417 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
419 if (!aScaleAlgo.checkValid("Scale builder by dimensions")) {
420 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
422 return aScaleAlgo.shape();
425 //===============================================================================================
426 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
427 std::shared_ptr<GeomAPI_Shape> theSourceShape,
428 std::shared_ptr<GeomAPI_Ax1> theAxis,
429 const double theStep,
430 const int theNumber) throw (GeomAlgoAPI_Exception)
432 ListOfShape aListOfShape;
433 for (int i=0; i<theNumber; i++) {
435 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
437 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
440 //===============================================================================================
441 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
442 std::shared_ptr<GeomAPI_Shape> theSourceShape,
443 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
444 const double theFirstStep,
445 const int theFirstNumber,
446 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
447 const double theSecondStep,
448 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
450 // Coord theFirstAxis
451 double x1 = theFirstAxis->dir()->x();
452 double y1 = theFirstAxis->dir()->y();
453 double z1 = theFirstAxis->dir()->z();
454 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
456 // Coord theSecondAxis
457 double x2 = theSecondAxis->dir()->x();
458 double y2 = theSecondAxis->dir()->y();
459 double z2 = theSecondAxis->dir()->z();
460 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
462 ListOfShape aListOfShape;
463 for (int j=0; j<theSecondStep; j++) {
464 for (int i=0; i<theFirstNumber; i++) {
465 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
466 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
467 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
468 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
471 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
474 //===============================================================================================
475 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
476 const double theRMin1, const double theRMax1,
477 const double theRMin2, const double theRMax2,
479 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
481 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
482 theZ, theStartPhi, theDeltaPhi);
484 if (!aConeSegmentAlgo.check()) {
485 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
488 aConeSegmentAlgo.build();
490 if(!aConeSegmentAlgo.isDone()) {
491 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
493 if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
494 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
496 return aConeSegmentAlgo.shape();