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