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