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