Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Symmetry.cpp
1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_Symmetry.cpp
4 // Created:     30 Nov 2016
5 // Author:      Clarisse Genrault (CEA)
6
7 #include <FeaturesPlugin_Symmetry.h>
8
9 #include <GeomAlgoAPI_PointBuilder.h>
10
11 #include <GeomAPI_Edge.h>
12 #include <GeomAPI_Face.h>
13 #include <GeomAPI_Lin.h>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAPI_Trsf.h>
16
17 #include <ModelAPI_AttributeSelectionList.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_ResultBody.h>
20 #include <ModelAPI_ResultPart.h>
21
22 #include <iostream>
23 #include <GeomAlgoAPI_FaceBuilder.h>
24
25 //=================================================================================================
26 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
27 {
28 }
29
30 //=================================================================================================
31 void FeaturesPlugin_Symmetry::initAttributes()
32 {
33   data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
34                        ModelAPI_AttributeString::typeId());
35
36   AttributeSelectionListPtr aSelection =
37     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
38     FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39
40   data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
41                        ModelAPI_AttributeSelection::typeId());
42
43   data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
44                        ModelAPI_AttributeSelection::typeId());
45
46   data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
47                        ModelAPI_AttributeSelection::typeId());
48 }
49
50 //=================================================================================================
51 void FeaturesPlugin_Symmetry::execute()
52 {
53   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
54   std::string aMethodType = aMethodTypeAttr->value();
55
56   if (aMethodType == CREATION_METHOD_BY_POINT()) {
57     performSymmetryByPoint();
58   }
59
60   if (aMethodType == CREATION_METHOD_BY_AXIS()) {
61     performSymmetryByAxis();
62   }
63
64   if (aMethodType == CREATION_METHOD_BY_PLANE()) {
65     performSymmetryByPlane();
66   }
67 }
68
69 //=================================================================================================
70 void FeaturesPlugin_Symmetry::performSymmetryByPoint()
71 {
72   // Getting objects.
73   ListOfShape anObjects;
74   std::list<ResultPtr> aContextes;
75   AttributeSelectionListPtr anObjectsSelList =
76     selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
77   if (anObjectsSelList->size() == 0) {
78     return;
79   }
80   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
81     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
82       anObjectsSelList->value(anObjectsIndex);
83     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
84     if(!anObject.get()) { // may be for not-activated parts
85       eraseResults();
86       return;
87     }
88     anObjects.push_back(anObject);
89     aContextes.push_back(anObjectAttr->context());
90   }
91
92   //Getting point.
93   std::shared_ptr<GeomAPI_Pnt> aPoint;
94   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
95     selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
96   if (anObjRef.get() != NULL) {
97     GeomShapePtr aShape1 = anObjRef->value();
98     if (!aShape1.get()) {
99       aShape1 = anObjRef->context()->shape();
100     }
101     if (aShape1) {
102       aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
103     }
104   }
105
106   // Moving each object.
107   int aResultIndex = 0;
108   std::list<ResultPtr>::iterator aContext = aContextes.begin();
109   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
110         anObjectsIt++, aContext++) {
111     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
112     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
113
114     // Setting result.
115     if (isPart) {
116       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
117       aTrsf->setSymmetry(aPoint);
118       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
119       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
120       aResultPart->setTrsf(*aContext, aTrsf);
121       setResult(aResultPart, aResultIndex);
122     } else {
123       GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPoint);
124
125       if (!aSymmetryAlgo.check()) {
126         setError(aSymmetryAlgo.getError());
127         return;
128       }
129
130       aSymmetryAlgo.build();
131
132       // Checking that the algorithm worked properly.
133       if(!aSymmetryAlgo.isDone()) {
134         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
135         setError(aFeatureError);
136         break;
137       }
138       if(aSymmetryAlgo.shape()->isNull()) {
139         static const std::string aShapeError = "Error: Resulting shape is Null.";
140         setError(aShapeError);
141         break;
142       }
143       if(!aSymmetryAlgo.isValid()) {
144         std::string aFeatureError = "Error: Resulting shape is not valid.";
145         setError(aFeatureError);
146         break;
147       }
148
149       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
150       loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
151       setResult(aResultBody, aResultIndex);
152     }
153     aResultIndex++;
154   }
155
156   // Remove the rest results if there were produced in the previous pass.
157   removeResults(aResultIndex);
158 }
159
160 //=================================================================================================
161 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
162 {
163   // Getting objects.
164   ListOfShape anObjects;
165   std::list<ResultPtr> aContextes;
166   AttributeSelectionListPtr anObjectsSelList =
167     selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
168   if (anObjectsSelList->size() == 0) {
169     return;
170   }
171   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
172     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
173       anObjectsSelList->value(anObjectsIndex);
174     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
175     if(!anObject.get()) { // may be for not-activated parts
176       eraseResults();
177       return;
178     }
179     anObjects.push_back(anObject);
180     aContextes.push_back(anObjectAttr->context());
181   }
182
183   //Getting axis.
184   std::shared_ptr<GeomAPI_Ax1> anAxis;
185   std::shared_ptr<GeomAPI_Edge> anEdge;
186   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
187     selection(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID());
188   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
189     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
190   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
191              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
192     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
193   }
194   if(anEdge) {
195     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
196                                                           anEdge->line()->direction()));
197   }
198
199   // Moving each object.
200   int aResultIndex = 0;
201   std::list<ResultPtr>::iterator aContext = aContextes.begin();
202   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
203         anObjectsIt++, aContext++) {
204     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
205     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
206
207     // Setting result.
208     if (isPart) {
209       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
210       aTrsf->setSymmetry(anAxis);
211       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
212       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
213       aResultPart->setTrsf(*aContext, aTrsf);
214       setResult(aResultPart, aResultIndex);
215     } else {
216       GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, anAxis);
217
218       if (!aSymmetryAlgo.check()) {
219         setError(aSymmetryAlgo.getError());
220         return;
221       }
222
223       aSymmetryAlgo.build();
224
225       // Checking that the algorithm worked properly.
226       if(!aSymmetryAlgo.isDone()) {
227         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
228         setError(aFeatureError);
229         break;
230       }
231       if(aSymmetryAlgo.shape()->isNull()) {
232         static const std::string aShapeError = "Error: Resulting shape is Null.";
233         setError(aShapeError);
234         break;
235       }
236       if(!aSymmetryAlgo.isValid()) {
237         std::string aFeatureError = "Error: Resulting shape is not valid.";
238         setError(aFeatureError);
239         break;
240       }
241
242       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
243       loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
244       setResult(aResultBody, aResultIndex);
245     }
246     aResultIndex++;
247   }
248
249   // Remove the rest results if there were produced in the previous pass.
250   removeResults(aResultIndex);
251 }
252
253 //=================================================================================================
254 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
255 {
256   // Getting objects.
257   ListOfShape anObjects;
258   std::list<ResultPtr> aContextes;
259   AttributeSelectionListPtr anObjectsSelList =
260     selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
261   if (anObjectsSelList->size() == 0) {
262     return;
263   }
264   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
265     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
266       anObjectsSelList->value(anObjectsIndex);
267     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
268     if(!anObject.get()) { // may be for not-activated parts
269       eraseResults();
270       return;
271     }
272     anObjects.push_back(anObject);
273     aContextes.push_back(anObjectAttr->context());
274   }
275
276   //Getting axis.
277   std::shared_ptr<GeomAPI_Ax2> aPlane;
278   std::shared_ptr<GeomAPI_Pln> aPln;
279   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
280     selection(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID());
281   if (anObjRef && anObjRef->value() && anObjRef->value()->isFace()) {
282     aPln = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->value()))->getPlane();
283   }
284   else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
285              anObjRef->context()->shape() && anObjRef->context()->shape()->isFace()) {
286     aPln =
287       std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->context()->shape()))->getPlane();
288   }
289   if (aPln) {
290     aPlane = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aPln->location(),
291                                                           aPln->direction()));
292   }
293
294   // Moving each object.
295   int aResultIndex = 0;
296   std::list<ResultPtr>::iterator aContext = aContextes.begin();
297   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
298         anObjectsIt++, aContext++) {
299     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
300     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
301
302     // Setting result.
303     if (isPart) {
304       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
305       aTrsf->setSymmetry(aPlane);
306       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
307       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
308       aResultPart->setTrsf(*aContext, aTrsf);
309       setResult(aResultPart, aResultIndex);
310     } else {
311       GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPlane);
312
313       if (!aSymmetryAlgo.check()) {
314         setError(aSymmetryAlgo.getError());
315         return;
316       }
317
318       aSymmetryAlgo.build();
319
320       // Checking that the algorithm worked properly.
321       if(!aSymmetryAlgo.isDone()) {
322         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
323         setError(aFeatureError);
324         break;
325       }
326       if(aSymmetryAlgo.shape()->isNull()) {
327         static const std::string aShapeError = "Error: Resulting shape is Null.";
328         setError(aShapeError);
329         break;
330       }
331       if(!aSymmetryAlgo.isValid()) {
332         std::string aFeatureError = "Error: Resulting shape is not valid.";
333         setError(aFeatureError);
334         break;
335       }
336
337       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
338       loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
339       setResult(aResultBody, aResultIndex);
340     }
341     aResultIndex++;
342   }
343
344   // Remove the rest results if there were produced in the previous pass.
345   removeResults(aResultIndex);
346 }
347
348 //=================================================================================================
349 void FeaturesPlugin_Symmetry::loadNamingDS(GeomAlgoAPI_Symmetry& theSymmetryAlgo,
350                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
351                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
352 {
353   // Store and name the result.
354   theResultBody->storeModified(theBaseShape, theSymmetryAlgo.shape());
355
356   // Name the faces
357   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theSymmetryAlgo.mapOfSubShapes();
358   int aReflectedTag = 1;
359   std::string aReflectedName = "Symmetried";
360   theResultBody->loadAndOrientModifiedShapes(&theSymmetryAlgo,
361                                               theBaseShape, GeomAPI_Shape::FACE,
362                                               aReflectedTag, aReflectedName, *aSubShapes.get());
363 }