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());
213 return aTranslationAlgo.shape();
216 //===============================================================================================
217 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
218 std::shared_ptr<GeomAPI_Shape> theSourceShape,
221 const double theDz) throw (GeomAlgoAPI_Exception)
223 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
225 if (!aTranslationAlgo.check()) {
226 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
229 aTranslationAlgo.build();
231 if(!aTranslationAlgo.isDone()) {
232 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
235 return aTranslationAlgo.shape();
238 //===============================================================================================
239 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
240 std::shared_ptr<GeomAPI_Shape> theSourceShape,
241 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
242 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
244 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
246 if (!aTranslationAlgo.check()) {
247 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
250 aTranslationAlgo.build();
252 if(!aTranslationAlgo.isDone()) {
253 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
256 return aTranslationAlgo.shape();
259 //===============================================================================================
260 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
261 std::shared_ptr<GeomAPI_Shape> theSourceShape,
262 std::shared_ptr<GeomAPI_Ax1> theAxis,
263 const double theAngle) throw (GeomAlgoAPI_Exception)
265 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
267 if (!aRotationAlgo.check()) {
268 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
271 aRotationAlgo.build();
273 if(!aRotationAlgo.isDone()) {
274 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
277 return aRotationAlgo.shape();
280 //===============================================================================================
281 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
282 std::shared_ptr<GeomAPI_Shape> theSourceShape,
283 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
284 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
285 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
287 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
289 if (!aRotationAlgo.check()) {
290 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
293 aRotationAlgo.build();
295 if(!aRotationAlgo.isDone()) {
296 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
299 return aRotationAlgo.shape();
302 //===============================================================================================
303 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
304 std::shared_ptr<GeomAPI_Shape> theSourceShape,
305 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
307 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
309 if (!aSymmetryAlgo.check()) {
310 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
313 aSymmetryAlgo.build();
315 if(!aSymmetryAlgo.isDone()) {
316 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
319 return aSymmetryAlgo.shape();
322 //===============================================================================================
323 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
324 std::shared_ptr<GeomAPI_Shape> theSourceShape,
325 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
327 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
329 if (!aSymmetryAlgo.check()) {
330 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
333 aSymmetryAlgo.build();
335 if(!aSymmetryAlgo.isDone()) {
336 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
339 return aSymmetryAlgo.shape();
342 //===============================================================================================
343 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
344 std::shared_ptr<GeomAPI_Shape> theSourceShape,
345 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
347 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
349 if (!aSymmetryAlgo.check()) {
350 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
353 aSymmetryAlgo.build();
355 if(!aSymmetryAlgo.isDone()) {
356 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
359 return aSymmetryAlgo.shape();
362 //===============================================================================================
363 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
364 std::shared_ptr<GeomAPI_Shape> theSourceShape,
365 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
366 const double theScaleFactor) throw (GeomAlgoAPI_Exception)
368 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
370 if (!aScaleAlgo.check()) {
371 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
376 if(!aScaleAlgo.isDone()) {
377 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
380 return aScaleAlgo.shape();
383 //===============================================================================================
384 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
385 std::shared_ptr<GeomAPI_Shape> theSourceShape,
386 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
387 const double theScaleFactorX,
388 const double theScaleFactorY,
389 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
391 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
392 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
394 if (!aScaleAlgo.check()) {
395 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
400 if(!aScaleAlgo.isDone()) {
401 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
404 return aScaleAlgo.shape();
407 //===============================================================================================
408 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
409 std::shared_ptr<GeomAPI_Shape> theSourceShape,
410 std::shared_ptr<GeomAPI_Ax1> theAxis,
411 const double theStep,
412 const int theNumber) throw (GeomAlgoAPI_Exception)
415 std::string aError = "Multitranslation builder ";
416 aError+=":: the first axis is not valid";
417 throw GeomAlgoAPI_Exception(aError);
421 std::string aError = "Multitranslation builder ";
422 aError+=":: the number of copies for the first direction is null or negative.";
423 throw GeomAlgoAPI_Exception(aError);
426 ListOfShape aListOfShape;
427 for (int i=0; i<theNumber; i++) {
429 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
431 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
434 //===============================================================================================
435 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
436 std::shared_ptr<GeomAPI_Shape> theSourceShape,
437 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
438 const double theFirstStep,
439 const int theFirstNumber,
440 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
441 const double theSecondStep,
442 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
445 std::string aError = "Multitranslation builder ";
446 aError+=":: the first axis is not valid";
447 throw GeomAlgoAPI_Exception(aError);
450 if (!theSecondAxis) {
451 std::string aError = "Multitranslation builder ";
452 aError+=":: the second axis is not valid";
453 throw GeomAlgoAPI_Exception(aError);
456 if (theFirstNumber <=0) {
457 std::string aError = "Multitranslation builder ";
458 aError+=":: the number of copies for the first direction is null or negative.";
459 throw GeomAlgoAPI_Exception(aError);
462 if (theSecondNumber <=0) {
463 std::string aError = "Multitranslation builder ";
464 aError+=":: the number of copies for the second direction is null or negative.";
465 throw GeomAlgoAPI_Exception(aError);
468 // Coord theFirstAxis
469 double x1 = theFirstAxis->dir()->x();
470 double y1 = theFirstAxis->dir()->y();
471 double z1 = theFirstAxis->dir()->z();
472 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
474 // Coord theSecondAxis
475 double x2 = theSecondAxis->dir()->x();
476 double y2 = theSecondAxis->dir()->y();
477 double z2 = theSecondAxis->dir()->z();
478 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
480 ListOfShape aListOfShape;
481 for (int j=0; j<theSecondStep; j++) {
482 for (int i=0; i<theFirstNumber; i++) {
483 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
484 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
485 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
486 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
489 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
492 //===============================================================================================
493 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
494 const double theRMin1, const double theRMax1,
495 const double theRMin2, const double theRMax2,
497 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
499 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
500 theZ, theStartPhi, theDeltaPhi);
502 if (!aConeSegmentAlgo.check()) {
503 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
506 aConeSegmentAlgo.build();
508 if(!aConeSegmentAlgo.isDone()) {
509 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
511 if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
512 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
514 return aConeSegmentAlgo.shape();