]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
Salome HOME
Issue #3236: Add buttons for shape type choice
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // Copyright (C) 2014-2019  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
18 //
19
20 #include "FeaturesPlugin_Extrusion.h"
21 #include "FeaturesPlugin_Tools.h"
22
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29
30 #include <GeomAlgoAPI_Prism.h>
31 #include <GeomAlgoAPI_Tools.h>
32
33 #include <GeomAPI_Dir.h>
34 #include <GeomAPI_Edge.h>
35 #include <GeomAPI_Lin.h>
36 #include <GeomAPI_ShapeIterator.h>
37
38 //=================================================================================================
39 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
40 {
41 }
42
43 //=================================================================================================
44 void FeaturesPlugin_Extrusion::initAttributes()
45 {
46   initCompositeSketchAttribtues(InitBaseObjectsList);
47
48   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
49
50   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
51   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
52
53   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
54   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
55
56   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
57   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
58
59   data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
60
61   data()->addAttribute(USE_SHAPES_ID(), ModelAPI_AttributeInteger::typeId());
62
63   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
65   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_OBJECT_ID());
66
67   initCompositeSketchAttribtues(InitSketchLauncher);
68 }
69
70 //=================================================================================================
71 void FeaturesPlugin_Extrusion::execute()
72 {
73   ListOfShape aBaseShapesList, aBoundaryShapes;
74   ListOfMakeShape aMakeShapesList;
75
76   // Make extrusions.
77   if(!makeExtrusions(aBaseShapesList, aBoundaryShapes, aMakeShapesList)) {
78     return;
79   }
80
81   // Store results.
82   int aResultIndex = 0;
83   ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
84   ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
85   for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
86         ++aBaseIt, ++anAlgoIt) {
87     if (aBoundaryShapes.empty())
88       storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
89     else
90       storeResultWithBoundaries(*aBaseIt, aBoundaryShapes, *anAlgoIt, aResultIndex++);
91   }
92
93   removeResults(aResultIndex);
94 }
95
96 //=================================================================================================
97 bool FeaturesPlugin_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
98                                               ListOfShape& theBoundaryShapes,
99                                               ListOfMakeShape& theMakeShapes)
100 {
101   theMakeShapes.clear();
102
103   // Getting base shapes.
104   getBaseShapes(theBaseShapes);
105
106   //Getting direction.
107   std::shared_ptr<GeomAPI_Dir> aDir;
108   getDirection(aDir);
109
110   // Getting sizes.
111   double aToSize = 0.0;
112   double aFromSize = 0.0;
113   getSizes(aToSize, aFromSize);
114
115   // Getting bounding planes.
116   GeomShapePtr aToShape;
117   GeomShapePtr aFromShape;
118
119   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
120     AttributeSelectionPtr 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       if (aToShape.get() && aToShape->isCompound()) {
127         GeomAPI_ShapeIterator anIt(aToShape);
128         aToShape = anIt.current();
129       }
130     }
131     aSelection = selection(FROM_OBJECT_ID());
132     if(aSelection.get()) {
133       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
134       if(!aFromShape.get() && aSelection->context().get()) {
135         aFromShape = aSelection->context()->shape();
136       }
137       if (aFromShape.get() && aFromShape->isCompound()) {
138         GeomAPI_ShapeIterator anIt(aFromShape);
139         aFromShape = anIt.current();
140       }
141     }
142   }
143   if (aToShape && !aToShape->isPlanar())
144     theBoundaryShapes.push_back(aToShape);
145   if (aFromShape && !aFromShape->isPlanar())
146     theBoundaryShapes.push_back(aFromShape);
147
148   // Generating result for each base shape.
149   std::string anError;
150   for(ListOfShape::const_iterator
151       anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
152     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
153
154     std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
155                                                                         aToShape, aToSize,
156                                                                         aFromShape, aFromSize));
157     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPrismAlgo, getKind(), anError)) {
158       setError(anError);
159       return false;
160     }
161
162     theMakeShapes.push_back(aPrismAlgo);
163   }
164
165   return true;
166 }
167
168 //=================================================================================================
169 void FeaturesPlugin_Extrusion::storeResultWithBoundaries(
170     const GeomShapePtr theBaseShape,
171     const ListOfShape& theBoundaryShapes,
172     const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
173     const int theIndex)
174 {
175   // Create result body.
176   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
177
178   // Store modified shapes.
179   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theBoundaryShapes, ListOfShape(),
180                                            theMakeShape, theMakeShape->shape());
181
182   // Store generated edges/faces.
183   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
184
185   setResult(aResultBody, theIndex);
186 }
187
188 //=================================================================================================
189 void FeaturesPlugin_Extrusion::getDirection(std::shared_ptr<GeomAPI_Dir>& theDir)
190 {
191   static const std::string aSelectionError = "Error: The direction shape selection is bad.";
192   AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
193   GeomShapePtr aShape = aSelection->value();
194   if (!aShape.get()) {
195     if (aSelection->context().get()) {
196       aShape = aSelection->context()->shape();
197     }
198   }
199
200   GeomEdgePtr anEdge;
201   if (aShape.get()) {
202     if (aShape->isEdge())
203     {
204       anEdge = aShape->edge();
205     }
206     else if (aShape->isCompound())
207     {
208       GeomAPI_ShapeIterator anIt(aShape);
209       anEdge = anIt.current()->edge();
210     }
211   }
212
213   if (anEdge.get()) {
214     if (anEdge->isLine()) {
215       theDir = anEdge->line()->direction();
216     }
217   }
218 }
219
220 //=================================================================================================
221 void FeaturesPlugin_Extrusion::getSizes(double& theToSize, double& theFromSize)
222 {
223   if (string(CREATION_METHOD())->value() == CREATION_METHOD_BY_SIZES()) {
224     theToSize = real(TO_SIZE_ID())->value();
225     theFromSize = real(FROM_SIZE_ID())->value();
226   } if (string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
227     theToSize = real(TO_OFFSET_ID())->value();
228     theFromSize = real(FROM_OFFSET_ID())->value();
229   } else {
230   }
231 }