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