]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp
Salome HOME
Make "Macro" menu be the last, not just after the Sketch.
[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_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
127   if (!anEdge.get())
128   {
129     setError(aSelectionError);
130     return;
131   }
132
133   std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
134                                                        anEdge->line()->direction()));
135
136   // Getting angle.
137   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
138
139   // Rotating each object.
140   std::string anError;
141   int aResultIndex = 0;
142   std::list<ResultPtr>::iterator aContext = aContextes.begin();
143   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
144         anObjectsIt++, aContext++) {
145     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
146     bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
147
148     // Setting result.
149     if (isPart) {
150       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
151       aTrsf->setRotation(anAxis, anAngle);
152       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
153       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
154       aResultPart->setTrsf(*aContext, aTrsf);
155       setResult(aResultPart, aResultIndex);
156     } else {
157       std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
158                                                                                    anAxis,
159                                                                                    anAngle));
160
161       if (!aRotationAlgo->check()) {
162         setError(aRotationAlgo->getError());
163         return;
164       }
165
166       aRotationAlgo->build();
167
168       // Checking that the algorithm worked properly.
169       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
170         setError(anError);
171         break;
172       }
173
174       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
175       aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
176       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "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
260       if (!aRotationAlgo->check()) {
261         setError(aRotationAlgo->getError());
262         return;
263       }
264
265       aRotationAlgo->build();
266
267       // Checking that the algorithm worked properly.
268       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
269         setError(anError);
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 }