Salome HOME
Issue #2593: CEA 2018-2 Geometrical Naming
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Symmetry.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_Symmetry.h>
22
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_PointBuilder.h>
25 #include <GeomAlgoAPI_FaceBuilder.h>
26 #include <GeomAlgoAPI_Copy.h>
27 #include <GeomAlgoAPI_MakeShapeList.h>
28
29 #include <GeomAPI_Edge.h>
30 #include <GeomAPI_Face.h>
31 #include <GeomAPI_Lin.h>
32 #include <GeomAPI_Pln.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_Trsf.h>
35
36 #include <ModelAPI_AttributeBoolean.h>
37 #include <ModelAPI_AttributeSelectionList.h>
38 #include <ModelAPI_AttributeString.h>
39 #include <ModelAPI_ResultBody.h>
40 #include <ModelAPI_ResultPart.h>
41
42 #include <FeaturesPlugin_Tools.h>
43
44 //=================================================================================================
45 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
46 {
47 }
48
49 //=================================================================================================
50 void FeaturesPlugin_Symmetry::initAttributes()
51 {
52   data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
53                        ModelAPI_AttributeString::typeId());
54
55   AttributeSelectionListPtr aSelection =
56     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
57     FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
58
59   data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
60                        ModelAPI_AttributeSelection::typeId());
61
62   data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
63                        ModelAPI_AttributeSelection::typeId());
64
65   data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
66                        ModelAPI_AttributeSelection::typeId());
67
68   data()->addAttribute(FeaturesPlugin_Symmetry::KEEP_ORIGINAL_RESULT(),
69                        ModelAPI_AttributeBoolean::typeId());
70 }
71
72 //=================================================================================================
73 void FeaturesPlugin_Symmetry::execute()
74 {
75   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
76   std::string aMethodType = aMethodTypeAttr->value();
77
78   if (aMethodType == CREATION_METHOD_BY_POINT()) {
79     performSymmetryByPoint();
80   }
81
82   if (aMethodType == CREATION_METHOD_BY_AXIS()) {
83     performSymmetryByAxis();
84   }
85
86   if (aMethodType == CREATION_METHOD_BY_PLANE()) {
87     performSymmetryByPlane();
88   }
89 }
90
91 //=================================================================================================
92 bool FeaturesPlugin_Symmetry::collectSourceObjects(ListOfShape& theSourceShapes,
93                                                    std::list<ResultPtr>& theSourceResults)
94 {
95   AttributeSelectionListPtr anObjectsSelList =
96     selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
97   if (anObjectsSelList->size() == 0) {
98     return false;
99   }
100   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
101     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
102       anObjectsSelList->value(anObjectsIndex);
103     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
104     if (!anObject.get()) { // may be for not-activated parts
105       eraseResults();
106       return false;
107     }
108     theSourceShapes.push_back(anObject);
109     theSourceResults.push_back(anObjectAttr->context());
110   }
111   return true;
112 }
113
114 //=================================================================================================
115 void FeaturesPlugin_Symmetry::performSymmetryByPoint()
116 {
117   // Getting objects.
118   ListOfShape anObjects;
119   std::list<ResultPtr> aContextes;
120   if (!collectSourceObjects(anObjects, aContextes))
121     return;
122
123   //Getting point.
124   std::shared_ptr<GeomAPI_Pnt> aPoint;
125   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
126     selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
127   if (anObjRef.get() != NULL) {
128     GeomShapePtr aShape1 = anObjRef->value();
129     if (!aShape1.get()) {
130       aShape1 = anObjRef->context()->shape();
131     }
132     if (aShape1) {
133       aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
134     }
135   }
136
137   // Moving each object.
138   int aResultIndex = 0;
139   std::list<ResultPtr>::iterator aContext = aContextes.begin();
140   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
141         anObjectsIt++, aContext++) {
142     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
143     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
144
145     // Setting result.
146     if (isPart) {
147       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
148       aTrsf->setSymmetry(aPoint);
149       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
150       buildResult(anOrigin, aTrsf, aResultIndex);
151     }
152     else {
153       std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
154         new GeomAlgoAPI_Symmetry(aBaseShape, aPoint));
155
156       if (!aSymmetryAlgo->check()) {
157         setError(aSymmetryAlgo->getError());
158         return;
159       }
160
161       aSymmetryAlgo->build();
162
163       // Checking that the algorithm worked properly.
164       if(!aSymmetryAlgo->isDone()) {
165         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
166         setError(aFeatureError);
167         break;
168       }
169       if(aSymmetryAlgo->shape()->isNull()) {
170         static const std::string aShapeError = "Error: Resulting shape is Null.";
171         setError(aShapeError);
172         break;
173       }
174       if(!aSymmetryAlgo->isValid()) {
175         std::string aFeatureError = "Error: Resulting shape is not valid.";
176         setError(aFeatureError);
177         break;
178       }
179
180       buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
181     }
182     aResultIndex++;
183   }
184
185   // Remove the rest results if there were produced in the previous pass.
186   removeResults(aResultIndex);
187 }
188
189 //=================================================================================================
190 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
191 {
192   // Getting objects.
193   ListOfShape anObjects;
194   std::list<ResultPtr> aContextes;
195   if (!collectSourceObjects(anObjects, aContextes))
196     return;
197
198   //Getting axis.
199   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
200   AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
201   GeomShapePtr aShape = anObjRef->value();
202   if (!aShape.get()) {
203     if (anObjRef->context().get()) {
204       aShape = anObjRef->context()->shape();
205     }
206   }
207   if (!aShape.get()) {
208     setError(aSelectionError);
209     return;
210   }
211
212   GeomEdgePtr anEdge;
213   if (aShape->isEdge())
214   {
215     anEdge = aShape->edge();
216   }
217   else if (aShape->isCompound())
218   {
219     GeomAPI_ShapeIterator anIt(aShape);
220     anEdge = anIt.current()->edge();
221   }
222   else
223   {
224     setError(aSelectionError);
225     return;
226   }
227
228   if (!anEdge.get())
229   {
230     setError(aSelectionError);
231     return;
232   }
233
234   std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
235                                                        anEdge->line()->direction()));
236
237
238   // Moving each object.
239   int aResultIndex = 0;
240   std::list<ResultPtr>::iterator aContext = aContextes.begin();
241   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
242         anObjectsIt++, aContext++) {
243     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
244     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
245
246     // Setting result.
247     if (isPart) {
248       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
249       aTrsf->setSymmetry(anAxis);
250       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
251       buildResult(anOrigin, aTrsf, aResultIndex);
252     }
253     else {
254       std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
255         new GeomAlgoAPI_Symmetry(aBaseShape, anAxis));
256
257       if (!aSymmetryAlgo->check()) {
258         setError(aSymmetryAlgo->getError());
259         return;
260       }
261
262       aSymmetryAlgo->build();
263
264       // Checking that the algorithm worked properly.
265       if(!aSymmetryAlgo->isDone()) {
266         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
267         setError(aFeatureError);
268         break;
269       }
270       if(aSymmetryAlgo->shape()->isNull()) {
271         static const std::string aShapeError = "Error: Resulting shape is Null.";
272         setError(aShapeError);
273         break;
274       }
275       if(!aSymmetryAlgo->isValid()) {
276         std::string aFeatureError = "Error: Resulting shape is not valid.";
277         setError(aFeatureError);
278         break;
279       }
280
281       buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
282     }
283     aResultIndex++;
284   }
285
286   // Remove the rest results if there were produced in the previous pass.
287   removeResults(aResultIndex);
288 }
289
290 //=================================================================================================
291 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
292 {
293   // Getting objects.
294   ListOfShape anObjects;
295   std::list<ResultPtr> aContextes;
296   if (!collectSourceObjects(anObjects, aContextes))
297     return;
298
299   //Getting plane.
300   static const std::string aSelectionError = "Error: The plane shape selection is bad.";
301   AttributeSelectionPtr anObjRef = selection(PLANE_OBJECT_ID());
302   GeomShapePtr aShape = anObjRef->value();
303   if (!aShape.get()) {
304     if (anObjRef->context().get()) {
305       aShape = anObjRef->context()->shape();
306     }
307   }
308   if (!aShape.get()) {
309     setError(aSelectionError);
310     return;
311   }
312
313   GeomFacePtr aFace;
314   if (aShape->isFace())
315   {
316     aFace = aShape->face();
317   }
318   else if (aShape->isCompound())
319   {
320     GeomAPI_ShapeIterator anIt(aShape);
321     aFace = anIt.current()->face();
322   }
323   else
324   {
325     setError(aSelectionError);
326     return;
327   }
328
329   if (!aFace.get())
330   {
331     setError(aSelectionError);
332     return;
333   }
334
335   std::shared_ptr<GeomAPI_Ax2> aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(),
336                                                       aFace->getPlane()->direction()));
337
338
339   // Moving each object.
340   int aResultIndex = 0;
341   std::list<ResultPtr>::iterator aContext = aContextes.begin();
342   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
343         anObjectsIt++, aContext++) {
344     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
345     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
346
347     // Setting result.
348     if (isPart) {
349       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
350       aTrsf->setSymmetry(aPlane);
351       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
352       buildResult(anOrigin, aTrsf, aResultIndex);
353     } else {
354       std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
355         new GeomAlgoAPI_Symmetry(aBaseShape, aPlane));
356
357       if (!aSymmetryAlgo->check()) {
358         setError(aSymmetryAlgo->getError());
359         return;
360       }
361
362       aSymmetryAlgo->build();
363
364       // Checking that the algorithm worked properly.
365       if(!aSymmetryAlgo->isDone()) {
366         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
367         setError(aFeatureError);
368         break;
369       }
370       if(aSymmetryAlgo->shape()->isNull()) {
371         static const std::string aShapeError = "Error: Resulting shape is Null.";
372         setError(aShapeError);
373         break;
374       }
375       if(!aSymmetryAlgo->isValid()) {
376         std::string aFeatureError = "Error: Resulting shape is not valid.";
377         setError(aFeatureError);
378         break;
379       }
380
381       buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
382     }
383     aResultIndex++;
384   }
385
386   // Remove the rest results if there were produced in the previous pass.
387   removeResults(aResultIndex);
388 }
389
390 //=================================================================================================
391 void FeaturesPlugin_Symmetry::buildResult(
392   std::shared_ptr<GeomAlgoAPI_Symmetry>& theSymmetryAlgo,
393   std::shared_ptr<GeomAPI_Shape> theBaseShape, int theResultIndex)
394 {
395   GeomAlgoAPI_MakeShapeList anAlgoList;
396   anAlgoList.appendAlgo(theSymmetryAlgo);
397   // Compose source shape and the result of symmetry.
398   GeomShapePtr aCompound;
399   if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
400     ListOfShape aShapes;
401     // add a copy of a base shape otherwise selection of this base shape is bad (2592)
402     std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
403     aShapes.push_back(aCopyAlgo->shape());
404     anAlgoList.appendAlgo(aCopyAlgo);
405
406     aShapes.push_back(theSymmetryAlgo->shape());
407     aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
408   } else
409     aCompound = theSymmetryAlgo->shape();
410
411   // Store and name the result.
412   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
413   aResultBody->storeModified(theBaseShape, aCompound);
414   loadNamingDS(anAlgoList, aResultBody, theBaseShape);
415   setResult(aResultBody, theResultIndex);
416 }
417
418 //=================================================================================================
419 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
420                                           std::shared_ptr<GeomAPI_Trsf> theTrsf,
421                                           int& theResultIndex)
422 {
423   if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
424     std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
425     ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
426     aCopy->setTrsf(theOriginal, anIdentity);
427     setResult(aCopy, theResultIndex);
428     ++theResultIndex;
429   }
430
431   ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
432   aResultPart->setTrsf(theOriginal, theTrsf);
433   setResult(aResultPart, theResultIndex);
434 }
435
436 //=================================================================================================
437 void FeaturesPlugin_Symmetry::loadNamingDS(GeomAlgoAPI_MakeShapeList& theAlgo,
438                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
439                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
440 {
441   // Name the faces
442   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theAlgo.mapOfSubShapes();
443   std::string aReflectedName = "Symmetried";
444   FeaturesPlugin_Tools::storeModifiedShapes(theAlgo, theResultBody,
445                                             theBaseShape, 1, 2, 3, aReflectedName,
446                                             *aSubShapes.get());
447 }