Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Revolution.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_Revolution.h"
22
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Validator.h>
28
29 #include <GeomAlgoAPI_Revolution.h>
30 #include <GeomAlgoAPI_Tools.h>
31
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Lin.h>
34 #include <GeomAPI_ShapeIterator.h>
35
36 //=================================================================================================
37 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
38 {
39 }
40
41 //=================================================================================================
42 void FeaturesPlugin_Revolution::initAttributes()
43 {
44   initCompositeSketchAttribtues(InitBaseObjectsList);
45
46   data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
47
48   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
49
50   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
51   data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
52
53   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
54   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
55
56   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
57   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
58
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
61
62   initCompositeSketchAttribtues(InitSketchLauncher);
63 }
64
65 //=================================================================================================
66 void FeaturesPlugin_Revolution::execute()
67 {
68   ListOfShape aBaseShapesList;
69   ListOfMakeShape aMakeShapesList;
70
71   // Make revolutions.
72   if(!makeRevolutions(aBaseShapesList, aMakeShapesList)) {
73     return;
74   }
75
76   // Store results.
77   int aResultIndex = 0;
78   ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
79   ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
80   for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
81         ++aBaseIt, ++anAlgoIt) {
82     storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
83   }
84
85   removeResults(aResultIndex);
86 }
87
88 //=================================================================================================
89 bool FeaturesPlugin_Revolution::makeRevolutions(ListOfShape& theBaseShapes,
90                                                 ListOfMakeShape& theMakeShapes)
91 {
92   theMakeShapes.clear();
93
94   // Getting base shapes.
95   getBaseShapes(theBaseShapes);
96
97   // Getting axis.
98   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
99   AttributeSelectionPtr aSelection = selection(AXIS_OBJECT_ID());
100   GeomShapePtr aShape = aSelection->value();
101   if (!aShape.get()) {
102     if (aSelection->context().get()) {
103       aShape = aSelection->context()->shape();
104     }
105   }
106   if (!aShape.get()) {
107     setError(aSelectionError);
108     return false;
109   }
110
111   GeomEdgePtr anEdge;
112   if (aShape->isEdge())
113   {
114     anEdge = aShape->edge();
115   }
116   else if (aShape->isCompound())
117   {
118     GeomAPI_ShapeIterator anIt(aShape);
119     anEdge = anIt.current()->edge();
120   }
121   else
122   {
123     setError(aSelectionError);
124     return false;
125   }
126
127   std::shared_ptr<GeomAPI_Ax1> anAxis;
128   if(anEdge.get()) {
129     if(anEdge->isLine()) {
130       anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
131                                                             anEdge->line()->direction()));
132     }
133   }
134
135   if(!anAxis.get()) {
136     return false;
137   }
138
139   // Getting angles.
140   double aToAngle = 0.0;
141   double aFromAngle = 0.0;
142
143   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_ANGLES()) {
144     aToAngle = real(TO_ANGLE_ID())->value();
145     aFromAngle = real(FROM_ANGLE_ID())->value();
146   } else {
147     aToAngle = real(TO_OFFSET_ID())->value();
148     aFromAngle = real(FROM_OFFSET_ID())->value();
149   }
150
151   // Getting bounding planes.
152   GeomShapePtr aToShape;
153   GeomShapePtr aFromShape;
154
155   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
156     aSelection = selection(TO_OBJECT_ID());
157     if(aSelection.get()) {
158       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
159       if(!aToShape.get() && aSelection->context().get()) {
160         aToShape = aSelection->context()->shape();
161       }
162       if (aToShape.get() && aToShape->isCompound()) {
163         GeomAPI_ShapeIterator anIt(aToShape);
164         aToShape = anIt.current();
165       }
166     }
167     aSelection = selection(FROM_OBJECT_ID());
168     if(aSelection.get()) {
169       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
170       if(!aFromShape.get() && aSelection->context().get()) {
171         aFromShape = aSelection->context()->shape();
172       }
173       if (aFromShape.get() && aFromShape->isCompound()) {
174         GeomAPI_ShapeIterator anIt(aFromShape);
175         aFromShape = anIt.current();
176       }
177     }
178   }
179
180   // Generating result for each base shape.
181   std::string anError;
182   for(ListOfShape::const_iterator
183       anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
184     GeomShapePtr aBaseShape = *anIter;
185
186     std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo(new GeomAlgoAPI_Revolution(
187                                                        aBaseShape, anAxis,
188                                                        aToShape, aToAngle,
189                                                        aFromShape, aFromAngle));
190     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRevolAlgo, getKind(), anError)) {
191       setError(anError);
192       return false;
193     }
194
195     theMakeShapes.push_back(aRevolAlgo);
196   }
197
198   return true;
199 }