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