Salome HOME
Task 3.2. To keep compounds’ sub-shapes for all operations (issue #3139)
[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       ListOfMakeShape aMakeShapeList;
177
178       for (int i=0; i<nbCopies; i++) {
179         std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
180           new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
181
182         // Checking that the algorithm worked properly.
183         if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
184             aTranslationAlgo, getKind(), anError)) {
185           setError(anError);
186           break;
187         }
188         aListOfShape.push_back(aTranslationAlgo->shape());
189         aMakeShapeList.push_back(aTranslationAlgo);
190       }
191
192       std::shared_ptr<GeomAlgoAPI_MakeShapeList>
193           aListOfTranslationAlgo(new GeomAlgoAPI_MakeShapeList(aMakeShapeList));
194
195       std::shared_ptr<GeomAPI_Shape> aCompound =
196         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
197       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
198
199       ListOfShape aBaseShapes;
200       aBaseShapes.push_back(aBaseShape);
201       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
202                                                aListOfTranslationAlgo, aCompound, "Translated");
203
204       setResult(aResultBody, aResultIndex);
205       aResultIndex++;
206     }
207   }
208
209   // Remove the rest results if there were produced in the previous pass.
210   removeResults(aResultIndex);
211 }
212
213 //=================================================================================================
214 void FeaturesPlugin_MultiTranslation::performTwoDirection()
215 {
216   // Getting objects.
217   ListOfShape anObjects;
218   std::list<ResultPtr> aContextes;
219   AttributeSelectionListPtr anObjectsSelList =
220     selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
221   if (anObjectsSelList->size() == 0) {
222     setError("Error: empty selection list");
223     return;
224   }
225   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
226     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
227       anObjectsSelList->value(anObjectsIndex);
228     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
229     if(!anObject.get()) { // may be for not-activated parts
230       return;
231     }
232     anObjects.push_back(anObject);
233     aContextes.push_back(anObjectAttr->context());
234   }
235
236   //Getting axis.
237   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
238   AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
239   GeomShapePtr aShape = anObjRef->value();
240   if (!aShape.get()) {
241     if (anObjRef->context().get()) {
242       aShape = anObjRef->context()->shape();
243     }
244   }
245   if (!aShape.get()) {
246     setError(aSelectionError);
247     return;
248   }
249
250   GeomEdgePtr anEdge;
251   if (aShape->isEdge())
252   {
253     anEdge = aShape->edge();
254   }
255   else if (aShape->isCompound())
256   {
257     GeomAPI_ShapeIterator anIt(aShape);
258     anEdge = anIt.current()->edge();
259   }
260
261   if (!anEdge.get())
262   {
263     setError(aSelectionError);
264     return;
265   }
266
267   std::shared_ptr<GeomAPI_Ax1> aFirstAxis(new GeomAPI_Ax1(anEdge->line()->location(),
268                                                           anEdge->line()->direction()));
269
270   //Getting axis.
271   anObjRef = selection(AXIS_SECOND_DIR_ID());
272   aShape = anObjRef->value();
273   if (!aShape.get()) {
274     if (anObjRef->context().get()) {
275       aShape = anObjRef->context()->shape();
276     }
277   }
278   if (!aShape.get()) {
279     setError(aSelectionError);
280     return;
281   }
282
283   if (aShape->isEdge())
284   {
285     anEdge = aShape->edge();
286   }
287   else if (aShape->isCompound())
288   {
289     GeomAPI_ShapeIterator anIt(aShape);
290     anEdge = anIt.current()->edge();
291   }
292
293   if (!anEdge.get())
294   {
295     setError(aSelectionError);
296     return;
297   }
298
299   std::shared_ptr<GeomAPI_Ax1> aSecondAxis(new GeomAPI_Ax1(anEdge->line()->location(),
300                                                            anEdge->line()->direction()));
301
302   // Getting step.
303   double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
304   double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
305
306   // Getting number of copies.
307   int aFirstNbCopies =
308     integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
309   int aSecondNbCopies =
310     integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
311
312   if (aFirstNbCopies <=0) {
313     std::string aFeatureError = "Multitranslation builder ";
314     aFeatureError+=":: the number of copies for the first direction is null or negative.";
315     setError(aFeatureError);
316     return;
317   }
318
319   if (aSecondNbCopies <=0) {
320     std::string aFeatureError = "Multitranslation builder ";
321     aFeatureError+=":: the number of copies for the second direction is null or negative.";
322     setError(aFeatureError);
323     return;
324   }
325
326   // Coord aFirstAxis
327   double x1 = aFirstAxis->dir()->x();
328   double y1 = aFirstAxis->dir()->y();
329   double z1 = aFirstAxis->dir()->z();
330   double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
331
332   // Coord aSecondAxis
333   double x2 = aSecondAxis->dir()->x();
334   double y2 = aSecondAxis->dir()->y();
335   double z2 = aSecondAxis->dir()->z();
336   double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
337
338   // Moving each object.
339   int aResultIndex = 0;
340   std::list<ResultPtr>::iterator aContext = aContextes.begin();
341   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
342         anObjectsIt++, aContext++) {
343     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
344     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
345
346     // Setting result.
347     if (isPart) {
348       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
349       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
350       for (int j=0; j<aSecondNbCopies; j++) {
351         for (int i=0; i<aFirstNbCopies; i++) {
352           double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
353           double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
354           double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
355           aTrsf->setTranslation(dx, dy, dz);
356           ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
357           aResultPart->setTrsf(*aContext, aTrsf);
358           setResult(aResultPart, aResultIndex);
359           aResultIndex++;
360         }
361       }
362     } else {
363       std::string anError;
364       ListOfShape aListOfShape;
365       ListOfMakeShape aMakeShapeList;
366
367       for (int j=0; j<aSecondNbCopies; j++) {
368         for (int i=0; i<aFirstNbCopies; i++) {
369           double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
370           double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
371           double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
372           std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
373             new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
374
375           // Checking that the algorithm worked properly.
376           if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
377               aTranslationAlgo, getKind(), anError)) {
378             setError(anError);
379             break;
380           }
381           aListOfShape.push_back(aTranslationAlgo->shape());
382           aMakeShapeList.push_back(aTranslationAlgo);
383         }
384       }
385
386       std::shared_ptr<GeomAlgoAPI_MakeShapeList>
387           aListOfTranslationAlgo(new GeomAlgoAPI_MakeShapeList(aMakeShapeList));
388
389       std::shared_ptr<GeomAPI_Shape> aCompound =
390         GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
391       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
392
393       ListOfShape aBaseShapes;
394       aBaseShapes.push_back(aBaseShape);
395       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
396                                                aListOfTranslationAlgo, aCompound, "Translated");
397
398       setResult(aResultBody, aResultIndex);
399       aResultIndex++;
400     }
401   }
402
403   // Remove the rest results if there were produced in the previous pass.
404   removeResults(aResultIndex);
405 }