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