]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Translation.cpp
Salome HOME
[Code coverage FeaturesPlugin]: Improve coverage of Partition, Symmetry, (Multi)Rotat...
[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
139   if (!anEdge.get())
140   {
141     setError(aSelectionError);
142     return;
143   }
144
145   std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
146                                                       anEdge->line()->direction()));
147
148
149   // Getting distance.
150   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
151
152   // Moving each object.
153   int aResultIndex = 0;
154   std::list<ResultPtr>::iterator aContext = aContextes.begin();
155   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
156         anObjectsIt++, aContext++) {
157     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
158     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
159
160     // Setting result.
161     if (isPart) {
162       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
163       aTrsf->setTranslation(anAxis, aDistance);
164       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
165       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
166       aResultPart->setTrsf(*aContext, aTrsf);
167       setResult(aResultPart, aResultIndex);
168     } else {
169       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
170         new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
171
172       if (!aTranslationAlgo->check()) {
173         setError(aTranslationAlgo->getError());
174         return;
175       }
176
177       aTranslationAlgo->build();
178
179       // Checking that the algorithm worked properly.
180       if(!aTranslationAlgo->isDone()) {
181         static const std::string aFeatureError = "Error: Translation algorithm failed.";
182         setError(aFeatureError);
183         break;
184       }
185       if(aTranslationAlgo->shape()->isNull()) {
186         static const std::string aShapeError = "Error: Resulting shape is Null.";
187         setError(aShapeError);
188         break;
189       }
190       if(!aTranslationAlgo->isValid()) {
191         std::string aFeatureError = "Error: Resulting shape is not valid.";
192         setError(aFeatureError);
193         break;
194       }
195
196       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
197       aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
198       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
199                                                aBaseShape,
200                                                aTranslationAlgo,
201                                                "Translated");
202       setResult(aResultBody, aResultIndex);
203     }
204     aResultIndex++;
205   }
206
207   // Remove the rest results if there were produced in the previous pass.
208   removeResults(aResultIndex);
209 }
210
211 //=================================================================================================
212 void FeaturesPlugin_Translation::performTranslationByDimensions()
213 {
214   // Getting objects.
215   ListOfShape anObjects;
216   std::list<ResultPtr> aContextes;
217   AttributeSelectionListPtr anObjectsSelList =
218     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
219   if (anObjectsSelList->size() == 0) {
220     return;
221   }
222   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
223     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
224       anObjectsSelList->value(anObjectsIndex);
225     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
226     if(!anObject.get()) { // may be for not-activated parts
227       return;
228     }
229     anObjects.push_back(anObject);
230     aContextes.push_back(anObjectAttr->context());
231   }
232
233   // Getting dimensions in X, in Y and in Z
234   double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
235   double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
236   double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
237
238   // Moving each object.
239   int aResultIndex = 0;
240   std::list<ResultPtr>::iterator aContext = aContextes.begin();
241   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
242         anObjectsIt++, aContext++) {
243     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
244     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
245
246     // Setting result.
247     if (isPart) {
248       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
249       aTrsf->setTranslation(aDX, aDY, aDZ);
250       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
251       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
252       aResultPart->setTrsf(*aContext, aTrsf);
253       setResult(aResultPart, aResultIndex);
254     } else {
255       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
256         new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
257
258       if (!aTranslationAlgo->check()) {
259         setError(aTranslationAlgo->getError());
260         return;
261       }
262
263       aTranslationAlgo->build();
264
265       // Checking that the algorithm worked properly.
266       if(!aTranslationAlgo->isDone()) {
267         static const std::string aFeatureError = "Error: Translation algorithm failed.";
268         setError(aFeatureError);
269         break;
270       }
271       if(aTranslationAlgo->shape()->isNull()) {
272         static const std::string aShapeError = "Error: Resulting shape is Null.";
273         setError(aShapeError);
274         break;
275       }
276       if(!aTranslationAlgo->isValid()) {
277         std::string aFeatureError = "Error: Resulting shape is not valid.";
278         setError(aFeatureError);
279         break;
280       }
281
282       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
283       aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
284       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
285                                                aBaseShape,
286                                                aTranslationAlgo,
287                                                "Translated");
288       setResult(aResultBody, aResultIndex);
289     }
290     aResultIndex++;
291   }
292
293   // Remove the rest results if there were produced in the previous pass.
294   removeResults(aResultIndex);
295 }
296
297 //=================================================================================================
298 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
299 {
300   // Getting objects.
301   ListOfShape anObjects;
302   std::list<ResultPtr> aContextes;
303   AttributeSelectionListPtr anObjectsSelList =
304     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
305   if (anObjectsSelList->size() == 0) {
306     return;
307   }
308   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
309     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
310       anObjectsSelList->value(anObjectsIndex);
311     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
312     if(!anObject.get()) { // may be for not-activated parts
313       return;
314     }
315     anObjects.push_back(anObject);
316     aContextes.push_back(anObjectAttr->context());
317   }
318
319   // Getting the start point and the end point
320   AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
321   AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
322   std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
323   std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
324   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
325     GeomShapePtr aShape1 = aRef1->value();
326     if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
327       aShape1 = aRef1->context()->shape();
328     GeomShapePtr aShape2 = aRef2->value();
329     if (!aShape2.get())
330       aShape2 = aRef2->context()->shape();
331     if (aShape1 && aShape2) {
332       aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
333       aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
334     }
335   }
336
337   // Moving each object.
338   int aResultIndex = 0;
339   std::list<ResultPtr>::iterator aContext = aContextes.begin();
340   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
341         anObjectsIt++, aContext++) {
342     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
343     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
344
345     // Setting result.
346     if (isPart) {
347       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
348       aTrsf->setTranslation(aFirstPoint, aSecondPoint);
349       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
350       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
351       aResultPart->setTrsf(*aContext, aTrsf);
352       setResult(aResultPart, aResultIndex);
353     } else {
354       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
355         new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
356
357       if (!aTranslationAlgo->check()) {
358         setError(aTranslationAlgo->getError());
359         return;
360       }
361
362       aTranslationAlgo->build();
363
364       // Checking that the algorithm worked properly.
365       if(!aTranslationAlgo->isDone()) {
366         static const std::string aFeatureError = "Error: Translation algorithm failed.";
367         setError(aFeatureError);
368         break;
369       }
370       if(aTranslationAlgo->shape()->isNull()) {
371         static const std::string aShapeError = "Error: Resulting shape is Null.";
372         setError(aShapeError);
373         break;
374       }
375       if(!aTranslationAlgo->isValid()) {
376         std::string aFeatureError = "Error: Resulting shape is not valid.";
377         setError(aFeatureError);
378         break;
379       }
380
381       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
382       aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
383       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
384                                                aBaseShape,
385                                                aTranslationAlgo,
386                                                "Translated");
387       setResult(aResultBody, aResultIndex);
388     }
389     aResultIndex++;
390   }
391
392   // Remove the rest results if there were produced in the previous pass.
393   removeResults(aResultIndex);
394 }