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