Salome HOME
Fix for the issue #2718 : results will be removed in case feature is invalid anyway...
[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       return false;
106     }
107     theSourceShapes.push_back(anObject);
108     theSourceResults.push_back(anObjectAttr->context());
109   }
110   return true;
111 }
112
113 //=================================================================================================
114 void FeaturesPlugin_Symmetry::performSymmetryByPoint()
115 {
116   // Getting objects.
117   ListOfShape anObjects;
118   std::list<ResultPtr> aContextes;
119   if (!collectSourceObjects(anObjects, aContextes))
120     return;
121
122   //Getting point.
123   std::shared_ptr<GeomAPI_Pnt> aPoint;
124   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
125     selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
126   if (anObjRef.get() != NULL) {
127     GeomShapePtr aShape1 = anObjRef->value();
128     if (!aShape1.get()) {
129       aShape1 = anObjRef->context()->shape();
130     }
131     if (aShape1) {
132       aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
133     }
134   }
135
136   // Moving each object.
137   int aResultIndex = 0;
138   std::list<ResultPtr>::iterator aContext = aContextes.begin();
139   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
140         anObjectsIt++, aContext++) {
141     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
142     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
143
144     // Setting result.
145     if (isPart) {
146       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
147       aTrsf->setSymmetry(aPoint);
148       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
149       buildResult(anOrigin, aTrsf, aResultIndex);
150     }
151     else {
152       std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
153         new GeomAlgoAPI_Symmetry(aBaseShape, aPoint));
154
155       if (!aSymmetryAlgo->check()) {
156         setError(aSymmetryAlgo->getError());
157         return;
158       }
159
160       aSymmetryAlgo->build();
161
162       // Checking that the algorithm worked properly.
163       if(!aSymmetryAlgo->isDone()) {
164         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
165         setError(aFeatureError);
166         break;
167       }
168       if(aSymmetryAlgo->shape()->isNull()) {
169         static const std::string aShapeError = "Error: Resulting shape is Null.";
170         setError(aShapeError);
171         break;
172       }
173       if(!aSymmetryAlgo->isValid()) {
174         std::string aFeatureError = "Error: Resulting shape is not valid.";
175         setError(aFeatureError);
176         break;
177       }
178
179       buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
180     }
181     aResultIndex++;
182   }
183
184   // Remove the rest results if there were produced in the previous pass.
185   removeResults(aResultIndex);
186 }
187
188 //=================================================================================================
189 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
190 {
191   // Getting objects.
192   ListOfShape anObjects;
193   std::list<ResultPtr> aContextes;
194   if (!collectSourceObjects(anObjects, aContextes))
195     return;
196
197   //Getting axis.
198   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
199   AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
200   GeomShapePtr aShape = anObjRef->value();
201   if (!aShape.get()) {
202     if (anObjRef->context().get()) {
203       aShape = anObjRef->context()->shape();
204     }
205   }
206   if (!aShape.get()) {
207     setError(aSelectionError);
208     return;
209   }
210
211   GeomEdgePtr anEdge;
212   if (aShape->isEdge())
213   {
214     anEdge = aShape->edge();
215   }
216   else if (aShape->isCompound())
217   {
218     GeomAPI_ShapeIterator anIt(aShape);
219     anEdge = anIt.current()->edge();
220   }
221   else
222   {
223     setError(aSelectionError);
224     return;
225   }
226
227   if (!anEdge.get())
228   {
229     setError(aSelectionError);
230     return;
231   }
232
233   std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
234                                                        anEdge->line()->direction()));
235
236
237   // Moving each object.
238   int aResultIndex = 0;
239   std::list<ResultPtr>::iterator aContext = aContextes.begin();
240   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
241         anObjectsIt++, aContext++) {
242     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
243     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
244
245     // Setting result.
246     if (isPart) {
247       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
248       aTrsf->setSymmetry(anAxis);
249       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
250       buildResult(anOrigin, aTrsf, aResultIndex);
251     }
252     else {
253       std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
254         new GeomAlgoAPI_Symmetry(aBaseShape, anAxis));
255
256       if (!aSymmetryAlgo->check()) {
257         setError(aSymmetryAlgo->getError());
258         return;
259       }
260
261       aSymmetryAlgo->build();
262
263       // Checking that the algorithm worked properly.
264       if(!aSymmetryAlgo->isDone()) {
265         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
266         setError(aFeatureError);
267         break;
268       }
269       if(aSymmetryAlgo->shape()->isNull()) {
270         static const std::string aShapeError = "Error: Resulting shape is Null.";
271         setError(aShapeError);
272         break;
273       }
274       if(!aSymmetryAlgo->isValid()) {
275         std::string aFeatureError = "Error: Resulting shape is not valid.";
276         setError(aFeatureError);
277         break;
278       }
279
280       buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
281     }
282     aResultIndex++;
283   }
284
285   // Remove the rest results if there were produced in the previous pass.
286   removeResults(aResultIndex);
287 }
288
289 //=================================================================================================
290 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
291 {
292   // Getting objects.
293   ListOfShape anObjects;
294   std::list<ResultPtr> aContextes;
295   if (!collectSourceObjects(anObjects, aContextes))
296     return;
297
298   //Getting plane.
299   static const std::string aSelectionError = "Error: The plane shape selection is bad.";
300   AttributeSelectionPtr anObjRef = selection(PLANE_OBJECT_ID());
301   GeomShapePtr aShape = anObjRef->value();
302   if (!aShape.get()) {
303     if (anObjRef->context().get()) {
304       aShape = anObjRef->context()->shape();
305     }
306   }
307   if (!aShape.get()) {
308     setError(aSelectionError);
309     return;
310   }
311
312   GeomFacePtr aFace;
313   if (aShape->isFace())
314   {
315     aFace = aShape->face();
316   }
317   else if (aShape->isCompound())
318   {
319     GeomAPI_ShapeIterator anIt(aShape);
320     aFace = anIt.current()->face();
321   }
322   else
323   {
324     setError(aSelectionError);
325     return;
326   }
327
328   if (!aFace.get())
329   {
330     setError(aSelectionError);
331     return;
332   }
333
334   std::shared_ptr<GeomAPI_Ax2> aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(),
335                                                       aFace->getPlane()->direction()));
336
337
338   // Moving each object.
339   int aResultIndex = 0;
340   std::list<ResultPtr>::iterator aContext = aContextes.begin();
341   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
342         anObjectsIt++, aContext++) {
343     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
344     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
345
346     // Setting result.
347     if (isPart) {
348       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
349       aTrsf->setSymmetry(aPlane);
350       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
351       buildResult(anOrigin, aTrsf, aResultIndex);
352     } else {
353       std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
354         new GeomAlgoAPI_Symmetry(aBaseShape, aPlane));
355
356       if (!aSymmetryAlgo->check()) {
357         setError(aSymmetryAlgo->getError());
358         return;
359       }
360
361       aSymmetryAlgo->build();
362
363       // Checking that the algorithm worked properly.
364       if(!aSymmetryAlgo->isDone()) {
365         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
366         setError(aFeatureError);
367         break;
368       }
369       if(aSymmetryAlgo->shape()->isNull()) {
370         static const std::string aShapeError = "Error: Resulting shape is Null.";
371         setError(aShapeError);
372         break;
373       }
374       if(!aSymmetryAlgo->isValid()) {
375         std::string aFeatureError = "Error: Resulting shape is not valid.";
376         setError(aFeatureError);
377         break;
378       }
379
380       buildResult(aSymmetryAlgo, aBaseShape, 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_Symmetry::buildResult(
391   std::shared_ptr<GeomAlgoAPI_Symmetry>& theSymmetryAlgo,
392   std::shared_ptr<GeomAPI_Shape> theBaseShape, int theResultIndex)
393 {
394   std::shared_ptr<GeomAlgoAPI_MakeShapeList> anAlgoList(new GeomAlgoAPI_MakeShapeList());
395   anAlgoList->appendAlgo(theSymmetryAlgo);
396   // Compose source shape and the result of symmetry.
397   GeomShapePtr aCompound;
398   if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
399     ListOfShape aShapes;
400     // add a copy of a base shape otherwise selection of this base shape is bad (2592)
401     std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
402     aShapes.push_back(aCopyAlgo->shape());
403     anAlgoList->appendAlgo(aCopyAlgo);
404
405     aShapes.push_back(theSymmetryAlgo->shape());
406     aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
407   } else
408     aCompound = theSymmetryAlgo->shape();
409
410   // Store and name the result.
411   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
412   aResultBody->storeModified(theBaseShape, aCompound);
413   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theBaseShape, anAlgoList, "Symmetried");
414   setResult(aResultBody, theResultIndex);
415 }
416
417 //=================================================================================================
418 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
419                                           std::shared_ptr<GeomAPI_Trsf> theTrsf,
420                                           int& theResultIndex)
421 {
422   if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
423     std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
424     ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
425     aCopy->setTrsf(theOriginal, anIdentity);
426     setResult(aCopy, theResultIndex);
427     ++theResultIndex;
428   }
429
430   ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
431   aResultPart->setTrsf(theOriginal, theTrsf);
432   setResult(aResultPart, theResultIndex);
433 }