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