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