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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "GeomAlgoAPI_ShapeAPI.h"
22 #include <GeomAlgoAPI_Box.h>
23 #include <GeomAlgoAPI_Cylinder.h>
24 #include <GeomAlgoAPI_CompoundBuilder.h>
25 #include <GeomAlgoAPI_ConeSegment.h>
26 #include <GeomAlgoAPI_EdgeBuilder.h>
27 #include <GeomAlgoAPI_Rotation.h>
28 #include <GeomAlgoAPI_Scale.h>
29 #include <GeomAlgoAPI_Symmetry.h>
30 #include <GeomAlgoAPI_Translation.h>
32 #include <GeomAPI_Lin.h>
36 namespace GeomAlgoAPI_ShapeAPI
38 //===============================================================================================
39 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
40 const double theDx, const double theDy,
41 const double theDz) throw (GeomAlgoAPI_Exception)
43 GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
45 if (!aBoxAlgo.check()) {
46 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
51 if(!aBoxAlgo.isDone()) {
52 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
54 if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
55 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
57 return aBoxAlgo.shape();
60 //===============================================================================================
61 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
62 std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
63 std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
65 GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
67 if (!aBoxAlgo.check()) {
68 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
73 if(!aBoxAlgo.isDone()) {
74 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
76 if (!aBoxAlgo.checkValid("Box builder with two points")) {
77 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
79 return aBoxAlgo.shape();
82 //===============================================================================================
83 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
84 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
85 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
87 // Check if the base point is OK
89 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
91 // Check if the edge is OK
93 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
96 std::shared_ptr<GeomAPI_Ax2> anAxis;
97 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
98 theEdge->line()->direction()));
100 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
102 if (!aCylinderAlgo.check()) {
103 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
106 aCylinderAlgo.build();
108 if(!aCylinderAlgo.isDone()) {
109 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
111 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
112 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
114 return aCylinderAlgo.shape();
117 //===============================================================================================
118 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
119 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
120 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
122 // Check if the base point is OK
124 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
126 // Check if the edge is OK
128 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
131 std::shared_ptr<GeomAPI_Ax2> anAxis;
132 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
133 theEdge->line()->direction()));
135 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
137 if (!aCylinderAlgo.check()) {
138 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
141 aCylinderAlgo.build();
143 if(!aCylinderAlgo.isDone()) {
144 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
146 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
147 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
149 return aCylinderAlgo.shape();
152 //===============================================================================================
153 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
154 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
156 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
157 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
158 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
159 std::shared_ptr<GeomAPI_Ax2> anAxis;
160 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
161 aEdge->line()->direction()));
163 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
165 if (!aCylinderAlgo.check()) {
166 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
169 aCylinderAlgo.build();
171 if(!aCylinderAlgo.isDone()) {
172 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
174 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
175 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
177 return aCylinderAlgo.shape();
180 //===============================================================================================
181 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
182 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
184 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
185 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
186 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
187 std::shared_ptr<GeomAPI_Ax2> anAxis;
188 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
189 aEdge->line()->direction()));
191 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
193 if (!aCylinderAlgo.check()) {
194 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
197 aCylinderAlgo.build();
199 if(!aCylinderAlgo.isDone()) {
200 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
202 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
203 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
205 return aCylinderAlgo.shape();
208 //===============================================================================================
209 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
210 std::shared_ptr<GeomAPI_Shape> theSourceShape,
211 std::shared_ptr<GeomAPI_Ax1> theAxis,
212 const double theDistance) throw (GeomAlgoAPI_Exception)
214 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
216 if (!aTranslationAlgo.check()) {
217 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
220 aTranslationAlgo.build();
222 if(!aTranslationAlgo.isDone()) {
223 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
226 return aTranslationAlgo.shape();
229 //===============================================================================================
230 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
231 std::shared_ptr<GeomAPI_Shape> theSourceShape,
234 const double theDz) throw (GeomAlgoAPI_Exception)
236 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
238 if (!aTranslationAlgo.check()) {
239 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
242 aTranslationAlgo.build();
244 if(!aTranslationAlgo.isDone()) {
245 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
248 return aTranslationAlgo.shape();
251 //===============================================================================================
252 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
253 std::shared_ptr<GeomAPI_Shape> theSourceShape,
254 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
255 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
257 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
259 if (!aTranslationAlgo.check()) {
260 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
263 aTranslationAlgo.build();
265 if(!aTranslationAlgo.isDone()) {
266 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
269 return aTranslationAlgo.shape();
272 //===============================================================================================
273 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
274 std::shared_ptr<GeomAPI_Shape> theSourceShape,
275 std::shared_ptr<GeomAPI_Ax1> theAxis,
276 const double theAngle) throw (GeomAlgoAPI_Exception)
278 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
280 if (!aRotationAlgo.check()) {
281 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
284 aRotationAlgo.build();
286 if(!aRotationAlgo.isDone()) {
287 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
290 return aRotationAlgo.shape();
293 //===============================================================================================
294 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
295 std::shared_ptr<GeomAPI_Shape> theSourceShape,
296 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
297 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
298 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
300 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
302 if (!aRotationAlgo.check()) {
303 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
306 aRotationAlgo.build();
308 if(!aRotationAlgo.isDone()) {
309 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
312 return aRotationAlgo.shape();
315 //===============================================================================================
316 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
317 std::shared_ptr<GeomAPI_Shape> theSourceShape,
318 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
320 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
322 if (!aSymmetryAlgo.check()) {
323 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
326 aSymmetryAlgo.build();
328 if(!aSymmetryAlgo.isDone()) {
329 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
332 return aSymmetryAlgo.shape();
335 //===============================================================================================
336 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
337 std::shared_ptr<GeomAPI_Shape> theSourceShape,
338 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
340 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
342 if (!aSymmetryAlgo.check()) {
343 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
346 aSymmetryAlgo.build();
348 if(!aSymmetryAlgo.isDone()) {
349 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
352 return aSymmetryAlgo.shape();
355 //===============================================================================================
356 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
357 std::shared_ptr<GeomAPI_Shape> theSourceShape,
358 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
360 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
362 if (!aSymmetryAlgo.check()) {
363 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
366 aSymmetryAlgo.build();
368 if(!aSymmetryAlgo.isDone()) {
369 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
372 return aSymmetryAlgo.shape();
375 //===============================================================================================
376 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
377 std::shared_ptr<GeomAPI_Shape> theSourceShape,
378 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
379 const double theScaleFactor) throw (GeomAlgoAPI_Exception)
381 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
383 if (!aScaleAlgo.check()) {
384 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
389 if(!aScaleAlgo.isDone()) {
390 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
393 return aScaleAlgo.shape();
396 //===============================================================================================
397 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
398 std::shared_ptr<GeomAPI_Shape> theSourceShape,
399 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
400 const double theScaleFactorX,
401 const double theScaleFactorY,
402 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
404 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
405 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
407 if (!aScaleAlgo.check()) {
408 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
413 if(!aScaleAlgo.isDone()) {
414 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
417 return aScaleAlgo.shape();
420 //===============================================================================================
421 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
422 std::shared_ptr<GeomAPI_Shape> theSourceShape,
423 std::shared_ptr<GeomAPI_Ax1> theAxis,
424 const double theStep,
425 const int theNumber) throw (GeomAlgoAPI_Exception)
428 std::string aError = "Multitranslation builder ";
429 aError+=":: the first axis is not valid";
430 throw GeomAlgoAPI_Exception(aError);
434 std::string aError = "Multitranslation builder ";
435 aError+=":: the number of copies for the first direction is null or negative.";
436 throw GeomAlgoAPI_Exception(aError);
439 ListOfShape aListOfShape;
440 for (int i=0; i<theNumber; i++) {
442 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
444 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
447 //===============================================================================================
448 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
449 std::shared_ptr<GeomAPI_Shape> theSourceShape,
450 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
451 const double theFirstStep,
452 const int theFirstNumber,
453 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
454 const double theSecondStep,
455 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
458 std::string aError = "Multitranslation builder ";
459 aError+=":: the first axis is not valid";
460 throw GeomAlgoAPI_Exception(aError);
463 if (!theSecondAxis) {
464 std::string aError = "Multitranslation builder ";
465 aError+=":: the second axis is not valid";
466 throw GeomAlgoAPI_Exception(aError);
469 if (theFirstNumber <=0) {
470 std::string aError = "Multitranslation builder ";
471 aError+=":: the number of copies for the first direction is null or negative.";
472 throw GeomAlgoAPI_Exception(aError);
475 if (theSecondNumber <=0) {
476 std::string aError = "Multitranslation builder ";
477 aError+=":: the number of copies for the second direction is null or negative.";
478 throw GeomAlgoAPI_Exception(aError);
481 // Coord theFirstAxis
482 double x1 = theFirstAxis->dir()->x();
483 double y1 = theFirstAxis->dir()->y();
484 double z1 = theFirstAxis->dir()->z();
485 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
487 // Coord theSecondAxis
488 double x2 = theSecondAxis->dir()->x();
489 double y2 = theSecondAxis->dir()->y();
490 double z2 = theSecondAxis->dir()->z();
491 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
493 ListOfShape aListOfShape;
494 for (int j=0; j<theSecondStep; j++) {
495 for (int i=0; i<theFirstNumber; i++) {
496 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
497 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
498 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
499 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
502 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
505 //===============================================================================================
506 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
507 const double theRMin1, const double theRMax1,
508 const double theRMin2, const double theRMax2,
510 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
512 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
513 theZ, theStartPhi, theDeltaPhi);
515 if (!aConeSegmentAlgo.check()) {
516 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
519 aConeSegmentAlgo.build();
521 if(!aConeSegmentAlgo.isDone()) {
522 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
524 if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
525 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
527 return aConeSegmentAlgo.shape();