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 // Check if the base point is OK
110 throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
112 // Check if the edge is OK
114 throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
117 std::shared_ptr<GeomAPI_Ax2> anAxis;
118 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
119 theEdge->line()->direction()));
121 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
123 if (!aCylinderAlgo.check()) {
124 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
127 aCylinderAlgo.build();
129 if(!aCylinderAlgo.isDone()) {
130 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
132 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
133 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
135 return aCylinderAlgo.shape();
138 //===============================================================================================
139 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
140 double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
142 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
143 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
144 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
145 std::shared_ptr<GeomAPI_Ax2> anAxis;
146 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
147 aEdge->line()->direction()));
149 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
151 if (!aCylinderAlgo.check()) {
152 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
155 aCylinderAlgo.build();
157 if(!aCylinderAlgo.isDone()) {
158 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
160 if (!aCylinderAlgo.checkValid("Cylinder builder")) {
161 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
163 return aCylinderAlgo.shape();
166 //===============================================================================================
167 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
168 double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
170 std::shared_ptr<GeomAPI_Pnt> aBasePoint =
171 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
172 std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
173 std::shared_ptr<GeomAPI_Ax2> anAxis;
174 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
175 aEdge->line()->direction()));
177 GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
179 if (!aCylinderAlgo.check()) {
180 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
183 aCylinderAlgo.build();
185 if(!aCylinderAlgo.isDone()) {
186 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
188 if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
189 throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
191 return aCylinderAlgo.shape();
194 //===============================================================================================
195 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
196 std::shared_ptr<GeomAPI_Shape> theSourceShape,
197 std::shared_ptr<GeomAPI_Ax1> theAxis,
198 const double theDistance) throw (GeomAlgoAPI_Exception)
200 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
202 if (!aTranslationAlgo.check()) {
203 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
206 aTranslationAlgo.build();
208 if(!aTranslationAlgo.isDone()) {
209 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
211 if (!aTranslationAlgo.checkValid("Translation builder with axis and distance")) {
212 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
214 return aTranslationAlgo.shape();
217 //===============================================================================================
218 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
219 std::shared_ptr<GeomAPI_Shape> theSourceShape,
222 const double theDz) throw (GeomAlgoAPI_Exception)
224 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
226 if (!aTranslationAlgo.check()) {
227 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
230 aTranslationAlgo.build();
232 if(!aTranslationAlgo.isDone()) {
233 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
235 if (!aTranslationAlgo.checkValid("Translation builder with dimensions")) {
236 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
238 return aTranslationAlgo.shape();
241 //===============================================================================================
242 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
243 std::shared_ptr<GeomAPI_Shape> theSourceShape,
244 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
245 std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
247 GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
249 if (!aTranslationAlgo.check()) {
250 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
253 aTranslationAlgo.build();
255 if(!aTranslationAlgo.isDone()) {
256 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
258 if (!aTranslationAlgo.checkValid("Translation builder with two points")) {
259 throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
261 return aTranslationAlgo.shape();
264 //===============================================================================================
265 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
266 std::shared_ptr<GeomAPI_Shape> theSourceShape,
267 std::shared_ptr<GeomAPI_Pnt> thePoint) throw (GeomAlgoAPI_Exception)
269 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
271 if (!aSymmetryAlgo.check()) {
272 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
275 aSymmetryAlgo.build();
277 if(!aSymmetryAlgo.isDone()) {
278 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
280 if (!aSymmetryAlgo.checkValid("Symmetry builder by a point")) {
281 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
283 return aSymmetryAlgo.shape();
286 //===============================================================================================
287 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
288 std::shared_ptr<GeomAPI_Shape> theSourceShape,
289 std::shared_ptr<GeomAPI_Ax1> theAxis) throw (GeomAlgoAPI_Exception)
291 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
293 if (!aSymmetryAlgo.check()) {
294 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
297 aSymmetryAlgo.build();
299 if(!aSymmetryAlgo.isDone()) {
300 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
302 if (!aSymmetryAlgo.checkValid("Symmetry builder by an axis")) {
303 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
305 return aSymmetryAlgo.shape();
308 //===============================================================================================
309 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
310 std::shared_ptr<GeomAPI_Shape> theSourceShape,
311 std::shared_ptr<GeomAPI_Ax2> thePlane) throw (GeomAlgoAPI_Exception)
313 GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
315 if (!aSymmetryAlgo.check()) {
316 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
319 aSymmetryAlgo.build();
321 if(!aSymmetryAlgo.isDone()) {
322 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
324 if (!aSymmetryAlgo.checkValid("Symmetry builder by a plane")) {
325 throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
327 return aSymmetryAlgo.shape();
330 //===============================================================================================
331 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
332 std::shared_ptr<GeomAPI_Shape> theSourceShape,
333 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
334 const double theScaleFactor) throw (GeomAlgoAPI_Exception)
336 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
338 if (!aScaleAlgo.check()) {
339 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
344 if(!aScaleAlgo.isDone()) {
345 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
347 if (!aScaleAlgo.checkValid("Scale builder by a scale factor")) {
348 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
350 return aScaleAlgo.shape();
353 //===============================================================================================
354 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
355 std::shared_ptr<GeomAPI_Shape> theSourceShape,
356 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
357 const double theScaleFactorX,
358 const double theScaleFactorY,
359 const double theScaleFactorZ) throw (GeomAlgoAPI_Exception)
361 GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
362 theScaleFactorX, theScaleFactorY, theScaleFactorZ);
364 if (!aScaleAlgo.check()) {
365 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
370 if(!aScaleAlgo.isDone()) {
371 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
373 if (!aScaleAlgo.checkValid("Scale builder by dimensions")) {
374 throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
376 return aScaleAlgo.shape();
379 //===============================================================================================
380 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
381 std::shared_ptr<GeomAPI_Shape> theSourceShape,
382 std::shared_ptr<GeomAPI_Ax1> theAxis,
383 const double theStep,
384 const int theNumber) throw (GeomAlgoAPI_Exception)
386 ListOfShape aListOfShape;
387 for (int i=0; i<theNumber; i++) {
389 push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
391 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
394 //===============================================================================================
395 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
396 std::shared_ptr<GeomAPI_Shape> theSourceShape,
397 std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
398 const double theFirstStep,
399 const int theFirstNumber,
400 std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
401 const double theSecondStep,
402 const int theSecondNumber) throw (GeomAlgoAPI_Exception)
404 // Coord theFirstAxis
405 double x1 = theFirstAxis->dir()->x();
406 double y1 = theFirstAxis->dir()->y();
407 double z1 = theFirstAxis->dir()->z();
408 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
410 // Coord theSecondAxis
411 double x2 = theSecondAxis->dir()->x();
412 double y2 = theSecondAxis->dir()->y();
413 double z2 = theSecondAxis->dir()->z();
414 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
416 ListOfShape aListOfShape;
417 for (int j=0; j<theSecondStep; j++) {
418 for (int i=0; i<theFirstNumber; i++) {
419 double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
420 double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
421 double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
422 aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
425 return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
428 //===============================================================================================
429 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
430 const double theRMin1, const double theRMax1,
431 const double theRMin2, const double theRMax2,
433 const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
435 GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
436 theZ, theStartPhi, theDeltaPhi);
438 if (!aConeSegmentAlgo.check()) {
439 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
442 aConeSegmentAlgo.build();
444 if(!aConeSegmentAlgo.isDone()) {
445 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
447 if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
448 throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
450 return aConeSegmentAlgo.shape();