Salome HOME
Issue #2962: Provide name of 'Change Sketch plane' operation in Undo/Redo lists
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Rotation.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <FeaturesPlugin_Rotation.h>
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultPart.h>
27
28 #include <GeomAlgoAPI_PointBuilder.h>
29 #include <GeomAlgoAPI_Tools.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   std::string anError;
141   int aResultIndex = 0;
142   std::list<ResultPtr>::iterator aContext = aContextes.begin();
143   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
144         anObjectsIt++, aContext++) {
145     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
146     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
147
148     // Setting result.
149     if (isPart) {
150       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
151       aTrsf->setRotation(anAxis, anAngle);
152       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
153       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
154       aResultPart->setTrsf(*aContext, aTrsf);
155       setResult(aResultPart, aResultIndex);
156     } else {
157       std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
158                                                                                    anAxis,
159                                                                                    anAngle));
160
161       if (!aRotationAlgo->check()) {
162         setError(aRotationAlgo->getError());
163         return;
164       }
165
166       aRotationAlgo->build();
167
168       // Checking that the algorithm worked properly.
169       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
170         setError(anError);
171         break;
172       }
173
174       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
175
176       ListOfShape aShapes;
177       aShapes.push_back(aBaseShape);
178       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
179                                                aShapes,
180                                                ListOfShape(),
181                                                aRotationAlgo,
182                                                aRotationAlgo->shape(),
183                                                "Rotated");
184       setResult(aResultBody, aResultIndex);
185     }
186     aResultIndex++;
187   }
188
189   // Remove the rest results if there were produced in the previous pass.
190   removeResults(aResultIndex);
191 }
192
193 //=================================================================================================
194 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
195 {
196   // Getting objects.
197   ListOfShape anObjects;
198   std::list<ResultPtr> aContextes;
199   AttributeSelectionListPtr anObjectsSelList =
200     selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
201   if (anObjectsSelList->size() == 0) {
202     return;
203   }
204   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
205     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
206       anObjectsSelList->value(anObjectsIndex);
207     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
208     if(!anObject.get()) {
209       return;
210     }
211     anObjects.push_back(anObject);
212     aContextes.push_back(anObjectAttr->context());
213   }
214
215   // Getting the center point and two points (start and end)
216   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
217   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
218   std::shared_ptr<GeomAPI_Pnt> anEndPoint;
219   std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
220     selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
221   std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
222     selection(FeaturesPlugin_Rotation::START_POINT_ID());
223   std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
224     selection(FeaturesPlugin_Rotation::END_POINT_ID());
225   if ((aCenterRef.get() != NULL) &&
226       (aStartPointRef.get() != NULL) &&
227       (anEndPointRef.get() != NULL)) {
228     GeomShapePtr aCenterShape = aCenterRef->value();
229     if (!aCenterShape.get() && aCenterRef->context().get())
230       aCenterShape = aCenterRef->context()->shape();
231     GeomShapePtr aStartShape = aStartPointRef->value();
232     if (!aStartShape.get() && aStartPointRef->context().get())
233       aStartShape = aStartPointRef->context()->shape();
234     GeomShapePtr anEndShape = anEndPointRef->value();
235     if (!anEndShape.get() && anEndPointRef->context().get())
236       anEndShape = anEndPointRef->context()->shape();
237     if (aStartShape && anEndShape && aCenterShape) {
238       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
239       aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
240       anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
241     }
242   }
243
244   // Rotating each object.
245   std::string anError;
246   int aResultIndex = 0;
247   std::list<ResultPtr>::iterator aContext = aContextes.begin();
248   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
249         anObjectsIt++, aContext++) {
250     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
251     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
252
253     // Setting result.
254     if (isPart) {
255        std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
256        aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
257        ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
258        ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
259        aResultPart->setTrsf(*aContext, aTrsf);
260        setResult(aResultPart, aResultIndex);
261     } else {
262       std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
263                                                                                    aCenterPoint,
264                                                                                    aStartPoint,
265                                                                                    anEndPoint));
266
267       if (!aRotationAlgo->check()) {
268         setError(aRotationAlgo->getError());
269         return;
270       }
271
272       aRotationAlgo->build();
273
274       // Checking that the algorithm worked properly.
275       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
276         setError(anError);
277         break;
278       }
279
280       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
281
282       ListOfShape aShapes;
283       aShapes.push_back(aBaseShape);
284       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
285                                                aShapes,
286                                                ListOfShape(),
287                                                aRotationAlgo,
288                                                aRotationAlgo->shape(),
289                                                "Rotated");
290       setResult(aResultBody, aResultIndex);
291     }
292     aResultIndex++;
293   }
294 }