Salome HOME
Merge remote-tracking branch 'origin/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   // Moving each object.
112   int aResultIndex = 0;
113   std::list<ResultPtr>::iterator aContext = aContextes.begin();
114   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
115         anObjectsIt++, aContext++) {
116     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
117     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
118
119     // Setting result.
120     if (isPart) {
121       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
122       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
123       for (int i=0; i<nbCopies; i++) {
124         aTrsf->setTranslation(anAxis, i*aStep);
125         ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
126         aResultPart->setTrsf(*aContext, aTrsf);
127         setResult(aResultPart, aResultIndex);
128         aResultIndex++;
129       }
130     } else {
131       ListOfShape aListOfShape;
132       std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
133
134       for (int i=0; i<nbCopies; i++) {
135         std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
136           new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
137
138         if (!aTranslationAlgo->check()) {
139           setError(aTranslationAlgo->getError());
140           break;
141         }
142
143         aTranslationAlgo->build();
144
145         // Checking that the algorithm worked properly.
146         if (!aTranslationAlgo->isDone()) {
147           static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
148           setError(aFeatureError);
149           break;
150         }
151         if (aTranslationAlgo->shape()->isNull()) {
152           static const std::string aShapeError = "Error : Resulting shape is null.";
153           setError(aShapeError);
154           break;
155         }
156         if (!aTranslationAlgo->isValid()) {
157           static const std::string aFeatureError = "Error : Resulting shape in not valid.";
158           setError(aFeatureError);
159           break;
160         }
161         aListOfShape.push_back(aTranslationAlgo->shape());
162         aListOfTranslationAlgo.push_back(aTranslationAlgo);
163       }
164       std::shared_ptr<GeomAPI_Shape> aCompound =
165         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
166       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
167       aResultBody->storeModified(aBaseShape, aCompound);
168       loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
169
170       setResult(aResultBody, aResultIndex);
171     }
172     aResultIndex++;
173   }
174
175   // Remove the rest results if there were produced in the previous pass.
176   removeResults(aResultIndex);
177 }
178
179 //=================================================================================================
180 void FeaturesPlugin_MultiTranslation::performTwoDirection()
181 {
182   // Getting objects.
183   ListOfShape anObjects;
184   std::list<ResultPtr> aContextes;
185   AttributeSelectionListPtr anObjectsSelList =
186     selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
187   if (anObjectsSelList->size() == 0) {
188     return;
189   }
190   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
191     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
192       anObjectsSelList->value(anObjectsIndex);
193     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
194     if(!anObject.get()) { // may be for not-activated parts
195       eraseResults();
196       return;
197     }
198     anObjects.push_back(anObject);
199     aContextes.push_back(anObjectAttr->context());
200   }
201
202   //Getting axis.
203   std::shared_ptr<GeomAPI_Ax1> aFirstAxis;
204   std::shared_ptr<GeomAPI_Edge> anEdge;
205   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
206     selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
207   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
208     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
209   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
210              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
211     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
212   }
213   if(anEdge) {
214     aFirstAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
215                                                               anEdge->line()->direction()));
216   }
217   std::shared_ptr<GeomAPI_Ax1> aSecondAxis;
218   anObjRef = selection(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID());
219   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
220     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
221   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
222              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
223     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
224   }
225   if(anEdge) {
226     aSecondAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
227                                                                anEdge->line()->direction()));
228   }
229
230   // Getting step.
231   double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
232   double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
233
234   // Getting number of copies.
235   int aFirstNbCopies =
236     integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
237   int aSecondNbCopies =
238     integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
239
240   // Coord aFirstAxis
241   double x1 = aFirstAxis->dir()->x();
242   double y1 = aFirstAxis->dir()->y();
243   double z1 = aFirstAxis->dir()->z();
244   double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
245
246   // Coord aSecondAxis
247   double x2 = aSecondAxis->dir()->x();
248   double y2 = aSecondAxis->dir()->y();
249   double z2 = aSecondAxis->dir()->z();
250   double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
251
252   // Moving each object.
253   int aResultIndex = 0;
254   std::list<ResultPtr>::iterator aContext = aContextes.begin();
255   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
256         anObjectsIt++, aContext++) {
257     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
258     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
259
260     // Setting result.
261     if (isPart) {
262       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
263       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
264       for (int j=0; j<aSecondNbCopies; j++) {
265         for (int i=0; i<aFirstNbCopies; i++) {
266           double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
267           double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
268           double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
269           aTrsf->setTranslation(dx, dy, dz);
270           ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
271           aResultPart->setTrsf(*aContext, aTrsf);
272           setResult(aResultPart, aResultIndex);
273           aResultIndex++;
274         }
275       }
276     } else {
277       ListOfShape aListOfShape;
278       std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
279
280       for (int j=0; j<aSecondNbCopies; j++) {
281         for (int i=0; i<aFirstNbCopies; i++) {
282           double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
283           double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
284           double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
285           std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
286             new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
287
288           if (!aTranslationAlgo->check()) {
289             setError(aTranslationAlgo->getError());
290             break;
291           }
292
293           aTranslationAlgo->build();
294
295           // Checking that the algorithm worked properly.
296           if (!aTranslationAlgo->isDone()) {
297             static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
298             setError(aFeatureError);
299             break;
300           }
301           if (aTranslationAlgo->shape()->isNull()) {
302             static const std::string aShapeError = "Error : Resulting shape is null.";
303             setError(aShapeError);
304             break;
305           }
306           if (!aTranslationAlgo->isValid()) {
307             static const std::string aFeatureError = "Error : Resulting shape in not valid.";
308             setError(aFeatureError);
309            break;
310           }
311           aListOfShape.push_back(aTranslationAlgo->shape());
312           aListOfTranslationAlgo.push_back(aTranslationAlgo);
313         }
314       }
315       std::shared_ptr<GeomAPI_Shape> aCompound =
316         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
317       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
318       aResultBody->storeModified(aBaseShape, aCompound);
319       loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
320       setResult(aResultBody, aResultIndex);
321     }
322     aResultIndex++;
323   }
324
325   // Remove the rest results if there were produced in the previous pass.
326   removeResults(aResultIndex);
327 }
328
329 //=================================================================================================
330 void FeaturesPlugin_MultiTranslation::loadNamingDS(
331     std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
332     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
333     std::shared_ptr<GeomAPI_Shape> theBaseShape)
334 {
335   int aTag = 1;
336   int anIndex = 1;
337   std::string aTranslatedName;
338
339   for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
340     theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
341     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
342
343     // naming of faces
344     aTranslatedName = "Translated_Face_" + std::to_string((long long) anIndex);
345     theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
346                                                aTag++, aTranslatedName, *aSubShapes.get(),
347                                                false, true);
348
349     // naming of edges
350     aTranslatedName = "Translated_Edge_" + std::to_string((long long) anIndex);
351     theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
352                                                aTag++, aTranslatedName, *aSubShapes.get(),
353                                                false, true);
354
355     // naming of vertex
356     aTranslatedName = "Translated_Vertex_" + std::to_string((long long) anIndex);
357     theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
358                                                aTag++, aTranslatedName, *aSubShapes.get(),
359                                                false, true);
360
361     ++anIndex;
362   }
363 }