Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Translation.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <FeaturesPlugin_Translation.h>
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultPart.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Lin.h>
32 #include <GeomAPI_ShapeIterator.h>
33 #include <GeomAPI_Trsf.h>
34
35 #include <GeomAlgoAPI_PointBuilder.h>
36 #include <GeomAlgoAPI_Tools.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   std::string anError;
154   int aResultIndex = 0;
155   std::list<ResultPtr>::iterator aContext = aContextes.begin();
156   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
157         anObjectsIt++, aContext++) {
158     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
159     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
160
161     // Setting result.
162     if (isPart) {
163       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
164       aTrsf->setTranslation(anAxis, aDistance);
165       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
166       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
167       aResultPart->setTrsf(*aContext, aTrsf);
168       setResult(aResultPart, aResultIndex);
169     } else {
170       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
171         new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
172
173       if (!aTranslationAlgo->check()) {
174         setError(aTranslationAlgo->getError());
175         return;
176       }
177
178       aTranslationAlgo->build();
179
180       // Checking that the algorithm worked properly.
181       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
182         setError(anError);
183         break;
184       }
185
186       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
187
188       ListOfShape aShapes;
189       aShapes.push_back(aBaseShape);
190       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
191                                                aShapes,
192                                                ListOfShape(),
193                                                aTranslationAlgo,
194                                                aTranslationAlgo->shape(),
195                                                "Translated");
196       setResult(aResultBody, aResultIndex);
197     }
198     aResultIndex++;
199   }
200
201   // Remove the rest results if there were produced in the previous pass.
202   removeResults(aResultIndex);
203 }
204
205 //=================================================================================================
206 void FeaturesPlugin_Translation::performTranslationByDimensions()
207 {
208   // Getting objects.
209   ListOfShape anObjects;
210   std::list<ResultPtr> aContextes;
211   AttributeSelectionListPtr anObjectsSelList =
212     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
213   if (anObjectsSelList->size() == 0) {
214     return;
215   }
216   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
217     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
218       anObjectsSelList->value(anObjectsIndex);
219     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
220     if(!anObject.get()) { // may be for not-activated parts
221       return;
222     }
223     anObjects.push_back(anObject);
224     aContextes.push_back(anObjectAttr->context());
225   }
226
227   // Getting dimensions in X, in Y and in Z
228   double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
229   double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
230   double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
231
232   // Moving each object.
233   std::string anError;
234   int aResultIndex = 0;
235   std::list<ResultPtr>::iterator aContext = aContextes.begin();
236   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
237         anObjectsIt++, aContext++) {
238     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
239     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
240
241     // Setting result.
242     if (isPart) {
243       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
244       aTrsf->setTranslation(aDX, aDY, aDZ);
245       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
246       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
247       aResultPart->setTrsf(*aContext, aTrsf);
248       setResult(aResultPart, aResultIndex);
249     } else {
250       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
251         new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
252
253       if (!aTranslationAlgo->check()) {
254         setError(aTranslationAlgo->getError());
255         return;
256       }
257
258       aTranslationAlgo->build();
259
260       // Checking that the algorithm worked properly.
261       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
262         setError(anError);
263         break;
264       }
265
266       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
267
268       ListOfShape aShapes;
269       aShapes.push_back(aBaseShape);
270       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
271                                                aShapes,
272                                                ListOfShape(),
273                                                aTranslationAlgo,
274                                                aTranslationAlgo->shape(),
275                                                "Translated");
276       setResult(aResultBody, aResultIndex);
277     }
278     aResultIndex++;
279   }
280
281   // Remove the rest results if there were produced in the previous pass.
282   removeResults(aResultIndex);
283 }
284
285 //=================================================================================================
286 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
287 {
288   // Getting objects.
289   ListOfShape anObjects;
290   std::list<ResultPtr> aContextes;
291   AttributeSelectionListPtr anObjectsSelList =
292     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
293   if (anObjectsSelList->size() == 0) {
294     return;
295   }
296   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
297     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
298       anObjectsSelList->value(anObjectsIndex);
299     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
300     if(!anObject.get()) { // may be for not-activated parts
301       return;
302     }
303     anObjects.push_back(anObject);
304     aContextes.push_back(anObjectAttr->context());
305   }
306
307   // Getting the start point and the end point
308   AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
309   AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
310   std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
311   std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
312   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
313     GeomShapePtr aShape1 = aRef1->value();
314     if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
315       aShape1 = aRef1->context()->shape();
316     GeomShapePtr aShape2 = aRef2->value();
317     if (!aShape2.get())
318       aShape2 = aRef2->context()->shape();
319     if (aShape1 && aShape2) {
320       aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
321       aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
322     }
323   }
324
325   // Moving each object.
326   std::string anError;
327   int aResultIndex = 0;
328   std::list<ResultPtr>::iterator aContext = aContextes.begin();
329   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
330         anObjectsIt++, aContext++) {
331     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
332     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
333
334     // Setting result.
335     if (isPart) {
336       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
337       aTrsf->setTranslation(aFirstPoint, aSecondPoint);
338       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
339       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
340       aResultPart->setTrsf(*aContext, aTrsf);
341       setResult(aResultPart, aResultIndex);
342     } else {
343       std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
344         new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
345
346       if (!aTranslationAlgo->check()) {
347         setError(aTranslationAlgo->getError());
348         return;
349       }
350
351       aTranslationAlgo->build();
352
353       // Checking that the algorithm worked properly.
354       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
355         setError(anError);
356         break;
357       }
358
359       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
360
361       ListOfShape aShapes;
362       aShapes.push_back(aBaseShape);
363       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
364                                                aShapes,
365                                                ListOfShape(),
366                                                aTranslationAlgo,
367                                                aTranslationAlgo->shape(),
368                                                "Translated");
369       setResult(aResultBody, aResultIndex);
370     }
371     aResultIndex++;
372   }
373
374   // Remove the rest results if there were produced in the previous pass.
375   removeResults(aResultIndex);
376 }