Salome HOME
e303f2b125c2def5ee388fcf618885bc56292b9d
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Translation.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_Translation.h>
22
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_BodyBuilder.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_ResultPart.h>
29 #include <ModelAPI_Session.h>
30
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_Trsf.h>
35
36 #include <GeomAlgoAPI_PointBuilder.h>
37 #include <GeomAlgoAPI_Tools.h>
38
39 #include <FeaturesPlugin_Tools.h>
40
41 //=================================================================================================
42 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
43 {
44 }
45
46 //=================================================================================================
47 void FeaturesPlugin_Translation::initAttributes()
48 {
49   data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(),
50                        ModelAPI_AttributeString::typeId());
51
52   AttributeSelectionListPtr aSelection =
53     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
54     FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
55
56   data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
57                        ModelAPI_AttributeSelection::typeId());
58   data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
59                        ModelAPI_AttributeDouble::typeId());
60
61   data()->addAttribute(FeaturesPlugin_Translation::DX_ID(),
62                        ModelAPI_AttributeDouble::typeId());
63   data()->addAttribute(FeaturesPlugin_Translation::DY_ID(),
64                        ModelAPI_AttributeDouble::typeId());
65   data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(),
66                        ModelAPI_AttributeDouble::typeId());
67
68   data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(),
69                        ModelAPI_AttributeSelection::typeId());
70   data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(),
71                        ModelAPI_AttributeSelection::typeId());
72 }
73
74 //=================================================================================================
75 void FeaturesPlugin_Translation::execute()
76 {
77   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD());
78   std::string aMethodType = aMethodTypeAttr->value();
79
80   if (aMethodType == CREATION_METHOD_BY_DISTANCE()) {
81     performTranslationByAxisAndDistance();
82   }
83
84   if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) {
85     performTranslationByDimensions();
86   }
87
88   if (aMethodType == CREATION_METHOD_BY_TWO_POINTS()) {
89     performTranslationByTwoPoints();
90   }
91 }
92
93 //=================================================================================================
94 void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
95 {
96   // Getting objects.
97   ListOfShape anObjects;
98   std::list<ResultPtr> aContextes;
99   AttributeSelectionListPtr anObjectsSelList =
100     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
101   if (anObjectsSelList->size() == 0) {
102     return;
103   }
104   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
105     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
106       anObjectsSelList->value(anObjectsIndex);
107     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
108     if(!anObject.get()) { // may be for not-activated parts
109       return;
110     }
111     anObjects.push_back(anObject);
112     aContextes.push_back(anObjectAttr->context());
113   }
114
115   //Getting axis.
116   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
117   AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
118   GeomShapePtr aShape = anObjRef->value();
119   if (!aShape.get()) {
120     if (anObjRef->context().get()) {
121       aShape = anObjRef->context()->shape();
122     }
123   }
124   if (!aShape.get()) {
125     setError(aSelectionError);
126     return;
127   }
128
129   GeomEdgePtr anEdge;
130   if (aShape->isEdge())
131   {
132     anEdge = aShape->edge();
133   }
134   else if (aShape->isCompound())
135   {
136     GeomAPI_ShapeIterator anIt(aShape);
137     anEdge = anIt.current()->edge();
138   }
139
140   if (!anEdge.get())
141   {
142     setError(aSelectionError);
143     return;
144   }
145
146   std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
147                                                       anEdge->line()->direction()));
148
149
150   // Getting distance.
151   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
152
153   // Moving each object.
154   std::string anError;
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       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
165       aTrsf->setTranslation(anAxis, aDistance);
166       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
167       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
168       aResultPart->setTrsf(*aContext, aTrsf);
169       setResult(aResultPart, aResultIndex);
170     } else {
171       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
172         new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
173
174       if (!aTranslationAlgo->check()) {
175         setError(aTranslationAlgo->getError());
176         return;
177       }
178
179       aTranslationAlgo->build();
180
181       // Checking that the algorithm worked properly.
182       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
183         setError(anError);
184         break;
185       }
186
187       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
188       aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
189       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
190                                                aBaseShape,
191                                                aTranslationAlgo,
192                                                "Translated");
193       setResult(aResultBody, aResultIndex);
194     }
195     aResultIndex++;
196   }
197
198   // Remove the rest results if there were produced in the previous pass.
199   removeResults(aResultIndex);
200 }
201
202 //=================================================================================================
203 void FeaturesPlugin_Translation::performTranslationByDimensions()
204 {
205   // Getting objects.
206   ListOfShape anObjects;
207   std::list<ResultPtr> aContextes;
208   AttributeSelectionListPtr anObjectsSelList =
209     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
210   if (anObjectsSelList->size() == 0) {
211     return;
212   }
213   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
214     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
215       anObjectsSelList->value(anObjectsIndex);
216     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
217     if(!anObject.get()) { // may be for not-activated parts
218       return;
219     }
220     anObjects.push_back(anObject);
221     aContextes.push_back(anObjectAttr->context());
222   }
223
224   // Getting dimensions in X, in Y and in Z
225   double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
226   double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
227   double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
228
229   // Moving each object.
230   std::string anError;
231   int aResultIndex = 0;
232   std::list<ResultPtr>::iterator aContext = aContextes.begin();
233   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
234         anObjectsIt++, aContext++) {
235     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
236     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
237
238     // Setting result.
239     if (isPart) {
240       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
241       aTrsf->setTranslation(aDX, aDY, aDZ);
242       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
243       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
244       aResultPart->setTrsf(*aContext, aTrsf);
245       setResult(aResultPart, aResultIndex);
246     } else {
247       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
248         new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
249
250       if (!aTranslationAlgo->check()) {
251         setError(aTranslationAlgo->getError());
252         return;
253       }
254
255       aTranslationAlgo->build();
256
257       // Checking that the algorithm worked properly.
258       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
259         setError(anError);
260         break;
261       }
262
263       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
264       aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
265       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
266                                                aBaseShape,
267                                                aTranslationAlgo,
268                                                "Translated");
269       setResult(aResultBody, aResultIndex);
270     }
271     aResultIndex++;
272   }
273
274   // Remove the rest results if there were produced in the previous pass.
275   removeResults(aResultIndex);
276 }
277
278 //=================================================================================================
279 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
280 {
281   // Getting objects.
282   ListOfShape anObjects;
283   std::list<ResultPtr> aContextes;
284   AttributeSelectionListPtr anObjectsSelList =
285     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
286   if (anObjectsSelList->size() == 0) {
287     return;
288   }
289   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
290     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
291       anObjectsSelList->value(anObjectsIndex);
292     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
293     if(!anObject.get()) { // may be for not-activated parts
294       return;
295     }
296     anObjects.push_back(anObject);
297     aContextes.push_back(anObjectAttr->context());
298   }
299
300   // Getting the start point and the end point
301   AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
302   AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
303   std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
304   std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
305   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
306     GeomShapePtr aShape1 = aRef1->value();
307     if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
308       aShape1 = aRef1->context()->shape();
309     GeomShapePtr aShape2 = aRef2->value();
310     if (!aShape2.get())
311       aShape2 = aRef2->context()->shape();
312     if (aShape1 && aShape2) {
313       aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
314       aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
315     }
316   }
317
318   // Moving each object.
319   std::string anError;
320   int aResultIndex = 0;
321   std::list<ResultPtr>::iterator aContext = aContextes.begin();
322   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
323         anObjectsIt++, aContext++) {
324     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
325     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
326
327     // Setting result.
328     if (isPart) {
329       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
330       aTrsf->setTranslation(aFirstPoint, aSecondPoint);
331       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
332       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
333       aResultPart->setTrsf(*aContext, aTrsf);
334       setResult(aResultPart, aResultIndex);
335     } else {
336       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
337         new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
338
339       if (!aTranslationAlgo->check()) {
340         setError(aTranslationAlgo->getError());
341         return;
342       }
343
344       aTranslationAlgo->build();
345
346       // Checking that the algorithm worked properly.
347       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
348         setError(anError);
349         break;
350       }
351
352       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
353       aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
354       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
355                                                aBaseShape,
356                                                aTranslationAlgo,
357                                                "Translated");
358       setResult(aResultBody, aResultIndex);
359     }
360     aResultIndex++;
361   }
362
363   // Remove the rest results if there were produced in the previous pass.
364   removeResults(aResultIndex);
365 }