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_Scale.h>
15 #include <GeomAlgoAPI_Symmetry.h>
16 #include <GeomAlgoAPI_Translation.h>
18 #include <GeomAPI_Lin.h>
22 namespace GeomAlgoAPI_ShapeAPI
24 //===============================================================================================
25 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
26 const double theDx, const double theDy,
27 const double theDz) throw (GeomAlgoAPI_Exception)
29 GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
31 if (!aBoxAlgo.check()) {
32 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
37 if(!aBoxAlgo.isDone()) {
38 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
40 if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
41 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
43 return aBoxAlgo.shape();
46 //===============================================================================================
47 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
48 std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
49 std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
51 GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
53 if (!aBoxAlgo.check()) {
54 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
59 if(!aBoxAlgo.isDone()) {
60 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
62 if (!aBoxAlgo.checkValid("Box builder with two points")) {
63 throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
65 return aBoxAlgo.shape();
68 //===============================================================================================
69 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
70 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
71 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
73 // Check if the base point is OK
75 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
77 // Check if the edge is OK
79 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
82 std::shared_ptr<GeomAPI_Ax2> anAxis;
83 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
84 theEdge->line()->direction()));
86 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
88 if (!aCylinderAlgo.check()) {
89 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
92 aCylinderAlgo.build();
94 if(!aCylinderAlgo.isDone()) {
95 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
97 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
98 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
100 return aCylinderAlgo.shape();
103 //===============================================================================================
104 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
105 std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
106 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
108 std::shared_ptr<GeomAPI_Ax2> anAxis;
109 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
110 theEdge->line()->direction()));
112 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
114 if (!aCylinderAlgo.check()) {
115 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
118 aCylinderAlgo.build();
120 if(!aCylinderAlgo.isDone()) {
121 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
123 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
124 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
126 return aCylinderAlgo.shape();
129 //===============================================================================================
130 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
131 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
133 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
134 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
135 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
136 std::shared_ptr<GeomAPI_Ax2> anAxis;
137 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
138 aEdge->line()->direction()));
140 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
142 if (!aCylinderAlgo.check()) {
143 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
146 aCylinderAlgo.build();
148 if(!aCylinderAlgo.isDone()) {
149 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
151 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
152 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
154 return aCylinderAlgo.shape();
157 //===============================================================================================
158 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
159 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
161 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
162 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
163 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
164 std::shared_ptr<GeomAPI_Ax2> anAxis;
165 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
166 aEdge->line()->direction()));
168 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
170 if (!aCylinderAlgo.check()) {
171 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
174 aCylinderAlgo.build();
176 if(!aCylinderAlgo.isDone()) {
177 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
179 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
180 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
182 return aCylinderAlgo.shape();
185 //===============================================================================================
186 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
187 std::shared_ptr<GeomAPI_Shape> theSourceShape,
188 std::shared_ptr<GeomAPI_Ax1> theAxis,
189 const double theDistance) throw (GeomAlgoAPI_Exception)
191 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
193 if (!aTranslationAlgo.check()) {
194 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
197 aTranslationAlgo.build();
199 if(!aTranslationAlgo.isDone()) {
200 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
202 if (!aTranslationAlgo.checkValid("Translation builder with axis and distance")) {
203 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
205 return aTranslationAlgo.shape();
208 //===============================================================================================
209 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
210 std::shared_ptr<GeomAPI_Shape> theSourceShape,
213 const double theDz) throw (GeomAlgoAPI_Exception)
215 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
217 if (!aTranslationAlgo.check()) {
218 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
221 aTranslationAlgo.build();
223 if(!aTranslationAlgo.isDone()) {
224 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
226 if (!aTranslationAlgo.checkValid("Translation builder with dimensions")) {
227 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
229 return aTranslationAlgo.shape();
232 //===============================================================================================
233 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
234 std::shared_ptr<GeomAPI_Shape> theSourceShape,
235 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
236 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
238 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
240 if (!aTranslationAlgo.check()) {
241 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
244 aTranslationAlgo.build();
246 if(!aTranslationAlgo.isDone()) {
247 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
249 if (!aTranslationAlgo.checkValid("Translation builder with two points")) {
250 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
252 return aTranslationAlgo.shape();
255 //===============================================================================================
256 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
257 std::shared_ptr<GeomAPI_Shape> theSourceShape,
258 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
260 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
262 if (!aSymmetryAlgo.check()) {
263 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
266 aSymmetryAlgo.build();
268 if(!aSymmetryAlgo.isDone()) {
269 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
271 if (!aSymmetryAlgo.checkValid("Symmetry builder by a point")) {
272 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
274 return aSymmetryAlgo.shape();
277 //===============================================================================================
278 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
279 std::shared_ptr<GeomAPI_Shape> theSourceShape,
280 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
282 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
284 if (!aSymmetryAlgo.check()) {
285 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
288 aSymmetryAlgo.build();
290 if(!aSymmetryAlgo.isDone()) {
291 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
293 if (!aSymmetryAlgo.checkValid("Symmetry builder by an axis")) {
294 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
296 return aSymmetryAlgo.shape();
299 //===============================================================================================
300 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
301 std::shared_ptr<GeomAPI_Shape> theSourceShape,
302 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
304 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
306 if (!aSymmetryAlgo.check()) {
307 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
310 aSymmetryAlgo.build();
312 if(!aSymmetryAlgo.isDone()) {
313 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
315 if (!aSymmetryAlgo.checkValid("Symmetry builder by a plane")) {
316 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
318 return aSymmetryAlgo.shape();
321 //===============================================================================================
322 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
323 std::shared_ptr<GeomAPI_Shape> theSourceShape,
324 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
325 const double theScaleFactor) throw (GeomAlgoAPI_Exception)
327 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
329 if (!aScaleAlgo.check()) {
330 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
335 if(!aScaleAlgo.isDone()) {
336 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
338 if (!aScaleAlgo.checkValid("Scale builder by a scale factor")) {
339 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
341 return aScaleAlgo.shape();
344 //===============================================================================================
345 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
346 std::shared_ptr<GeomAPI_Shape> theSourceShape,
347 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
348 const double theScaleFactorX,
349 const double theScaleFactorY,
350 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
352 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
353 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
355 if (!aScaleAlgo.check()) {
356 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
361 if(!aScaleAlgo.isDone()) {
362 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
364 if (!aScaleAlgo.checkValid("Scale builder by dimensions")) {
365 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
367 return aScaleAlgo.shape();
370 //===============================================================================================
371 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
372 std::shared_ptr<GeomAPI_Shape> theSourceShape,
373 std::shared_ptr<GeomAPI_Ax1> theAxis,
374 const double theStep,
375 const int theNumber) throw (GeomAlgoAPI_Exception)
377 ListOfShape aListOfShape;
378 for (int i=0; i<theNumber; i++) {
380 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
382 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
385 //===============================================================================================
386 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
387 std::shared_ptr<GeomAPI_Shape> theSourceShape,
388 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
389 const double theFirstStep,
390 const int theFirstNumber,
391 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
392 const double theSecondStep,
393 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
395 // Coord theFirstAxis
396 double x1 = theFirstAxis->dir()->x();
397 double y1 = theFirstAxis->dir()->y();
398 double z1 = theFirstAxis->dir()->z();
399 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
401 // Coord theSecondAxis
402 double x2 = theSecondAxis->dir()->x();
403 double y2 = theSecondAxis->dir()->y();
404 double z2 = theSecondAxis->dir()->z();
405 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
407 ListOfShape aListOfShape;
408 for (int j=0; j<theSecondStep; j++) {
409 for (int i=0; i<theFirstNumber; i++) {
410 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
411 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
412 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
413 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
416 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
419 //===============================================================================================
420 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
421 const double theRMin1, const double theRMax1,
422 const double theRMin2, const double theRMax2,
424 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
426 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
427 theZ, theStartPhi, theDeltaPhi);
429 if (!aConeSegmentAlgo.check()) {
430 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
433 aConeSegmentAlgo.build();
435 if(!aConeSegmentAlgo.isDone()) {
436 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
438 if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
439 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
441 return aConeSegmentAlgo.shape();