]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp
Salome HOME
Issue #2593: CEA 2018-2 Geometrical Naming
[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_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   else
127   {
128     setError(aSelectionError);
129     return;
130   }
131
132   if (!anEdge.get())
133   {
134     setError(aSelectionError);
135     return;
136   }
137
138   std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
139                                                        anEdge->line()->direction()));
140
141   // Getting angle.
142   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
143
144   // Rotating each object.
145   int aResultIndex = 0;
146   std::list<ResultPtr>::iterator aContext = aContextes.begin();
147   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
148         anObjectsIt++, aContext++) {
149     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
150     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
151
152     // Setting result.
153     if (isPart) {
154       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
155       aTrsf->setRotation(anAxis, anAngle);
156       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
157       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
158       aResultPart->setTrsf(*aContext, aTrsf);
159       setResult(aResultPart, aResultIndex);
160     } else {
161       GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
162
163       if (!aRotationAlgo.check()) {
164         setError(aRotationAlgo.getError());
165         return;
166       }
167
168       aRotationAlgo.build();
169
170       // Checking that the algorithm worked properly.
171       if(!aRotationAlgo.isDone()) {
172         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
173         setError(aFeatureError);
174         break;
175       }
176       if(aRotationAlgo.shape()->isNull()) {
177         static const std::string aShapeError = "Error: Resulting shape is Null.";
178         setError(aShapeError);
179         break;
180       }
181       if(!aRotationAlgo.isValid()) {
182         std::string aFeatureError = "Error: Resulting shape is not valid.";
183         setError(aFeatureError);
184         break;
185       }
186
187       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
188       loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
189       setResult(aResultBody, aResultIndex);
190     }
191     aResultIndex++;
192   }
193
194   // Remove the rest results if there were produced in the previous pass.
195   removeResults(aResultIndex);
196 }
197
198 //=================================================================================================
199 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
200 {
201   // Getting objects.
202   ListOfShape anObjects;
203   std::list<ResultPtr> aContextes;
204   AttributeSelectionListPtr anObjectsSelList =
205     selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
206   if (anObjectsSelList->size() == 0) {
207     return;
208   }
209   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
210     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
211       anObjectsSelList->value(anObjectsIndex);
212     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
213     if(!anObject.get()) {
214       return;
215     }
216     anObjects.push_back(anObject);
217     aContextes.push_back(anObjectAttr->context());
218   }
219
220   // Getting the center point and two points (start and end)
221   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
222   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
223   std::shared_ptr<GeomAPI_Pnt> anEndPoint;
224   std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
225     selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
226   std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
227     selection(FeaturesPlugin_Rotation::START_POINT_ID());
228   std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
229     selection(FeaturesPlugin_Rotation::END_POINT_ID());
230   if ((aCenterRef.get() != NULL) && (aStartPointRef.get() != NULL)
231       && (anEndPointRef.get() != NULL)) {
232     GeomShapePtr aCenterShape = aCenterRef->value();
233     if (!aCenterShape.get())
234       aCenterShape = aCenterRef->context()->shape();
235     GeomShapePtr aStartShape = aStartPointRef->value();
236     if (!aStartShape.get())
237       aStartShape = aStartPointRef->context()->shape();
238       GeomShapePtr anEndShape = anEndPointRef->value();
239     if (!anEndShape.get())
240       anEndShape = anEndPointRef->context()->shape();
241     if (aStartShape && anEndShape && aCenterShape) {
242       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
243       aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
244       anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
245     }
246   }
247
248   // Rotating each object.
249   int aResultIndex = 0;
250   std::list<ResultPtr>::iterator aContext = aContextes.begin();
251   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
252         anObjectsIt++, aContext++) {
253     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
254     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
255
256     // Setting result.
257     if (isPart) {
258        std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
259        aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
260        ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
261        ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
262        aResultPart->setTrsf(*aContext, aTrsf);
263        setResult(aResultPart, aResultIndex);
264     } else {
265       GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, aCenterPoint, aStartPoint, 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(!aRotationAlgo.isDone()) {
276         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
277         setError(aFeatureError);
278         break;
279       }
280       if(aRotationAlgo.shape()->isNull()) {
281         static const std::string aShapeError = "Error : Resulting shape is Null.";
282         setError(aShapeError);
283         break;
284       }
285       if(!aRotationAlgo.isValid()) {
286         std::string aFeatureError = "Error: Resulting shape is not valid.";
287         setError(aFeatureError);
288         break;
289       }
290
291       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
292       loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
293       setResult(aResultBody, aResultIndex);
294     }
295     aResultIndex++;
296   }
297 }
298
299 //=================================================================================================
300 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
301                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
302                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
303 {
304   // Store result.
305   theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
306
307   std::string aRotatedName = "Rotated";
308   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
309
310   FeaturesPlugin_Tools::storeModifiedShapes(theRotaionAlgo, theResultBody,
311                                             theBaseShape, 1, 2, 3, aRotatedName,
312                                             *aSubShapes.get());
313 }