Salome HOME
[Code coverage FeaturesPlugin]: Improve coverage of Partition, Symmetry, (Multi)Rotat...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Rotation.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_Rotation.h>
22
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultPart.h>
28
29 #include <GeomAlgoAPI_PointBuilder.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 <FeaturesPlugin_Tools.h>
37
38 //=================================================================================================
39 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
40 {
41 }
42
43 //=================================================================================================
44 void FeaturesPlugin_Rotation::initAttributes()
45 {
46   data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
47                        ModelAPI_AttributeString::typeId());
48
49   AttributeSelectionListPtr aSelection =
50     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
51     FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
52
53   data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
54                        ModelAPI_AttributeSelection::typeId());
55   data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
56
57   data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
58                        ModelAPI_AttributeSelection::typeId());
59   data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
60                        ModelAPI_AttributeSelection::typeId());
61   data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
62                        ModelAPI_AttributeSelection::typeId());
63 }
64
65 //=================================================================================================
66 void FeaturesPlugin_Rotation::execute()
67 {
68   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
69   std::string aMethodType = aMethodTypeAttr->value();
70
71   if (aMethodType == CREATION_METHOD_BY_ANGLE()) {
72     performTranslationByAxisAndAngle();
73   }
74
75   if (aMethodType == CREATION_METHOD_BY_THREE_POINTS()) {
76     performTranslationByThreePoints();
77   }
78 }
79
80 //=================================================================================================
81 void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
82 {
83   // Getting objects.
84   ListOfShape anObjects;
85   std::list<ResultPtr> aContextes;
86   AttributeSelectionListPtr anObjectsSelList =
87     selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
88   if (anObjectsSelList->size() == 0) {
89     return;
90   }
91   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
92     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
93       anObjectsSelList->value(anObjectsIndex);
94     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
95     if(!anObject.get()) {
96       return;
97     }
98     anObjects.push_back(anObject);
99     aContextes.push_back(anObjectAttr->context());
100   }
101
102   //Getting axis.
103   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
104   AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
105   GeomShapePtr aShape = anObjRef->value();
106   if (!aShape.get()) {
107     if (anObjRef->context().get()) {
108       aShape = anObjRef->context()->shape();
109     }
110   }
111   if (!aShape.get()) {
112     setError(aSelectionError);
113     return;
114   }
115
116   GeomEdgePtr anEdge;
117   if (aShape->isEdge())
118   {
119     anEdge = aShape->edge();
120   }
121   else if (aShape->isCompound())
122   {
123     GeomAPI_ShapeIterator anIt(aShape);
124     anEdge = anIt.current()->edge();
125   }
126
127   if (!anEdge.get())
128   {
129     setError(aSelectionError);
130     return;
131   }
132
133   std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
134                                                        anEdge->line()->direction()));
135
136   // Getting angle.
137   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
138
139   // Rotating each object.
140   int aResultIndex = 0;
141   std::list<ResultPtr>::iterator aContext = aContextes.begin();
142   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
143         anObjectsIt++, aContext++) {
144     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
145     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
146
147     // Setting result.
148     if (isPart) {
149       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
150       aTrsf->setRotation(anAxis, anAngle);
151       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
152       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
153       aResultPart->setTrsf(*aContext, aTrsf);
154       setResult(aResultPart, aResultIndex);
155     } else {
156       std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
157                                                                                    anAxis,
158                                                                                    anAngle));
159
160       if (!aRotationAlgo->check()) {
161         setError(aRotationAlgo->getError());
162         return;
163       }
164
165       aRotationAlgo->build();
166
167       // Checking that the algorithm worked properly.
168       if(!aRotationAlgo->isDone()) {
169         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
170         setError(aFeatureError);
171         break;
172       }
173       if(aRotationAlgo->shape()->isNull()) {
174         static const std::string aShapeError = "Error: Resulting shape is Null.";
175         setError(aShapeError);
176         break;
177       }
178       if(!aRotationAlgo->isValid()) {
179         std::string aFeatureError = "Error: Resulting shape is not valid.";
180         setError(aFeatureError);
181         break;
182       }
183
184       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
185       aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
186       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
187       setResult(aResultBody, aResultIndex);
188     }
189     aResultIndex++;
190   }
191
192   // Remove the rest results if there were produced in the previous pass.
193   removeResults(aResultIndex);
194 }
195
196 //=================================================================================================
197 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
198 {
199   // Getting objects.
200   ListOfShape anObjects;
201   std::list<ResultPtr> aContextes;
202   AttributeSelectionListPtr anObjectsSelList =
203     selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
204   if (anObjectsSelList->size() == 0) {
205     return;
206   }
207   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
208     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
209       anObjectsSelList->value(anObjectsIndex);
210     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
211     if(!anObject.get()) {
212       return;
213     }
214     anObjects.push_back(anObject);
215     aContextes.push_back(anObjectAttr->context());
216   }
217
218   // Getting the center point and two points (start and end)
219   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
220   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
221   std::shared_ptr<GeomAPI_Pnt> anEndPoint;
222   std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
223     selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
224   std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
225     selection(FeaturesPlugin_Rotation::START_POINT_ID());
226   std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
227     selection(FeaturesPlugin_Rotation::END_POINT_ID());
228   if ((aCenterRef.get() != NULL) &&
229       (aStartPointRef.get() != NULL) &&
230       (anEndPointRef.get() != NULL)) {
231     GeomShapePtr aCenterShape = aCenterRef->value();
232     if (!aCenterShape.get() && aCenterRef->context().get())
233       aCenterShape = aCenterRef->context()->shape();
234     GeomShapePtr aStartShape = aStartPointRef->value();
235     if (!aStartShape.get() && aStartPointRef->context().get())
236       aStartShape = aStartPointRef->context()->shape();
237     GeomShapePtr anEndShape = anEndPointRef->value();
238     if (!anEndShape.get() && anEndPointRef->context().get())
239       anEndShape = anEndPointRef->context()->shape();
240     if (aStartShape && anEndShape && aCenterShape) {
241       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
242       aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
243       anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
244     }
245   }
246
247   // Rotating each object.
248   int aResultIndex = 0;
249   std::list<ResultPtr>::iterator aContext = aContextes.begin();
250   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
251         anObjectsIt++, aContext++) {
252     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
253     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
254
255     // Setting result.
256     if (isPart) {
257        std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
258        aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
259        ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
260        ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
261        aResultPart->setTrsf(*aContext, aTrsf);
262        setResult(aResultPart, aResultIndex);
263     } else {
264       std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
265                                                                                    aCenterPoint,
266                                                                                    aStartPoint,
267                                                                                    anEndPoint));
268
269       if (!aRotationAlgo->check()) {
270         setError(aRotationAlgo->getError());
271         return;
272       }
273
274       aRotationAlgo->build();
275
276       // Checking that the algorithm worked properly.
277       if(!aRotationAlgo->isDone()) {
278         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
279         setError(aFeatureError);
280         break;
281       }
282       if(aRotationAlgo->shape()->isNull()) {
283         static const std::string aShapeError = "Error : Resulting shape is Null.";
284         setError(aShapeError);
285         break;
286       }
287       if(!aRotationAlgo->isValid()) {
288         std::string aFeatureError = "Error: Resulting shape is not valid.";
289         setError(aFeatureError);
290         break;
291       }
292
293       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
294       aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
295       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
296       setResult(aResultBody, aResultIndex);
297     }
298     aResultIndex++;
299   }
300 }