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