Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_MultiTranslation.cpp
1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_MultiTranslation.cpp
4 // Created:     30 Jan 2017
5 // Author:      Clarisse Genrault (CEA)
6
7 #include <FeaturesPlugin_MultiTranslation.h>
8
9 #include <GeomAlgoAPI_CompoundBuilder.h>
10
11 #include <GeomAPI_Ax1.h>
12 #include <GeomAPI_Edge.h>
13 #include <GeomAPI_Lin.h>
14
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeInteger.h>
17 #include <ModelAPI_AttributeSelectionList.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_ResultBody.h>
20 #include <ModelAPI_ResultPart.h>
21
22 #include <math.h>
23
24 //=================================================================================================
25 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
26 {
27 }
28
29 //=================================================================================================
30 void FeaturesPlugin_MultiTranslation::initAttributes()
31 {
32   AttributeSelectionListPtr aSelection =
33     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34     FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
35     ModelAPI_AttributeSelectionList::typeId()));
36
37   data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID(),
38                        ModelAPI_AttributeSelection::typeId());
39   data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID(),
40                        ModelAPI_AttributeDouble::typeId());
41   data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID(),
42                        ModelAPI_AttributeInteger::typeId());
43
44   data()->addAttribute(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID(),
45                        ModelAPI_AttributeString::typeId());
46   data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID(),
47                        ModelAPI_AttributeSelection::typeId());
48   data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID(),
49                        ModelAPI_AttributeDouble::typeId());
50   data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID(),
51                        ModelAPI_AttributeInteger::typeId());
52 }
53
54 //=================================================================================================
55 void FeaturesPlugin_MultiTranslation::execute()
56 {
57   std::string useSecondDir = string(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID())->value();
58   if(!useSecondDir.empty()) {
59     performTwoDirection();
60   } else {
61     performOneDirection();
62   }
63 }
64
65 //=================================================================================================
66 void FeaturesPlugin_MultiTranslation::performOneDirection()
67 {
68   // Getting objects.
69   ListOfShape anObjects;
70   std::list<ResultPtr> aContextes;
71   AttributeSelectionListPtr anObjectsSelList =
72     selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
73   if (anObjectsSelList->size() == 0) {
74     return;
75   }
76   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
77     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
78       anObjectsSelList->value(anObjectsIndex);
79     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
80     if(!anObject.get()) { // may be for not-activated parts
81       eraseResults();
82       return;
83     }
84     anObjects.push_back(anObject);
85     aContextes.push_back(anObjectAttr->context());
86   }
87
88   //Getting axis.
89   std::shared_ptr<GeomAPI_Ax1> anAxis;
90   std::shared_ptr<GeomAPI_Edge> anEdge;
91   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
92     selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
93   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
94     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
95   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
96              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
97     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
98   }
99   if(anEdge) {
100     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
101                                                           anEdge->line()->direction()));
102   }
103
104   // Getting step.
105   double aStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
106
107   // Getting number of copies.
108   int nbCopies =
109     integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
110
111   if (nbCopies <=0) {
112     std::string aFeatureError = "Multitranslation builder ";
113     aFeatureError+=":: the number of copies for the first direction is null or negative.";
114     setError(aFeatureError);
115   }
116
117   // Moving each object.
118   int aResultIndex = 0;
119   std::list<ResultPtr>::iterator aContext = aContextes.begin();
120   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
121         anObjectsIt++, aContext++) {
122     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
123     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
124
125     // Setting result.
126     if (isPart) {
127       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
128       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
129       for (int i=0; i<nbCopies; i++) {
130         aTrsf->setTranslation(anAxis, i*aStep);
131         ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
132         aResultPart->setTrsf(*aContext, aTrsf);
133         setResult(aResultPart, aResultIndex);
134         aResultIndex++;
135       }
136     } else {
137       ListOfShape aListOfShape;
138       std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
139
140       for (int i=0; i<nbCopies; i++) {
141         std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
142           new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
143
144         if (!aTranslationAlgo->check()) {
145           setError(aTranslationAlgo->getError());
146           break;
147         }
148
149         aTranslationAlgo->build();
150
151         // Checking that the algorithm worked properly.
152         if (!aTranslationAlgo->isDone()) {
153           static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
154           setError(aFeatureError);
155           break;
156         }
157         if (aTranslationAlgo->shape()->isNull()) {
158           static const std::string aShapeError = "Error : Resulting shape is null.";
159           setError(aShapeError);
160           break;
161         }
162         if (!aTranslationAlgo->isValid()) {
163           static const std::string aFeatureError = "Error : Resulting shape in not valid.";
164           setError(aFeatureError);
165           break;
166         }
167         aListOfShape.push_back(aTranslationAlgo->shape());
168         aListOfTranslationAlgo.push_back(aTranslationAlgo);
169       }
170       std::shared_ptr<GeomAPI_Shape> aCompound =
171         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
172       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
173       aResultBody->storeModified(aBaseShape, aCompound);
174       loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
175
176       setResult(aResultBody, aResultIndex);
177     }
178     aResultIndex++;
179   }
180
181   // Remove the rest results if there were produced in the previous pass.
182   removeResults(aResultIndex);
183 }
184
185 //=================================================================================================
186 void FeaturesPlugin_MultiTranslation::performTwoDirection()
187 {
188   // Getting objects.
189   ListOfShape anObjects;
190   std::list<ResultPtr> aContextes;
191   AttributeSelectionListPtr anObjectsSelList =
192     selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
193   if (anObjectsSelList->size() == 0) {
194     return;
195   }
196   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
197     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
198       anObjectsSelList->value(anObjectsIndex);
199     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
200     if(!anObject.get()) { // may be for not-activated parts
201       eraseResults();
202       return;
203     }
204     anObjects.push_back(anObject);
205     aContextes.push_back(anObjectAttr->context());
206   }
207
208   //Getting axis.
209   std::shared_ptr<GeomAPI_Ax1> aFirstAxis;
210   std::shared_ptr<GeomAPI_Edge> anEdge;
211   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
212     selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
213   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
214     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
215   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
216              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
217     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
218   }
219   if(anEdge) {
220     aFirstAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
221                                                               anEdge->line()->direction()));
222   }
223   std::shared_ptr<GeomAPI_Ax1> aSecondAxis;
224   anObjRef = selection(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID());
225   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
226     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
227   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
228              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
229     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
230   }
231   if(anEdge) {
232     aSecondAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
233                                                                anEdge->line()->direction()));
234   }
235
236   // Getting step.
237   double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
238   double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
239
240   // Getting number of copies.
241   int aFirstNbCopies =
242     integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
243   int aSecondNbCopies =
244     integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
245
246   if (aFirstNbCopies <=0) {
247     std::string aFeatureError = "Multitranslation builder ";
248     aFeatureError+=":: the number of copies for the first direction is null or negative.";
249     setError(aFeatureError);
250   }
251
252   if (aSecondNbCopies <=0) {
253     std::string aFeatureError = "Multitranslation builder ";
254     aFeatureError+=":: the number of copies for the second direction is null or negative.";
255     setError(aFeatureError);
256   }
257
258   // Coord aFirstAxis
259   double x1 = aFirstAxis->dir()->x();
260   double y1 = aFirstAxis->dir()->y();
261   double z1 = aFirstAxis->dir()->z();
262   double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
263
264   // Coord aSecondAxis
265   double x2 = aSecondAxis->dir()->x();
266   double y2 = aSecondAxis->dir()->y();
267   double z2 = aSecondAxis->dir()->z();
268   double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
269
270   // Moving each object.
271   int aResultIndex = 0;
272   std::list<ResultPtr>::iterator aContext = aContextes.begin();
273   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
274         anObjectsIt++, aContext++) {
275     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
276     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
277
278     // Setting result.
279     if (isPart) {
280       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
281       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
282       for (int j=0; j<aSecondNbCopies; j++) {
283         for (int i=0; i<aFirstNbCopies; i++) {
284           double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
285           double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
286           double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
287           aTrsf->setTranslation(dx, dy, dz);
288           ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
289           aResultPart->setTrsf(*aContext, aTrsf);
290           setResult(aResultPart, aResultIndex);
291           aResultIndex++;
292         }
293       }
294     } else {
295       ListOfShape aListOfShape;
296       std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
297
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           std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
304             new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
305
306           if (!aTranslationAlgo->check()) {
307             setError(aTranslationAlgo->getError());
308             break;
309           }
310
311           aTranslationAlgo->build();
312
313           // Checking that the algorithm worked properly.
314           if (!aTranslationAlgo->isDone()) {
315             static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
316             setError(aFeatureError);
317             break;
318           }
319           if (aTranslationAlgo->shape()->isNull()) {
320             static const std::string aShapeError = "Error : Resulting shape is null.";
321             setError(aShapeError);
322             break;
323           }
324           if (!aTranslationAlgo->isValid()) {
325             static const std::string aFeatureError = "Error : Resulting shape in not valid.";
326             setError(aFeatureError);
327            break;
328           }
329           aListOfShape.push_back(aTranslationAlgo->shape());
330           aListOfTranslationAlgo.push_back(aTranslationAlgo);
331         }
332       }
333       std::shared_ptr<GeomAPI_Shape> aCompound =
334         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
335       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
336       aResultBody->storeModified(aBaseShape, aCompound);
337       loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
338       setResult(aResultBody, aResultIndex);
339     }
340     aResultIndex++;
341   }
342
343   // Remove the rest results if there were produced in the previous pass.
344   removeResults(aResultIndex);
345 }
346
347 //=================================================================================================
348 void FeaturesPlugin_MultiTranslation::loadNamingDS(
349     std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
350     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
351     std::shared_ptr<GeomAPI_Shape> theBaseShape)
352 {
353   int aTag = 1;
354   int anIndex = 1;
355   std::string aTranslatedName;
356
357   for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
358     theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
359     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
360
361     // naming of faces
362     aTranslatedName = "Translated_Face_" + std::to_string((long long) anIndex);
363     theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
364                                                aTag++, aTranslatedName, *aSubShapes.get(),
365                                                false, true);
366
367     // naming of edges
368     aTranslatedName = "Translated_Edge_" + std::to_string((long long) anIndex);
369     theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
370                                                aTag++, aTranslatedName, *aSubShapes.get(),
371                                                false, true);
372
373     // naming of vertex
374     aTranslatedName = "Translated_Vertex_" + std::to_string((long long) anIndex);
375     theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
376                                                aTag++, aTranslatedName, *aSubShapes.get(),
377                                                false, true);
378
379     ++anIndex;
380   }
381 }