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_Cylinder.h>
25 #include <GeomAlgoAPI_CompoundBuilder.h>
26 #include <GeomAlgoAPI_ConeSegment.h>
27 #include <GeomAlgoAPI_EdgeBuilder.h>
28 #include <GeomAlgoAPI_Rotation.h>
29 #include <GeomAlgoAPI_Scale.h>
30 #include <GeomAlgoAPI_Symmetry.h>
31 #include <GeomAlgoAPI_Translation.h>
33 #include <GeomAPI_Lin.h>
37 namespace GeomAlgoAPI_ShapeAPI
39 //===============================================================================================
40 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
41 const double theDx, const double theDy,
42 const double theDz) throw (GeomAlgoAPI_Exception)
44 GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
46 if (!aBoxAlgo.check()) {
47 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
52 if(!aBoxAlgo.isDone()) {
53 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
55 if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
56 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
58 return aBoxAlgo.shape();
61 //===============================================================================================
62 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
63 std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
64 std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
66 GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
68 if (!aBoxAlgo.check()) {
69 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
74 if(!aBoxAlgo.isDone()) {
75 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
77 if (!aBoxAlgo.checkValid("Box builder with two points")) {
78 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
80 return aBoxAlgo.shape();
83 //===============================================================================================
84 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
85 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
86 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
88 // Check if the base point is OK
90 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
92 // Check if the edge is OK
94 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
97 std::shared_ptr<GeomAPI_Ax2> anAxis;
98 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
99 theEdge->line()->direction()));
101 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
103 if (!aCylinderAlgo.check()) {
104 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
107 aCylinderAlgo.build();
109 if(!aCylinderAlgo.isDone()) {
110 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
112 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
113 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
115 return aCylinderAlgo.shape();
118 //===============================================================================================
119 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
120 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
121 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
123 // Check if the base point is OK
125 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
127 // Check if the edge is OK
129 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
132 std::shared_ptr<GeomAPI_Ax2> anAxis;
133 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
134 theEdge->line()->direction()));
136 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
138 if (!aCylinderAlgo.check()) {
139 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
142 aCylinderAlgo.build();
144 if(!aCylinderAlgo.isDone()) {
145 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
147 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
148 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
150 return aCylinderAlgo.shape();
153 //===============================================================================================
154 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
155 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
157 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
158 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
159 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
160 std::shared_ptr<GeomAPI_Ax2> anAxis;
161 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
162 aEdge->line()->direction()));
164 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
166 if (!aCylinderAlgo.check()) {
167 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
170 aCylinderAlgo.build();
172 if(!aCylinderAlgo.isDone()) {
173 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
175 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
176 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
178 return aCylinderAlgo.shape();
181 //===============================================================================================
182 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
183 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
185 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
186 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
187 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
188 std::shared_ptr<GeomAPI_Ax2> anAxis;
189 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
190 aEdge->line()->direction()));
192 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
194 if (!aCylinderAlgo.check()) {
195 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
198 aCylinderAlgo.build();
200 if(!aCylinderAlgo.isDone()) {
201 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
203 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
204 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
206 return aCylinderAlgo.shape();
209 //===============================================================================================
210 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
211 std::shared_ptr<GeomAPI_Shape> theSourceShape,
212 std::shared_ptr<GeomAPI_Ax1> theAxis,
213 const double theDistance) throw (GeomAlgoAPI_Exception)
215 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
217 if (!aTranslationAlgo.check()) {
218 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
221 aTranslationAlgo.build();
223 if(!aTranslationAlgo.isDone()) {
224 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
227 return aTranslationAlgo.shape();
230 //===============================================================================================
231 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
232 std::shared_ptr<GeomAPI_Shape> theSourceShape,
235 const double theDz) throw (GeomAlgoAPI_Exception)
237 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
239 if (!aTranslationAlgo.check()) {
240 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
243 aTranslationAlgo.build();
245 if(!aTranslationAlgo.isDone()) {
246 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
249 return aTranslationAlgo.shape();
252 //===============================================================================================
253 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
254 std::shared_ptr<GeomAPI_Shape> theSourceShape,
255 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
256 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
258 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
260 if (!aTranslationAlgo.check()) {
261 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
264 aTranslationAlgo.build();
266 if(!aTranslationAlgo.isDone()) {
267 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
270 return aTranslationAlgo.shape();
273 //===============================================================================================
274 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
275 std::shared_ptr<GeomAPI_Shape> theSourceShape,
276 std::shared_ptr<GeomAPI_Ax1> theAxis,
277 const double theAngle) throw (GeomAlgoAPI_Exception)
279 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
281 if (!aRotationAlgo.check()) {
282 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
285 aRotationAlgo.build();
287 if(!aRotationAlgo.isDone()) {
288 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
291 return aRotationAlgo.shape();
294 //===============================================================================================
295 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
296 std::shared_ptr<GeomAPI_Shape> theSourceShape,
297 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
298 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
299 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
301 GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
303 if (!aRotationAlgo.check()) {
304 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
307 aRotationAlgo.build();
309 if(!aRotationAlgo.isDone()) {
310 throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
313 return aRotationAlgo.shape();
316 //===============================================================================================
317 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
318 std::shared_ptr<GeomAPI_Shape> theSourceShape,
319 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
321 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
323 if (!aSymmetryAlgo.check()) {
324 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
327 aSymmetryAlgo.build();
329 if(!aSymmetryAlgo.isDone()) {
330 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
333 return aSymmetryAlgo.shape();
336 //===============================================================================================
337 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
338 std::shared_ptr<GeomAPI_Shape> theSourceShape,
339 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
341 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
343 if (!aSymmetryAlgo.check()) {
344 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
347 aSymmetryAlgo.build();
349 if(!aSymmetryAlgo.isDone()) {
350 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
353 return aSymmetryAlgo.shape();
356 //===============================================================================================
357 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
358 std::shared_ptr<GeomAPI_Shape> theSourceShape,
359 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
361 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
363 if (!aSymmetryAlgo.check()) {
364 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
367 aSymmetryAlgo.build();
369 if(!aSymmetryAlgo.isDone()) {
370 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());
394 return aScaleAlgo.shape();
397 //===============================================================================================
398 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
399 std::shared_ptr<GeomAPI_Shape> theSourceShape,
400 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
401 const double theScaleFactorX,
402 const double theScaleFactorY,
403 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
405 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
406 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
408 if (!aScaleAlgo.check()) {
409 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
414 if(!aScaleAlgo.isDone()) {
415 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
418 return aScaleAlgo.shape();
421 //===============================================================================================
422 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
423 std::shared_ptr<GeomAPI_Shape> theSourceShape,
424 std::shared_ptr<GeomAPI_Ax1> theAxis,
425 const double theStep,
426 const int theNumber) throw (GeomAlgoAPI_Exception)
429 std::string aError = "Multitranslation builder ";
430 aError+=":: the first axis is not valid";
431 throw GeomAlgoAPI_Exception(aError);
435 std::string aError = "Multitranslation builder ";
436 aError+=":: the number of copies for the first direction is null or negative.";
437 throw GeomAlgoAPI_Exception(aError);
440 ListOfShape aListOfShape;
441 for (int i=0; i<theNumber; i++) {
443 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
445 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
448 //===============================================================================================
449 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
450 std::shared_ptr<GeomAPI_Shape> theSourceShape,
451 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
452 const double theFirstStep,
453 const int theFirstNumber,
454 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
455 const double theSecondStep,
456 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
459 std::string aError = "Multitranslation builder ";
460 aError+=":: the first axis is not valid";
461 throw GeomAlgoAPI_Exception(aError);
464 if (!theSecondAxis) {
465 std::string aError = "Multitranslation builder ";
466 aError+=":: the second axis is not valid";
467 throw GeomAlgoAPI_Exception(aError);
470 if (theFirstNumber <=0) {
471 std::string aError = "Multitranslation builder ";
472 aError+=":: the number of copies for the first direction is null or negative.";
473 throw GeomAlgoAPI_Exception(aError);
476 if (theSecondNumber <=0) {
477 std::string aError = "Multitranslation builder ";
478 aError+=":: the number of copies for the second direction is null or negative.";
479 throw GeomAlgoAPI_Exception(aError);
482 // Coord theFirstAxis
483 double x1 = theFirstAxis->dir()->x();
484 double y1 = theFirstAxis->dir()->y();
485 double z1 = theFirstAxis->dir()->z();
486 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
488 // Coord theSecondAxis
489 double x2 = theSecondAxis->dir()->x();
490 double y2 = theSecondAxis->dir()->y();
491 double z2 = theSecondAxis->dir()->z();
492 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
494 ListOfShape aListOfShape;
495 for (int j=0; j<theSecondStep; j++) {
496 for (int i=0; i<theFirstNumber; i++) {
497 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
498 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
499 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
500 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
503 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
506 //===============================================================================================
507 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
508 const double theRMin1, const double theRMax1,
509 const double theRMin2, const double theRMax2,
511 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
513 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
514 theZ, theStartPhi, theDeltaPhi);
516 if (!aConeSegmentAlgo.check()) {
517 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
520 aConeSegmentAlgo.build();
522 if(!aConeSegmentAlgo.isDone()) {
523 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
525 if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
526 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
528 return aConeSegmentAlgo.shape();