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