]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Revolution.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto: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
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Lin.h>
32
33 //=================================================================================================
34 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
35 {
36 }
37
38 //=================================================================================================
39 void FeaturesPlugin_Revolution::initAttributes()
40 {
41   initCompositeSketchAttribtues(InitBaseObjectsList);
42
43   data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
44
45   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
46
47   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
48   data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
49
50   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
51   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
52
53   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
54   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
55
56   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
57   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
58
59   initCompositeSketchAttribtues(InitSketchLauncher);
60 }
61
62 //=================================================================================================
63 void FeaturesPlugin_Revolution::execute()
64 {
65   ListOfShape aBaseShapesList;
66   ListOfMakeShape aMakeShapesList;
67
68   // Make revolutions.
69   if(!makeRevolutions(aBaseShapesList, aMakeShapesList)) {
70     return;
71   }
72
73   // Store results.
74   int aResultIndex = 0;
75   ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
76   ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
77   for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
78         ++aBaseIt, ++anAlgoIt) {
79     storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
80   }
81
82   removeResults(aResultIndex);
83 }
84
85 //=================================================================================================
86 bool FeaturesPlugin_Revolution::makeRevolutions(ListOfShape& theBaseShapes,
87                                                 ListOfMakeShape& theMakeShapes)
88 {
89   theMakeShapes.clear();
90
91   // Getting base shapes.
92   getBaseShapes(theBaseShapes);
93
94   //Getting axis.
95   std::shared_ptr<GeomAPI_Ax1> anAxis;
96   std::shared_ptr<GeomAPI_Edge> anEdge;
97   AttributeSelectionPtr aSelection = selection(AXIS_OBJECT_ID());
98   if(aSelection.get() && aSelection->value().get() && aSelection->value()->isEdge()) {
99     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aSelection->value()));
100   } else if(aSelection->context().get() &&
101             aSelection->context()->shape().get() &&
102             aSelection->context()->shape()->isEdge()) {
103     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aSelection->context()->shape()));
104   }
105   if(anEdge.get()) {
106     if(anEdge->isLine()) {
107       anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
108                                                             anEdge->line()->direction()));
109     }
110   }
111
112   if(!anAxis.get()) {
113     return false;
114   }
115
116   // Getting angles.
117   double aToAngle = 0.0;
118   double aFromAngle = 0.0;
119
120   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_ANGLES()) {
121     aToAngle = real(TO_ANGLE_ID())->value();
122     aFromAngle = real(FROM_ANGLE_ID())->value();
123   } else {
124     aToAngle = real(TO_OFFSET_ID())->value();
125     aFromAngle = real(FROM_OFFSET_ID())->value();
126   }
127
128   // Getting bounding planes.
129   GeomShapePtr aToShape;
130   GeomShapePtr aFromShape;
131
132   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
133     aSelection = selection(TO_OBJECT_ID());
134     if(aSelection.get()) {
135       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
136       if(!aToShape.get() && aSelection->context().get()) {
137         aToShape = aSelection->context()->shape();
138       }
139     }
140     aSelection = selection(FROM_OBJECT_ID());
141     if(aSelection.get()) {
142       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
143       if(!aFromShape.get() && aSelection->context().get()) {
144         aFromShape = aSelection->context()->shape();
145       }
146     }
147   }
148
149   // Generating result for each base shape.
150   for(ListOfShape::const_iterator
151       anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
152     GeomShapePtr aBaseShape = *anIter;
153
154     std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo(new GeomAlgoAPI_Revolution(
155                                                        aBaseShape, anAxis,
156                                                        aToShape, aToAngle,
157                                                        aFromShape, aFromAngle));
158     if(!isMakeShapeValid(aRevolAlgo)) {
159       return false;
160     }
161
162     theMakeShapes.push_back(aRevolAlgo);
163   }
164
165   return true;
166 }