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