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