Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/modules/shaper
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeAPI.cpp
1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_ShapeAPI.cpp
4 // Created:     17 Mar 2016
5 // Author:      Clarisse Genrault (CEA)
6
7 #include "GeomAlgoAPI_ShapeAPI.h"
8
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>
17
18 #include <GeomAPI_Lin.h>
19
20 #include <math.h>
21
22 namespace GeomAlgoAPI_ShapeAPI
23 {
24   //===============================================================================================
25   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
26     const double theDx, const double theDy,
27     const double theDz) throw (GeomAlgoAPI_Exception)
28   {
29     GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
30
31     if (!aBoxAlgo.check()) {
32       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
33     }
34
35     aBoxAlgo.build();
36
37     if(!aBoxAlgo.isDone()) {
38       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
39     }
40     if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
41       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
42     }
43     return aBoxAlgo.shape();
44   }
45
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)
50   {
51     GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
52
53     if (!aBoxAlgo.check()) {
54       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
55     }
56
57     aBoxAlgo.build();
58
59     if(!aBoxAlgo.isDone()) {
60       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
61     }
62     if (!aBoxAlgo.checkValid("Box builder with two points")) {
63       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
64     }
65     return aBoxAlgo.shape();
66   }
67
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)
72   {
73     // Check if the base point is OK
74     if (!theBasePoint) {
75       throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
76     }
77     // Check if the edge is OK
78     if (!theEdge) {
79       throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
80     }
81
82     std::shared_ptr<GeomAPI_Ax2> anAxis;
83     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
84                                                           theEdge->line()->direction()));
85
86     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
87
88     if (!aCylinderAlgo.check()) {
89       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
90     }
91
92     aCylinderAlgo.build();
93
94     if(!aCylinderAlgo.isDone()) {
95       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
96     }
97     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
98       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
99     }
100     return aCylinderAlgo.shape();
101   }
102
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)
107   {
108     // Check if the base point is OK
109     if (!theBasePoint) {
110       throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
111     }
112     // Check if the edge is OK
113     if (!theEdge) {
114       throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
115     }
116
117     std::shared_ptr<GeomAPI_Ax2> anAxis;
118     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
119                                                           theEdge->line()->direction()));
120
121     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
122
123     if (!aCylinderAlgo.check()) {
124       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
125     }
126
127     aCylinderAlgo.build();
128
129     if(!aCylinderAlgo.isDone()) {
130       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
131     }
132     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
133       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
134     }
135     return aCylinderAlgo.shape();
136   }
137
138   //===============================================================================================
139   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
140     double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
141   {
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()));
148
149     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
150
151     if (!aCylinderAlgo.check()) {
152       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
153     }
154
155     aCylinderAlgo.build();
156
157     if(!aCylinderAlgo.isDone()) {
158       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
159     }
160     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
161       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
162     }
163     return aCylinderAlgo.shape();
164   }
165
166   //===============================================================================================
167   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
168     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
169   {
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()));
176
177     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
178
179     if (!aCylinderAlgo.check()) {
180       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
181     }
182
183     aCylinderAlgo.build();
184
185     if(!aCylinderAlgo.isDone()) {
186       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
187     }
188     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
189       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
190     }
191     return aCylinderAlgo.shape();
192   }
193
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)
199   {
200     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
201
202     if (!aTranslationAlgo.check()) {
203       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
204     }
205
206     aTranslationAlgo.build();
207
208     if(!aTranslationAlgo.isDone()) {
209       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
210     }
211     if (!aTranslationAlgo.checkValid("Translation builder with axis and distance")) {
212       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
213     }
214     return aTranslationAlgo.shape();
215   }
216
217   //===============================================================================================
218   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
219     std::shared_ptr<GeomAPI_Shape> theSourceShape,
220     const double theDx,
221     const double theDy,
222     const double theDz) throw (GeomAlgoAPI_Exception)
223   {
224     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
225
226     if (!aTranslationAlgo.check()) {
227       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
228     }
229
230     aTranslationAlgo.build();
231
232     if(!aTranslationAlgo.isDone()) {
233       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
234     }
235     if (!aTranslationAlgo.checkValid("Translation builder with dimensions")) {
236       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
237     }
238     return aTranslationAlgo.shape();
239   }
240
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)
246   {
247     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
248
249     if (!aTranslationAlgo.check()) {
250       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
251     }
252
253     aTranslationAlgo.build();
254
255     if(!aTranslationAlgo.isDone()) {
256       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
257     }
258     if (!aTranslationAlgo.checkValid("Translation builder with two points")) {
259       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
260     }
261     return aTranslationAlgo.shape();
262   }
263
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)
268   {
269     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
270
271     if (!aSymmetryAlgo.check()) {
272       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
273     }
274
275     aSymmetryAlgo.build();
276
277     if(!aSymmetryAlgo.isDone()) {
278       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
279     }
280     if (!aSymmetryAlgo.checkValid("Symmetry builder by a point")) {
281       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
282     }
283     return aSymmetryAlgo.shape();
284   }
285
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)
290   {
291     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
292
293     if (!aSymmetryAlgo.check()) {
294       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
295     }
296
297     aSymmetryAlgo.build();
298
299     if(!aSymmetryAlgo.isDone()) {
300       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
301     }
302     if (!aSymmetryAlgo.checkValid("Symmetry builder by an axis")) {
303       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
304     }
305     return aSymmetryAlgo.shape();
306   }
307
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)
312   {
313     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
314
315     if (!aSymmetryAlgo.check()) {
316       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
317     }
318
319     aSymmetryAlgo.build();
320
321     if(!aSymmetryAlgo.isDone()) {
322       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
323     }
324     if (!aSymmetryAlgo.checkValid("Symmetry builder by a plane")) {
325       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
326     }
327     return aSymmetryAlgo.shape();
328   }
329
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)
335   {
336     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
337
338     if (!aScaleAlgo.check()) {
339       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
340     }
341
342     aScaleAlgo.build();
343
344     if(!aScaleAlgo.isDone()) {
345       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
346     }
347     if (!aScaleAlgo.checkValid("Scale builder by a scale factor")) {
348       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
349     }
350     return aScaleAlgo.shape();
351   }
352
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)
360   {
361     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
362                                  theScaleFactorX, theScaleFactorY, theScaleFactorZ);
363
364     if (!aScaleAlgo.check()) {
365       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
366     }
367
368     aScaleAlgo.build();
369
370     if(!aScaleAlgo.isDone()) {
371       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
372     }
373     if (!aScaleAlgo.checkValid("Scale builder by dimensions")) {
374       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
375     }
376     return aScaleAlgo.shape();
377   }
378
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)
385   {
386     ListOfShape aListOfShape;
387     for (int i=0; i<theNumber; i++) {
388       aListOfShape.
389         push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
390     }
391     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
392   }
393
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)
403   {
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);
409
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);
415
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));
423       }
424     }
425     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
426   }
427
428   //===============================================================================================
429   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
430     const double theRMin1, const double theRMax1,
431     const double theRMin2, const double theRMax2,
432     const double theZ,
433     const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
434   {
435     GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
436                                              theZ, theStartPhi, theDeltaPhi);
437
438     if (!aConeSegmentAlgo.check()) {
439       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
440     }
441
442     aConeSegmentAlgo.build();
443
444     if(!aConeSegmentAlgo.isDone()) {
445       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
446     }
447     if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
448       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
449     }
450     return aConeSegmentAlgo.shape();
451   }
452 }