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