]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp
Salome HOME
Refactor the transformation algorithms
[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_Ax1.h>
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       // Checking that the algorithm worked properly.
162       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
163         setError(anError);
164         break;
165       }
166
167       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
168
169       ListOfShape aShapes;
170       aShapes.push_back(aBaseShape);
171       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
172                                                aShapes,
173                                                ListOfShape(),
174                                                aRotationAlgo,
175                                                aRotationAlgo->shape(),
176                                                "Rotated");
177       setResult(aResultBody, aResultIndex);
178     }
179     aResultIndex++;
180   }
181
182   // Remove the rest results if there were produced in the previous pass.
183   removeResults(aResultIndex);
184 }
185
186 //=================================================================================================
187 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
188 {
189   // Getting objects.
190   ListOfShape anObjects;
191   std::list<ResultPtr> aContextes;
192   AttributeSelectionListPtr anObjectsSelList =
193     selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
194   if (anObjectsSelList->size() == 0) {
195     return;
196   }
197   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
198     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
199       anObjectsSelList->value(anObjectsIndex);
200     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
201     if(!anObject.get()) {
202       return;
203     }
204     anObjects.push_back(anObject);
205     aContextes.push_back(anObjectAttr->context());
206   }
207
208   // Getting the center point and two points (start and end)
209   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
210   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
211   std::shared_ptr<GeomAPI_Pnt> anEndPoint;
212   std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
213     selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
214   std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
215     selection(FeaturesPlugin_Rotation::START_POINT_ID());
216   std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
217     selection(FeaturesPlugin_Rotation::END_POINT_ID());
218   if ((aCenterRef.get() != NULL) &&
219       (aStartPointRef.get() != NULL) &&
220       (anEndPointRef.get() != NULL)) {
221     GeomShapePtr aCenterShape = aCenterRef->value();
222     if (!aCenterShape.get() && aCenterRef->context().get())
223       aCenterShape = aCenterRef->context()->shape();
224     GeomShapePtr aStartShape = aStartPointRef->value();
225     if (!aStartShape.get() && aStartPointRef->context().get())
226       aStartShape = aStartPointRef->context()->shape();
227     GeomShapePtr anEndShape = anEndPointRef->value();
228     if (!anEndShape.get() && anEndPointRef->context().get())
229       anEndShape = anEndPointRef->context()->shape();
230     if (aStartShape && anEndShape && aCenterShape) {
231       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
232       aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
233       anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
234     }
235   }
236
237   // Rotating each object.
238   std::string anError;
239   int aResultIndex = 0;
240   std::list<ResultPtr>::iterator aContext = aContextes.begin();
241   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
242         anObjectsIt++, aContext++) {
243     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
244     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
245
246     // Setting result.
247     if (isPart) {
248        std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
249        aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
250        ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
251        ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
252        aResultPart->setTrsf(*aContext, aTrsf);
253        setResult(aResultPart, aResultIndex);
254     } else {
255       std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
256                                                                                    aCenterPoint,
257                                                                                    aStartPoint,
258                                                                                    anEndPoint));
259       // Checking that the algorithm worked properly.
260       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
261         setError(anError);
262         break;
263       }
264
265       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
266
267       ListOfShape aShapes;
268       aShapes.push_back(aBaseShape);
269       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
270                                                aShapes,
271                                                ListOfShape(),
272                                                aRotationAlgo,
273                                                aRotationAlgo->shape(),
274                                                "Rotated");
275       setResult(aResultBody, aResultIndex);
276     }
277     aResultIndex++;
278   }
279 }