Salome HOME
Issue #2593: CEA 2018-2 Geometrical Naming
[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   else
127   {
128     setError(aSelectionError);
129     return;
130   }
131
132   if (!anEdge.get())
133   {
134     setError(aSelectionError);
135     return;
136   }
137
138   std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
139                                                        anEdge->line()->direction()));
140
141   // Getting angle.
142   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
143
144   // Rotating each object.
145   int aResultIndex = 0;
146   std::list<ResultPtr>::iterator aContext = aContextes.begin();
147   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
148         anObjectsIt++, aContext++) {
149     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
150     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
151
152     // Setting result.
153     if (isPart) {
154       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
155       aTrsf->setRotation(anAxis, anAngle);
156       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
157       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
158       aResultPart->setTrsf(*aContext, aTrsf);
159       setResult(aResultPart, aResultIndex);
160     } else {
161       std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
162                                                                                    anAxis,
163                                                                                    anAngle));
164
165       if (!aRotationAlgo->check()) {
166         setError(aRotationAlgo->getError());
167         return;
168       }
169
170       aRotationAlgo->build();
171
172       // Checking that the algorithm worked properly.
173       if(!aRotationAlgo->isDone()) {
174         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
175         setError(aFeatureError);
176         break;
177       }
178       if(aRotationAlgo->shape()->isNull()) {
179         static const std::string aShapeError = "Error: Resulting shape is Null.";
180         setError(aShapeError);
181         break;
182       }
183       if(!aRotationAlgo->isValid()) {
184         std::string aFeatureError = "Error: Resulting shape is not valid.";
185         setError(aFeatureError);
186         break;
187       }
188
189       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
190       aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
191       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
192       setResult(aResultBody, aResultIndex);
193     }
194     aResultIndex++;
195   }
196
197   // Remove the rest results if there were produced in the previous pass.
198   removeResults(aResultIndex);
199 }
200
201 //=================================================================================================
202 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
203 {
204   // Getting objects.
205   ListOfShape anObjects;
206   std::list<ResultPtr> aContextes;
207   AttributeSelectionListPtr anObjectsSelList =
208     selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
209   if (anObjectsSelList->size() == 0) {
210     return;
211   }
212   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
213     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
214       anObjectsSelList->value(anObjectsIndex);
215     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
216     if(!anObject.get()) {
217       return;
218     }
219     anObjects.push_back(anObject);
220     aContextes.push_back(anObjectAttr->context());
221   }
222
223   // Getting the center point and two points (start and end)
224   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
225   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
226   std::shared_ptr<GeomAPI_Pnt> anEndPoint;
227   std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
228     selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
229   std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
230     selection(FeaturesPlugin_Rotation::START_POINT_ID());
231   std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
232     selection(FeaturesPlugin_Rotation::END_POINT_ID());
233   if ((aCenterRef.get() != NULL) && (aStartPointRef.get() != NULL)
234       && (anEndPointRef.get() != NULL)) {
235     GeomShapePtr aCenterShape = aCenterRef->value();
236     if (!aCenterShape.get())
237       aCenterShape = aCenterRef->context()->shape();
238     GeomShapePtr aStartShape = aStartPointRef->value();
239     if (!aStartShape.get())
240       aStartShape = aStartPointRef->context()->shape();
241       GeomShapePtr anEndShape = anEndPointRef->value();
242     if (!anEndShape.get())
243       anEndShape = anEndPointRef->context()->shape();
244     if (aStartShape && anEndShape && aCenterShape) {
245       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
246       aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
247       anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
248     }
249   }
250
251   // Rotating each object.
252   int aResultIndex = 0;
253   std::list<ResultPtr>::iterator aContext = aContextes.begin();
254   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
255         anObjectsIt++, aContext++) {
256     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
257     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
258
259     // Setting result.
260     if (isPart) {
261        std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
262        aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
263        ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
264        ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
265        aResultPart->setTrsf(*aContext, aTrsf);
266        setResult(aResultPart, aResultIndex);
267     } else {
268       std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
269                                                                                    aCenterPoint,
270                                                                                    aStartPoint,
271                                                                                    anEndPoint));
272
273       if (!aRotationAlgo->check()) {
274         setError(aRotationAlgo->getError());
275         return;
276       }
277
278       aRotationAlgo->build();
279
280       // Checking that the algorithm worked properly.
281       if(!aRotationAlgo->isDone()) {
282         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
283         setError(aFeatureError);
284         break;
285       }
286       if(aRotationAlgo->shape()->isNull()) {
287         static const std::string aShapeError = "Error : Resulting shape is Null.";
288         setError(aShapeError);
289         break;
290       }
291       if(!aRotationAlgo->isValid()) {
292         std::string aFeatureError = "Error: Resulting shape is not valid.";
293         setError(aFeatureError);
294         break;
295       }
296
297       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
298       aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
299       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
300       setResult(aResultBody, aResultIndex);
301     }
302     aResultIndex++;
303   }
304 }