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