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