Salome HOME
a27dc623b950d9a0ada050a3a9c1b0af98be7ce9
[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     std::shared_ptr<GeomAPI_Ax2> anAxis;
74     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
75                                                           theEdge->line()->direction()));
76
77     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
78
79     if (!aCylinderAlgo.check()) {
80       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
81     }
82
83     aCylinderAlgo.build();
84
85     if(!aCylinderAlgo.isDone()) {
86       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
87     }
88     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
89       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
90     }
91     return aCylinderAlgo.shape();
92   }
93
94   //===============================================================================================
95   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
96     std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
97     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
98   {
99     std::shared_ptr<GeomAPI_Ax2> anAxis;
100     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
101                                                           theEdge->line()->direction()));
102
103     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
104
105     if (!aCylinderAlgo.check()) {
106       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
107     }
108
109     aCylinderAlgo.build();
110
111     if(!aCylinderAlgo.isDone()) {
112       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
113     }
114     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
115       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
116     }
117     return aCylinderAlgo.shape();
118   }
119
120   //===============================================================================================
121   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
122     double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
123   {
124     std::shared_ptr<GeomAPI_Pnt> aBasePoint =
125       std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
126     std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
127     std::shared_ptr<GeomAPI_Ax2> anAxis;
128     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
129                                                           aEdge->line()->direction()));
130
131     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
132
133     if (!aCylinderAlgo.check()) {
134       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
135     }
136
137     aCylinderAlgo.build();
138
139     if(!aCylinderAlgo.isDone()) {
140       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
141     }
142     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
143       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
144     }
145     return aCylinderAlgo.shape();
146   }
147
148   //===============================================================================================
149   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
150     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
151   {
152     std::shared_ptr<GeomAPI_Pnt> aBasePoint =
153       std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
154     std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
155     std::shared_ptr<GeomAPI_Ax2> anAxis;
156     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
157                                                           aEdge->line()->direction()));
158
159     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
160
161     if (!aCylinderAlgo.check()) {
162       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
163     }
164
165     aCylinderAlgo.build();
166
167     if(!aCylinderAlgo.isDone()) {
168       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
169     }
170     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
171       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
172     }
173     return aCylinderAlgo.shape();
174   }
175
176   //===============================================================================================
177   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
178     std::shared_ptr<GeomAPI_Shape> theSourceShape,
179     std::shared_ptr<GeomAPI_Ax1>   theAxis,
180     const double theDistance) throw (GeomAlgoAPI_Exception)
181   {
182     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
183
184     if (!aTranslationAlgo.check()) {
185       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
186     }
187
188     aTranslationAlgo.build();
189
190     if(!aTranslationAlgo.isDone()) {
191       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
192     }
193     if (!aTranslationAlgo.checkValid("Translation builder with axis and distance")) {
194       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
195     }
196     return aTranslationAlgo.shape();
197   }
198
199   //===============================================================================================
200   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
201     std::shared_ptr<GeomAPI_Shape> theSourceShape,
202     const double theDx,
203     const double theDy,
204     const double theDz) throw (GeomAlgoAPI_Exception)
205   {
206     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
207
208     if (!aTranslationAlgo.check()) {
209       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
210     }
211
212     aTranslationAlgo.build();
213
214     if(!aTranslationAlgo.isDone()) {
215       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
216     }
217     if (!aTranslationAlgo.checkValid("Translation builder with dimensions")) {
218       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
219     }
220     return aTranslationAlgo.shape();
221   }
222
223   //===============================================================================================
224   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
225     std::shared_ptr<GeomAPI_Shape> theSourceShape,
226     std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
227     std::shared_ptr<GeomAPI_Pnt>   theEndPoint) throw (GeomAlgoAPI_Exception)
228   {
229     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
230
231     if (!aTranslationAlgo.check()) {
232       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
233     }
234
235     aTranslationAlgo.build();
236
237     if(!aTranslationAlgo.isDone()) {
238       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
239     }
240     if (!aTranslationAlgo.checkValid("Translation builder with two points")) {
241       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
242     }
243     return aTranslationAlgo.shape();
244   }
245
246   //===============================================================================================
247   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
248     std::shared_ptr<GeomAPI_Shape> theSourceShape,
249     std::shared_ptr<GeomAPI_Pnt>   thePoint) throw (GeomAlgoAPI_Exception)
250   {
251     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
252
253     if (!aSymmetryAlgo.check()) {
254       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
255     }
256
257     aSymmetryAlgo.build();
258
259     if(!aSymmetryAlgo.isDone()) {
260       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
261     }
262     if (!aSymmetryAlgo.checkValid("Symmetry builder by a point")) {
263       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
264     }
265     return aSymmetryAlgo.shape();
266   }
267
268   //===============================================================================================
269   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
270     std::shared_ptr<GeomAPI_Shape> theSourceShape,
271     std::shared_ptr<GeomAPI_Ax1>   theAxis) throw (GeomAlgoAPI_Exception)
272   {
273     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
274
275     if (!aSymmetryAlgo.check()) {
276       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
277     }
278
279     aSymmetryAlgo.build();
280
281     if(!aSymmetryAlgo.isDone()) {
282       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
283     }
284     if (!aSymmetryAlgo.checkValid("Symmetry builder by an axis")) {
285       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
286     }
287     return aSymmetryAlgo.shape();
288   }
289
290   //===============================================================================================
291   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
292     std::shared_ptr<GeomAPI_Shape> theSourceShape,
293     std::shared_ptr<GeomAPI_Ax2>   thePlane) throw (GeomAlgoAPI_Exception)
294   {
295     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
296
297     if (!aSymmetryAlgo.check()) {
298       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
299     }
300
301     aSymmetryAlgo.build();
302
303     if(!aSymmetryAlgo.isDone()) {
304       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
305     }
306     if (!aSymmetryAlgo.checkValid("Symmetry builder by a plane")) {
307       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
308     }
309     return aSymmetryAlgo.shape();
310   }
311
312   //===============================================================================================
313   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
314     std::shared_ptr<GeomAPI_Shape> theSourceShape,
315     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
316     const double                   theScaleFactor) throw (GeomAlgoAPI_Exception)
317   {
318     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
319
320     if (!aScaleAlgo.check()) {
321       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
322     }
323
324     aScaleAlgo.build();
325
326     if(!aScaleAlgo.isDone()) {
327       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
328     }
329     if (!aScaleAlgo.checkValid("Scale builder by a scale factor")) {
330       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
331     }
332     return aScaleAlgo.shape();
333   }
334
335   //===============================================================================================
336   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
337     std::shared_ptr<GeomAPI_Shape> theSourceShape,
338     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
339     const double                   theScaleFactorX,
340     const double                   theScaleFactorY,
341     const double                   theScaleFactorZ) throw (GeomAlgoAPI_Exception)
342   {
343     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
344                                  theScaleFactorX, theScaleFactorY, theScaleFactorZ);
345
346     if (!aScaleAlgo.check()) {
347       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
348     }
349
350     aScaleAlgo.build();
351
352     if(!aScaleAlgo.isDone()) {
353       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
354     }
355     if (!aScaleAlgo.checkValid("Scale builder by dimensions")) {
356       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
357     }
358     return aScaleAlgo.shape();
359   }
360
361   //===============================================================================================
362   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
363     std::shared_ptr<GeomAPI_Shape> theSourceShape,
364     std::shared_ptr<GeomAPI_Ax1> theAxis,
365     const double theStep,
366     const int theNumber) throw (GeomAlgoAPI_Exception)
367   {
368     ListOfShape aListOfShape;
369     for (int i=0; i<theNumber; i++) {
370       aListOfShape.
371         push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
372     }
373     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
374   }
375
376   //===============================================================================================
377   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
378     std::shared_ptr<GeomAPI_Shape> theSourceShape,
379     std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
380     const double theFirstStep,
381     const int theFirstNumber,
382     std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
383     const double theSecondStep,
384     const int theSecondNumber) throw (GeomAlgoAPI_Exception)
385   {
386     // Coord theFirstAxis
387     double x1 = theFirstAxis->dir()->x();
388     double y1 = theFirstAxis->dir()->y();
389     double z1 = theFirstAxis->dir()->z();
390     double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
391
392     // Coord theSecondAxis
393     double x2 = theSecondAxis->dir()->x();
394     double y2 = theSecondAxis->dir()->y();
395     double z2 = theSecondAxis->dir()->z();
396     double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
397
398     ListOfShape aListOfShape;
399     for (int j=0; j<theSecondStep; j++) {
400       for (int i=0; i<theFirstNumber; i++) {
401         double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
402         double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
403         double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
404         aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
405       }
406     }
407     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
408   }
409
410   //===============================================================================================
411   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
412     const double theRMin1, const double theRMax1,
413     const double theRMin2, const double theRMax2,
414     const double theZ,
415     const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
416   {
417     GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
418                                              theZ, theStartPhi, theDeltaPhi);
419
420     if (!aConeSegmentAlgo.check()) {
421       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
422     }
423
424     aConeSegmentAlgo.build();
425
426     if(!aConeSegmentAlgo.isDone()) {
427       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
428     }
429     if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
430       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
431     }
432     return aConeSegmentAlgo.shape();
433   }
434 }