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