Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.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_Extrusion.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_Prism.h>
30
31 #include <GeomAPI_Dir.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Lin.h>
34
35 //=================================================================================================
36 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
37 {
38 }
39
40 //=================================================================================================
41 void FeaturesPlugin_Extrusion::initAttributes()
42 {
43   initCompositeSketchAttribtues(InitBaseObjectsList);
44
45   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
46
47   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
48   data()->addAttribute(FROM_SIZE_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   data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
57
58   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_OBJECT_ID());
61
62   initCompositeSketchAttribtues(InitSketchLauncher);
63 }
64
65 //=================================================================================================
66 void FeaturesPlugin_Extrusion::execute()
67 {
68   ListOfShape aBaseShapesList;
69   ListOfMakeShape aMakeShapesList;
70
71   // Make extrusions.
72   if(!makeExtrusions(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_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
90                                               ListOfMakeShape& theMakeShapes)
91 {
92   theMakeShapes.clear();
93
94   // Getting base shapes.
95   getBaseShapes(theBaseShapes);
96
97   //Getting direction.
98   std::shared_ptr<GeomAPI_Dir> aDir;
99   std::shared_ptr<GeomAPI_Edge> anEdge;
100   AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
101   if(aSelection.get() && aSelection->value().get() && aSelection->value()->isEdge()) {
102     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aSelection->value()));
103   } else if(aSelection->context().get() &&
104             aSelection->context()->shape().get() &&
105             aSelection->context()->shape()->isEdge()) {
106     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aSelection->context()->shape()));
107   }
108   if(anEdge.get()) {
109     if(anEdge->isLine()) {
110       aDir = anEdge->line()->direction();
111     }
112   }
113
114   // Getting sizes.
115   double aToSize = 0.0;
116   double aFromSize = 0.0;
117
118   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_SIZES()) {
119     aToSize = real(TO_SIZE_ID())->value();
120     aFromSize = real(FROM_SIZE_ID())->value();
121   } else {
122     aToSize = real(TO_OFFSET_ID())->value();
123     aFromSize = real(FROM_OFFSET_ID())->value();
124   }
125
126   // Getting bounding planes.
127   GeomShapePtr aToShape;
128   GeomShapePtr aFromShape;
129
130   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
131     aSelection = selection(TO_OBJECT_ID());
132     if(aSelection.get()) {
133       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
134       if(!aToShape.get() && aSelection->context().get()) {
135         aToShape = aSelection->context()->shape();
136       }
137     }
138     aSelection = selection(FROM_OBJECT_ID());
139     if(aSelection.get()) {
140       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
141       if(!aFromShape.get() && aSelection->context().get()) {
142         aFromShape = aSelection->context()->shape();
143       }
144     }
145   }
146
147   // Generating result for each base shape.
148   for(ListOfShape::const_iterator
149       anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
150     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
151
152     std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
153                                                                         aToShape, aToSize,
154                                                                         aFromShape, aFromSize));
155     if(!isMakeShapeValid(aPrismAlgo)) {
156       return false;
157     }
158
159     theMakeShapes.push_back(aPrismAlgo);
160   }
161
162   return true;
163 }