Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Revolution.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_Revolution.cpp
4 // Created:     12 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include "FeaturesPlugin_Revolution.h"
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelection.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_Session.h>
13 #include <ModelAPI_Validator.h>
14
15 #include <GeomAlgoAPI_Revolution.h>
16
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Lin.h>
19
20 //=================================================================================================
21 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
22 {
23 }
24
25 //=================================================================================================
26 void FeaturesPlugin_Revolution::initAttributes()
27 {
28   initCompositeSketchAttribtues(InitBaseObjectsList);
29
30   data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
31
32   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
33
34   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
35   data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
36
37   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
38   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
39
40   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
41   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
42
43   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
44   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
45
46   initCompositeSketchAttribtues(InitSketchLauncher);
47 }
48
49 //=================================================================================================
50 void FeaturesPlugin_Revolution::execute()
51 {
52   ListOfShape aBaseShapesList;
53   ListOfMakeShape aMakeShapesList;
54
55   // Make revolutions.
56   if(!makeRevolutions(aBaseShapesList, aMakeShapesList)) {
57     return;
58   }
59
60   // Store results.
61   int aResultIndex = 0;
62   ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
63   ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
64   for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
65         ++aBaseIt, ++anAlgoIt) {
66     storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
67   }
68
69   removeResults(aResultIndex);
70 }
71
72 //=================================================================================================
73 bool FeaturesPlugin_Revolution::makeRevolutions(ListOfShape& theBaseShapes,
74                                                 ListOfMakeShape& theMakeShapes)
75 {
76   theMakeShapes.clear();
77
78   // Getting base shapes.
79   getBaseShapes(theBaseShapes);
80
81   //Getting axis.
82   std::shared_ptr<GeomAPI_Ax1> anAxis;
83   std::shared_ptr<GeomAPI_Edge> anEdge;
84   AttributeSelectionPtr aSelection = selection(AXIS_OBJECT_ID());
85   if(aSelection.get() && aSelection->value().get() && aSelection->value()->isEdge()) {
86     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aSelection->value()));
87   } else if(aSelection->context().get() &&
88             aSelection->context()->shape().get() &&
89             aSelection->context()->shape()->isEdge()) {
90     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aSelection->context()->shape()));
91   }
92   if(anEdge.get()) {
93     if(anEdge->isLine()) {
94       anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
95                                                             anEdge->line()->direction()));
96     }
97   }
98
99   if(!anAxis.get()) {
100     return false;
101   }
102
103   // Getting angles.
104   double aToAngle = 0.0;
105   double aFromAngle = 0.0;
106
107   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_ANGLES()) {
108     aToAngle = real(TO_ANGLE_ID())->value();
109     aFromAngle = real(FROM_ANGLE_ID())->value();
110   } else {
111     aToAngle = real(TO_OFFSET_ID())->value();
112     aFromAngle = real(FROM_OFFSET_ID())->value();
113   }
114
115   // Getting bounding planes.
116   GeomShapePtr aToShape;
117   GeomShapePtr aFromShape;
118
119   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
120     aSelection = selection(TO_OBJECT_ID());
121     if(aSelection.get()) {
122       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
123       if(!aToShape.get() && aSelection->context().get()) {
124         aToShape = aSelection->context()->shape();
125       }
126     }
127     aSelection = selection(FROM_OBJECT_ID());
128     if(aSelection.get()) {
129       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
130       if(!aFromShape.get() && aSelection->context().get()) {
131         aFromShape = aSelection->context()->shape();
132       }
133     }
134   }
135
136   // Generating result for each base shape.
137   for(ListOfShape::const_iterator
138       anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
139     GeomShapePtr aBaseShape = *anIter;
140
141     std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo(new GeomAlgoAPI_Revolution(
142                                                        aBaseShape, anAxis,
143                                                        aToShape, aToAngle,
144                                                        aFromShape, aFromAngle));
145     if(!isMakeShapeValid(aRevolAlgo)) {
146       return false;
147     }
148
149     theMakeShapes.push_back(aRevolAlgo);
150   }
151
152   return true;
153 }