Salome HOME
[Code coverage]: Move checking the algorithm's result into separate function
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_MultiRotation.cpp
1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_MultiRotation.cpp
4 // Created:     30 Jan 2017
5 // Author:      Clarisse Genrault (CEA)
6
7 #include <FeaturesPlugin_MultiRotation.h>
8
9 #include <GeomAlgoAPI_CompoundBuilder.h>
10 #include <GeomAlgoAPI_ShapeTools.h>
11 #include <GeomAlgoAPI_Tools.h>
12 #include <GeomAlgoAPI_Translation.h>
13
14 #include <GeomAPI_ShapeExplorer.h>
15
16 #include <GeomAPI_Ax1.h>
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Lin.h>
19 #include <GeomAPI_ShapeIterator.h>
20 #include <GeomAPI_Trsf.h>
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeInteger.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultPart.h>
28
29 #include <math.h>
30 #include <iostream>
31
32 //=================================================================================================
33 FeaturesPlugin_MultiRotation::FeaturesPlugin_MultiRotation()
34 {
35 }
36
37 //=================================================================================================
38 void FeaturesPlugin_MultiRotation::initAttributes()
39 {
40   AttributeSelectionListPtr aSelection =
41     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
42     FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID(),
43     ModelAPI_AttributeSelectionList::typeId()));
44
45   data()->addAttribute(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID(),
46                        ModelAPI_AttributeSelection::typeId());
47   data()->addAttribute(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID(),
48                        ModelAPI_AttributeString::typeId());
49   data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID(),
50                        ModelAPI_AttributeDouble::typeId());
51   data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID(),
52                        ModelAPI_AttributeInteger::typeId());
53
54 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
55   data()->addAttribute(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID(),
56                        ModelAPI_AttributeString::typeId());
57   data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID(),
58                        ModelAPI_AttributeDouble::typeId());
59   data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID(),
60                        ModelAPI_AttributeInteger::typeId());
61 #endif
62 }
63
64 //=================================================================================================
65 void FeaturesPlugin_MultiRotation::execute()
66 {
67 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
68   std::string useRadialDir = string(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID())->value();
69   if (useRadialDir.empty()) {
70     performRotation1D();
71   } else {
72     performRotation2D();
73   }
74 #else
75   performRotation1D();
76 #endif
77 }
78
79 //=================================================================================================
80 void FeaturesPlugin_MultiRotation::performRotation1D()
81 {
82   // Getting objects.
83   ListOfShape anObjects;
84   std::list<ResultPtr> aContextes;
85   AttributeSelectionListPtr anObjectsSelList =
86     selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
87   if (anObjectsSelList->size() == 0) {
88     return;
89   }
90   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
91     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
92       anObjectsSelList->value(anObjectsIndex);
93     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
94     if(!anObject.get()) { // may be for not-activated parts
95       return;
96     }
97     anObjects.push_back(anObject);
98     aContextes.push_back(anObjectAttr->context());
99   }
100
101   //Getting axis.
102   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
103   AttributeSelectionPtr anObjRef = selection(AXIS_ANGULAR_ID());
104   GeomShapePtr aShape = anObjRef->value();
105   if (!aShape.get()) {
106     if (anObjRef->context().get()) {
107       aShape = anObjRef->context()->shape();
108     }
109   }
110   if (!aShape.get()) {
111     setError(aSelectionError);
112     return;
113   }
114
115   GeomEdgePtr anEdge;
116   if (aShape->isEdge())
117   {
118     anEdge = aShape->edge();
119   }
120   else if (aShape->isCompound())
121   {
122     GeomAPI_ShapeIterator anIt(aShape);
123     anEdge = anIt.current()->edge();
124   }
125
126   if (!anEdge.get())
127   {
128     setError(aSelectionError);
129     return;
130   }
131
132   std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
133                                                       anEdge->line()->direction()));
134
135   // Getting number of copies.
136   int nbCopies =
137     integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
138
139   if (nbCopies <=0) {
140     std::string aFeatureError = "Multirotation builder ";
141     aFeatureError+=":: the number of copies for the angular direction is null or negative.";
142     setError(aFeatureError);
143     return;
144   }
145
146   // Getting angle
147   double anAngle;
148   std::string useAngularStep =
149     string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
150   if (!useAngularStep.empty()) {
151     anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
152   } else {
153     anAngle = 360./nbCopies;
154   }
155
156   // Moving each object.
157   int aResultIndex = 0;
158   std::list<ResultPtr>::iterator aContext = aContextes.begin();
159   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
160         anObjectsIt++, aContext++) {
161     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
162     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
163
164     // Setting result.
165     if (isPart) {
166       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
167       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
168       for (int i=0; i<nbCopies; i++) {
169         aTrsf->setRotation(anAxis, i*anAngle);
170         ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
171         aResultPart->setTrsf(*aContext, aTrsf);
172         setResult(aResultPart, aResultIndex);
173         aResultIndex++;
174       }
175     } else {
176       std::string anError;
177       ListOfShape aListOfShape;
178       std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
179
180       for (int i=0; i<nbCopies; i++) {
181         std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
182           new GeomAlgoAPI_Rotation(aBaseShape, anAxis, i*anAngle));
183
184         if (!aRotationnAlgo->check()) {
185           setError(aRotationnAlgo->getError());
186           break;
187         }
188
189         aRotationnAlgo->build();
190
191         // Checking that the algorithm worked properly.
192         if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationnAlgo, getKind(), anError)) {
193           setError(anError);
194           break;
195         }
196         aListOfShape.push_back(aRotationnAlgo->shape());
197         aListOfRotationAlgo.push_back(aRotationnAlgo);
198       }
199       std::shared_ptr<GeomAPI_Shape> aCompound =
200         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
201       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
202       aResultBody->storeModified(aBaseShape, aCompound);
203       loadNamingDS(aListOfRotationAlgo, aResultBody, aBaseShape);
204
205       setResult(aResultBody, aResultIndex);
206     }
207     aResultIndex++;
208   }
209
210   // Remove the rest results if there were produced in the previous pass.
211   removeResults(aResultIndex);
212 }
213
214 //=================================================================================================
215 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
216 void FeaturesPlugin_MultiRotation::performRotation2D()
217 {
218   // Getting objects.
219   ListOfShape anObjects;
220   std::list<ResultPtr> aContextes;
221   AttributeSelectionListPtr anObjectsSelList =
222     selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
223   if (anObjectsSelList->size() == 0) {
224     return;
225   }
226   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
227     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
228       anObjectsSelList->value(anObjectsIndex);
229     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
230     if(!anObject.get()) { // may be for not-activated parts
231       return;
232     }
233     anObjects.push_back(anObject);
234     aContextes.push_back(anObjectAttr->context());
235   }
236
237   //Getting axis.
238   std::shared_ptr<GeomAPI_Ax1> anAxis;
239   std::shared_ptr<GeomAPI_Edge> anEdge;
240   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
241     selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
242   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
243     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
244   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
245              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
246     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
247   }
248   if(anEdge) {
249     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
250                                                           anEdge->line()->direction()));
251   }
252
253   // Getting number of copies int he angular direction.
254   int nbAngular =
255     integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
256
257   if (nbAngular <=0) {
258     std::string aFeatureError = "Multirotation builder ";
259     aFeatureError+=":: the number of copies for the angular direction is null or negative.";
260     setError(aFeatureError);
261     return;
262   }
263
264   // Getting number of copies int he radial direction.
265   int nbRadial =
266     integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
267
268   if (nbRadial <=0) {
269     std::string aFeatureError = "Multirotation builder ";
270     aFeatureError+=":: the number of copies for the radial direction is null or negative.";
271     setError(aFeatureError);
272     return;
273   }
274
275   // Getting angle
276   double anAngle;
277   std::string useAngularStep =
278     string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
279   if (!useAngularStep.empty()) {
280     anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
281   } else {
282     anAngle = 360./nbAngular;
283   }
284
285   // Getting step
286   double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
287
288   // Moving each object.
289   int aResultIndex = 0;
290   std::list<ResultPtr>::iterator aContext = aContextes.begin();
291   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
292         anObjectsIt++, aContext++) {
293     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
294     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
295
296     std::shared_ptr<GeomAPI_Dir> aDir =
297       GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
298     double x = aDir->x();
299     double y = aDir->y();
300     double z = aDir->z();
301     double norm = sqrt(x*x+y*y+z*z);
302
303     // Setting result.
304     if (isPart) {
305       /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
306       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
307       for (int j=0; j<aSecondNbCopies; j++) {
308         for (int i=0; i<aFirstNbCopies; i++) {
309           double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
310           double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
311           double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
312           aTrsf->setTranslation(dx, dy, dz);
313           ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
314           aResultPart->setTrsf(*aContext, aTrsf);
315           setResult(aResultPart, aResultIndex);
316           aResultIndex++;
317         }
318       }*/
319     } else {
320       ListOfShape aListOfShape;
321       std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
322       std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
323       for (int j=0; j<nbRadial; j++) {
324         // Translation
325         double dx = j*aStep*x/norm;
326         double dy = j*aStep*y/norm;
327         double dz = j*aStep*z/norm;
328         std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
329           new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
330
331         if (!aTranslationAlgo->check()) {
332           setError(aTranslationAlgo->getError());
333           break;
334         }
335
336         aTranslationAlgo->build();
337
338         // Checking that the algorithm worked properly.
339         if (!aTranslationAlgo->isDone()) {
340           static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
341           setError(aFeatureError);
342           break;
343         }
344         if (aTranslationAlgo->shape()->isNull()) {
345           static const std::string aShapeError = "Error : Resulting shape is null.";
346           setError(aShapeError);
347           break;
348         }
349         if (!aTranslationAlgo->isValid()) {
350           static const std::string aFeatureError = "Error : Resulting shape in not valid.";
351           setError(aFeatureError);
352           break;
353         }
354         aListOfShape.push_back(aTranslationAlgo->shape());
355         aListOfTranslationAlgo.push_back(aTranslationAlgo);
356         for (int i=1; i<nbAngular; i++) {
357           std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
358             new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
359           if (!aRotationnAlgo->check()) {
360             setError(aTranslationAlgo->getError());
361             break;
362           }
363           aRotationnAlgo->build();// Checking that the algorithm worked properly.
364           if (!aRotationnAlgo->isDone()) {
365             static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
366             setError(aFeatureError);
367             break;
368           }
369           if (aRotationnAlgo->shape()->isNull()) {
370             static const std::string aShapeError = "Error : Resulting shape is null.";
371             setError(aShapeError);
372             break;
373           }
374           if (!aRotationnAlgo->isValid()) {
375             static const std::string aFeatureError = "Error : Resulting shape in not valid.";
376             setError(aFeatureError);
377             break;
378           }
379           aListOfShape.push_back(aRotationnAlgo->shape());
380           aListOfRotationAlgo.push_back(aRotationnAlgo);
381         }
382       }
383       std::shared_ptr<GeomAPI_Shape> aCompound =
384         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
385       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
386       aResultBody->storeModified(aBaseShape, aCompound);
387
388       loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
389       loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
390       setResult(aResultBody, aResultIndex);
391     }
392     aResultIndex++;
393   }
394
395   // Remove the rest results if there were produced in the previous pass.
396   removeResults(aResultIndex);
397 }
398
399 //=================================================================================================
400 void FeaturesPlugin_MultiRotation::loadNamingDS2(
401     std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
402     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
403     std::shared_ptr<GeomAPI_Shape> theBaseShape)
404 {
405   for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
406     theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
407     // naming of faces
408     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
409
410     // naming of edges
411     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
412
413     // naming of vertex
414     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
415   }
416 }
417
418 //=================================================================================================
419 void FeaturesPlugin_MultiRotation::loadNamingDS3(
420     std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
421     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
422     std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
423 {
424   int anIndex = nb+1;
425   std::string aRotatedName;
426
427   for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
428     theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
429
430     // naming of faces
431     int numFace = 1;
432     GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
433     for(; anExp.more(); anExp.next()) {
434        aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
435        aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
436        theResultBody->generated(anExp.current(), aRotatedName);
437        ++numFace;
438     }
439     ++anIndex;
440   }
441 }
442 #endif
443
444 //=================================================================================================
445 void FeaturesPlugin_MultiRotation::loadNamingDS(
446     std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
447     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
448     std::shared_ptr<GeomAPI_Shape> theBaseShape)
449 {
450   for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
451     theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
452     // naming of faces
453     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
454
455     // naming of edges
456     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
457
458     // naming of vertex
459     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
460   }
461 }