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