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