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