]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.cpp
Salome HOME
Fix bug for translation (id 2237 and id 2250).
[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_Rotation.h>
15 #include <GeomAlgoAPI_Scale.h>
16 #include <GeomAlgoAPI_Symmetry.h>
17 #include <GeomAlgoAPI_Translation.h>
18
19 #include <GeomAPI_Lin.h>
20
21 #include <math.h>
22
23 namespace GeomAlgoAPI_ShapeAPI
24 {
25   //===============================================================================================
26   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
27     const double theDx, const double theDy,
28     const double theDz) throw (GeomAlgoAPI_Exception)
29   {
30     GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
31
32     if (!aBoxAlgo.check()) {
33       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
34     }
35
36     aBoxAlgo.build();
37
38     if(!aBoxAlgo.isDone()) {
39       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
40     }
41     if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
42       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
43     }
44     return aBoxAlgo.shape();
45   }
46
47   //===============================================================================================
48   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
49     std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
50     std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
51   {
52     GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
53
54     if (!aBoxAlgo.check()) {
55       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
56     }
57
58     aBoxAlgo.build();
59
60     if(!aBoxAlgo.isDone()) {
61       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
62     }
63     if (!aBoxAlgo.checkValid("Box builder with two points")) {
64       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
65     }
66     return aBoxAlgo.shape();
67   }
68
69   //===============================================================================================
70   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
71     std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
72     double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
73   {
74     // Check if the base point is OK
75     if (!theBasePoint) {
76       throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
77     }
78     // Check if the edge is OK
79     if (!theEdge) {
80       throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
81     }
82
83     std::shared_ptr<GeomAPI_Ax2> anAxis;
84     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
85                                                           theEdge->line()->direction()));
86
87     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
88
89     if (!aCylinderAlgo.check()) {
90       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
91     }
92
93     aCylinderAlgo.build();
94
95     if(!aCylinderAlgo.isDone()) {
96       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
97     }
98     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
99       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
100     }
101     return aCylinderAlgo.shape();
102   }
103
104   //===============================================================================================
105   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
106     std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
107     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
108   {
109     // Check if the base point is OK
110     if (!theBasePoint) {
111       throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
112     }
113     // Check if the edge is OK
114     if (!theEdge) {
115       throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
116     }
117
118     std::shared_ptr<GeomAPI_Ax2> anAxis;
119     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
120                                                           theEdge->line()->direction()));
121
122     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
123
124     if (!aCylinderAlgo.check()) {
125       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
126     }
127
128     aCylinderAlgo.build();
129
130     if(!aCylinderAlgo.isDone()) {
131       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
132     }
133     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
134       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
135     }
136     return aCylinderAlgo.shape();
137   }
138
139   //===============================================================================================
140   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
141     double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
142   {
143     std::shared_ptr<GeomAPI_Pnt> aBasePoint =
144       std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
145     std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
146     std::shared_ptr<GeomAPI_Ax2> anAxis;
147     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
148                                                           aEdge->line()->direction()));
149
150     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
151
152     if (!aCylinderAlgo.check()) {
153       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
154     }
155
156     aCylinderAlgo.build();
157
158     if(!aCylinderAlgo.isDone()) {
159       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
160     }
161     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
162       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
163     }
164     return aCylinderAlgo.shape();
165   }
166
167   //===============================================================================================
168   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
169     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
170   {
171     std::shared_ptr<GeomAPI_Pnt> aBasePoint =
172       std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
173     std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
174     std::shared_ptr<GeomAPI_Ax2> anAxis;
175     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
176                                                           aEdge->line()->direction()));
177
178     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
179
180     if (!aCylinderAlgo.check()) {
181       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
182     }
183
184     aCylinderAlgo.build();
185
186     if(!aCylinderAlgo.isDone()) {
187       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
188     }
189     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
190       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
191     }
192     return aCylinderAlgo.shape();
193   }
194
195   //===============================================================================================
196   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
197     std::shared_ptr<GeomAPI_Shape> theSourceShape,
198     std::shared_ptr<GeomAPI_Ax1>   theAxis,
199     const double theDistance) throw (GeomAlgoAPI_Exception)
200   {
201     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
202
203     if (!aTranslationAlgo.check()) {
204       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
205     }
206
207     aTranslationAlgo.build();
208
209     if(!aTranslationAlgo.isDone()) {
210       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
211     }
212
213     return aTranslationAlgo.shape();
214   }
215
216   //===============================================================================================
217   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
218     std::shared_ptr<GeomAPI_Shape> theSourceShape,
219     const double theDx,
220     const double theDy,
221     const double theDz) throw (GeomAlgoAPI_Exception)
222   {
223     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
224
225     if (!aTranslationAlgo.check()) {
226       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
227     }
228
229     aTranslationAlgo.build();
230
231     if(!aTranslationAlgo.isDone()) {
232       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
233     }
234
235     return aTranslationAlgo.shape();
236   }
237
238   //===============================================================================================
239   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
240     std::shared_ptr<GeomAPI_Shape> theSourceShape,
241     std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
242     std::shared_ptr<GeomAPI_Pnt>   theEndPoint) throw (GeomAlgoAPI_Exception)
243   {
244     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
245
246     if (!aTranslationAlgo.check()) {
247       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
248     }
249
250     aTranslationAlgo.build();
251
252     if(!aTranslationAlgo.isDone()) {
253       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
254     }
255
256     return aTranslationAlgo.shape();
257   }
258
259   //===============================================================================================
260   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
261     std::shared_ptr<GeomAPI_Shape> theSourceShape,
262     std::shared_ptr<GeomAPI_Ax1> theAxis,
263     const double theAngle) throw (GeomAlgoAPI_Exception)
264   {
265     GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
266
267     if (!aRotationAlgo.check()) {
268       throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
269     }
270
271     aRotationAlgo.build();
272
273     if(!aRotationAlgo.isDone()) {
274       throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
275     }
276
277     return aRotationAlgo.shape();
278   }
279
280   //===============================================================================================
281   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
282     std::shared_ptr<GeomAPI_Shape> theSourceShape,
283     std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
284     std::shared_ptr<GeomAPI_Pnt> theStartPoint,
285     std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
286   {
287     GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
288
289     if (!aRotationAlgo.check()) {
290       throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
291     }
292
293     aRotationAlgo.build();
294
295     if(!aRotationAlgo.isDone()) {
296       throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
297     }
298
299     return aRotationAlgo.shape();
300   }
301
302   //===============================================================================================
303   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
304     std::shared_ptr<GeomAPI_Shape> theSourceShape,
305     std::shared_ptr<GeomAPI_Pnt>   thePoint) throw (GeomAlgoAPI_Exception)
306   {
307     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
308
309     if (!aSymmetryAlgo.check()) {
310       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
311     }
312
313     aSymmetryAlgo.build();
314
315     if(!aSymmetryAlgo.isDone()) {
316       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
317     }
318
319     return aSymmetryAlgo.shape();
320   }
321
322   //===============================================================================================
323   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
324     std::shared_ptr<GeomAPI_Shape> theSourceShape,
325     std::shared_ptr<GeomAPI_Ax1>   theAxis) throw (GeomAlgoAPI_Exception)
326   {
327     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
328
329     if (!aSymmetryAlgo.check()) {
330       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
331     }
332
333     aSymmetryAlgo.build();
334
335     if(!aSymmetryAlgo.isDone()) {
336       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
337     }
338
339     return aSymmetryAlgo.shape();
340   }
341
342   //===============================================================================================
343   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
344     std::shared_ptr<GeomAPI_Shape> theSourceShape,
345     std::shared_ptr<GeomAPI_Ax2>   thePlane) throw (GeomAlgoAPI_Exception)
346   {
347     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
348
349     if (!aSymmetryAlgo.check()) {
350       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
351     }
352
353     aSymmetryAlgo.build();
354
355     if(!aSymmetryAlgo.isDone()) {
356       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
357     }
358
359     return aSymmetryAlgo.shape();
360   }
361
362   //===============================================================================================
363   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
364     std::shared_ptr<GeomAPI_Shape> theSourceShape,
365     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
366     const double                   theScaleFactor) throw (GeomAlgoAPI_Exception)
367   {
368     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
369
370     if (!aScaleAlgo.check()) {
371       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
372     }
373
374     aScaleAlgo.build();
375
376     if(!aScaleAlgo.isDone()) {
377       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
378     }
379
380     return aScaleAlgo.shape();
381   }
382
383   //===============================================================================================
384   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
385     std::shared_ptr<GeomAPI_Shape> theSourceShape,
386     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
387     const double                   theScaleFactorX,
388     const double                   theScaleFactorY,
389     const double                   theScaleFactorZ) throw (GeomAlgoAPI_Exception)
390   {
391     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
392                                  theScaleFactorX, theScaleFactorY, theScaleFactorZ);
393
394     if (!aScaleAlgo.check()) {
395       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
396     }
397
398     aScaleAlgo.build();
399
400     if(!aScaleAlgo.isDone()) {
401       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
402     }
403
404     return aScaleAlgo.shape();
405   }
406
407   //===============================================================================================
408   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
409     std::shared_ptr<GeomAPI_Shape> theSourceShape,
410     std::shared_ptr<GeomAPI_Ax1> theAxis,
411     const double theStep,
412     const int theNumber) throw (GeomAlgoAPI_Exception)
413   {
414     if (theNumber <=0) {
415       std::string aError = "Multitranslation builder ";
416       aError+=":: the number of copies for the first direction is null or negative.";
417       throw GeomAlgoAPI_Exception(aError);
418     }
419
420     if (!theAxis) {
421       std::string aError = "Multitranslation builder ";
422       aError+=":: the first axis is not valid";
423       throw GeomAlgoAPI_Exception(aError);
424     }
425
426     ListOfShape aListOfShape;
427     for (int i=0; i<theNumber; i++) {
428       aListOfShape.
429         push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
430     }
431     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
432   }
433
434   //===============================================================================================
435   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
436     std::shared_ptr<GeomAPI_Shape> theSourceShape,
437     std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
438     const double theFirstStep,
439     const int theFirstNumber,
440     std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
441     const double theSecondStep,
442     const int theSecondNumber) throw (GeomAlgoAPI_Exception)
443   {
444     if (theFirstNumber <=0) {
445       std::string aError = "Multitranslation builder ";
446       aError+=":: the number of copies for the first direction is null or negative.";
447       throw GeomAlgoAPI_Exception(aError);
448     }
449
450     if (theSecondNumber <=0) {
451       std::string aError = "Multitranslation builder ";
452       aError+=":: the number of copies for the second direction is null or negative.";
453       throw GeomAlgoAPI_Exception(aError);
454     }
455
456     if (!theFirstAxis) {
457       std::string aError = "Multitranslation builder ";
458       aError+=":: the first axis is not valid";
459       throw GeomAlgoAPI_Exception(aError);
460     }
461
462     if (!theSecondAxis) {
463       std::string aError = "Multitranslation builder ";
464       aError+=":: the second axis is not valid";
465       throw GeomAlgoAPI_Exception(aError);
466     }
467
468     // Coord theFirstAxis
469     double x1 = theFirstAxis->dir()->x();
470     double y1 = theFirstAxis->dir()->y();
471     double z1 = theFirstAxis->dir()->z();
472     double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
473
474     // Coord theSecondAxis
475     double x2 = theSecondAxis->dir()->x();
476     double y2 = theSecondAxis->dir()->y();
477     double z2 = theSecondAxis->dir()->z();
478     double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
479
480     ListOfShape aListOfShape;
481     for (int j=0; j<theSecondStep; j++) {
482       for (int i=0; i<theFirstNumber; i++) {
483         double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
484         double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
485         double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
486         aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
487       }
488     }
489     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
490   }
491
492   //===============================================================================================
493   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
494     const double theRMin1, const double theRMax1,
495     const double theRMin2, const double theRMax2,
496     const double theZ,
497     const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
498   {
499     GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
500                                              theZ, theStartPhi, theDeltaPhi);
501
502     if (!aConeSegmentAlgo.check()) {
503       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
504     }
505
506     aConeSegmentAlgo.build();
507
508     if(!aConeSegmentAlgo.isDone()) {
509       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
510     }
511     if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
512       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
513     }
514     return aConeSegmentAlgo.shape();
515   }
516 }