Salome HOME
68151d9c6af07e6328f6e6a75b1b9b9cfa1d7763
[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         // Checking that the algorithm worked properly.
206         if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationnAlgo, getKind(), anError)) {
207           setError(anError);
208           break;
209         }
210         aListOfShape.push_back(aRotationnAlgo->shape());
211         aListOfRotationAlgo->appendAlgo(aRotationnAlgo);
212       }
213       std::shared_ptr<GeomAPI_Shape> aCompound =
214         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
215       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
216
217       ListOfShape aBaseShapes;
218       aBaseShapes.push_back(aBaseShape);
219       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
220                                                aListOfRotationAlgo, aCompound, "Rotated");
221
222       setResult(aResultBody, aResultIndex);
223       aResultIndex++;
224     }
225   }
226
227   // Remove the rest results if there were produced in the previous pass.
228   removeResults(aResultIndex);
229 }
230
231 //=================================================================================================
232 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
233 void FeaturesPlugin_MultiRotation::performRotation2D()
234 {
235   // Getting objects.
236   ListOfShape anObjects;
237   std::list<ResultPtr> aContextes;
238   AttributeSelectionListPtr anObjectsSelList =
239     selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
240   if (anObjectsSelList->size() == 0) {
241     return;
242   }
243   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
244     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
245       anObjectsSelList->value(anObjectsIndex);
246     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
247     if(!anObject.get()) { // may be for not-activated parts
248       return;
249     }
250     anObjects.push_back(anObject);
251     aContextes.push_back(anObjectAttr->context());
252   }
253
254   //Getting axis.
255   std::shared_ptr<GeomAPI_Ax1> anAxis;
256   std::shared_ptr<GeomAPI_Edge> anEdge;
257   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
258     selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
259   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
260     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
261   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
262              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
263     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
264   }
265   if(anEdge) {
266     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
267                                                           anEdge->line()->direction()));
268   }
269
270   // Getting number of copies int he angular direction.
271   int nbAngular =
272     integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
273
274   if (nbAngular <=0) {
275     std::string aFeatureError = "Multirotation builder ";
276     aFeatureError+=":: the number of copies for the angular direction is null or negative.";
277     setError(aFeatureError);
278     return;
279   }
280
281   // Getting number of copies int he radial direction.
282   int nbRadial =
283     integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
284
285   if (nbRadial <=0) {
286     std::string aFeatureError = "Multirotation builder ";
287     aFeatureError+=":: the number of copies for the radial direction is null or negative.";
288     setError(aFeatureError);
289     return;
290   }
291
292   // Getting angle
293   double anAngle;
294   std::string useAngularStep =
295     string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
296   if (!useAngularStep.empty()) {
297     anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
298   } else {
299     anAngle = 360./nbAngular;
300   }
301
302   // Getting step
303   double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
304
305   // Moving each object.
306   int aResultIndex = 0;
307   std::list<ResultPtr>::iterator aContext = aContextes.begin();
308   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
309         anObjectsIt++, aContext++) {
310     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
311     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
312
313     std::shared_ptr<GeomAPI_Dir> aDir =
314       GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
315     double x = aDir->x();
316     double y = aDir->y();
317     double z = aDir->z();
318     double norm = sqrt(x*x+y*y+z*z);
319
320     // Setting result.
321     if (isPart) {
322       /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
323       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
324       for (int j=0; j<aSecondNbCopies; j++) {
325         for (int i=0; i<aFirstNbCopies; i++) {
326           double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
327           double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
328           double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
329           aTrsf->setTranslation(dx, dy, dz);
330           ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
331           aResultPart->setTrsf(*aContext, aTrsf);
332           setResult(aResultPart, aResultIndex);
333           aResultIndex++;
334         }
335       }*/
336     } else {
337       ListOfShape aListOfShape;
338       std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
339       std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
340       for (int j=0; j<nbRadial; j++) {
341         // Translation
342         double dx = j*aStep*x/norm;
343         double dy = j*aStep*y/norm;
344         double dz = j*aStep*z/norm;
345         std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
346           new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
347
348         if (!aTranslationAlgo->check()) {
349           setError(aTranslationAlgo->getError());
350           break;
351         }
352
353         aTranslationAlgo->build();
354
355         // Checking that the algorithm worked properly.
356         if (!aTranslationAlgo->isDone()) {
357           static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
358           setError(aFeatureError);
359           break;
360         }
361         if (aTranslationAlgo->shape()->isNull()) {
362           static const std::string aShapeError = "Error : Resulting shape is null.";
363           setError(aShapeError);
364           break;
365         }
366         if (!aTranslationAlgo->isValid()) {
367           static const std::string aFeatureError = "Error : Resulting shape in not valid.";
368           setError(aFeatureError);
369           break;
370         }
371         aListOfShape.push_back(aTranslationAlgo->shape());
372         aListOfTranslationAlgo.push_back(aTranslationAlgo);
373         for (int i=1; i<nbAngular; i++) {
374           std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
375             new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
376           if (!aRotationnAlgo->check()) {
377             setError(aTranslationAlgo->getError());
378             break;
379           }
380           aRotationnAlgo->build();// Checking that the algorithm worked properly.
381           if (!aRotationnAlgo->isDone()) {
382             static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
383             setError(aFeatureError);
384             break;
385           }
386           if (aRotationnAlgo->shape()->isNull()) {
387             static const std::string aShapeError = "Error : Resulting shape is null.";
388             setError(aShapeError);
389             break;
390           }
391           if (!aRotationnAlgo->isValid()) {
392             static const std::string aFeatureError = "Error : Resulting shape in not valid.";
393             setError(aFeatureError);
394             break;
395           }
396           aListOfShape.push_back(aRotationnAlgo->shape());
397           aListOfRotationAlgo.push_back(aRotationnAlgo);
398         }
399       }
400       std::shared_ptr<GeomAPI_Shape> aCompound =
401         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
402       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
403       aResultBody->storeModified(aBaseShape, aCompound);
404
405       loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
406       loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
407       setResult(aResultBody, aResultIndex);
408       aResultIndex++;
409     }
410   }
411
412   // Remove the rest results if there were produced in the previous pass.
413   removeResults(aResultIndex);
414 }
415
416 //=================================================================================================
417 void FeaturesPlugin_MultiRotation::loadNamingDS2(
418     std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
419     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
420     std::shared_ptr<GeomAPI_Shape> theBaseShape)
421 {
422   for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
423     theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
424     // naming of faces
425     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
426
427     // naming of edges
428     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
429
430     // naming of vertex
431     theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
432   }
433 }
434
435 //=================================================================================================
436 void FeaturesPlugin_MultiRotation::loadNamingDS3(
437     std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
438     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
439     std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
440 {
441   int anIndex = nb+1;
442   std::string aRotatedName;
443
444   for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
445     theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
446
447     // naming of faces
448     int numFace = 1;
449     GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
450     for(; anExp.more(); anExp.next()) {
451        aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
452        aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
453        theResultBody->generated(anExp.current(), aRotatedName);
454        ++numFace;
455     }
456     ++anIndex;
457   }
458 }
459 #endif